Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert combining of Flowzone rulesets #2

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 0 additions & 29 deletions assets/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,32 +89,3 @@ rulesets:
- actor_id: 291899
actor_type: Integration
bypass_mode: always

- name: Flowzone
target: branch
enforcement: active
conditions:
ref_name:
include:
- ~DEFAULT_BRANCH
exclude: []
repository_name:
exclude: []
include: []
rules:
- type: required_status_checks
parameters:
strict_required_status_checks_policy: true
required_status_checks:
- context: Flowzone / All jobs
integration_id: 15368
bypass_actors:
- actor_id: 1
actor_type: OrganizationAdmin
bypass_mode: always
- actor_id: 5
actor_type: RepositoryRole
bypass_mode: always
- actor_id: 291899
actor_type: Integration
bypass_mode: always
58 changes: 5 additions & 53 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ async function processRepository(octokit, repoName) {
return;
}

if (['.github'].includes(repoData.data.name)) {
// Skip the .github repository
return;
}

console.log(`Processing repository: ${repoName}`);

try {
Expand All @@ -65,59 +70,6 @@ async function processRepository(octokit, repoName) {

const rulesetData = convertBranchProtectionToRuleset(protectionData.data);

// Check for 'Flowzone' in rules
const hasFlowzoneCheck = rulesetData.rules.some(
(rule) =>
rule.type === "required_status_checks" &&
rule.parameters.required_status_checks.some((check) =>
check.context.startsWith("Flowzone")
)
);

if (hasFlowzoneCheck) {
// Load the YAML document
const fileContents = fs.readFileSync(orgSettingsPath, "utf8");
const doc = yaml.parseDocument(fileContents);

// Get the rulesets array
const rulesets = doc.get('rulesets');

// Find the Flowzone ruleset
const flowzoneRuleset = rulesets.items.find(
(ruleset) => ruleset.get('name') === "Flowzone"
);

// Get the 'include' YAMLSeq from the repository_name conditions
const includeSeq = flowzoneRuleset.getIn(['conditions', 'repository_name', 'include']);

// Convert the YAMLSeq to a JavaScript array and check if the repoName is included
if (!includeSeq.toJSON().includes(repoName)) {

if (includeSeq.toJSON().length < 1) {
// replace the empty array with a bulleted list
flowzoneRuleset.deleteIn(['conditions', 'repository_name', 'include']);
flowzoneRuleset.addIn(['conditions', 'repository_name'], new yaml.Pair('include', [repoName]));
} else {
// Add the repository name to the include list
includeSeq.add(repoName);
}

// Write the updated document back to the file, preserving comments and formatting
fs.writeFileSync(orgSettingsPath, doc.toString(), "utf8");
}
}

// Exclude 'Flowzone' from the rulesetData rules before pushing it to rulesets
rulesetData.rules = rulesetData.rules.map((rule) => {
if (rule.type === "required_status_checks") {
rule.parameters.required_status_checks =
rule.parameters.required_status_checks.filter(
(check) => !check.context.startsWith("Flowzone")
);
}
return rule;
});

// Remove 'policy-bot' contexts and filter out rules without contexts
rulesetData.rules = rulesetData.rules
.map((rule) => {
Expand Down
Loading