Skip to content

Commit

Permalink
Merge pull request #1636 from intuit/pr-check
Browse files Browse the repository at this point in the history
improve pr-check usage + don't fail on runs in CI base branch
  • Loading branch information
hipstersmoothie committed Nov 6, 2020
2 parents 9e3a1f2 + e77ab8e commit 8e7b76f
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 9 deletions.
7 changes: 1 addition & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,7 @@ jobs:
at: ~/auto
- run:
name: Check for SemVer Label
command: |
if [ ! -z "$CIRCLE_PR_NUMBER" ]; then
yarn auto pr-check --url $CIRCLE_PULL_REQUEST
else
echo "No pull request, not checking for semver label"
fi
command: yarn auto pr-check --url $CIRCLE_PULL_REQUEST

test:
<<: *defaults
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/parse-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ export const commands: AutoCommand[] = [
{
name: "pr-check",
group: "Pull Request Interaction Commands",
description: "Check that a pull request has a SemVer label",
description:
"Check that a pull request has a SemVer label and run all pr-check plugins.",
require: ["url"],
options: [
pr,
Expand Down
73 changes: 73 additions & 0 deletions packages/core/src/__tests__/auto-ci-base-branch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import envCi from "env-ci";
import { dummyLog } from "../utils/logger";

jest.mock("env-ci");

const envSpy = envCi as jest.Mock;
envSpy.mockImplementation(() => ({
isCi: true,
branch: "master",
}));

import { Auto } from "../auto";

const defaults = {
owner: "foo",
repo: "bar",
};

jest.mock("@octokit/rest", () => {
const Octokit = class MockOctokit {
static plugin = () => Octokit;

authenticate = () => undefined;

search = {
issuesAndPullRequests: () => ({ data: { items: [] } }),
};

repos = {
get: jest.fn().mockReturnValue({}),
};

hook = {
error: () => undefined,
};
};

return { Octokit };
});

describe("Auto", () => {
describe("pr-check", () => {
jest.setTimeout(10 * 1000);
let createStatus: jest.Mock;

beforeEach(() => {
createStatus = jest.fn();
});

const required = {
url: "https://google.com",
};

test("should exit successfully if ran from master + CI", async () => {
const auto = new Auto(defaults);
const exit = jest.fn();

envSpy.mockImplementationOnce(() => ({
isCi: true,
branch: "master",
}));

// @ts-ignore
process.exit = exit;
auto.logger = dummyLog();
await auto.loadConfig();
auto.git!.createStatus = createStatus;

await auto.prCheck({ ...required });
expect(exit).toHaveBeenCalledWith(0);
});
});
});
23 changes: 21 additions & 2 deletions packages/core/src/auto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,27 @@ export default class Auto {
this.logger.verbose.info(`Using command: 'pr-check' for '${url}'`);

const target_url = url;
const prNumber = this.getPrNumber("prCheck", pr);
const prNumber = getPrNumberFromEnv(pr);

if (!prNumber) {
// If pr-check is ran on CI on master then we exit successfully since
// running pr-check in this scenario wouldn't make sense anyway. Enables
// adding this command without resorting to bash if/else statements.
if (env.isCi && (env.branch === "master" || this.inPrereleaseBranch())) {
process.exit(0);
}

// Otherwise the command should fail since no PR number was provided or found
this.logger.log.error(
endent`
Could not detect PR number. pr-check must be run from either a PR or have the PR number supplied via the --pr flag.
In some CIs your branch might be built before you open a PR and posting the canary version will fail. In this case subsequent builds should succeed.
`
);
process.exit(1);
}

let msg;
let sha;

Expand Down Expand Up @@ -1139,7 +1159,6 @@ export default class Auto {
}

/** Create a canary (or test) version of the project */
// eslint-disable-next-line complexity
async canary(args: ICanaryOptions = {}): Promise<ShipitInfo | undefined> {
const options = { ...this.getCommandDefault("canary"), ...args };

Expand Down

0 comments on commit 8e7b76f

Please sign in to comment.