Created
November 2, 2018 02:33
-
-
Save pavelvlasov/020a88472d0fdf867f9b5c45c7699540 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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'; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
GitHub pull reminder hook build on http://webtask.io.
Set secrets to
Setup schedule to whenever you want it to be executed, e.g. every 3 hours, every hour and etc.