@@ -11,6 +11,17 @@ import {
11
11
isMessageAfterMergeRequired ,
12
12
} from "./src/helpers.js" ;
13
13
14
+ try {
15
+ const packageJson = await import ( "./package.json" , {
16
+ assert : { type : "json" } ,
17
+ } ) ;
18
+ var APP_VERSION = packageJson . default . version ;
19
+ } catch ( err ) {
20
+ console . error ( "Failed to get the version number" ) ;
21
+ }
22
+ console . log ( `Application version: ${ APP_VERSION } ` ) ;
23
+ console . log ( `Website address: ${ process . env . WEBSITE_ADDRESS } ` ) ;
24
+
14
25
// Load environment variables from .env file
15
26
dotenv . config ( ) ;
16
27
@@ -21,14 +32,14 @@ const appId = process.env.APP_ID;
21
32
// Then set GITHUB_APP_PRIVATE_KEY_BASE64 environment variable with the value of ./base64EncodedKey.txt content
22
33
const GITHUB_APP_PRIVATE_KEY = process . env . GITHUB_APP_PRIVATE_KEY_BASE64
23
34
? Buffer . from ( process . env . GITHUB_APP_PRIVATE_KEY_BASE64 , "base64" ) . toString (
24
- "utf8" ,
35
+ "utf8"
25
36
)
26
37
: null ;
27
38
const privateKey =
28
39
GITHUB_APP_PRIVATE_KEY ||
29
40
fs . readFileSync (
30
41
process . env . PRIVATE_KEY_PATH || "./GITHUB_APP_PRIVATE_KEY.pem" ,
31
- "utf8" ,
42
+ "utf8"
32
43
) ;
33
44
const secret = process . env . WEBHOOK_SECRET ;
34
45
const enterpriseHostname = process . env . ENTERPRISE_HOSTNAME ;
@@ -56,26 +67,14 @@ app.octokit.log.debug(`Authenticated as '${data.name}'`);
56
67
// Subscribe to the "pull_request.opened" webhook event
57
68
app . webhooks . on ( "pull_request.opened" , async ( { octokit, payload } ) => {
58
69
console . log (
59
- `Received a pull request event for #${ payload . pull_request . number } by ${ payload . pull_request . user . type } : ${ payload . pull_request . user . login } ` ,
70
+ `Received a pull request event for #${ payload . pull_request . number } by ${ payload . pull_request . user . type } : ${ payload . pull_request . user . login } `
60
71
) ;
61
72
try {
62
73
if ( ! isCLARequired ( payload . pull_request ) ) {
74
+ console . log ( "CLA not required for this PR" ) ;
63
75
return ;
64
76
}
65
77
// If the user is not a member of the organization and haven't yet signed CLA,
66
- // ask them to sign the CLA
67
- const comment = getMessage ( "ask-to-sign-cla" , {
68
- username : payload . pull_request . user . login ,
69
- org : payload . repository . owner . login ,
70
- repo : payload . repository . name ,
71
- pr_number : payload . pull_request . number ,
72
- } ) ;
73
- await octokit . rest . issues . createComment ( {
74
- owner : payload . repository . owner . login ,
75
- repo : payload . repository . name ,
76
- issue_number : payload . pull_request . number ,
77
- body : comment ,
78
- } ) ;
79
78
// Add a label to the PR
80
79
octokit . rest . issues . addLabels ( {
81
80
owner : payload . repository . owner . login ,
@@ -86,7 +85,40 @@ app.webhooks.on("pull_request.opened", async ({ octokit, payload }) => {
86
85
} catch ( error ) {
87
86
if ( error . response ) {
88
87
console . error (
89
- `Error! Status: ${ error . response . status } . Message: ${ error . response . data . message } ` ,
88
+ `Error! Status: ${ error . response . status } . Message: ${ error . response . data . message } `
89
+ ) ;
90
+ } else {
91
+ console . error ( error ) ;
92
+ }
93
+ }
94
+ } ) ;
95
+
96
+ app . webhooks . on ( "pull_request.labeled" , async ( { octokit, payload } ) => {
97
+ const { number, pull_request, label, sender, repository, action } = payload ;
98
+ console . log (
99
+ `Label #${ label . name } ${ action } by ${ sender . login } on ${ pull_request . issue_url } : ${ pull_request . title } `
100
+ ) ;
101
+ try {
102
+ if ( label . name === "Pending CLA" ) {
103
+ console . log ( "Adding comment to the issue/PR to ask for CLA signature" ) ;
104
+ // ask them to sign the CLA
105
+ const comment = getMessage ( "ask-to-sign-cla" , {
106
+ username : pull_request . user . login ,
107
+ org : repository . owner . login ,
108
+ repo : repository . name ,
109
+ pr_number : pull_request . number ,
110
+ } ) ;
111
+ await octokit . rest . issues . createComment ( {
112
+ owner : repository . owner . login ,
113
+ repo : repository . name ,
114
+ issue_number : pull_request . number ,
115
+ body : comment ,
116
+ } ) ;
117
+ }
118
+ } catch ( error ) {
119
+ if ( error . response ) {
120
+ console . error (
121
+ `Error! Status: ${ error . response . status } . Message: ${ error . response . data . message } `
90
122
) ;
91
123
} else {
92
124
console . error ( error ) ;
@@ -96,7 +128,7 @@ app.webhooks.on("pull_request.opened", async ({ octokit, payload }) => {
96
128
97
129
app . webhooks . on ( "pull_request.closed" , async ( { octokit, payload } ) => {
98
130
console . log (
99
- `Closed a pull request event for #${ payload . pull_request . number } ` ,
131
+ `Closed a pull request event for #${ payload . pull_request . number } `
100
132
) ;
101
133
if ( ! payload . pull_request . merged ) return ;
102
134
console . log ( `This PR is merged` ) ;
@@ -120,7 +152,7 @@ app.webhooks.on("pull_request.closed", async ({ octokit, payload }) => {
120
152
} catch ( error ) {
121
153
if ( error . response ) {
122
154
console . error (
123
- `Error! Status: ${ error . response . status } . Message: ${ error . response . data . message } ` ,
155
+ `Error! Status: ${ error . response . status } . Message: ${ error . response . data . message } `
124
156
) ;
125
157
} else {
126
158
console . error ( error ) ;
@@ -140,7 +172,7 @@ app.webhooks.on("issues.opened", async ({ octokit, payload }) => {
140
172
} catch ( error ) {
141
173
if ( error . response ) {
142
174
console . error (
143
- `Error! Status: ${ error . response . status } . Message: ${ error . response . data . message } ` ,
175
+ `Error! Status: ${ error . response . status } . Message: ${ error . response . data . message } `
144
176
) ;
145
177
} else {
146
178
console . error ( error ) ;
206
238
. listen ( port , ( ) => {
207
239
console . log ( `Server is listening for events at: ${ localWebhookUrl } ` ) ;
208
240
console . log (
209
- "Server is also serving the homepage at: http://localhost:" + port ,
241
+ "Server is also serving the homepage at: http://localhost:" + port
210
242
) ;
211
243
console . log ( "Press Ctrl + C to quit." ) ;
212
244
} ) ;
0 commit comments