Skip to content

Commit

Permalink
start moving to Netlify functions due to Azure issues
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Dec 18, 2024
1 parent 576475a commit f89d29a
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 139 deletions.
21 changes: 0 additions & 21 deletions api_status/function.json

This file was deleted.

4 changes: 2 additions & 2 deletions api_status/index.js → netlify/functions/status.js
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 };
});
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 };
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"archetype": "0.13.0",
"axios": "0.21.4",
"cheerio": "1.0.0-rc.2",
"extrovert": "0.0.24",
"mongoose": "8.x",
"ramda": "0.28.0"
},
Expand Down
8 changes: 3 additions & 5 deletions src/actions/createReleaseFromChangelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

module.exports = task => async function createReleaseFromChangelog(ref) {
module.exports = async function createReleaseFromChangelog(ref) {
if (ref == null) {
return;
}
Expand All @@ -15,7 +15,7 @@ module.exports = task => async function createReleaseFromChangelog(ref) {
}
return 'master';
})();
const changelog = await task.sideEffect(githubOAuth.getChangelog, {
const changelog = await githubOAuth.getChangelog({
branch
});
const lines = changelog.split('\n');
Expand All @@ -38,7 +38,5 @@ module.exports = task => async function createReleaseFromChangelog(ref) {
let body = changelogLines;
const tagAndName = body[0].slice(0, body[0].indexOf(' '));
body = body.join('\n');
await task.sideEffect(function createRelease({ tagAndName, body }) {
return githubOAuth.createRelease(tagAndName, body);
}, { tagAndName, body });
return await githubOAuth.createRelease(tagAndName, body);
};
19 changes: 0 additions & 19 deletions webhookGitHubComment/function.json

This file was deleted.

0 comments on commit f89d29a

Please sign in to comment.