How I sync Github pull request status to Asana task flow

function match(toMatch) {
let result = toMatch.match(/#(ref)?([0–9]{16})|([0–9]{16})/g);
if (result) {
return result.map(item => item.replace(‘#’, ‘’).replace(‘ref’, ‘’));
}
}
// github.js
getAsanaIds: async data => {
let ids = [];
// check title
const title = data[‘pull_request’][‘title’];
const matchTitle = match(title);
if (matchTitle) matchTitle.forEach(item => ids.push(item));
// check body
const body = data[‘pull_request’][‘body’];
const matchBody = match(body);
if (matchBody) matchBody.forEach(item => ids.push(item));
// check commits
const commits = await getCommits(data);
for (const commit of commits) {
const matchCommit = await match(commit[‘commit’][‘message’]);
if (matchCommit) matchCommit.forEach(item => ids.push(item));
}
// check comments and review comments
const comments = (await getComments(data)).concat(
await getReviewComments(data),
);
for (const comment of comments) {
const matchComment = await match(comment[‘body’]);
if (matchComment) matchComment.forEach(item => ids.push(item));
}
if (ids.length === 0) throw Error(‘No Asana task ID found!’);const uniqueIds = […new Set(ids)];
return uniqueIds;
},
// asana.js
getAsanaTask: async asanaId => {
const task = await client.tasks.findById(asanaId);
if (!task) throw Error(‘Failed to find Asana task with id: ‘ + asanaId);
return task;
},
// github.js
getAsanaSectionId: (asanaSections, data) => {
let section;
if (data.merged === false && data.state === ‘open’) {
if (data.base === ‘develop’) section = ‘in review’;
if (data.base === ‘release’) section = ‘staging ready’;
if (data.base === ‘master’) section = ‘production ready’;
}
if (data.merged === true && data.state == ‘closed’) {
if (data.base === ‘develop’) section = ‘on test’;
if (data.base === ‘release’) section = ‘on staging’;
if (data.base === ‘master’) section = ‘done’;
}
for (const item of Object.keys(asanaSections)) {
if (item.toLowerCase().includes(section)) {
return asanaSections[item];
}
}
}
// asana.js
addAsanaTask: async ({ asanaId, projectId, sectionId }) => {
const data = {
project: projectId,
section: sectionId,
};
const result = await client.tasks.addProject(asanaId, data);
if (Object.keys(result).length != 0) {
throw Error(“Failed to change Asana task’s section!”);
}
},
// github.js
getPullRequestData: async data => {
let commit_urls = [];
const commits = await getCommits(data);
for (const commit of commits) {
const item = ` ${commit[‘html_url’]} — ${commit[‘commit’][‘message’]} — ${commit[‘committer’][‘login’]}`;
commit_urls.push(item);
}
return {
title: data[‘pull_request’][‘title’],
body: data[‘pull_request’][‘body’],
url: data[‘pull_request’][‘html_url’],
state: data[‘pull_request’][‘state’],
user: {
login: data[‘pull_request’][‘user’][‘login’],
},
head: data[‘pull_request’][‘head’][‘ref’],
base: data[‘pull_request’][‘base’][‘ref’],
merged: data[‘pull_request’][‘merged’],
commits: commit_urls,
};
}
// asana.js
addComment: async (asanaId, githubData) => {
const comment = {
text: `Pull Request ${githubData.url} from ${githubData.user.login}
Title: ${githubData.title} — Body: ${githubData.body}
From: ${githubData.head} — To: ${githubData.base} — State: ${githubData.state} — Merged: ${githubData.merged}
Commits: ${githubData.commits}`,
};
const story = await client.tasks.addComment(asanaId, comment);if (!story)
throw Error(
‘Failed to add comment to Asana task with id: ‘ + asanaId,
);
},
async function notify(githubData) {
const text = `Pull Request ${githubData.url} from ${githubData.user.login}
Title: ${githubData.title} — Body: ${githubData.body}
From: ${githubData.head} — To: ${githubData.base}
State: ${githubData.state} — Merged: ${githubData.merged}`;
try {
const channel =
githubData.base === ‘master’
? ‘#pull-requests-master’
: ‘#pull-requests’;
const res = await bot.chat.meMessage({
token,
channel,
text,
});
if (res.ok) console.log(‘Notified Slack successfully’);
} catch (e) {
console.log(‘Failed to notify Slack’, e);
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Hung Tran Nguyen

From translator -> Manual QC Engineer -> Automation QA Engineer -> NodeJS Developer -> yearning to learn more