mirror of
https://github.com/fleetdm/fleet
synced 2026-05-16 13:38:43 +00:00
* trivial (comments) * Remove old unfreeze/freeze logic * trivial (clarify comment) * Trivial (fix weird character) * Extrapolate DRI mappings into config. * Explain why this exists * Extrapolate logic * Use extrapolated logic + add 5 second wait time to prevent accidents + clean up * Use extrapolated logic and fix omission in helper * Make freezing actually happen and document usage * In script, don't freeze PRs as long as they're preapproved to be edited by SOMEBODY * Lint fixes
89 lines
3.7 KiB
JavaScript
Vendored
89 lines
3.7 KiB
JavaScript
Vendored
module.exports = {
|
|
|
|
|
|
friendlyName: 'Get "is PR preapproved?"',
|
|
|
|
|
|
description: '',
|
|
|
|
|
|
inputs: {
|
|
prNumber: { type: 'number', example: 382, required: true },
|
|
githubUserToCheck: { type: 'string', example: 'mikermcneil', description: 'If excluded, then this returns `true` if all of the PRs changed paths are preapproved for SOMEONE.' },
|
|
isGithubUserMaintainerOrDoesntMatter: { type: 'boolean', required: true, description: 'Whether (a) the user is a maintainer, or (b) it even matters for this check whether the user is a maintainer.' },// FUTURE: « this could be replaced with an extra GitHub API call herein, but doesn't seem worth it
|
|
},
|
|
|
|
|
|
exits: {
|
|
|
|
success: {
|
|
outputFriendlyName: 'Is PR preapproved?',
|
|
outputDescription: 'Whether the provided GitHub user is the DRI for all changed paths.',
|
|
outputType: 'boolean',
|
|
},
|
|
|
|
},
|
|
|
|
|
|
fn: async function ({prNumber, githubUserToCheck, isGithubUserMaintainerOrDoesntMatter}) {
|
|
|
|
require('assert')(sails.config.custom.githubRepoDRIByPath);
|
|
require('assert')(sails.config.custom.githubAccessToken);
|
|
|
|
let DRI_BY_PATH = sails.config.custom.githubRepoDRIByPath;
|
|
let owner = 'fleetdm';
|
|
let repo = 'fleet';
|
|
let baseHeaders = {
|
|
'User-Agent': 'sails run freeze-open-pull-requests',
|
|
'Authorization': `token ${sails.config.custom.githubAccessToken}`
|
|
};
|
|
|
|
// Check the PR's author versus the intersection of DRIs for all changed files.
|
|
return await sails.helpers.flow.build(async()=>{
|
|
|
|
let isDRIForAllChangedPathsStill = false;
|
|
|
|
// [?] 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)
|
|
|
|
isDRIForAllChangedPathsStill = _.all(changedPaths, (changedPath)=>{
|
|
changedPath = changedPath.replace(/\/+$/,'');// « trim trailing slashes, just in case (b/c otherwise could loop forever)
|
|
|
|
// sails.log.verbose(`…checking DRI of changed path "${changedPath}"`);
|
|
|
|
let selfMergers = DRI_BY_PATH[changedPath] ? [].concat(DRI_BY_PATH[changedPath]) : [];// « ensure array
|
|
if (!githubUserToCheck && selfMergers.length >= 1) {// « not checking a user, so just make sure all these paths are preapproved for SOMEONE
|
|
return true;
|
|
}
|
|
if (githubUserToCheck && (selfMergers.includes(githubUserToCheck.toLowerCase()) || (isGithubUserMaintainerOrDoesntMatter && selfMergers.includes('*')))) {
|
|
return true;
|
|
}//•
|
|
let numRemainingPathsToCheck = changedPath.split('/').length;
|
|
while (numRemainingPathsToCheck > 0) {
|
|
let ancestralPath = changedPath.split('/').slice(0, -1 * numRemainingPathsToCheck).join('/');
|
|
// sails.log.verbose(`…checking DRI of ancestral path "${ancestralPath}" for changed path`);
|
|
let selfMergers = DRI_BY_PATH[ancestralPath] ? [].concat(DRI_BY_PATH[ancestralPath]) : [];// « ensure array
|
|
if (!githubUserToCheck && selfMergers.length >= 1) {// « not checking a user, so just make sure all these paths are preapproved for SOMEONE
|
|
return true;
|
|
}
|
|
if (githubUserToCheck && (selfMergers.includes(githubUserToCheck.toLowerCase()) || (isGithubUserMaintainerOrDoesntMatter && selfMergers.includes('*')))) {
|
|
return true;
|
|
}//•
|
|
numRemainingPathsToCheck--;
|
|
}//∞
|
|
});//∞
|
|
|
|
if (isDRIForAllChangedPathsStill && changedPaths.length < 100) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|