Skip to content

Commit f89d29a

Browse files
committed
start moving to Netlify functions due to Azure issues
1 parent 576475a commit f89d29a

File tree

6 files changed

+86
-139
lines changed

6 files changed

+86
-139
lines changed

api_status/function.json

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict'
22

3-
const azureWrapper = require('../util/azureWrapper');
3+
const extrovert = require('extrovert');
44
const pkg = require('../package.json');
55

6-
module.exports = azureWrapper(async function status() {
6+
module.exports = extrovert.toNetlifyFunction(async function status() {
77
return { ok: 1, version: pkg.version, nodeVersion: process.version };
88
});
Lines changed: 80 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,81 @@
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
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"archetype": "0.13.0",
1010
"axios": "0.21.4",
1111
"cheerio": "1.0.0-rc.2",
12+
"extrovert": "0.0.24",
1213
"mongoose": "8.x",
1314
"ramda": "0.28.0"
1415
},

src/actions/createReleaseFromChangelog.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const githubOAuth = require('../integrations/githubOAuth');
44

5-
module.exports = task => async function createReleaseFromChangelog(ref) {
5+
module.exports = async function createReleaseFromChangelog(ref) {
66
if (ref == null) {
77
return;
88
}
@@ -15,7 +15,7 @@ module.exports = task => async function createReleaseFromChangelog(ref) {
1515
}
1616
return 'master';
1717
})();
18-
const changelog = await task.sideEffect(githubOAuth.getChangelog, {
18+
const changelog = await githubOAuth.getChangelog({
1919
branch
2020
});
2121
const lines = changelog.split('\n');
@@ -38,7 +38,5 @@ module.exports = task => async function createReleaseFromChangelog(ref) {
3838
let body = changelogLines;
3939
const tagAndName = body[0].slice(0, body[0].indexOf(' '));
4040
body = body.join('\n');
41-
await task.sideEffect(function createRelease({ tagAndName, body }) {
42-
return githubOAuth.createRelease(tagAndName, body);
43-
}, { tagAndName, body });
41+
return await githubOAuth.createRelease(tagAndName, body);
4442
};

webhookGitHubComment/function.json

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)