1
0
mirror of synced 2025-12-23 11:54:18 -05:00

new script to add a comment to all open PRs in docs-internal

This commit is contained in:
Sarah Schneider
2021-05-17 15:41:46 -04:00
parent 0235ab938c
commit 805ddfec49
2 changed files with 90 additions and 1 deletions

View File

@@ -0,0 +1,57 @@
#!/usr/bin/env node
const { listPulls, createReviewComment } = require('../helpers/git-utils')
// [start-readme]
//
// This script finds all open PRs from active branches that touch content files, and adds a comment
// with a prompt to enter a slash command and update files. The idea is to help writers and other Hubbers
// update their open branches and mitigate conflicts with the main branch.
//
// [end-readme]
// check for required PAT
if (!process.env.GITHUB_TOKEN) {
console.error('Error! You must have a GITHUB_TOKEN set in an .env file to run this script.')
process.exit(1)
}
const options = {
owner: 'github',
repo: 'docs-internal'
}
const comment = `
Hello! The docs-engineering team has just published an update that touches all content files in the repo. To reduce conflicts with \`main\`, we wanted to offer a programmatic option to update your branch.
To take advantage of this option, in a new comment on this PR, type and enter: \`/update-content-in-branch\`.
This is a custom slash command that will trigger a GitHub Action workflow to do the following:
1. Check out your branch.
2. Run some special scripts.
3. Commit and push the results back to your branch.
Once that's done, you can update from \`main\`. You may still have some conflicts to resolve.
Feel free to ask if you have questions or need help!
For a 5min demo of what the scripts do and why they're needed, check out https://www.loom.com/share/fa6501580b2a44d7a8a4357ee51e0c99.
`
main()
async function main () {
const allPulls = await listPulls(options.owner, options.repo)
// get the URL of open PRs only
const openPullNumbers = allPulls
.filter(pull => pull.state === 'open')
.map(pull => pull.number)
// for every open PR, create a review comment
await Promise.all(openPullNumbers.map(async (pullNumber) => {
await createReviewComment(options.owner, options.repo, pullNumber, comment)
console.log(`added a comment to PR #${pullNumber}`)
}))
}

View File

@@ -95,11 +95,43 @@ async function getContents (owner, repo, ref, path) {
} }
} }
// https://docs.github.com/en/rest/reference/pulls#list-pull-requests
async function listPulls (owner, repo) {
try {
const { data } = await github.pulls.list({
owner,
repo,
per_page: 100
})
return data
} catch (err) {
console.log(`error listing pulls in ${owner}/${repo}`)
throw (err)
}
}
async function createReviewComment (owner, repo, pullNumber, body) {
try {
const { data } = await github.pulls.createReviewComment({
owner,
repo,
pull_number: pullNumber,
body
})
return data
} catch (err) {
console.log(`error creating a review comment on PR ${pullNumber} in ${owner}/${repo}`)
throw (err)
}
}
module.exports = { module.exports = {
getTree, getTree,
getTreeSha, getTreeSha,
getCommitSha, getCommitSha,
getContentsForBlob, getContentsForBlob,
getContents, getContents,
listMatchingRefs listMatchingRefs,
listPulls,
createReviewComment
} }