1
1
// @ts -ignore
2
2
import 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" ;
5
5
import { getChangedPackages } from "./get-changed-packages" ;
6
6
import {
7
7
ReleasePlan ,
@@ -15,27 +15,23 @@ import { ValidationError } from "@changesets/errors";
15
15
const getReleasePlanMessage = ( releasePlan : ReleasePlan | null ) => {
16
16
if ( ! releasePlan ) return "" ;
17
17
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
+ ) ;
25
22
26
23
let table = markdownTable ( [
27
24
[ "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
+ } ) ,
39
35
] ) ;
40
36
41
37
return `<details><summary>This PR includes ${
@@ -101,41 +97,44 @@ ${changedPackages.map((x) => `"${x}": patch`).join("\n")}
101
97
${ title }
102
98
` ) ;
103
99
104
- type PRContext = Context < Webhooks . EventPayloads . WebhookPayloadPullRequest > ;
100
+ type PRContext = EmitterWebhookEvent <
101
+ "pull_request.opened" | "pull_request.synchronize"
102
+ > &
103
+ Omit < Context , keyof EmitterWebhookEvent > ;
105
104
106
105
const getCommentId = (
107
106
context : PRContext ,
108
107
params : { repo : string ; owner : string ; issue_number : number }
109
108
) =>
110
- context . github . issues . listComments ( params ) . then ( ( comments ) => {
109
+ context . octokit . issues . listComments ( params ) . then ( ( comments ) => {
111
110
const changesetBotComment = comments . data . find (
112
111
// TODO: find what the current user is in some way or something
113
112
( 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]"
116
115
) ;
117
116
return changesetBotComment ? changesetBotComment . id : null ;
118
117
} ) ;
119
118
120
119
const hasChangesetBeenAdded = (
121
- changedFilesPromise : ReturnType < PRContext [ "github " ] [ "pulls" ] [ "listFiles" ] >
120
+ changedFilesPromise : ReturnType < PRContext [ "octokit " ] [ "pulls" ] [ "listFiles" ] >
122
121
) =>
123
122
changedFilesPromise . then ( ( files ) =>
124
123
files . data . some (
125
124
( file ) =>
126
125
file . status === "added" &&
127
126
/ ^ \. 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"
129
128
)
130
129
) ;
131
130
132
- export default ( app : Application ) => {
131
+ export default ( app : Probot ) => {
133
132
app . auth ( ) ;
134
133
app . log ( "Yay, the app was loaded!" ) ;
135
134
136
135
app . on (
137
136
[ "pull_request.opened" , "pull_request.synchronize" ] ,
138
- async ( context : PRContext ) => {
137
+ async ( context ) => {
139
138
if (
140
139
context . payload . pull_request . head . ref . startsWith ( "changeset-release" )
141
140
) {
@@ -153,52 +152,49 @@ export default (app: Application) => {
153
152
} ;
154
153
155
154
const latestCommitSha = context . payload . pull_request . head . sha ;
156
- let changedFilesPromise = context . github . pulls . listFiles ( {
155
+ let changedFilesPromise = context . octokit . pulls . listFiles ( {
157
156
...repo ,
158
157
pull_number : number ,
159
158
} ) ;
160
159
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 ) ;
202
198
203
199
let addChangesetUrl = `${
204
200
context . payload . pull_request . head . repo . html_url
@@ -214,7 +210,6 @@ export default (app: Application) => {
214
210
215
211
let prComment = {
216
212
...repo ,
217
- comment_id : commentId ,
218
213
issue_number : number ,
219
214
body :
220
215
( hasChangeset
@@ -226,11 +221,13 @@ export default (app: Application) => {
226
221
) ) + errFromFetchingChangedFiles ,
227
222
} ;
228
223
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
+ } ) ;
232
229
}
233
- return context . github . issues . createComment ( prComment ) ;
230
+ return context . octokit . issues . createComment ( prComment ) ;
234
231
} catch ( err ) {
235
232
console . error ( err ) ;
236
233
throw err ;
0 commit comments