From 20afcb8f298dcb77cc94eb197e3455fa687c1035 Mon Sep 17 00:00:00 2001 From: Eric Date: Mon, 1 Aug 2022 22:27:54 -0500 Subject: [PATCH] Add #handbook label to handbook only PRs (#6967) * add #handbook label to handbook PRs * Update get-is-pr-only-handbook-changes.js * remove while loop from _.all() * revert changes to github token name * Simplifications Co-authored-by: Mike McNeil --- .../webhooks/receive-from-github.js | 10 ++++ .../get-is-pr-only-handbook-changes.js | 52 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 website/api/helpers/github-automations/get-is-pr-only-handbook-changes.js diff --git a/website/api/controllers/webhooks/receive-from-github.js b/website/api/controllers/webhooks/receive-from-github.js index 17da2abe77..e19ad8f510 100644 --- a/website/api/controllers/webhooks/receive-from-github.js +++ b/website/api/controllers/webhooks/receive-from-github.js @@ -238,6 +238,8 @@ module.exports = { isGithubUserMaintainerOrDoesntMatter: GITHUB_USERNAMES_OF_BOTS_AND_MAINTAINERS.includes(sender.login.toLowerCase()) }); + let isHandbookPR = await sails.helpers.githubAutomations.getIsPrOnlyHandbookChanges.with({prNumber: prNumber}); + // Check whether the "main" branch is currently frozen (i.e. a feature freeze) // [?] https://docs.mergefreeze.com/web-api#get-freeze-status let mergeFreezeMainBranchStatusReport = await sails.helpers.http.get('https://www.mergefreeze.com/api/branches/fleetdm/fleet/main', { access_token: sails.config.custom.mergeFreezeAccessToken }); //eslint-disable-line camelcase @@ -249,6 +251,14 @@ module.exports = { Date.now() < (new Date('Jul 28, 2022 14:00 UTC')).getTime() ); + // Add the #handbook label to PRs that only make changes to the handbook. + if(isHandbookPR) { + // [?] https://docs.github.com/en/rest/issues/labels#add-labels-to-an-issue + await sails.helpers.http.post(`https://api.github.com/repos/${owner}/${repo}/issue/${prNumber}/labels`, { + labels: '#handbook' + }, baseHeaders); + } + // Now, if appropriate, auto-approve the change. if (isAutoApproved) { // [?] https://docs.github.com/en/rest/reference/pulls#create-a-review-for-a-pull-request diff --git a/website/api/helpers/github-automations/get-is-pr-only-handbook-changes.js b/website/api/helpers/github-automations/get-is-pr-only-handbook-changes.js new file mode 100644 index 0000000000..04f092346e --- /dev/null +++ b/website/api/helpers/github-automations/get-is-pr-only-handbook-changes.js @@ -0,0 +1,52 @@ +module.exports = { + + + friendlyName: 'Get "Is PR only handbook changes"', + + + description: 'Checks each file in a GitHub pull request, and returns true if the PR only changes files in the handbook folder', + + + inputs: { + prNumber: { type: 'number', example: 382, required: true }, + }, + + exits: { + + success: { + outputFriendlyName: 'Is PR only handbook changes', + outputDescription: 'Whether the provided pull request only makes changes to the handbook', + outputType: 'boolean', + }, + + }, + + + fn: async function ({prNumber}) { + + require('assert')(sails.config.custom.githubAccessToken); + + let owner = 'fleetdm'; + let repo = 'fleet'; + let baseHeaders = { + 'User-Agent': 'Fleet labels', + 'Authorization': `token ${sails.config.custom.githubAccessToken}` + }; + + // [?] 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).retry(), 'filename');// (don't worry, it's the whole path, not the filename) + + // Check the path of each file that this PR makes changes to. + let isHandbookOnlyPR = _.all(changedPaths, (changedPath)=>{ + return changedPath.match(/^handbook\//); + });//∞ + + return isHandbookOnlyPR; + + } + + +}; +