Skip to content

Commit

Permalink
Merge pull request #1383 from stabbylambda/label-exists
Browse files Browse the repository at this point in the history
Add `--exists $LABEL` to label command
  • Loading branch information
hipstersmoothie committed Jul 16, 2020
2 parents 16ef1a7 + ed25e74 commit 44c0653
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/pages/blog/using-shipit.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ For example,. the following will only run the test:visual script when the PR has
```bash
export PATH=$(npm bin):$PATH

if [ `auto label --pr $PR_NUMBER | grep -c '^Visual'` -ne 0 ];
if auto label --pr $PR_NUMBER --exists Visual;
then
npm run test:visual
fi
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/docs/extras/label.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The following will only run the test:visual script when the PR has has the
```bash
export PATH=$(npm bin):$PATH

if [ auto label --pr $PR_NUMBER | grep -c '^Visual' -ne 0 ];
if auto label --pr $PR_NUMBER --exists Visual;
then
npm run test:visual
fi
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/src/parse-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,12 @@ export const commands: AutoCommand[] = [
"Get the labels for a pull request. Doesn't do much, but the return value lets you write you own scripts based off of the PR labels!",
options: [
{ ...pr, description: `${pr.description} (defaults to last merged PR)` },
{
name: "exists",
type: String,
group: "main",
description: "Checks for existence of a specific label",
},
],
examples: ["{green $} auto label --pr 123"],
},
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Auto, {
IShipItOptions,
IVersionOptions,
INextOptions,
LabelExistsError,
} from "@auto-it/core";
import endent from "endent";
import on from "await-to-js";
Expand Down Expand Up @@ -115,7 +116,7 @@ export async function run(command: string, args: ApiOptions) {
`);
console.log("");
auto.logger.verbose.error(error);
} else {
} else if (!(error instanceof LabelExistsError)) {
console.log(error);
}

Expand Down
27 changes: 27 additions & 0 deletions packages/core/src/__tests__/auto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,33 @@ describe("Auto", () => {
expect(console.log).toHaveBeenCalledWith("foo");
});

test("should check for a given label", async () => {
const auto = new Auto(defaults);
auto.logger = dummyLog();
await auto.loadConfig();

const getLabels = jest.fn();
auto.git!.getLabels = getLabels;
getLabels.mockReturnValueOnce(["foo"]);
jest.spyOn(console, "log").mockImplementation();

await auto.label({ pr: 13, exists: "foo" });
expect(console.log).toHaveBeenCalledWith("foo");
});

test("should throw if a check for a label fails", async () => {
const auto = new Auto(defaults);
auto.logger = dummyLog();
await auto.loadConfig();

const getLabels = jest.fn();
auto.git!.getLabels = getLabels;
getLabels.mockReturnValueOnce(["bar"]);
jest.spyOn(console, "log").mockImplementation();

await expect(auto.label({ pr: 13, exists: "foo" })).rejects.toThrow();
});

test("should get labels for last merged PR", async () => {
const auto = new Auto(defaults);
auto.logger = dummyLog();
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/auto-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export interface ICreateLabelsOptions {
export interface ILabelOptions {
/** PR to get the labels for */
pr?: number;

/** Label to check for */
exists?: string;
}

export interface IPRCheckOptions {
Expand Down
21 changes: 20 additions & 1 deletion packages/core/src/auto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,17 @@ function escapeRegExp(str: string) {
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
}

/** The Error that gets thrown when a label existence check fails */
export class LabelExistsError extends Error {
/**
* @param label - the label we were checking existence for
*/
constructor(label: string) {
super(`The label ${label} does not exist`);
Object.setPrototypeOf(this, new.target.prototype); // restore prototype chain
}
}

/**
* The "auto" node API. Its public interface matches the
* commands you can run from the CLI
Expand Down Expand Up @@ -751,7 +762,7 @@ export default class Auto {
/**
* Get the labels on a specific PR. Defaults to the labels of the last merged PR
*/
async label({ pr }: ILabelOptions = {}) {
async label({ pr, exists }: ILabelOptions = {}) {
if (!this.git) {
throw this.createErrorMessage();
}
Expand Down Expand Up @@ -779,6 +790,14 @@ export default class Auto {
}
}

if (exists) {
if (labels.indexOf(exists) === -1) {
throw new LabelExistsError(exists);
}

return;
}

if (labels.length) {
console.log(labels.join("\n"));
}
Expand Down

0 comments on commit 44c0653

Please sign in to comment.