From 1eeac2232beb0f451a04e55597000bb22ab7a3d7 Mon Sep 17 00:00:00 2001 From: Andy Brown Date: Mon, 9 Dec 2019 15:01:25 -0800 Subject: [PATCH] ci: disallow opening pr against stable branch unless a release (#1740) --- .../conventional-pr/src/conventional-pr.ts | 32 +++++++++++++------ .github/actions/conventional-pr/src/utils.ts | 26 +++++++++++++++ 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/.github/actions/conventional-pr/src/conventional-pr.ts b/.github/actions/conventional-pr/src/conventional-pr.ts index ddbd8fa028..6794cbc873 100644 --- a/.github/actions/conventional-pr/src/conventional-pr.ts +++ b/.github/actions/conventional-pr/src/conventional-pr.ts @@ -1,7 +1,13 @@ import * as core from '@actions/core'; import * as github from '@actions/github'; -import { validateTitle, validateBody } from './utils'; +import { + validateTitle, + validateBody, + validateBaseBranch, + PullRequestInfo, + isRelease, +} from './utils'; const OWNER = github.context.repo.owner; const REPO = github.context.repo.repo; @@ -22,6 +28,7 @@ const prQuery = ` pullRequest(number: $prNumber) { title body + baseRefName } } } @@ -43,22 +50,29 @@ async function run() { repo: REPO, prNumber, }); - const pr = repository.pullRequest; + const pr = repository?.pullRequest as PullRequestInfo; if (!pr) { core.setFailed('Not in a Pull Request context.'); return; } - const titleErrors = validateTitle(pr.title); - const bodyErrors = validateBody(pr.body); + if (!isRelease(pr)) { + const titleErrors = validateTitle(pr.title); + const bodyErrors = validateBody(pr.body); + const branchErrors = validateBaseBranch(pr.title, pr.baseRefName); - if (titleErrors.length) { - core.setFailed(titleErrors.join('\n')); - } + if (titleErrors.length) { + core.setFailed(titleErrors.join('\n')); + } - if (bodyErrors.length) { - core.setFailed(bodyErrors.join('\n')); + if (bodyErrors.length) { + core.setFailed(bodyErrors.join('\n')); + } + + if (branchErrors.length) { + core.setFailed(branchErrors.join('\n')); + } } } catch (err) { core.error(err); diff --git a/.github/actions/conventional-pr/src/utils.ts b/.github/actions/conventional-pr/src/utils.ts index 5e76cc4f11..407b03953f 100644 --- a/.github/actions/conventional-pr/src/utils.ts +++ b/.github/actions/conventional-pr/src/utils.ts @@ -1,5 +1,10 @@ import * as core from '@actions/core'; +export interface PullRequestInfo { + title: string; + body: string; + baseRefName: string; +} type ValidationResult = string[]; const validTypes = [ @@ -52,3 +57,24 @@ export function validateBody(body: string): ValidationResult { return errors; } + +export function isRelease(pr: PullRequestInfo) { + return pr.title.startsWith('release: ') && pr.baseRefName === 'stable'; +} + +export function validateBaseBranch( + title: string, + baseBranch: string +): ValidationResult { + let errors: ValidationResult = []; + + if (title.startsWith('release: ') && baseBranch !== 'stable') { + errors.push("[Release] Release pull request must target 'stable' branch."); + } else if (baseBranch === 'stable') { + errors.push( + "[Branch] Pull requests cannot target 'stable' branch. Perhaps you meant to create a release or are targeting the wrong branch." + ); + } + + return errors; +}