Skip to content

Instantly share code, notes, and snippets.

@pavelvlasov
Created November 2, 2018 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pavelvlasov/020a88472d0fdf867f9b5c45c7699540 to your computer and use it in GitHub Desktop.
Save pavelvlasov/020a88472d0fdf867f9b5c45c7699540 to your computer and use it in GitHub Desktop.
'use strict';
const {promisify} = require('util');
const request = promisify(require('request'));
const Promise = global.Promise;
const getPullRequestsQuery = (owner, name) => {
return `
query GET_REVIEWS_QUERY {
repository(owner:"${owner}", name:"${name}") {
pullRequests(first:10,states:OPEN) {
edges {
node {
id
title
createdAt
reviewRequests(first:10) {
edges {
node {
id
requestedReviewer {
... on User {
login
}
}
}
}
}
reviews(first:10) {
edges {
node {
id
author {
... on User {
login
}
}
}
}
}
}
}
}
}
}
`;
};
const createCommentMutation = (pullRequestId, comment) => {
return `
mutation {
addComment(input:{subjectId:"${pullRequestId}", body:"${comment}"}) {
clientMutationId
}
}
`;
}
const sendGitHubQuery = async (token, query) => {
const response = await request({
method: 'post',
url: 'https://api.github.com/graphql',
json: {query},
headers: {
Authorization: `Bearer ${token}`,
['User-Agent']: "PR nudger",
['Content-Type']: 'application/json'
}
});
return response.body && response.body.data;
}
module.exports = async function (context) {
const GITHUB_TOKEN = context.secrets.GITHUB_TOKEN;
const OWNER = context.secrets.OWNER;
const REPO = context.secrets.REPO;
const data = await sendGitHubQuery(GITHUB_TOKEN, getPullRequestsQuery(OWNER, REPO));
const sendReviewReminderIfNeeded = async (pullRequest) => {
const reviewRequests = pullRequest.reviewRequests.edges.map(edge => edge.node);
if (reviewRequests.length > 0) {
const comment = `hey ${reviewRequests.map(reviewRequest => `@${reviewRequest.requestedReviewer.login}`).join(' ')}
this pull request awaiting your review
`;
await sendGitHubQuery(GITHUB_TOKEN, createCommentMutation(pullRequest.id, comment));
}
}
if (data.repository && data.repository.pullRequests) {
const pullRequests = data.repository.pullRequests.edges.map(edge => edge.node);
await Promise.all(pullRequests.map(sendReviewReminderIfNeeded));
}
return 'OK';
};
@pavelvlasov
Copy link
Author

GitHub pull reminder hook build on http://webtask.io.

Set secrets to

GITHUB_TOKEN=<your personal github access token>
OWNER=<repo owner>
REPO=<repo name>

Setup schedule to whenever you want it to be executed, e.g. every 3 hours, every hour and etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment