Skip to content

Instantly share code, notes, and snippets.

@antydemant
Last active February 11, 2021 10:04
Show Gist options
  • Save antydemant/0563fc2e92c5245e3d4d3a10c0968c18 to your computer and use it in GitHub Desktop.
Save antydemant/0563fc2e92c5245e3d4d3a10c0968c18 to your computer and use it in GitHub Desktop.
Weekly PRs by Core UI team
const axios = require('axios');
const todayDate = new Date();
const reportStartDate = new Date();
const reportEndDate = new Date();
reportStartDate.setDate(todayDate.getDate());
reportEndDate.setDate(reportStartDate.getDate() - 7);
const baseTeamMembersUrl = 'https://api.github.com/orgs/datarobot/teams/core-ui/members';
const basePullRequestsUrl = 'https://api.github.com/search/issues?';
const baseUserUrl = 'https://api.github.com/users/';
const baseSlackHookUrl = 'https://hooks.slack.com/services/<your_slack_hook_url_to_post_messages_in_the_channel>';
const baseSlackHookPingUrl = 'https://hooks.slack.com/services/<your_slack_hook_url_to_post_test_messages_in_the_channel>';
const queryPullRequestsUrl = `${basePullRequestsUrl}q=${encodeURIComponent(`is:pr org:datarobot archived:false author:MEMBER merged:>${reportEndDate.getFullYear()}-${(`${reportEndDate.getMonth() + 1 }`).padStart(2,'0')}-${(`${reportEndDate.getDate() + 1 }`).padStart(2,'0')}`;
const headers = {
Accept: 'application/vnd.github.v3+json',
Authorization: `Token <your_github_token>`,
};
const excludeUsers = ['djulen', 'chasdr'];
const includeUsers = [];
if(todayDate.getDay() !== 3) {
axios.post(baseSlackHookPingUrl, {
text: 'Today is not Wednesday. Ping System: OK \n GitHub API Query: ' + queryPullRequestsUrl,
});
return;
}
function matchJiraIssue(title) {
const matchedIssues = title.match(/([A-Z][A-Z0-9]+-[0-9]+)/g);
if(!!matchedIssues) {
matchedIssues.forEach((issue) => {
title = title.replace(issue, `<https://datarobot.atlassian.net/browse/${issue}|${issue}>`);
});
}
return title;
}
async function execute() {
const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
let message = '*Core UI: Weekly status for Team Members*\n';
message += `Week *${ months[reportEndDate.getMonth()] } ${ reportEndDate.getDate()} - ${ months[reportStartDate.getMonth()] } ${ reportStartDate.getDate() }* \n`;
const { data } = await axios.get(baseTeamMembersUrl, { headers });
const teamMembers = data.map((member) => member.login).filter((member) => !excludeUsers.includes(member));
teamMembers.push(...includeUsers);
for (const member of teamMembers) {
const { data: prData } = await axios.get(queryPullRequestsUrl.replace('MEMBER', member), { headers });
const { data: userData } = await axios.get(baseUserUrl + member, { headers });
message += `${userData.name || userData.login}\n`;
for (const pull of prData.items) {
const newTitle = matchJiraIssue(pull.title);
const { data: prDetailedInfo } = await axios.get(pull.pull_request.url,{ headers });
const icon = prDetailedInfo.merged ? ':merged:' : ':closed:';
message += `\t ${icon} [<${pull.pull_request.html_url}| PR>] ${newTitle} \n`;
}
}
axios.post(baseSlackHookUrl, {
text: message
});
}
execute();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment