UNPKG

45.5 kBTypeScriptView Raw
1//
2// Autogenerated from scripts/danger-dts.ts
3//
4
5import { Octokit as GitHub } from "@octokit/rest"
6import type * as Types from "@gitbeaker/rest"
7import { File } from "parse-diff"
8
9type MarkdownString = string
10interface BitBucketCloudJSONDSL {
11 /** The pull request and repository metadata */
12 metadata: RepoMetaData
13 /** The PR metadata */
14 pr: BitBucketCloudPRDSL
15 /** The commits associated with the pull request */
16 commits: BitBucketCloudCommit[]
17 /** The comments on the pull request */
18 comments: BitBucketCloudPRComment[]
19 /** The activities such as OPENING, CLOSING, MERGING or UPDATING a pull request */
20 activities: BitBucketCloudPRActivity[]
21}
22
23interface BitBucketCloudAPIDSL {
24 /** Gets the contents of a file from a repo (defaults to yours) */
25 getFileContents(filePath: string, repoSlug?: string, refspec?: string): Promise<string>
26
27 /** Make a get call against the bitbucket server API */
28 get(path: string, headers: any, suppressErrors?: boolean): Promise<any>
29
30 /** Make a post call against the bitbucket server API */
31 post(path: string, headers: any, body: any, suppressErrors?: boolean): Promise<any>
32
33 /** Make a put call against the bitbucket server API */
34 put(path: string, headers: any, body: any): Promise<any>
35
36 /** Make a delete call against the bitbucket server API */
37 delete(path: string, headers: any, body: any): Promise<any>
38}
39
40interface BitBucketCloudDSL extends BitBucketCloudJSONDSL {
41 /**
42 * An authenticated API so you can extend danger's behavior.
43 */
44 api: BitBucketCloudAPIDSL
45}
46
47interface BitBucketCloudPagedResponse<T> {
48 pagelen: number
49 size: number
50 page: number
51 next: string | undefined
52 previous: string | undefined
53 values: T[]
54}
55interface BitBucketCloudPRDSL {
56 /** The PR's ID */
57 id: number
58 /** Title of the pull request. */
59 title: string
60 /** The text describing the PR */
61 description: string
62 /** The pull request's current status. */
63 state: "OPEN" | "MERGED" | "DECLINED" | "SUPERSEDED"
64 /** When the pr was created, in ISO 8601 format */
65 created_on: string
66 /** When the pr was updated, in ISO 8601 format */
67 updated_on: string
68 /** The PR's source, The repo Danger is running on */
69 source: BitBucketCloudMergeRef
70 /** The PR's destination */
71 destination: BitBucketCloudMergeRef
72 /** The creator of the PR */
73 author: BitBucketCloudUser
74 /** People requested as reviewers */
75 reviewers: BitBucketCloudUser[]
76 /** People who have participated in the PR */
77 participants: BitBucketCloudPRParticipant[]
78 /** Misc links for hypermedia conformance */
79 links: BitBucketCloudLinks<
80 "decline" | "commits" | "self" | "comments" | "merge" | "html" | "activity" | "diff" | "approve" | "statuses"
81 >
82}
83
84interface BitBucketCloudMergeRef {
85 commit: {
86 hash: string
87 }
88 branch: {
89 name: string
90 }
91 repository: BitBucketCloudRepo
92}
93
94type BitBucketCloudLinks<Names extends string> = {
95 [key in Names]: {
96 href: string
97 }
98}
99
100interface BitBucketCloudPRParticipant {
101 /*The user who participated in this PR */
102 user: BitBucketCloudUser
103
104 /** How did they contribute */
105 role: "REVIEWER" | "PARTICIPANT"
106
107 /** Did they approve of the PR? */
108 approved: boolean
109}
110
111interface BitBucketCloudRepo {
112 name: string
113 full_name: string
114 uuid: string
115}
116
117interface BitBucketCloudUser {
118 /** The uuid of the commit author */
119 uuid: string
120
121 /** The display name of the commit author */
122 display_name: string
123
124 /** The nick name of the commit author */
125 nickname: string
126
127 /** The acount id of the commit author */
128 account_id: string
129}
130
131/** A BitBucketCloud specific implementation of a git commit. */
132interface BitBucketCloudCommit {
133 /** The SHA for the commit */
134 hash: string
135
136 /** The author of the commit, assumed to be the person who wrote the code. */
137 author: {
138 /** Format: `Foo Bar <foo@bar.com>` */
139 raw: string
140 user: BitBucketCloudUser
141 }
142
143 /** When the commit was committed to the project, in ISO 8601 format */
144 date: string
145 /** The commit's message */
146 message: string
147 /** The commit's parents */
148 parents: {
149 /** The full SHA */
150 hash: string
151 }[]
152
153 /** The commit's links */
154 links: BitBucketCloudLinks<"html">
155}
156
157interface BitBucketCloudContent {
158 raw: string
159 markup: string
160 html: string
161 type: "rendered"
162}
163
164interface BitBucketCloudPRComment {
165 deleted: boolean
166 links: BitBucketCloudLinks<"self" | "html">
167
168 pullrequest: {
169 id: number
170 links: BitBucketCloudLinks<"self" | "html">
171 title: string
172 }
173 content: BitBucketCloudContent
174
175 /** When the comment was created, in ISO 8601 format */
176 created_on: string
177 user: BitBucketCloudUser
178
179 /** When the comment was updated, in ISO 8601 format */
180 updated_on: string
181 type: string
182 id: number
183
184 inline?: {
185 to: number | null
186 from: number
187 path: string
188 }
189}
190
191interface BitBucketCloudPRActivity {
192 comment?: BitBucketCloudPRComment
193 pull_request: {
194 id: number
195 title: string
196 }
197}
198// This is `danger.bitbucket_server` inside the JSON
199
200interface BitBucketServerJSONDSL {
201 /** The pull request and repository metadata */
202 metadata: RepoMetaData
203 /** The related JIRA issues */
204 issues: JIRAIssue[]
205 /** The PR metadata */
206 pr: BitBucketServerPRDSL
207 /** The commits associated with the pull request */
208 commits: BitBucketServerCommit[]
209 /** The comments on the pull request */
210 comments: BitBucketServerPRActivity[]
211 /** The activities such as OPENING, CLOSING, MERGING or UPDATING a pull request */
212 activities: BitBucketServerPRActivity[]
213}
214
215interface BitBucketServerAPIDSL {
216 /** Gets the contents of a file from a repo (defaults to yours) */
217 getFileContents(filePath: string, repoSlug?: string, refspec?: string): Promise<string>
218
219 /** Make a get call against the bitbucket server API */
220 get(path: string, headers: any, suppressErrors?: boolean): Promise<any>
221
222 /** Make a post call against the bitbucket server API */
223 post(path: string, headers: any, body: any, suppressErrors?: boolean): Promise<any>
224
225 /** Make a put call against the bitbucket server API */
226 put(path: string, headers: any, body: any): Promise<any>
227
228 /** Make a delete call against the bitbucket server API */
229 delete(path: string, headers: any, body: any): Promise<any>
230}
231
232// This is `danger.bitbucket_server`
233
234/** The BitBucketServer metadata for your PR */
235interface BitBucketServerDSL extends BitBucketServerJSONDSL {
236 /**
237 * An authenticated API so you can extend danger's behavior.
238 */
239 api: BitBucketServerAPIDSL
240}
241
242/**
243 * This is `danger.bitbucket_server.issues` It refers to the issues that are linked to the Pull Request.
244 */
245interface JIRAIssue {
246 /** The unique key for the issue e.g. JRA-11 */
247 key: string
248 /** The user-facing URL for that issue */
249 url: string
250}
251
252// This is `danger.bitbucket_server.pr`
253//
254// References:
255// - https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/pullrequests/%7Bpull_request_id%7D
256// - https://docs.atlassian.com/bitbucket-server/javadoc/4.3.2/api/reference/classes.html
257
258/**
259 * An exact copy of the PR's reference JSON. This interface has type'd the majority
260 * of it for tooling's sake, but any extra metadata which BitBucket Server send
261 * will still be inside the JS object.
262 */
263
264interface BitBucketServerPRDSL {
265 /** The PR's ID */
266 id: number
267 /** The API version */
268 version: number
269 /** Title of the pull request. */
270 title: string
271 /** The text describing the PR */
272 description: string
273 /** The pull request's current status. */
274 state: "OPEN" | "MERGED" | "DECLINED" | "SUPERSEDED"
275 /** Is the PR open? */
276 open: boolean
277 /** Is the PR closed? */
278 closed: boolean
279 /** Date PR created as number of milliseconds since the unix epoch */
280 createdDate: number
281 /** Date PR updated as number of milliseconds since the unix epoch */
282 updatedDate: number
283 /** The PR submitter's reference */
284 fromRef: BitBucketServerMergeRef
285 /** The repo Danger is running on */
286 toRef: BitBucketServerMergeRef
287 /** Was this PR locked? */
288 locked: boolean
289 /** The creator of the PR */
290 author: BitBucketServerPRParticipant & { role: "AUTHOR" }
291 /** People requested as reviewers */
292 reviewers: (BitBucketServerPRParticipant & { role: "REVIEWER" })[]
293 /** People who have participated in the PR */
294 participants: (BitBucketServerPRParticipant & { role: "PARTICIPANT" })[]
295 /** Misc links for hypermedia conformance */
296 links: BitBucketServerLinks<"self">
297}
298
299// These are the individual subtypes of objects inside the larger DSL objects above.
300
301/** A BitBucketServer specific implementation of a git commit. */
302interface BitBucketServerCommit {
303 /** The SHA for the commit */
304 id: string
305 /** The shortened SHA for the commit */
306 displayId: string
307 /** The author of the commit, assumed to be the person who wrote the code. */
308 author: {
309 /** The id of the commit author */
310 name: string
311 /** The display name of the commit author */
312 displayName: string
313 /** The email of the commit author */
314 emailAddress: string
315 }
316 /** The UNIX timestamp for when the commit was authored */
317 authorTimestamp: number
318 /** The author of the commit, assumed to be the person who committed/merged the code into a project. */
319 committer: {
320 /** The id of the commit committer */
321 name: string
322 /** The display name of the commit committer */
323 displayName: string
324 /** The email of the commit committer */
325 emailAddress: string
326 }
327 /** When the commit was committed to the project */
328 committerTimestamp: number
329 /** The commit's message */
330 message: string
331 /** The commit's parents */
332 parents: {
333 /** The full SHA */
334 id: string
335 /** The simplified sha */
336 displayId: string
337 }[]
338}
339
340interface BitBucketServerDiff {
341 /** The file reference when moved */
342 destination?: BitBucketServerFile
343 /** The original file reference */
344 source?: BitBucketServerFile
345 /** A set of diff changes */
346 hunks: BitBucketServerHunk[]
347 /** If the hunk is massive, then it will be truncated */
348 truncated: boolean
349 /** The commit SHA which changed this hunk */
350 toHash: string
351 /** Last SHA where this hunk was changed */
352 fromHash: string
353 /** The settings for the whitespace */
354 whitespace: "SHOW" | "IGNORE_ALL"
355}
356
357interface BitBucketServerFile {
358 components: string[]
359 name: string
360 parent: string
361 toString: string
362}
363
364interface BitBucketServerHunk {
365 destinationLine: number
366 destinationSpan: number
367 segments: BitBucketServerSegment[]
368 sourceLine: number
369 sourceSpan: number
370 truncated: boolean
371}
372
373interface BitBucketServerSegment {
374 lines: BitBucketServerLine[]
375 truncated: boolean
376 type: "ADDED" | "REMOVED" | "CONTEXT"
377}
378
379interface BitBucketServerLine {
380 source: number
381 destination: number
382 line: string
383 truncated: boolean
384 conflictMarker?: "OURS"
385 commentIds?: number[]
386}
387
388interface BitBucketServerPRParticipant {
389 /*The user for */
390 user: BitBucketServerUser
391 /** How did they contribute */
392 role: "AUTHOR" | "REVIEWER" | "PARTICIPANT"
393 /** Did they approve of the PR? */
394 approved: boolean
395 /** Their review feedback */
396 status: "APPROVED" | "UNAPPROVED" | "NEEDS_WORK"
397}
398
399/**
400 * A BitBucketServer user account.
401 */
402interface BitBucketServerUser {
403 /** The name of the user */
404 name: string
405 /** The email for the user */
406 emailAddress: string
407 /** The unique user ID */
408 id: number
409 /** The name to use when referencing the user */
410 displayName: string
411 /** Is the account active */
412 active: boolean
413 /** The user's slug for URLs */
414 slug: string
415 /** The type of a user, "NORMAL" being a typical user3 */
416 type: "NORMAL" | "SERVICE"
417}
418
419/**
420 * A BitBucket Server Repo
421 */
422interface BitBucketServerRepo {
423 /** The slug for the repo */
424 slug: string
425 /** The repo name */
426 name?: string
427 /** The type of SCM tool, probably "git" */
428 scmId: string
429 /** Is the repo public? */
430 public: boolean
431 /** Can someone fork this repo? */
432 forkable: boolean
433 /** Links for the projects */
434 links: BitBucketServerLinks<"self" | "clone">
435 /** An abstraction for grouping repos */
436 project: {
437 /** The project unique id */
438 id: number
439 /** The project's human readable project key */
440 key: string
441 /** Is the project publicly available */
442 public: boolean
443 /** The name of the project */
444 name: string
445 /** The project's type */
446 type: string
447 /** Hyperlinks for the project */
448 links: BitBucketServerLinks<"self">
449 }
450}
451
452type BitBucketServerLinks<Names> = {
453 [key in keyof Names]: {
454 href: string
455 name?: string
456 }[]
457}
458
459interface BitBucketServerMergeRef {
460 id: string
461 displayId: string
462 latestCommit: string
463 repository: BitBucketServerRepo
464}
465
466interface BitBucketServerPRActivity {
467 action: "COMMENTED" | "OPENED" | "MERGED" | "DECLINED" | "UPDATED"
468 comment?: BitBucketServerPRComment
469 commentAction?: "ADDED" | "UPDATED"
470 commentAnchor?: {
471 diffType: "COMMIT" | "EFFECTIVE" | "REQUIRED" | "RANGE"
472 line: number
473 lineType: "CONTEXT" | "ADDED" | "REMOVED"
474 fileType: "FROM" | "TO"
475 fromHash: string
476 path: string
477 srcPath: string
478 toHash: string
479 }
480 createdDate: number
481 id: number
482 user: BitBucketServerUser
483}
484
485interface BitBucketServerPRComment {
486 author: BitBucketServerUser
487 comments: BitBucketServerPRActivity[]
488 createdDate: number
489 updatedDate: number
490 id: number
491 permittedOperations: {
492 deletable: boolean
493 editable: boolean
494 }
495 text: string
496 version: number
497 parent?: {
498 id: number
499 }
500}
501
502interface BitBucketServerPagedResponse<T> {
503 size: number
504 limit: number
505 isLastPage: boolean
506 start: number
507 filter: never | null // TODO: remove never
508 nextPageStart: number | null
509 values: T
510}
511
512interface BitBucketServerChangesValueAddCopyModifyDelete {
513 type: "ADD" | "COPY" | "MODIFY" | "DELETE" | "UNKNOWN"
514 path: {
515 toString: string
516 }
517}
518
519interface BitBucketServerChangesValueMove {
520 type: "MOVE"
521 path: {
522 toString: string
523 }
524 srcPath: {
525 toString: string
526 }
527}
528
529// prettier-ignore
530type BitBucketServerChangesValue = BitBucketServerChangesValueAddCopyModifyDelete | BitBucketServerChangesValueMove
531
532/** A platform agnostic reference to a Git commit */
533interface GitCommit {
534 /** The SHA for the commit */
535 sha: string
536 /** Who wrote the commit */
537 author: GitCommitAuthor
538 /** Who deployed the commit */
539 committer: GitCommitAuthor
540 /** The commit message */
541 message: string
542 /** Potential parent commits, and other assorted metadata */
543 tree: any
544 /** SHAs for the commit's parents */
545 parents?: string[]
546 /** Link to the commit */
547 url: string
548}
549
550/** An author of a commit */
551interface GitCommitAuthor {
552 /** The display name for the author */
553 name: string
554 /** The authors email */
555 email: string
556 /** ISO6801 date string */
557 date: string
558}
559/**
560 * The shape of the JSON passed between Danger and a subprocess. It's built
561 * to be expanded in the future.
562 */
563interface DangerJSON {
564 danger: DangerDSLJSONType
565}
566
567/**
568 * The available Peril interface, it is possible that this is not
569 * always up to date with true DSL in Peril, but I'll be giving it
570 * a good shot.
571 */
572
573interface PerilDSL {
574 /**
575 * A set of key:value string based on ENV vars that have
576 * been set to be exposed to your Peril config
577 */
578 env: any
579
580 /**
581 * Allows you to schedule a task declared in your Peril config to run in a certain time-frame,
582 * e.g `runTask("reminder_pr_merge", "in 2 days", { number: 2 })`. For more details on how this
583 * works, see the Peril documentation.
584 * @param taskName the name found in your Peril config
585 * @param time the time interval (uses human-internal module)
586 * @param data data which will be passed through to the script
587 */
588 runTask: (taskName: string, time: string, data: any) => void
589
590 /**
591 * When running a task, the data passed in when the task
592 * was originally scheduled, you can also get this as the first
593 * argument in a default function. Deprecated, use a default export
594 * function. I'll remove this sometime.
595 */
596 data?: any
597}
598
599/**
600 * The root of the Danger JSON DSL.
601 */
602
603interface DangerDSLJSONType {
604 /** The data only version of Git DSL */
605 git: GitJSONDSL
606 /** The data only version of GitHub DSL */
607 github?: GitHubDSL
608 /** The data only version of BitBucket Server DSL */
609 bitbucket_server?: BitBucketServerJSONDSL
610 /** The data only version of BitBucket Cloud DSL */
611 bitbucket_cloud?: BitBucketCloudJSONDSL
612 /** The data only version of GitLab DSL */
613 gitlab?: GitLabDSL
614 /**
615 * Used in the Danger JSON DSL to pass metadata between
616 * processes. It will be undefined when used inside the Danger DSL
617 */
618 settings: {
619 /**
620 * Saves each client re-implementing logic to grab these vars
621 * for their API clients
622 */
623 github: {
624 /** API token for the GitHub client to use */
625 accessToken: string
626 /** Optional URL for enterprise GitHub */
627 baseURL: string | undefined
628 /** Optional headers to add to a request */
629 additionalHeaders: any
630 }
631 /**
632 * This is still a bit of a WIP, but this should
633 * pass args/opts from the original CLI call through
634 * to the process.
635 */
636 cliArgs: CliArgs
637 }
638}
639
640/**
641 * The Danger DSL provides the metadata for introspection
642 * in order to create your own rules.
643 */
644interface DangerDSLType {
645 /**
646 * Details specific to the git changes within the code changes.
647 * Currently, this is just the raw file paths that have been
648 * added, removed or modified.
649 */
650 readonly git: GitDSL
651 /**
652 * The GitHub metadata. This covers things like PR info,
653 * comments and reviews on the PR, label metadata, commits with
654 * GitHub user identities and some useful utility functions
655 * for displaying links to files.
656 *
657 * Strictly speaking, `github` is a nullable type, if you are not using
658 * GitHub then it will be undefined. For the DSL convenience sake though, it
659 * is classed as non-nullable
660 *
661 * Provides an authenticated API so you can work directly
662 * with the GitHub API. This is an instance of the "@octokit/rest" npm
663 * module.
664 *
665 * Finally, if running through Peril on an event other than a PR
666 * this is the full JSON from the webhook. [github-webhook-event-types](https://github.com/orta/github-webhook-event-types) has the full
667 * typings for those webhooks.
668 */
669 readonly github: GitHubDSL
670
671 /**
672 * The BitBucket Server metadata. This covers things like PR info,
673 * comments and reviews on the PR, related issues, commits, comments
674 * and activities.
675 *
676 * Strictly speaking, `bitbucket_server` is a nullable type, if you are not using
677 * BitBucket Server then it will be undefined. For the DSL convenience sake though, it
678 * is classed as non-nullable
679 */
680 readonly bitbucket_server: BitBucketServerDSL
681
682 /**
683 * The BitBucket Cloud metadata. This covers things like PR info,
684 * comments and reviews on the PR, commits, comments, and activities.
685 *
686 * Strictly speaking, `bitbucket_cloud` is a nullable type, if you are not using
687 * BitBucket Cloud then it will be undefined. For the DSL convenience sake though, it
688 * is classed as non-nullable
689 */
690 readonly bitbucket_cloud: BitBucketCloudDSL
691 /**
692 * The GitLab metadata. This covers things like PR info,
693 * comments and reviews on the MR, commits, comments
694 * and activities.
695 *
696 * Strictly speaking, `gitlab` is a nullable type, if you are not using
697 * GitLab then it will be undefined. For the DSL convenience sake though, it
698 * is classed as non-nullable
699 */
700 readonly gitlab: GitLabDSL
701
702 /**
703 * Functions which are globally useful in most Dangerfiles. Right
704 * now, these functions are around making sentences of arrays, or
705 * for making a like of href links easily.
706 */
707 readonly utils: DangerUtilsDSL
708}
709/**
710 * The representation of what running a Dangerfile generates.
711 * This needs to be passed between processes, so data only please.
712 */
713interface DangerResults {
714 /**
715 * Failed messages
716 */
717 fails: Violation[]
718
719 /**
720 * Messages for info
721 */
722 warnings: Violation[]
723
724 /**
725 * A set of messages to show inline
726 */
727 messages: Violation[]
728
729 /**
730 * Markdown messages to attach at the bottom of the comment
731 */
732 markdowns: Violation[]
733
734 github?: {
735 /**
736 * Markdown text which gets added as a summary in the first
737 * page which you see when you click through to the PR results.
738 *
739 * https://github.blog/2022-05-09-supercharging-github-actions-with-job-summaries/ */
740 stepSummary?: string
741 }
742
743 /** Meta information about the runtime evaluation */
744 meta?: {
745 /** E.g. "dangerJS", or "Danger Swift" */
746 runtimeName: string
747 /** e.g. "https://danger.systems/js" */
748 runtimeHref: string
749 }
750}
751
752interface DangerRuntimeContainer extends DangerResults {
753 /**
754 * Asynchronous functions to be run after parsing
755 */
756 scheduled?: any[]
757}
758
759interface DangerInlineResults {
760 /**
761 * Path to the file
762 */
763 file: string
764
765 /**
766 * Line in the file
767 */
768 line: number
769
770 /**
771 * Failed messages
772 */
773 fails: string[]
774
775 /**
776 * Messages for info
777 */
778 warnings: string[]
779
780 /**
781 * A set of messages to show inline
782 */
783 messages: string[]
784
785 /**
786 * Markdown messages to attach at the bottom of the comment
787 */
788 markdowns: string[]
789}
790/**
791 * The Danger Utils DSL contains utility functions
792 * that are specific to universal Danger use-cases.
793 */
794interface DangerUtilsDSL {
795 /**
796 * Creates a link using HTML.
797 *
798 * If `href` and `text` are falsy, null is returned.
799 * If `href` is falsy and `text` is truthy, `text` is returned.
800 * If `href` is truthy and `text` is falsy, an <a> tag is returned with `href` as its href and text value.
801 * Otherwise, if `href` and `text` are truthy, an <a> tag is returned with the `href` and `text` inserted as expected.
802 *
803 * @param {string} href The HTML link's destination.
804 * @param {string} text The HTML link's text.
805 * @returns {string|null} The HTML <a> tag.
806 */
807 href(href: string, text: string): string | null
808
809 /**
810 * Converts an array of strings into a sentence.
811 *
812 * @param {string[]} array The array of strings.
813 * @returns {string} The sentence.
814 */
815 sentence(array: string[]): string
816}
817/** All Text diff values will be this shape */
818interface TextDiff {
819 /** The value before the PR's applied changes */
820 before: string
821 /** The value after the PR's applied changes */
822 after: string
823 /** A string containing the full set of changes */
824 diff: string
825 /** A string containing just the added lines */
826 added: string
827 /** A string containing just the removed lines */
828 removed: string
829}
830
831/** Git diff sliced into chunks */
832interface StructuredDiff {
833 /** Git diff chunks */
834 chunks: File["chunks"]
835 /** The file path pre-change */
836 fromPath: string | undefined
837}
838
839/** The results of running a JSON patch */
840interface JSONPatch {
841 /** The JSON in a file at the PR merge base */
842 before: any
843 /** The JSON in a file from the PR submitter */
844 after: any
845 /** The set of operations to go from one JSON to another JSON */
846 diff: JSONPatchOperation[]
847}
848
849/** An individual operation inside an rfc6902 JSON Patch */
850interface JSONPatchOperation {
851 /** An operation type */
852 op: string
853 /** The JSON keypath which the operation applies on */
854 path: string
855 /** The changes for applied */
856 value: string
857}
858
859/** All JSON diff values will be this shape */
860interface JSONDiffValue {
861 /** The value before the PR's applied changes */
862 before: any
863 /** The value after the PR's applied changes */
864 after: any
865 /** If both before & after are arrays, then you optionally get what is added. Empty if no additional objects. */
866 added?: any[]
867 /** If both before & after are arrays, then you optionally get what is removed. Empty if no removed objects. */
868 removed?: any[]
869}
870
871/** A map of string keys to JSONDiffValue */
872interface JSONDiff {
873 [name: string]: JSONDiffValue
874}
875
876// This is `danger.git`
877
878/**
879 *
880 * The Git Related Metadata which is available inside the Danger DSL JSON
881 *
882 * @namespace JSONDSL
883 */
884
885interface GitJSONDSL {
886 /**
887 * Filepaths with changes relative to the git root
888 */
889 readonly modified_files: string[]
890
891 /**
892 * Newly created filepaths relative to the git root
893 */
894 readonly created_files: string[]
895
896 /**
897 * Removed filepaths relative to the git root
898 */
899 readonly deleted_files: string[]
900
901 /** The Git commit metadata */
902 readonly commits: GitCommit[]
903}
904
905/** The shape of the Chainsmoker response */
906type GitMatchResult = {
907 /** Did any file paths match from the git modified list? */
908 modified: any
909 /** Did any file paths match from the git created list? */
910 created: any
911 /** Did any file paths match from the combination of the git modified and created list? */
912 edited: any
913 /** Did any file paths match from the git deleted list? */
914 deleted: any
915}
916
917/** The git specific metadata for a PR */
918interface GitDSL extends GitJSONDSL {
919 /**
920 * The git commit Danger is comparing from.
921 */
922 base: string
923
924 /**
925 * The git commit Danger is comparing to.
926 */
927 head: string
928
929 /**
930 * A Chainsmoker object to help match paths as an elegant DSL. It
931 * lets you write a globbed string and then get booleans on whether
932 * there are matches within a certain part of the git DSL.
933 *
934 * Use this to create an object which has booleans set on 4 keys
935 * `modified`, `created`, `edited` (created + modified) and `deleted`.
936 *
937 * @example
938 * const packageJSON = danger.git.fileMatch("package.json")
939 * const lockfile = danger.git.fileMatch("yarn.lock")
940 *
941 * if (packageJSON.modified && !lockfile.modified) {
942 * warn("You might have forgotten to run `yarn`.")
943 * }
944 *
945 * @example
946 * const needsSchemaChange = danger.git.fileMatch("src/app/analytics/*.ts")
947 * const schema = danger.git.fileMatch("src/app/analytics/schema.ts")
948 *
949 * if (needsSchemaChange.edited && !schema.modified) {
950 * fail("Changes to the analytics files need to edit update the schema.")
951 * }
952 */
953 fileMatch: Chainsmoker<GitMatchResult>
954
955 /**
956 * Offers the diff for a specific file
957 *
958 * @param {string} filename the path to the json file
959 */
960 diffForFile(filename: string): Promise<TextDiff | null>
961
962 /**
963 * Offers the structured diff for a specific file
964 *
965 * @param {string} filename the path to the json file
966 */
967 structuredDiffForFile(filename: string): Promise<StructuredDiff | null>
968
969 /**
970 * Provides a JSON patch (rfc6902) between the two versions of a JSON file,
971 * returns null if you don't have any changes for the file in the diff.
972 *
973 * Note that if you are looking to just see changes like: before, after, added or removed - you
974 * should use `JSONDiffForFile` instead, as this can be a bit unwieldy for a Dangerfile.
975 *
976 * @param {string} filename the path to the json file
977 */
978 JSONPatchForFile(filename: string): Promise<JSONPatch | null>
979
980 /**
981 * Provides a simplified JSON diff between the two versions of a JSON file. This will always
982 * be an object whose keys represent what has changed inside a JSON file.
983 *
984 * Any changed values will be represented with the same path, but with a different object instead.
985 * This object will always show a `before` and `after` for the changes. If both values are arrays or
986 * objects the `before` and `after`, then there will also be `added` and `removed` inside the object.
987 *
988 * In the case of two objects, the `added` and `removed` will be an array of keys rather than the values.
989 *
990 * This object is represented as `JSONDiffValue` but I don't know how to make TypeScript force
991 * declare that kind of type structure.
992 *
993 * This should make it really easy to do work when specific keypaths have changed inside a JSON file.
994 *
995 * @param {string} filename the path to the json file
996 */
997 JSONDiffForFile(filename: string): Promise<JSONDiff>
998
999 /**
1000 * Offers the overall lines of code added/removed in the diff
1001 *
1002 * @param {string} pattern an option glob pattern to filer files that will considered for lines of code.
1003 */
1004 linesOfCode(pattern?: string): Promise<number | null>
1005}
1006// This is `danger.github` inside the JSON
1007
1008interface GitHubJSONDSL {
1009 /** The issue metadata for a code review session */
1010 issue: GitHubIssue
1011 /** The PR metadata for a code review session */
1012 pr: GitHubPRDSL
1013 /** The PR metadata specifically formatted for using with the GitHub API client */
1014 thisPR: GitHubAPIPR
1015 /** The github commit metadata for a code review session */
1016 commits: GitHubCommit[]
1017 /** The reviews left on this pull request */
1018 reviews: GitHubReview[]
1019 /** The people/teams requested to review this PR */
1020 requested_reviewers: GitHubReviewers
1021}
1022
1023// This is `danger.github`
1024
1025/** The GitHub metadata for your PR */
1026interface GitHubDSL extends GitHubJSONDSL {
1027 /**
1028 * An authenticated API so you can extend danger's behavior by using the [GitHub v3 API](https://developer.github.com/v3/).
1029 *
1030 * A set up instance of the "github" npm module. You can get the full [API here](https://octokit.github.io/node-github/).
1031 */
1032 api: GitHub
1033 /** A scope for useful functions related to GitHub */
1034 utils: GitHubUtilsDSL
1035 /**
1036 * Sets a markdown summary which shows on the overview page for the
1037 * results of all steps in your CI job.
1038 *
1039 * See: https://github.blog/2022-05-09-supercharging-github-actions-with-job-summaries/
1040 */
1041 setSummaryMarkdown: (markdown: string) => void
1042}
1043
1044/** Useful functions for GitHub related work */
1045interface GitHubUtilsDSL {
1046 /**
1047 * Creates HTML for a sentence of clickable links for an array of paths.
1048 * This uses the source of the PR as the target, not the destination repo.
1049 * You can manually set the target repo and branch however, to make it work how you want.
1050 *
1051 * @param {string} paths A list of strings representing file paths
1052 * @param {string} useBasename Show either the file name, or the full path - defaults to just file name e.g. true.
1053 * @param {string} repoSlug An optional override for the repo slug, ex: "orta/ORStackView"
1054 * @param {string} branch An optional override for the branch, ex: "v3"
1055 * @returns {string} A HTML string of <a>'s built as a sentence.
1056 */
1057 fileLinks(paths: string[], useBasename?: boolean, repoSlug?: string, branch?: string): string
1058
1059 /**
1060 * Downloads a file's contents via the GitHub API. You'll want to use
1061 * this instead of `fs.readFile` when aiming to support working with Peril.
1062 *
1063 * @param {string} path The path fo the file that exists
1064 * @param {string} repoSlug An optional reference to the repo's slug: e.g. danger/danger-js
1065 * @param {string} ref An optional reference to a branch/sha
1066 */
1067 fileContents(path: string, repoSlug?: string, ref?: string): Promise<string>
1068
1069 /**
1070 * An API for creating, updating and closing an issue. Basically
1071 * this is really useful for reporting back via a separate
1072 * issue that you may want to keep up to date at all times.
1073 *
1074 * @param {string} id The unique ID for the message to create, close
1075 * @param {string} content the content of the message
1076 * @param {any} config settings for the issue
1077 * @returns {string} A HTML string of <a>'s built as a sentence.
1078 */
1079 createUpdatedIssueWithID: (
1080 id: string,
1081 content: string,
1082 config: { title: string; open: boolean; owner: string; repo: string }
1083 ) => Promise<string>
1084
1085 /**
1086 * An API for creating, or setting a label to an issue. Usable from Peril
1087 * by adding an additional param for settings about a repo.
1088 *
1089 * @param {obj} labelConfig The config for the label
1090 * @param {obj | undefined} Optional: the config for the issue
1091 * @returns {Promise<undefined>} No return value.
1092 */
1093 createOrAddLabel: (
1094 labelConfig: { name: string; color: string; description: string },
1095 repoConfig?: { owner: string; repo: string; id: number }
1096 ) => Promise<void>
1097 createOrUpdatePR: (
1098 config: {
1099 /** PR title */
1100 title: string
1101 /** PR body */
1102 body: string
1103 /** The danger in danger/danger-js - defaults to the PR base name if undefined */
1104 owner?: string
1105 /** The danger-js in danger/danger-js - defaults to the PR base repo if undefined */
1106 repo?: string
1107 /** A message for the commit */
1108 commitMessage: string
1109 /** The name of the branch on the repo */
1110 newBranchName: string
1111 /** Base branch for the new branch e.g. what should Danger create the new branch from */
1112 baseBranch: string
1113 },
1114 fileMap: any
1115 ) => Promise<any>
1116}
1117
1118/**
1119 * This is `danger.github.issue` It refers to the issue that makes up the Pull Request.
1120 * GitHub treats all pull requests as a special type of issue. This DSL contains only parts of the issue that are
1121 * not found in the PR DSL, however it does contain the full JSON structure.
1122 *
1123 * A GitHub Issue
1124 */
1125interface GitHubIssue {
1126 /**
1127 * The labels associated with this issue
1128 */
1129 labels: GitHubIssueLabel[]
1130}
1131
1132// Subtypes specific to issues
1133
1134interface GitHubIssueLabel {
1135 /** The identifying number of this label */
1136 id: number
1137
1138 /** The URL that links to this label */
1139 url: string
1140
1141 /** The name of the label */
1142 name: string
1143
1144 /** The color associated with this label */
1145 color: string
1146}
1147
1148interface GitHubIssueComment {
1149 /**
1150 * UUID for the comment
1151 */
1152 id: string
1153
1154 /**
1155 * The User who made the comment
1156 */
1157 user: GitHubUser
1158
1159 /**
1160 * Textual representation of comment
1161 */
1162 body: string
1163}
1164
1165// This is `danger.github.pr`
1166
1167/**
1168 * An exact copy of the PR's reference JSON. This interface has type'd the majority
1169 * of it for tooling's sake, but any extra metadata which GitHub send will still be
1170 * inside the JS object.
1171 */
1172
1173interface GitHubPRDSL {
1174 /**
1175 * The UUID for the PR
1176 */
1177 number: number
1178
1179 /**
1180 * The state for the PR
1181 */
1182 state: "closed" | "open" | "locked" | "merged"
1183
1184 /**
1185 * Has the PR been locked to contributors only?
1186 */
1187 locked: boolean
1188
1189 /**
1190 * The title of the PR
1191 */
1192 title: string
1193
1194 /**
1195 * The markdown body message of the PR
1196 */
1197 body: string
1198
1199 /**
1200 * ISO6801 Date string for when PR was created
1201 */
1202 created_at: string
1203
1204 /**
1205 * ISO6801 Date string for when PR was updated
1206 */
1207 updated_at: string
1208
1209 /**
1210 * optional ISO6801 Date string for when PR was closed
1211 */
1212 closed_at: string | null
1213
1214 /**
1215 * Optional ISO6801 Date string for when PR was merged.
1216 * Danger probably shouldn't be running in this state.
1217 */
1218 merged_at: string | null
1219
1220 /**
1221 * Merge reference for the _other_ repo.
1222 */
1223 head: GitHubMergeRef
1224
1225 /**
1226 * Merge reference for _this_ repo.
1227 */
1228 base: GitHubMergeRef
1229
1230 /**
1231 * The User who submitted the PR
1232 */
1233 user: GitHubUser
1234
1235 /**
1236 * The User who is assigned the PR
1237 */
1238 assignee: GitHubUser
1239
1240 /**
1241 * The Users who are assigned to the PR
1242 */
1243 assignees: GitHubUser[]
1244
1245 /**
1246 * Is in draft state?
1247 */
1248 draft: boolean
1249
1250 /**
1251 * Has the PR been merged yet?
1252 */
1253 merged: boolean
1254
1255 /**
1256 * The number of comments on the PR
1257 */
1258 comments: number
1259
1260 /**
1261 * The number of review-specific comments on the PR
1262 */
1263 review_comments: number
1264
1265 /**
1266 * The number of commits in the PR
1267 */
1268 commits: number
1269
1270 /**
1271 * The number of additional lines in the PR
1272 */
1273 additions: number
1274
1275 /**
1276 * The number of deleted lines in the PR
1277 */
1278 deletions: number
1279
1280 /**
1281 * The number of changed files in the PR
1282 */
1283 changed_files: number
1284
1285 /**
1286 * The link back to this PR as user-facing
1287 */
1288 html_url: string
1289
1290 /** How does the PR author relate to this repo/org? */
1291 author_association:
1292 | "COLLABORATOR"
1293 | "CONTRIBUTOR"
1294 | "FIRST_TIMER"
1295 | "FIRST_TIME_CONTRIBUTOR"
1296 | "MEMBER"
1297 | "NONE"
1298 | "OWNER"
1299}
1300
1301// These are the individual subtypes of objects inside the larger DSL objects above.
1302
1303/** A GitHub specific implementation of a git commit, it has GitHub user names instead of an email. */
1304interface GitHubCommit {
1305 /** The raw commit metadata */
1306 commit: GitCommit
1307 /** The SHA for the commit */
1308 sha: string
1309 /** the url for the commit on GitHub */
1310 url: string
1311 /** The GitHub user who wrote the code */
1312 author: GitHubUser
1313 /** The GitHub user who shipped the code */
1314 committer: GitHubUser
1315 /** An array of parent commit shas */
1316 parents: any[]
1317}
1318
1319/**
1320 * A GitHub user account.
1321 */
1322interface GitHubUser {
1323 /**
1324 * Generic UUID
1325 */
1326 id: number
1327 /**
1328 * The handle for the user/org
1329 */
1330 login: string
1331 /**
1332 * Whether the user is an org, or a user
1333 */
1334 type: "User" | "Organization" | "Bot"
1335 /**
1336 * The url for a users's image
1337 */
1338 avatar_url: string
1339 /**
1340 * The href for a users's page
1341 */
1342 href: string
1343}
1344
1345/**
1346 * A GitHub Repo
1347 */
1348interface GitHubRepo {
1349 /**
1350 * Generic UUID
1351 */
1352 id: number
1353
1354 /**
1355 * The name of the repo, e.g. "Danger-JS"
1356 */
1357 name: string
1358
1359 /**
1360 * The full name of the owner + repo, e.g. "Danger/Danger-JS"
1361 */
1362 full_name: string
1363
1364 /**
1365 * The owner of the repo
1366 */
1367 owner: GitHubUser
1368
1369 /**
1370 * Is the repo publicly accessible?
1371 */
1372 private: boolean
1373
1374 /**
1375 * The textual description of the repo
1376 */
1377 description: string
1378
1379 /**
1380 * Is the repo a fork?
1381 */
1382 fork: boolean
1383
1384 /**
1385 * Is someone assigned to this PR?
1386 */
1387 assignee: GitHubUser
1388
1389 /**
1390 * Are there people assigned to this PR?
1391 */
1392 assignees: GitHubUser[]
1393 /**
1394 * The root web URL for the repo, e.g. https://github.com/artsy/emission
1395 */
1396 html_url: string
1397}
1398
1399interface GitHubMergeRef {
1400 /**
1401 * The human display name for the merge reference, e.g. "artsy:master"
1402 */
1403 label: string
1404
1405 /**
1406 * The reference point for the merge, e.g. "master"
1407 */
1408 ref: string
1409
1410 /**
1411 * The reference point for the merge, e.g. "704dc55988c6996f69b6873c2424be7d1de67bbe"
1412 */
1413 sha: string
1414
1415 /**
1416 * The user that owns the merge reference e.g. "artsy"
1417 */
1418 user: GitHubUser
1419 /**
1420 * The repo from whch the reference comes from
1421 */
1422 repo: GitHubRepo
1423}
1424
1425/**
1426 * GitHubReview
1427 * While a review is pending, it will only have a user. Once a review is complete, the rest of
1428 * the review attributes will be present
1429 * @export
1430 * @interface GitHubReview
1431 */
1432interface GitHubReview {
1433 /**
1434 * The user requested to review, or the user who has completed the review
1435 */
1436 user: GitHubUser
1437 /**
1438 * If there is a review, this provides the ID for it
1439 */
1440 id?: number
1441
1442 /**
1443 * If there is a review, the body of the review
1444 */
1445 body?: string
1446
1447 /**
1448 * If there is a review, the commit ID this review was made on
1449 */
1450 commit_id?: string
1451
1452 /**
1453 * The state of the review
1454 * APPROVED, REQUEST_CHANGES, COMMENT or PENDING
1455 */
1456 state?: "APPROVED" | "REQUEST_CHANGES" | "COMMENT" | "PENDING"
1457}
1458
1459/** Provides the current PR in an easily used way for params in `github.api` calls */
1460interface GitHubAPIPR {
1461 /** The repo owner */
1462 owner: string
1463 /** The repo name */
1464 repo: string
1465 /**
1466 * The PR number
1467 * @deprecated use `pull_number` instead
1468 */
1469 number: number
1470 /** The PR number */
1471 pull_number: number
1472}
1473
1474interface GitHubReviewers {
1475 /** Users that have been requested */
1476 users: GitHubUser[]
1477 /** Teams that have been requested */
1478 teams: any[]
1479}
1480// getPlatformReviewDSLRepresentation
1481interface GitLabJSONDSL {
1482 /** Info about the repo */
1483 metadata: RepoMetaData
1484 /** Info about the merge request */
1485 mr: Types.ExpandedMergeRequestSchema
1486 /** All the individual commits in the merge request */
1487 commits: Types.CommitSchema[]
1488 /** Merge Request-level MR approvals Configuration */
1489 approvals: Types.MergeRequestLevelMergeRequestApprovalSchema
1490}
1491
1492// danger.gitlab
1493/** The GitLab metadata for your MR */
1494interface GitLabDSL extends GitLabJSONDSL {
1495 utils: {
1496 fileContents(path: string, repoSlug?: string, ref?: string): Promise<string>
1497 addLabels(...labels: string[]): Promise<boolean>
1498 removeLabels(...labels: string[]): Promise<boolean>
1499 }
1500 api: InstanceType<typeof Types.Gitlab>
1501}
1502
1503/** Key details about a repo */
1504interface RepoMetaData {
1505 /** A path like "artsy/eigen" */
1506 repoSlug: string
1507 /** The ID for the pull/merge request "11" */
1508 pullRequestID: string
1509}
1510/**
1511 * The result of user doing warn, message or fail, built this way for
1512 * expansion later.
1513 */
1514interface Violation {
1515 /** The string representation */
1516 message: string
1517
1518 /** Optional path to the file */
1519 file?: string
1520
1521 /** Optional line in the file */
1522 line?: number
1523
1524 /** Optional icon for table (Only valid for messages) */
1525 icon?: string
1526}
1527/**
1528 * Describes the possible arguments that
1529 * could be used when calling the CLI
1530 */
1531interface CliArgs {
1532 /** The base reference used by danger PR (e.g. not master) */
1533 base: string
1534 /** For debugging */
1535 verbose: string
1536 /** Used by danger-js o allow having a custom CI */
1537 externalCiProvider: string
1538 /** Use SDTOUT instead of posting to the code review side */
1539 textOnly: string
1540 /** A custom path for the dangerfile (can also be a remote reference) */
1541 dangerfile: string
1542 /** So you can have many danger runs in one code review */
1543 id: string
1544 /** Use staged changes */
1545 staging?: boolean
1546}
1547
1548// NOTE: if add something new here, you need to change dslGenerator.ts
1549/** A function with a callback function, which Danger wraps in a Promise */
1550type CallbackableFn = (callback: (done: any) => void) => void
1551
1552/**
1553 * Types of things which Danger will schedule for you, it's recommended that you
1554 * just throw in an `async () => { [...] }` function. Can also handle a function
1555 * that has a single 'done' arg.
1556 */
1557type Scheduleable = Promise<any> | Promise<void> | CallbackableFn
1558/**
1559 * A Dangerfile, in Peril, is evaluated as a script, and so async code does not work
1560 * out of the box. By using the `schedule` function you can now register a
1561 * section of code to evaluate across multiple tick cycles.
1562 *
1563 * `schedule` currently handles two types of arguments, either a promise or a function with a resolve arg.
1564 *
1565 * @param {Function} asyncFunction the function to run asynchronously
1566 */
1567declare function schedule(asyncFunction: Scheduleable): void
1568
1569/**
1570 * Highlights critical issues. Message is shown inside a HTML table.
1571 *
1572 * @param {MarkdownString} message the String to output
1573 * @param {string | undefined} file a file which this message should be attached to
1574 * @param {number | undefined} line the line which this message should be attached to
1575 */
1576declare function fail(message: MarkdownString, file?: string, line?: number): void
1577
1578/**
1579 * Highlights low-priority issues. Message is shown inside a HTML table.
1580 *
1581 * @param {MarkdownString} message the String to output
1582 * @param {string | undefined} file a file which this message should be attached to
1583 * @param {number | undefined} line the line which this message should be attached to
1584 */
1585declare function warn(message: MarkdownString, file?: string, line?: number): void
1586
1587/**
1588 * Adds a message to the Danger table, the only difference between this
1589 * and warn is the emoji which shows in the table.
1590 *
1591 * @param {MarkdownString} message the String to output
1592 * @param {string | undefined} file a file which this message should be attached to
1593 * @param {number | undefined} line the line which this message should be attached to
1594 */
1595declare function message(message: MarkdownString, file?: string, line?: number): void
1596/**
1597 * Adds a message to the Danger table, the only difference between this
1598 * and warn is the default emoji which shows in the table.
1599 * You can also specifiy a custom emoji to show in the table for each message
1600 *
1601 * @param {MarkdownString} message the String to output
1602 * @param {{file?: string, line?: string, icon?: MarkdownString}} [opts]
1603 * @param opts.file a file which this message should be attached to
1604 * @param opts.line the line which this message should be attached to
1605 * @param opts.icon icon string or image to show in table, take care not to break table formatting
1606 */
1607declare function message(message: MarkdownString, opts?: { file?: string; line?: number; icon?: MarkdownString }): void
1608
1609/**
1610 * Adds raw markdown into the Danger comment, under the table
1611 *
1612 * @param {MarkdownString} message the String to output
1613 * @param {string | undefined} file a file which this message should be attached to
1614 * @param {number | undefined} line the line which this message should be attached to
1615 */
1616declare function markdown(message: MarkdownString, file?: string, line?: number): void
1617
1618/**
1619 * The root Danger object. This contains all of the metadata you
1620 * will be looking for in order to generate useful rules.
1621 */
1622declare const danger: DangerDSLType
1623
1624/**
1625 * When Peril is running your Dangerfile, the Danger DSL is
1626 * extended with additional options.
1627 */
1628declare const peril: PerilDSL
1629
1630/**
1631 * The current results of a Danger run, this can be useful if you
1632 * are wanting to introspect on whether a build has already failed.
1633 */
1634declare const results: DangerRuntimeContainer
1635type Pattern = string
1636type Path = string
1637type KeyedPatterns<T> = {
1638 readonly [K in keyof T]: Pattern[]
1639}
1640type KeyedPaths<T> = {
1641 readonly [K in keyof T]: Path[]
1642}
1643type _MatchResult<T> = {
1644 readonly [K in keyof T]: boolean
1645}
1646type MatchResult<T> = _MatchResult<T> & {
1647 /** Returns an object containing arrays of matched files instead of the usual boolean values. */
1648 getKeyedPaths(): KeyedPaths<T>
1649}
1650/** A vendored copy of the Chainsmoker module on NPM */
1651type Chainsmoker<T> = (...patterns: Pattern[]) => MatchResult<T>