|
1 | | -importdotenvfrom"dotenv"; |
2 | | -importfsfrom"fs"; |
3 | | -importhttpfrom"http"; |
4 | | -import{App}from"octokit"; |
5 | | -import{createNodeMiddleware}from"@octokit/webhooks"; |
| 1 | +importdotenvfrom'dotenv' |
| 2 | +importfsfrom'fs' |
| 3 | +importhttpfrom'http' |
| 4 | +import{Octokit,App}from'octokit' |
| 5 | +import{createNodeMiddleware}from'@octokit/webhooks' |
6 | 6 |
|
7 | 7 | // Load environment variables from .env file |
8 | | -dotenv.config(); |
| 8 | +dotenv.config() |
9 | 9 |
|
10 | 10 | // Set configured values |
11 | | -constappId=process.env.APP_ID; |
12 | | -constprivateKeyPath=process.env.PRIVATE_KEY_PATH; |
13 | | -constprivateKey=fs.readFileSync(privateKeyPath,"utf8"); |
14 | | -constsecret=process.env.WEBHOOK_SECRET; |
15 | | -constenterpriseHostname=process.env.ENTERPRISE_HOSTNAME; |
16 | | -constmessageForNewPRs=fs.readFileSync("./message.md","utf8"); |
| 11 | +constappId=process.env.APP_ID |
| 12 | +constprivateKeyPath=process.env.PRIVATE_KEY_PATH |
| 13 | +constprivateKey=fs.readFileSync(privateKeyPath,'utf8') |
| 14 | +constsecret=process.env.WEBHOOK_SECRET |
| 15 | +constenterpriseHostname=process.env.ENTERPRISE_HOSTNAME |
| 16 | +constmessageForNewPRs=fs.readFileSync('./message.md','utf8') |
17 | 17 |
|
18 | 18 | // Create an authenticated Octokit client authenticated as a GitHub App |
19 | 19 | constapp=newApp({ |
20 | | -appId:appId, |
21 | | -privateKey:privateKey, |
| 20 | + appId, |
| 21 | + privateKey, |
22 | 22 | webhooks:{ |
23 | | -secret:secret |
| 23 | + secret |
24 | 24 | }, |
25 | 25 | ...(enterpriseHostname&&{ |
26 | 26 | Octokit:Octokit.defaults({ |
27 | | -baseUrl:`https://${enterpriseHostname}/api/v3`, |
28 | | -}), |
29 | | -}), |
| 27 | +baseUrl:`https://${enterpriseHostname}/api/v3` |
| 28 | +}) |
| 29 | +}) |
30 | 30 | }) |
31 | 31 |
|
32 | 32 | // Optional: Get & log the authenticated app's name |
33 | | -const{ data}=awaitapp.octokit.request("/app"); |
| 33 | +const{ data}=awaitapp.octokit.request('/app') |
34 | 34 |
|
35 | 35 | // Read more about custom logging: https://github.com/octokit/core.js#logging |
36 | | -app.octokit.log.debug(`Authenticated as '${data.name}'`); |
| 36 | +app.octokit.log.debug(`Authenticated as '${data.name}'`) |
37 | 37 |
|
38 | 38 | // Subscribe to the "pull_request.opened" webhook event |
39 | | -app.webhooks.on("pull_request.opened",async({ octokit, payload})=>{ |
40 | | -console.log(`Received a pull request event for #${payload.pull_request.number}`); |
41 | | -awaitoctokit.rest.issues.createComment({ |
42 | | -owner:payload.repository.owner.login, |
43 | | -repo:payload.repository.name, |
44 | | -issue_number:payload.pull_request.number, |
45 | | -body:messageForNewPRs, |
46 | | -}); |
47 | | -}); |
| 39 | +app.webhooks.on('pull_request.opened',async({ octokit, payload})=>{ |
| 40 | +console.log(`Received a pull request event for #${payload.pull_request.number}`) |
| 41 | +try{ |
| 42 | +awaitoctokit.rest.issues.createComment({ |
| 43 | +owner:payload.repository.owner.login, |
| 44 | +repo:payload.repository.name, |
| 45 | +issue_number:payload.pull_request.number, |
| 46 | +body:messageForNewPRs |
| 47 | +}) |
| 48 | +}catch(error){ |
| 49 | +if(error.response){ |
| 50 | +console.error(`Error! Status:${error.response.status}. Message:${error.response.data.message}`) |
| 51 | +}else{ |
| 52 | +console.error(error) |
| 53 | +} |
| 54 | +} |
| 55 | +}) |
48 | 56 |
|
49 | 57 | // Optional: Handle errors |
50 | 58 | app.webhooks.onError((error)=>{ |
51 | | -if(error.name==="AggregateError"){ |
| 59 | +if(error.name==='AggregateError'){ |
52 | 60 | // Log Secret verification errors |
53 | | -console.log(`Error processing request:${error.event}`); |
| 61 | +console.log(`Error processing request:${error.event}`) |
54 | 62 | }else{ |
55 | | -console.log(error); |
| 63 | +console.log(error) |
56 | 64 | } |
57 | | -}); |
| 65 | +}) |
58 | 66 |
|
59 | 67 | // Launch a web server to listen for GitHub webhooks |
60 | | -constport=process.env.PORT||3000; |
61 | | -constpath="/api/webhook"; |
62 | | -constlocalWebhookUrl=`http://localhost:${port}${path}`; |
| 68 | +constport=process.env.PORT||3000 |
| 69 | +constpath='/api/webhook' |
| 70 | +constlocalWebhookUrl=`http://localhost:${port}${path}` |
63 | 71 |
|
64 | 72 | // See https://github.com/octokit/webhooks.js/#createnodemiddleware for all options |
65 | | -constmiddleware=createNodeMiddleware(app.webhooks,{ path}); |
| 73 | +constmiddleware=createNodeMiddleware(app.webhooks,{ path}) |
66 | 74 |
|
67 | | -http.createServer(middleware).listen(port,()=>{ |
68 | | -console.log(`Server is listening for events at:${localWebhookUrl}`); |
| 75 | +http.createServer(middleware).listen(port,()=>{ |
| 76 | +console.log(`Server is listening for events at:${localWebhookUrl}`) |
69 | 77 | console.log('Press Ctrl + C to quit.') |
70 | | -}); |
| 78 | +}) |