Skip to content

Commit

Permalink
Add commit status namespace, add commenting with todos
Browse files Browse the repository at this point in the history
  • Loading branch information
rtsao committed Oct 25, 2017
1 parent 06fd89b commit d08616f
Showing 1 changed file with 35 additions and 12 deletions.
47 changes: 35 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
* LICENSE file in the root directory of this source tree.
*/

const {join} = require('path');

module.exports = robot => {
robot.on('pull_request.opened', check);
robot.on('pull_request.synchronize', check);
robot.on('pull_request.edited', check);

robot.on('issues.closed', reopenTodos);

Expand Down Expand Up @@ -64,13 +61,18 @@ module.exports = robot => {
const params = Object.assign(
{
sha: pr.head.sha,
context: 'TODOs',
context: 'probot/todos',
},
status,
);
return github.repos.createStatus(context.repo(params));
}

setStatus({
state: 'pending',
description: 'Checking for TODOs',
});

const compare = await context.github.repos.compareCommits(
context.repo({
base: pr.base.sha,
Expand All @@ -84,15 +86,22 @@ module.exports = robot => {

const repoUrl = context.payload.repository.html_url;

// Get link to line/blame of TODO
// NOTE: for some reason GitHub breaks these URLs...
function getUrl(todo) {
return join(
function getBlameUrl(todo) {
return [
repoUrl,
'blame',
pr.head.sha,
`${todo.filename}#L${todo.line}`,
);
].join('/');
}

function getBlobUrl(todo) {
return [
repoUrl,
'blob',
pr.head.sha,
`${todo.filename}#L${todo.line}`,
].join('/');
}

const files = await Promise.all(
Expand Down Expand Up @@ -127,11 +136,25 @@ module.exports = robot => {

// Early return if issue numbers are missing
if (missingIssues.length) {
return setStatus({
setStatus({
state: 'failure',
description: 'TODO without open GitHub issue',
target_url: getUrl(missingIssues[0]),
target_url: getBlameUrl(missingIssues[0]),
});

const urls = missingIssues.map(getBlobUrl);

const commentBody = ['Found TODOs without GitHub issues:', ...urls].join(
'\n',
);

context.github.issues.createComment(
context.issue({
body: commentBody,
}),
);

return;
}

const issues = withIssues.map(async todo => {
Expand All @@ -155,7 +178,7 @@ module.exports = robot => {
return setStatus({
state: 'failure',
description: 'No open issue for TODO',
target_url: getUrl(todo),
target_url: getBlameUrl(todo),
});
}
}
Expand Down

0 comments on commit d08616f

Please sign in to comment.