1- 'use strict' ;
2-
3- const axios = require ( 'axios' ) ;
4- const azureWrapper = require ( '../util/azureWrapper' ) ;
5- const createReleaseFromChangelog = require ( '../src/actions/createReleaseFromChangelog' ) ;
6- const { createTokenAuth } = require ( '@octokit/auth-token' ) ;
7- const config = require ( '../.config' ) ;
8- const connect = require ( '../src/db' ) ;
9-
10- const ignoreUsers = new Set ( config . ignoreUsers ) ;
11-
12- module . exports = azureWrapper ( async function webhookGitHubComment ( context , req ) {
13- const conn = await connect ( ) ;
14- const Subscriber = conn . model ( 'Subscriber' ) ;
15- const Task = conn . model ( 'Task' ) ;
16-
17- const task = await Task . create ( {
18- method : req . method ,
19- url : req . url ,
20- params : req . body
21- } ) ;
22-
23- const { token } = await createTokenAuth ( config . githubAccessTokenForMongoose ) ( ) ;
24-
25- const { action, issue, sender, ref, ref_type } = req . body ;
26-
27- await task . log ( `Action: ${ action } ` ) ;
28-
29- if ( action === 'opened' && issue != null ) {
30- // Opened new issue
31- await task . log ( 'Opened new issue' ) ;
32- const orgs = await axios . get ( sender [ 'organizations_url' ] ) . then ( res => res . data ) ;
33- const orgNames = orgs . map ( org => org . login ) ;
34- const orgIds = orgs . map ( org => org . id ) ;
35-
36- if ( ignoreUsers . has ( sender . login ) ) {
37- return { ok : 1 } ;
38- }
39-
40- const subscriber = await Subscriber . findOne ( {
41- $or : [
42- { githubUsername : sender . login } ,
43- { githubUserId : sender . id } ,
44- { githubOrganization : { $in : orgNames } } ,
45- { githubOrganizationId : { $in : orgIds } } ,
46- { 'githubOrganizationMembers.login' : sender . login } ,
47- { 'githubOrganizationMembers.id' : sender . id }
48- ]
49- } ) ;
50-
51- if ( subscriber == null ) {
52- return { ok : 1 } ;
53- }
54-
55- // Is a subscriber, add priority label
56- await axios . post ( `${ issue . url } /labels` , { labels : [ 'priority' ] } , {
57- headers : {
58- authorization : `bearer ${ token } `
59- }
60- } ) ;
61-
62- // Send to Slack
63- const url = 'https://slack.com/api/chat.postMessage' ;
64- await axios . post ( url , {
65- channel : '#pro-notifications' ,
66- blocks : [
67- { type : 'divider' } ,
68- {
69- type : 'section' ,
70- text : {
71- type : 'mrkdwn' ,
72- text : `*NEW ISSUE CREATED!* \n\n ${ issue . user . login } has posted an issue titled: ${ issue . title } `
73- }
74- } ,
75- { type : 'divider' } ,
76- {
77- type : 'section' ,
78- text : {
79- type : 'mrkdwn' ,
80- text : `*DESCRIPTION* \n\n ${ issue . body } `
81- }
82- } ,
83- ]
84- } , { headers : { authorization : `Bearer ${ config . slackToken } ` } } ) ;
85- } else if ( ref != null && ref_type === 'tag' ) {
86- // Assume tag was created, so create a release
87- await createReleaseFromChangelog ( task ) ( ref ) ;
88- } else {
89- await task . log ( 'Skipped' ) ;
90- }
91-
92- return { ok : 1 } ;
1+ 'use strict' ;
2+
3+ const axios = require ( 'axios' ) ;
4+ const createReleaseFromChangelog = require ( '../../src/actions/createReleaseFromChangelog' ) ;
5+ const { createTokenAuth } = require ( '@octokit/auth-token' ) ;
6+ const config = require ( '../../.config' ) ;
7+ const connect = require ( '../../src/db' ) ;
8+ const extrovert = require ( 'extrovert' ) ;
9+
10+ const ignoreUsers = new Set ( config . ignoreUsers ) ;
11+
12+ module . exports = extrovert . toNetlifyFunction ( async function webhookGitHubComment ( params ) {
13+ const conn = await connect ( ) ;
14+ const Subscriber = conn . model ( 'Subscriber' ) ;
15+
16+ const { token } = await createTokenAuth ( config . githubAccessTokenForMongoose ) ( ) ;
17+
18+ const { action, issue, sender, ref, ref_type } = req . body ;
19+
20+ if ( action === 'opened' && issue != null ) {
21+ // Opened new issue
22+ const orgs = await axios . get ( sender [ 'organizations_url' ] ) . then ( res => res . data ) ;
23+ const orgNames = orgs . map ( org => org . login ) ;
24+ const orgIds = orgs . map ( org => org . id ) ;
25+
26+ if ( ignoreUsers . has ( sender . login ) ) {
27+ return { ok : 1 } ;
28+ }
29+
30+ const subscriber = await Subscriber . findOne ( {
31+ $or : [
32+ { githubUsername : sender . login } ,
33+ { githubUserId : sender . id } ,
34+ { githubOrganization : { $in : orgNames } } ,
35+ { githubOrganizationId : { $in : orgIds } } ,
36+ { 'githubOrganizationMembers.login' : sender . login } ,
37+ { 'githubOrganizationMembers.id' : sender . id }
38+ ]
39+ } ) ;
40+
41+ if ( subscriber == null ) {
42+ return { ok : 1 } ;
43+ }
44+
45+ // Is a subscriber, add priority label
46+ await axios . post ( `${ issue . url } /labels` , { labels : [ 'priority' ] } , {
47+ headers : {
48+ authorization : `bearer ${ token } `
49+ }
50+ } ) ;
51+
52+ // Send to Slack
53+ const url = 'https://slack.com/api/chat.postMessage' ;
54+ await axios . post ( url , {
55+ channel : '#pro-notifications' ,
56+ blocks : [
57+ { type : 'divider' } ,
58+ {
59+ type : 'section' ,
60+ text : {
61+ type : 'mrkdwn' ,
62+ text : `*NEW ISSUE CREATED!* \n\n ${ issue . user . login } has posted an issue titled: ${ issue . title } `
63+ }
64+ } ,
65+ { type : 'divider' } ,
66+ {
67+ type : 'section' ,
68+ text : {
69+ type : 'mrkdwn' ,
70+ text : `*DESCRIPTION* \n\n ${ issue . body } `
71+ }
72+ } ,
73+ ]
74+ } , { headers : { authorization : `Bearer ${ config . slackToken } ` } } ) ;
75+ } else if ( ref != null && ref_type === 'tag' ) {
76+ // Assume tag was created, so create a release
77+ await createReleaseFromChangelog ( ref ) ;
78+ }
79+
80+ return { ok : 1 } ;
9381} ) ;
0 commit comments