From 42c64fac8b6d4546ae1dd82330b5cdf80c32e873 Mon Sep 17 00:00:00 2001 From: andrewmitchellsemestry Date: Tue, 29 Nov 2022 12:51:40 +0000 Subject: [PATCH] Add Semestry branch naming convention to checks --- README.md | 19 ++++++++++++------- action.yml | 16 ++++++++++++++++ index.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 74 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index e2b85f4..b1dcf0b 100644 --- a/README.md +++ b/README.md @@ -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**
- 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 @@ -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|master)$` | +| `development_branch_pattern` | Regex for matching development branches. Default: `^(dev|develop|development)$` | +| `semestry_staging_branch_pattern` | Regex for matching development branches. Default: `^(X.Y-staging|X.Y.Z-staging)$` | +| `semestry_testing_branch_pattern` | Regex for matching development branches. Default: `^(X.Y-test|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/` | + diff --git a/action.yml b/action.yml index 9fedf52..d575f9d 100644 --- a/action.yml +++ b/action.yml @@ -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 @@ -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' diff --git a/index.js b/index.js index 3e51f04..ec30098 100644 --- a/index.js +++ b/index.js @@ -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; } @@ -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()) { @@ -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); @@ -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()); } @@ -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); @@ -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; } @@ -147,4 +191,4 @@ async function postCommentIfBaseChanged(comment) { } } -run(); \ No newline at end of file +run();