Skip branch protections for edits to certain paths (#4245)

* Stub out a way of automatically poking a hole in branch protections for DRIs

* cleaned things out in there

* prototype of autoapproval automation

prototype of autoapproval automation for DRIs editing their files

* should never happen
This commit is contained in:
Mike McNeil 2022-02-17 03:43:59 -06:00 committed by GitHub
parent 0a59adc2dd
commit c1b9161e25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -158,20 +158,19 @@ module.exports = {
// ╚██╗╚██████╔╝██║ ███████╗██║ ╚████║███████╗██████╔╝ ██╔╝ ███████╗██████╔╝██║ ██║ ███████╗██████╔╝ ██╔╝ ██║ ██║███████╗╚██████╔╝██║ ███████╗██║ ╚████║███████╗██████╔╝██╔╝
// ╚═╝ ╚═════╝ ╚═╝ ╚══════╝╚═╝ ╚═══╝╚══════╝╚═════╝ ╚═╝ ╚══════╝╚═════╝ ╚═╝ ╚═╝ ╚══════╝╚═════╝ ╚═╝ ╚═╝ ╚═╝╚══════╝ ╚═════╝ ╚═╝ ╚══════╝╚═╝ ╚═══╝╚══════╝╚═════╝ ╚═╝
//
// // Handle opened/reopened/edited PR by commenting/labeling/unlabeling it
// // (if appropriate).
// // > Note: If we apply the "needs cleanup" label here, then any subsequent
// // > edits of the PR should trigger a webhook request that causes the bot
// // > to re-examine the PR's title for compliance with the repo guidelines.
// let owner = repository.owner.login;
// let repo = repository.name;
// let issueNumber = issueOrPr.number;
// if (action === 'edited' && pr.state !== 'open') {
// // If this is an edit to an already-closed pull request, then do nothing.
// } else if (action === 'reopened') {
// let wasReopenedByBot = GITHUB_USERNAMES_OF_BOTS_AND_MAINTAINERS.includes(sender.login);
// if (!wasReopenedByBot) {
let owner = repository.owner.login;
let repo = repository.name;
let prNumber = issueOrPr.number;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Want to do more?
//
// For some working, recent, easily-tweaked example code that manages a conversation with the GitHub bot
// to get help submitters of PRs/issues get them up to spec, see:
// https://github.com/fleetdm/fleet/blob/0a59adc2dd65bce5c1201a752e9c218faea7be35/website/api/controllers/webhooks/receive-from-github.js#L145-L216
//
// To potentially reuse:
// let newBotComment =
// `Oh hey again, @${issueOrPr.user.login}. Now that this pull request is reopened, it's on our radar. Please let us know if there's any new information we should be aware of!\n`+
// `<hr/>\n`+
@ -179,40 +178,67 @@ module.exports = {
// `Please remember: never post in a public forum if you believe you've found a genuine security vulnerability. Instead, [disclose it responsibly](https://sailsjs.com/security).\n`+
// `\n`+
// `For help with questions about Sails, [click here](http://sailsjs.com/support).\n`;
// await GitHub.commentOnIssue.with({ comment: newBotComment, issueNumber, owner, repo, credentials });
// }//fi
// } else if (!pr.title.match(/^\[(proposal|patch|implements #\d+|fixes #\d+|misc)\]/i)) {
// await sails.helpers.flow.simultaneously([
// async() => await GitHub.addLabelsToIssue.with({ labels: [ 'needs cleanup' ], issueNumber, owner, repo, credentials }),
// async() => await GitHub.commentOnIssue.with({ comment: `Hi @${pr.user.login}! It looks like the title of your pull request doesn&rsquo;t quite match our [guidelines](https://sailsjs.com/contribute) yet. Would you please edit your pull request's title so that it begins with \`[proposal]\`, \`[patch]\`, \`[fixes #<issue number>]\`, \`[implements #<other PR number>]\`, or \`[misc]\`? Once you've edited it, I'll take another look!`, issueNumber, owner, repo, credentials })
// ]);
// } else {
// let removeNeedsCleanupLabel = pr.labels.some(({name}) => name === 'needs cleanup');
// if (removeNeedsCleanupLabel) {
// await GitHub.removeLabelFromIssue.with({ label: 'needs cleanup', issueNumber, owner, repo, credentials });
// }//fi
// if (action === 'opened' || removeNeedsCleanupLabel) {
// await GitHub.commentOnIssue.with({
// comment:
// `Thanks for submitting this pull request, @${pr.user.login}! We'll look at it ASAP.\n`+
// `\n`+
// `In the mean time, here are some ways you can help speed things along:\n`+
// ` - discuss this pull request with [other contributors](https://gitter.im/balderdashy/sails) and get their feedback. _(Reactions and comments can help us make better decisions, anticipate compatibility problems, and prevent bugs.)_\n`+
// ` - ask [another JavaScript developer](https://gitter.im/balderdashy/sails) to review the files changed in this pull request. _(Peer reviews definitely don't guarantee perfection, but they help catch mistakes and enourage collaborative thinking. Code reviews are so useful that some open source projects require a minimum number of reviews before even considering a merge!)_\n`+
// ` - if appropriate, ask your business to [sponsor your pull request](https://sailsjs.com/support). _(Open source is our passion, and our core maintainers volunteer many of their nights and weekends working on Sails. But you only get so many nights and weekends in life, and stuff gets done a lot faster when you can work on it during normal daylight hours.)_\n`+
// ` - make sure you've answered the "why?" _(Before we can review and merge a pull request, we feel it is important to fully understand the use case: the human reason these changes are important for you, your team, or your organization.)_\n`+
// `<hr/>\n`+
// `\n`+
// `Please remember: never post in a public forum if you believe you've found a genuine security vulnerability. Instead, [disclose it responsibly](https://sailsjs.com/security).\n`+
// `\n`+
// `For help with questions about Sails, [click here](http://sailsjs.com/support).\n`,
// issueNumber,
// owner,
// repo,
// credentials
// });
// }//fi
// }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if (action === 'edited' && pr.state !== 'open') {// PR edited ‡
// This is an edit to an already-closed pull request.
// (Do nothing.)
} else if (action === 'reopened') {// PR reopened ‡
// This is a closed pull request, being reopened.
// (Do nothing.)
} else {// PR opened ‡
let baseHeaders = {
'User-Agent': 'Fleetie pie',
'Authorization': `token ${sails.config.custom.githubAccessToken}`
};
// Check whether auto-approval is warranted.
let isAutoApproved = await sails.helpers.flow.build(async()=>{
let isSenderDRIForAllChangedPaths = false;
let DRI_BY_PATH = {
'handbook/README.md': 'mikermcneil',
// TODO: finish this (see handbook/people for list of DRIs)
};
// [?] https://docs.github.com/en/rest/reference/pulls#list-pull-requests-files
let changedPaths = _.pluck(await sails.helpers.http.get(`https://api.github.com/repos/${owner}/${repo}/pulls/${prNumber}/files`, {
per_page: 100,//eslint-disable-line camelcase
}, baseHeaders), 'filename');// (don't worry, it's the whole path, not the filename)
isSenderDRIForAllChangedPaths = _.all(changedPaths, (changedPath)=>{
changedPath = changedPath.replace(/\/+$/,'');// « trim trailing slashes, just in case (b/c otherwise could loop forever)
require('assert')(sender.login !== undefined);
if (sender.login === DRI_BY_PATH[changedPath]) {
return true;
}
let numRemainingPathsToCheck = changedPath.split('/').length;
while (numRemainingPathsToCheck > 0) {
let ancestralPath = changedPath.split('/').slice(0, -1 * numRemainingPathsToCheck).join('/');
if (sender.login === DRI_BY_PATH[ancestralPath]) {
return true;
}
numRemainingPathsToCheck--;
}//∞
});//∞
if (isSenderDRIForAllChangedPaths && changedPaths.length < 100) {
return true;
} else {
return false;
}
});
// Now, if appropriate, auto-approve the PR.
if (isAutoApproved) {
// [?] https://docs.github.com/en/rest/reference/pulls#create-a-review-for-a-pull-request
await sails.helpers.http.post(`https://api.github.com/repos/${owner}/${repo}/pulls/${prNumber}/reviews`, {
event: 'APPROVE'
}, baseHeaders);
}
}
} else if (ghNoun === 'issue_comment' && ['created'].includes(action) && (issueOrPr&&issueOrPr.state === 'open')) {
// ██████╗ ██████╗ ███╗ ███╗███╗ ███╗███████╗███╗ ██╗████████╗
// ██╔════╝██╔═══██╗████╗ ████║████╗ ████║██╔════╝████╗ ██║╚══██╔══╝