Skip to content

Commit

Permalink
Add fensak configuration to ensure merges to release branch can only …
Browse files Browse the repository at this point in the history
…happen from main branch

Signed-off-by: Yoriyasu Yano <[email protected]>
  • Loading branch information
yorinasub17 committed Oct 20, 2023
1 parent 8df597b commit 0bae751
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 126 deletions.
32 changes: 6 additions & 26 deletions fensak.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
# This file contains the Fensak configuration file for this GitHub Organization.
# The config file specifies which rules should be applied to the different repositories in your organization.
#
# This contains a starting point for a fictional repo `my-repo` in your org. Update to point to your real repos and
# rules to get started using Fensak.
#
# Refer to https://github.com/fensak-io/fensak/blob/main/fskconfig/schema.json
# for the expected config schema.

repos:
# Each entry in this configuration represents a single repository, and the corresponding rule that should be run
# to determine auto-approval for PRs in the repo.
my-repo:
# The path to the rule relative to the repo root.
ruleFile: rules/sample.ts

# The following attribute is optional. When omitted, the rule language is automatically determined based on the rule
# file extension. Refer to the schema for the list of allowed values here.
# ruleLang: ts

# The following attribute is optional. This setting controls how many approvals should be required for PRs where the
# rule does not pass. The default is 1.
# requiredApprovals: 1
fensak:
requiredRuleFile: rules/fensak_required_rule.ts
requiredApprovals: 0

# You can add more repositories by extending the repos map with more entries like so:
#
# my-other-repo:
# ruleFile: rules/sample.ts
dashboard:
requiredRuleFile: rules/fensak_required_rule.ts
requiredApprovals: 0
21 changes: 21 additions & 0 deletions rules/fensak_required_rule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* A Fensak rule for the repositories that use release branch strategy. This rule requires that the source branch of the
* release branch is set to main. This ensures that only the main branch can merge into the release branch.
*/

// fensak remove-start
import type { IChangeSetMetadata, IPatch } from "npm:@fensak-io/reng@^1.2.1";
// fensak remove-end

// deno-lint-ignore no-unused-vars
function main(_inp: IPatch[], metadata: IChangeSetMetadata): boolean {
if (metadata.targetBranch === "release" && metadata.sourceBranch !== "main") {
console.log(
"Rejecting since merging non-main branch into the release branch.",
);
return false;
}

// Allow all other PRs.
return true;
}
66 changes: 66 additions & 0 deletions rules/fensak_required_rule_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {
assert,
assertFalse,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import {
compileRuleFn,
IGitHubRepository,
patchFromGitHubPullRequest,
RuleFnSourceLang,
RuleLogMode,
runRule,
} from "npm:@fensak-io/reng@^1.2.1";
import { Octokit } from "npm:@octokit/rest@^20.0.0";

const __dirname = new URL(".", import.meta.url).pathname;
const ruleFn = compileRuleFn(
await Deno.readTextFile(`${__dirname}/fensak_required_rule.ts`),
RuleFnSourceLang.Typescript,
);
const octokit = new Octokit({
auth: Deno.env.get("GITHUB_API_TOKEN"),
});
const fensakRepo: IGitHubRepository = {
owner: "fensak-io",
name: "fensak",
};
const opts = { logMode: RuleLogMode.Console };

Deno.test("Normal release PR should be approved", async () => {
// View PR at
// https://github.com/fensak-io/fensak/pull/125
const patches = await patchFromGitHubPullRequest(octokit, fensakRepo, 125);
const result = await runRule(
ruleFn,
patches.patchList,
patches.metadata,
opts,
);
assert(result.approve);
});

Deno.test("Normal PRs to main should be approved", async () => {
// View PR at
// https://github.com/fensak-io/fensak/pull/124
const patches = await patchFromGitHubPullRequest(octokit, fensakRepo, 124);
const result = await runRule(
ruleFn,
patches.patchList,
patches.metadata,
opts,
);
assert(result.approve);
});

Deno.test("PRs to release from a random branch should be rejected", async () => {
// View PR at
// https://github.com/fensak-io/fensak/pull/126
const patches = await patchFromGitHubPullRequest(octokit, fensakRepo, 126);
const result = await runRule(
ruleFn,
patches.patchList,
patches.metadata,
opts,
);
assertFalse(result.approve);
});
31 changes: 0 additions & 31 deletions rules/sample.ts

This file was deleted.

69 changes: 0 additions & 69 deletions rules/sample_test.ts

This file was deleted.

0 comments on commit 0bae751

Please sign in to comment.