11// @ts -ignore
22import humanId from "human-id" ;
3- import { Application , Context } from "probot" ;
4- import Webhooks from "@octokit/webhooks" ;
3+ import { Probot , Context } from "probot" ;
4+ import { EmitterWebhookEvent } from "@octokit/webhooks" ;
55import { getChangedPackages } from "./get-changed-packages" ;
66import {
77 ReleasePlan ,
@@ -15,27 +15,23 @@ import { ValidationError } from "@changesets/errors";
1515const getReleasePlanMessage = ( releasePlan : ReleasePlan | null ) => {
1616 if ( ! releasePlan ) return "" ;
1717
18- const publishableReleases = releasePlan . releases
19- . filter (
20- (
21- x
22- ) : x is ComprehensiveRelease & { type : Exclude < VersionType , "none" > } =>
23- x . type !== "none"
24- )
18+ const publishableReleases = releasePlan . releases . filter (
19+ ( x ) : x is ComprehensiveRelease & { type : Exclude < VersionType , "none" > } =>
20+ x . type !== "none"
21+ ) ;
2522
2623 let table = markdownTable ( [
2724 [ "Name" , "Type" ] ,
28- ...publishableReleases
29- . map ( ( x ) => {
30- return [
31- x . name ,
32- {
33- major : "Major" ,
34- minor : "Minor" ,
35- patch : "Patch" ,
36- } [ x . type ] ,
37- ] ;
38- } ) ,
25+ ...publishableReleases . map ( ( x ) => {
26+ return [
27+ x . name ,
28+ {
29+ major : "Major" ,
30+ minor : "Minor" ,
31+ patch : "Patch" ,
32+ } [ x . type ] ,
33+ ] ;
34+ } ) ,
3935 ] ) ;
4036
4137 return `<details><summary>This PR includes ${
@@ -101,41 +97,44 @@ ${changedPackages.map((x) => `"${x}": patch`).join("\n")}
10197${ title }
10298` ) ;
10399
104- type PRContext = Context < Webhooks . EventPayloads . WebhookPayloadPullRequest > ;
100+ type PRContext = EmitterWebhookEvent <
101+ "pull_request.opened" | "pull_request.synchronize"
102+ > &
103+ Omit < Context , keyof EmitterWebhookEvent > ;
105104
106105const getCommentId = (
107106 context : PRContext ,
108107 params : { repo : string ; owner : string ; issue_number : number }
109108) =>
110- context . github . issues . listComments ( params ) . then ( ( comments ) => {
109+ context . octokit . issues . listComments ( params ) . then ( ( comments ) => {
111110 const changesetBotComment = comments . data . find (
112111 // TODO: find what the current user is in some way or something
113112 ( comment ) =>
114- comment . user . login === "changeset-bot[bot]" ||
115- comment . user . login === "changesets-test-bot[bot]"
113+ comment . user ? .login === "changeset-bot[bot]" ||
114+ comment . user ? .login === "changesets-test-bot[bot]"
116115 ) ;
117116 return changesetBotComment ? changesetBotComment . id : null ;
118117 } ) ;
119118
120119const hasChangesetBeenAdded = (
121- changedFilesPromise : ReturnType < PRContext [ "github " ] [ "pulls" ] [ "listFiles" ] >
120+ changedFilesPromise : ReturnType < PRContext [ "octokit " ] [ "pulls" ] [ "listFiles" ] >
122121) =>
123122 changedFilesPromise . then ( ( files ) =>
124123 files . data . some (
125124 ( file ) =>
126125 file . status === "added" &&
127126 / ^ \. c h a n g e s e t \/ .+ \. m d $ / . test ( file . filename ) &&
128- file . filename !== ' .changeset/README.md'
127+ file . filename !== " .changeset/README.md"
129128 )
130129 ) ;
131130
132- export default ( app : Application ) => {
131+ export default ( app : Probot ) => {
133132 app . auth ( ) ;
134133 app . log ( "Yay, the app was loaded!" ) ;
135134
136135 app . on (
137136 [ "pull_request.opened" , "pull_request.synchronize" ] ,
138- async ( context : PRContext ) => {
137+ async ( context ) => {
139138 if (
140139 context . payload . pull_request . head . ref . startsWith ( "changeset-release" )
141140 ) {
@@ -153,52 +152,49 @@ export default (app: Application) => {
153152 } ;
154153
155154 const latestCommitSha = context . payload . pull_request . head . sha ;
156- let changedFilesPromise = context . github . pulls . listFiles ( {
155+ let changedFilesPromise = context . octokit . pulls . listFiles ( {
157156 ...repo ,
158157 pull_number : number ,
159158 } ) ;
160159
161- console . log ( context . payload ) ;
162-
163- const [
164- commentId ,
165- hasChangeset ,
166- { changedPackages, releasePlan } ,
167- ] = await Promise . all ( [
168- // we know the comment won't exist on opened events
169- // ok, well like technically that's wrong
170- // but reducing time is nice here so that
171- // deploying this doesn't cost money
172- context . payload . action === "synchronize"
173- ? getCommentId ( context , { ...repo , issue_number : number } )
174- : undefined ,
175- hasChangesetBeenAdded ( changedFilesPromise ) ,
176- getChangedPackages ( {
177- repo : context . payload . pull_request . head . repo . name ,
178- owner : context . payload . pull_request . head . repo . owner . login ,
179- ref : context . payload . pull_request . head . ref ,
180- changedFiles : changedFilesPromise . then ( ( x ) =>
181- x . data . map ( ( x ) => x . filename )
182- ) ,
183- octokit : context . github ,
184- installationToken : (
185- await ( await app . auth ( ) ) . apps . createInstallationAccessToken ( {
186- installation_id : context . payload . installation ! . id ,
187- } )
188- ) . data . token ,
189- } ) . catch ( ( err ) => {
190- if ( err instanceof ValidationError ) {
191- errFromFetchingChangedFiles = `<details><summary>💥 An error occurred when fetching the changed packages and changesets in this PR</summary>\n\n\`\`\`\n${ err . message } \n\`\`\`\n\n</details>\n` ;
192- } else {
193- console . error ( err ) ;
194- captureException ( err ) ;
195- }
196- return {
197- changedPackages : [ "@fake-scope/fake-pkg" ] ,
198- releasePlan : null ,
199- } ;
200- } ) ,
201- ] as const ) ;
160+ const [ commentId , hasChangeset , { changedPackages, releasePlan } ] =
161+ await Promise . all ( [
162+ // we know the comment won't exist on opened events
163+ // ok, well like technically that's wrong
164+ // but reducing time is nice here so that
165+ // deploying this doesn't cost money
166+ context . payload . action === "synchronize"
167+ ? getCommentId ( context , { ...repo , issue_number : number } )
168+ : undefined ,
169+ hasChangesetBeenAdded ( changedFilesPromise ) ,
170+ getChangedPackages ( {
171+ repo : context . payload . pull_request . head . repo . name ,
172+ owner : context . payload . pull_request . head . repo . owner . login ,
173+ ref : context . payload . pull_request . head . ref ,
174+ changedFiles : changedFilesPromise . then ( ( x ) =>
175+ x . data . map ( ( x ) => x . filename )
176+ ) ,
177+ octokit : context . octokit ,
178+ installationToken : (
179+ await (
180+ await app . auth ( )
181+ ) . apps . createInstallationAccessToken ( {
182+ installation_id : context . payload . installation ! . id ,
183+ } )
184+ ) . data . token ,
185+ } ) . catch ( ( err ) => {
186+ if ( err instanceof ValidationError ) {
187+ errFromFetchingChangedFiles = `<details><summary>💥 An error occurred when fetching the changed packages and changesets in this PR</summary>\n\n\`\`\`\n${ err . message } \n\`\`\`\n\n</details>\n` ;
188+ } else {
189+ console . error ( err ) ;
190+ captureException ( err ) ;
191+ }
192+ return {
193+ changedPackages : [ "@fake-scope/fake-pkg" ] ,
194+ releasePlan : null ,
195+ } ;
196+ } ) ,
197+ ] as const ) ;
202198
203199 let addChangesetUrl = `${
204200 context . payload . pull_request . head . repo . html_url
@@ -214,7 +210,6 @@ export default (app: Application) => {
214210
215211 let prComment = {
216212 ...repo ,
217- comment_id : commentId ,
218213 issue_number : number ,
219214 body :
220215 ( hasChangeset
@@ -226,11 +221,13 @@ export default (app: Application) => {
226221 ) ) + errFromFetchingChangedFiles ,
227222 } ;
228223
229- if ( prComment . comment_id != null ) {
230- // @ts -ignore
231- return context . github . issues . updateComment ( prComment ) ;
224+ if ( commentId != null ) {
225+ return context . octokit . issues . updateComment ( {
226+ ...prComment ,
227+ comment_id : commentId ,
228+ } ) ;
232229 }
233- return context . github . issues . createComment ( prComment ) ;
230+ return context . octokit . issues . createComment ( prComment ) ;
234231 } catch ( err ) {
235232 console . error ( err ) ;
236233 throw err ;
0 commit comments