-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
start moving to Netlify functions due to Azure issues
- Loading branch information
Showing
6 changed files
with
86 additions
and
139 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
'use strict' | ||
|
||
const azureWrapper = require('../util/azureWrapper'); | ||
const extrovert = require('extrovert'); | ||
const pkg = require('../package.json'); | ||
|
||
module.exports = azureWrapper(async function status() { | ||
module.exports = extrovert.toNetlifyFunction(async function status() { | ||
return { ok: 1, version: pkg.version, nodeVersion: process.version }; | ||
}); |
172 changes: 80 additions & 92 deletions
172
webhookGitHubComment/index.js → netlify/functions/webhookGitHubComment.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,81 @@ | ||
'use strict'; | ||
|
||
const axios = require('axios'); | ||
const azureWrapper = require('../util/azureWrapper'); | ||
const createReleaseFromChangelog = require('../src/actions/createReleaseFromChangelog'); | ||
const { createTokenAuth } = require('@octokit/auth-token'); | ||
const config = require('../.config'); | ||
const connect = require('../src/db'); | ||
|
||
const ignoreUsers = new Set(config.ignoreUsers); | ||
|
||
module.exports = azureWrapper(async function webhookGitHubComment(context, req) { | ||
const conn = await connect(); | ||
const Subscriber = conn.model('Subscriber'); | ||
const Task = conn.model('Task'); | ||
|
||
const task = await Task.create({ | ||
method: req.method, | ||
url: req.url, | ||
params: req.body | ||
}); | ||
|
||
const { token } = await createTokenAuth(config.githubAccessTokenForMongoose)(); | ||
|
||
const { action, issue, sender, ref, ref_type } = req.body; | ||
|
||
await task.log(`Action: ${action}`); | ||
|
||
if (action === 'opened' && issue != null) { | ||
// Opened new issue | ||
await task.log('Opened new issue'); | ||
const orgs = await axios.get(sender['organizations_url']).then(res => res.data); | ||
const orgNames = orgs.map(org => org.login); | ||
const orgIds = orgs.map(org => org.id); | ||
|
||
if (ignoreUsers.has(sender.login)) { | ||
return { ok: 1 }; | ||
} | ||
|
||
const subscriber = await Subscriber.findOne({ | ||
$or: [ | ||
{ githubUsername: sender.login }, | ||
{ githubUserId: sender.id }, | ||
{ githubOrganization: { $in: orgNames } }, | ||
{ githubOrganizationId: { $in: orgIds } }, | ||
{ 'githubOrganizationMembers.login': sender.login }, | ||
{ 'githubOrganizationMembers.id': sender.id } | ||
] | ||
}); | ||
|
||
if (subscriber == null) { | ||
return { ok: 1 }; | ||
} | ||
|
||
// Is a subscriber, add priority label | ||
await axios.post(`${issue.url}/labels`, { labels: ['priority'] }, { | ||
headers: { | ||
authorization: `bearer ${token}` | ||
} | ||
}); | ||
|
||
// Send to Slack | ||
const url = 'https://slack.com/api/chat.postMessage'; | ||
await axios.post(url, { | ||
channel: '#pro-notifications', | ||
blocks: [ | ||
{type: 'divider'}, | ||
{ | ||
type: 'section', | ||
text: { | ||
type: 'mrkdwn', | ||
text: `*NEW ISSUE CREATED!* \n\n ${issue.user.login} has posted an issue titled: ${issue.title}` | ||
} | ||
}, | ||
{type: 'divider'}, | ||
{ | ||
type: 'section', | ||
text: { | ||
type: 'mrkdwn', | ||
text: `*DESCRIPTION* \n\n ${issue.body}` | ||
} | ||
}, | ||
] | ||
}, { headers: { authorization: `Bearer ${config.slackToken}` } }); | ||
} else if (ref != null && ref_type === 'tag') { | ||
// Assume tag was created, so create a release | ||
await createReleaseFromChangelog(task)(ref); | ||
} else { | ||
await task.log('Skipped'); | ||
} | ||
|
||
return { ok: 1 }; | ||
'use strict'; | ||
|
||
const axios = require('axios'); | ||
const createReleaseFromChangelog = require('../../src/actions/createReleaseFromChangelog'); | ||
const { createTokenAuth } = require('@octokit/auth-token'); | ||
const config = require('../../.config'); | ||
const connect = require('../../src/db'); | ||
const extrovert = require('extrovert'); | ||
|
||
const ignoreUsers = new Set(config.ignoreUsers); | ||
|
||
module.exports = extrovert.toNetlifyFunction(async function webhookGitHubComment(params) { | ||
const conn = await connect(); | ||
const Subscriber = conn.model('Subscriber'); | ||
|
||
const { token } = await createTokenAuth(config.githubAccessTokenForMongoose)(); | ||
|
||
const { action, issue, sender, ref, ref_type } = req.body; | ||
|
||
if (action === 'opened' && issue != null) { | ||
// Opened new issue | ||
const orgs = await axios.get(sender['organizations_url']).then(res => res.data); | ||
const orgNames = orgs.map(org => org.login); | ||
const orgIds = orgs.map(org => org.id); | ||
|
||
if (ignoreUsers.has(sender.login)) { | ||
return { ok: 1 }; | ||
} | ||
|
||
const subscriber = await Subscriber.findOne({ | ||
$or: [ | ||
{ githubUsername: sender.login }, | ||
{ githubUserId: sender.id }, | ||
{ githubOrganization: { $in: orgNames } }, | ||
{ githubOrganizationId: { $in: orgIds } }, | ||
{ 'githubOrganizationMembers.login': sender.login }, | ||
{ 'githubOrganizationMembers.id': sender.id } | ||
] | ||
}); | ||
|
||
if (subscriber == null) { | ||
return { ok: 1 }; | ||
} | ||
|
||
// Is a subscriber, add priority label | ||
await axios.post(`${issue.url}/labels`, { labels: ['priority'] }, { | ||
headers: { | ||
authorization: `bearer ${token}` | ||
} | ||
}); | ||
|
||
// Send to Slack | ||
const url = 'https://slack.com/api/chat.postMessage'; | ||
await axios.post(url, { | ||
channel: '#pro-notifications', | ||
blocks: [ | ||
{type: 'divider'}, | ||
{ | ||
type: 'section', | ||
text: { | ||
type: 'mrkdwn', | ||
text: `*NEW ISSUE CREATED!* \n\n ${issue.user.login} has posted an issue titled: ${issue.title}` | ||
} | ||
}, | ||
{type: 'divider'}, | ||
{ | ||
type: 'section', | ||
text: { | ||
type: 'mrkdwn', | ||
text: `*DESCRIPTION* \n\n ${issue.body}` | ||
} | ||
}, | ||
] | ||
}, { headers: { authorization: `Bearer ${config.slackToken}` } }); | ||
} else if (ref != null && ref_type === 'tag') { | ||
// Assume tag was created, so create a release | ||
await createReleaseFromChangelog(ref); | ||
} | ||
|
||
return { ok: 1 }; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.