Skip to content

Commit

Permalink
ci: disallow opening pr against stable branch unless a release (#1740)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-b-r-o-w-n authored and cwhitten committed Dec 9, 2019
1 parent 21d1b7f commit 1eeac22
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
32 changes: 23 additions & 9 deletions .github/actions/conventional-pr/src/conventional-pr.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,6 +28,7 @@ const prQuery = `
pullRequest(number: $prNumber) {
title
body
baseRefName
}
}
}
Expand All @@ -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);
Expand Down
26 changes: 26 additions & 0 deletions .github/actions/conventional-pr/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import * as core from '@actions/core';

export interface PullRequestInfo {
title: string;
body: string;
baseRefName: string;
}
type ValidationResult = string[];

const validTypes = [
Expand Down Expand Up @@ -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;
}

0 comments on commit 1eeac22

Please sign in to comment.