Skip to content

Commit

Permalink
Add Semestry branch naming convention to checks
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewmitchellsemestry committed Dec 5, 2022
1 parent 1020cb2 commit 42c64fa
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 9 deletions.
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ It's feature are:
Because pull requests for hotfix branches should never target other branches than these.

- **Convert pull requests for cascading feature branches to draft automatically**<br>
This prevents them from being merged accendentally into the base feature branch.
This prevents them from being merged accidentally into the base feature branch.

## Usage

Expand Down Expand Up @@ -51,9 +51,14 @@ You can override configuration options using in the workflow file. For example:
feature_branch_prefix: 'feat/'
```
| Option | Description |
|------------------------------|---------------------------------------------------------------------------------|
| `main_branch_pattern` | Regex for matching main branches. Default: `^(main|master)$` |
| `development_branch_pattern` | Regex for matching development branches. Default: `^(dev|develop|development)$` | |
| `feature_branch_prefix` | Feature branch prefix. Default: `feature/` |
| `hotfix_branch_prefix` | Hotfix branch prefix. Default: `hotfix/` |
| Option | Description |
|-----------------------------------|-------------------------------------------------------------------------------------------|
| `main_branch_pattern` | Regex for matching main branches. Default: `^(main&#124;master)$` |
| `development_branch_pattern` | Regex for matching development branches. Default: `^(dev&#124;develop&#124;development)$` |
| `semestry_staging_branch_pattern` | Regex for matching development branches. Default: `^(X.Y-staging&#124;X.Y.Z-staging)$` |
| `semestry_testing_branch_pattern` | Regex for matching development branches. Default: `^(X.Y-test&#124;X.Y.Z-test)$` |
| `feature_branch_prefix` | Feature branch prefix. Default: `feature/` |
| `hotfix_branch_prefix` | Hotfix branch prefix. Default: `hotfix/` |
| `fix_branch_prefix` | Fix branch prefix. Default `fix/` |
| `staging_branch_prefix` | Staging branch prefix. Default `staging/` |

16 changes: 16 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ inputs:
description: 'Development branch pattern'
required: true
default: '^(dev|develop|development)$'
semestry_staging_branch_pattern:
description: 'Semestry staging branch pattern'
required: true
default: '^([0-9]+(\.[0-9]+)+-staging|development-staging)$'
semestry_testing_branch_pattern:
description: 'Semestry Testing branch pattern'
required: true
default: '^([0-9]+(\.[0-9]+)+-test|development-test)$'
feature_branch_prefix:
description: 'Feature branch prefix'
required: true
Expand All @@ -17,6 +25,14 @@ inputs:
description: 'Hotfix branch prefix'
required: true
default: 'hotfix/'
fix_branch_prefix:
description: 'Fix branch prefix'
required: true
default: 'fix/'
staging_branch_prefix:
description: 'Staging branch prefix'
required: true
default: 'staging/'
runs:
using: 'node16'
main: 'index.js'
48 changes: 46 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function isTriggeredByPullRequestEvent() {
if (eventName === "pull_request" || eventName === "pull_request_target") {
return true;
}

core.setFailed("This action should only be used with 'pull_request' or 'pull_request_target' events.");
return false;
}
Expand All @@ -32,6 +32,8 @@ async function checkTargetBranch() {
if (isFeatureBranch(head)) {
if (isDevelopmentBranch(base)) {
console.log("Head branch is a feature branch and targets a development branch.");
} else if (isSemestryTestingBranch(base)) {
console.log("Head branch is a feature branch and targets a Semestry version-test branch.");
} else if (isFeatureBranch(base)) {
console.log("Head branch is a feature branch and targets another feature branch.");
if (!isPullRequestDraft()) {
Expand All @@ -56,6 +58,22 @@ async function checkTargetBranch() {
console.log(msg);
await postCommentIfBaseChanged(msg);
}
} else if (isFixBranch(head)) {
if (isSemestryTestingBranch(base)) {
console.log("Head branch is a fix branch and targets a Semestry version-test branch.");
} else {
let msg = "⛔ Pull requests for fix branches should target a version-test branch.";
console.log(msg);
await postCommentIfBaseChanged(msg);
}
} else if (isStagingBranch(head)) {
if (isSemestryStagingBranch(base)) {
console.log("Head branch is a staging branch and targets a Semestry version-staging branch.");
} else {
let msg = "⛔ Pull requests for staging branches should target a version-staging branch.";
console.log(msg);
await postCommentIfBaseChanged(msg);
}
} else {
let msg = `⚠ The head branch name doesn't have the \`${getFeatureBranchPrefix()}\` or \`${getHotfixBranchPrefix()}\` prefix.`;
console.log(msg);
Expand All @@ -79,6 +97,14 @@ function getHotfixBranchPrefix() {
return core.getInput("hotfix_branch_prefix", { required: true });
}

function getFixBranchPrefix() {
return core.getInput("fix_branch_prefix", { required: true });
}

function getStagingBranchPrefix() {
return core.getInput("staging_branch_prefix", { required: true });
}

function isFeatureBranch(branch) {
return branch.startsWith(getFeatureBranchPrefix());
}
Expand All @@ -87,6 +113,14 @@ function isHotfixBranch(branch) {
return branch.startsWith(getHotfixBranchPrefix());
}

function isFixBranch(branch) {
return branch.startsWith(getFixBranchPrefix());
}

function isStagingBranch(branch) {
return branch.startsWith(getStagingBranchPrefix());
}

function isMainBranch(branch) {
const pattern = core.getInput("main_branch_pattern", { required: true });
return new RegExp(pattern).test(branch);
Expand All @@ -97,6 +131,16 @@ function isDevelopmentBranch(branch) {
return new RegExp(pattern).test(branch);
}

function isSemestryStagingBranch(branch) {
const pattern = core.getInput("semestry_staging_branch_pattern", { required: true });
return new RegExp(pattern).test(branch);
}

function isSemestryTestingBranch(branch) {
const pattern = core.getInput("semestry_testing_branch_pattern", { required: true });
return new RegExp(pattern).test(branch);
}

function isPullRequestDraft() {
return github.context.payload.pull_request.draft;
}
Expand Down Expand Up @@ -147,4 +191,4 @@ async function postCommentIfBaseChanged(comment) {
}
}

run();
run();

0 comments on commit 42c64fa

Please sign in to comment.