Skip to content

Commit

Permalink
debug21
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianCArnold committed Mar 16, 2023
1 parent f962a46 commit b302213
Show file tree
Hide file tree
Showing 8 changed files with 1,530 additions and 123 deletions.
70 changes: 40 additions & 30 deletions build/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,46 +54,56 @@ function fetch_config() {
return yaml_1.default.parse(ymlContent);
});
}
function get_reviews() {
function fetch_changed_files() {
return __awaiter(this, void 0, void 0, function* () {
const octokit = get_octokit();
const context = get_context();
if (!context.payload.pull_request) {
throw 'No pull request found.';
}
let reviewsResult = yield octokit.pulls.listReviews({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number
});
return reviewsResult.data;
const octokit = get_octokit();
const changed_files = [];
const per_page = 100;
let page = 0;
let number_of_files_in_current_page;
do {
page += 1;
const { data: response_body } = yield octokit.pulls.listFiles({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
page,
per_page,
});
number_of_files_in_current_page = response_body.length;
changed_files.push(...response_body.map((file) => file.filename));
} while (number_of_files_in_current_page === per_page);
return changed_files;
});
}
function explainStatus(group, approvers, totalRequired) {
function get_reviews() {
return __awaiter(this, void 0, void 0, function* () {
const octokit = get_octokit();
const context = get_context();
let missingRequired = totalRequired;
let fullText = "";
for (let member in approvers) {
if (approvers[member]) {
missingRequired--;
}
fullText += `${member} ${approvers[member] ? '✅' : '❌'}\n`;
if (!context.payload.pull_request) {
throw 'No pull request found.';
}
yield octokit.checks.create({
owner: context.repo.owner,
repo: context.repo.repo,
head_sha: context.sha,
name: "my-check-name",
status: "completed",
conclusion: missingRequired > 0 ? 'failure' : 'success',
output: {
title: 'Required Approvers',
summary: 'Missing',
text: fullText
}
});
const result = [];
const per_page = 100;
let page = 0;
let number_of_files_in_current_page;
do {
page += 1;
const reviewsResult = yield octokit.pulls.listReviews({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
page: page,
per_page: per_page
});
number_of_files_in_current_page = reviewsResult.data.length;
result.push(...reviewsResult.data);
} while (number_of_files_in_current_page === per_page);
return result;
});
}
let cacheContext = null;
Expand All @@ -107,5 +117,5 @@ let get_octokit = () => cacheOctoKit || (cacheOctoKit = github.getOctokit(get_to
exports.default = {
fetch_config,
get_reviews,
explainStatus
fetch_changed_files
};
46 changes: 37 additions & 9 deletions build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core"));
const minimatch = __importStar(require("minimatch"));
const github_1 = __importDefault(require("./github"));
function run() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
core.info('Fetching configuration...');
let config;
Expand All @@ -57,13 +59,20 @@ function run() {
let requirementCounts = {};
let requirementMembers = {};
core.debug('Retrieving required group configurations...');
for (let req in config.groups) {
let { affected: affectedGroups, unaffected: unaffectedGroups } = identifyGroupsByChangedFiles(config, yield github_1.default.fetch_changed_files());
for (let req in affectedGroups) {
core.debug(` - Group: ${req}`);
requirementCounts[req] = config.groups[req].required;
if (affectedGroups[req].required == undefined) {
core.warning(' - Group Required Count not specified, assuming 1 approver from group required.');
affectedGroups[req].required = 1;
}
else {
requirementCounts[req] = (_a = affectedGroups[req].required) !== null && _a !== void 0 ? _a : 1;
}
requirementMembers[req] = {};
core.debug(` - Requiring ${config.groups[req].required} of the following:`);
for (let i in config.groups[req].members) {
let member = config.groups[req].members[i];
core.debug(` - Requiring ${affectedGroups[req].required} of the following:`);
for (let i in affectedGroups[req].members) {
let member = affectedGroups[req].members[i];
requirementMembers[req][member] = false;
core.debug(` - ${member}`);
}
Expand Down Expand Up @@ -111,9 +120,9 @@ function run() {
if (groupApprovalCount >= groupApprovalRequired) {
//Enough Approvers
core.startGroup(`We have all required approval(s) from group: ${group}.`);
let appCount = 1;
let appCount = 0;
for (let approval in groupApprovedStrings) {
core.info(`(${appCount++}/${groupApprovalRequired}) ✅ ${groupApprovedStrings[approval]}`);
core.info(`(${++appCount}/${groupApprovalRequired}) ✅ ${groupApprovedStrings[approval]}`);
}
for (let unapproval in groupNotApprovedStrings) {
core.info(`(${appCount}/${groupApprovalRequired}) ${groupNotApprovedStrings[unapproval]}`);
Expand All @@ -124,9 +133,9 @@ function run() {
failed = true;
failedGroups.push(group);
core.startGroup(`We have (${groupApprovalCount}/${groupApprovalRequired}) approval(s) from group: ${group}.`);
let appCount = 1;
let appCount = 0;
for (let approval in groupApprovedStrings) {
core.info(`(${appCount++}/${groupApprovalRequired}) ✅ ${groupApprovedStrings[approval]}`);
core.info(`(${++appCount}/${groupApprovalRequired}) ✅ ${groupApprovedStrings[approval]}`);
}
for (let unapproval in groupNotApprovedStrings) {
core.info(`(${appCount}/${groupApprovalRequired}) ❌ ${groupNotApprovedStrings[unapproval]}`);
Expand All @@ -139,6 +148,25 @@ function run() {
}
});
}
function identifyGroupsByChangedFiles(config, changedFiles) {
const affected = [];
const unaffected = [];
for (let groupName in config.groups) {
const group = config.groups[groupName];
const fileGlobs = group.paths;
if (fileGlobs == null || fileGlobs == undefined || fileGlobs.length == 0) {
core.warning(`No specific path globs assigned for group ${groupName}, assuming global approval.`);
affected.push(group);
}
else if (fileGlobs.filter(glob => minimatch.match(changedFiles, glob, { nonull: false, matchBase: true }).length > 0).length > 0) {
affected.push(group);
}
else {
unaffected.push(group);
}
}
return { affected, unaffected };
}
module.exports = {
run,
};
Expand Down
Loading

0 comments on commit b302213

Please sign in to comment.