Skip to content

Commit

Permalink
feat: Implement additional deploy params
Browse files Browse the repository at this point in the history
The following parameters are added to the deploy function.

* force: removes required contexts
* task: changes a task
  • Loading branch information
colinjfw committed Jan 6, 2020
1 parent 23dbdda commit a627815
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 21 deletions.
18 changes: 14 additions & 4 deletions src/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,20 @@ export async function config(
return conf;
}

function getDeployBody(target: Target, data: any): DeployBody {
function getDeployBody(
target: Target,
data: any,
force?: boolean,
task?: string
): DeployBody {
return withPreview({
task: target.task || "deploy",
task: task || target.task || "deploy",
transient_environment: target.transient_environment || false,
production_environment: target.production_environment || false,
environment: render(target.environment || "production", data),
auto_merge: target.auto_merge || false,
required_contexts: target.required_contexts || [],
// The presence of force sets the required contexts to an empty array [].
required_contexts: force ? [] : target.required_contexts || [],
description: render(target.description, data),
payload: {
target: target.name,
Expand All @@ -105,13 +111,17 @@ export async function deploy(
target,
ref,
sha,
force,
task,
pr
}: {
owner: string;
repo: string;
target: string;
ref: string;
sha: string;
force?: boolean;
task?: string;
pr?: PullsGetResponse;
}
) {
Expand Down Expand Up @@ -146,7 +156,7 @@ export async function deploy(
owner,
repo,
ref,
...getDeployBody(targetVal, params)
...getDeployBody(targetVal, params, force, task)
};

if (await kv.isLockedEnv(repository.data.id, body.environment)) {
Expand Down
14 changes: 5 additions & 9 deletions test/auto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,19 @@ describe("auto", () => {
["refs/heads/*", "refs/heads/master"],
["refs/*", "refs/tags/simple-tag"],
["refs/tags/*", "refs/tags/simple-tag"],
["refs/tags/v*", "refs/tags/v1.2.3"],
["refs/tags/v*", "refs/tags/v1.2.3"]
];
const unmatched = [
["refs/heads/staging", "refs/heads/master"],
["refs/tags/v", "refs/tags/v1.2.3"],
["refs/heads/*", "refs/tags/simple-tag"],
["refs/heads/*", "refs/tags/simple-tag"]
];

matches.forEach(m => {
it(`matches ${m[0]}`, () =>
expect(match(m[0], m[1])).toBe(true)
)
it(`matches ${m[0]}`, () => expect(match(m[0], m[1])).toBe(true));
});
unmatched.forEach(m => {
it(`unmatched ${m[0]}`, () =>
expect(match(m[0], m[1])).toBe(false)
)
it(`unmatched ${m[0]}`, () => expect(match(m[0], m[1])).toBe(false));
});
});

Expand Down Expand Up @@ -92,7 +88,7 @@ describe("auto", () => {
factory.config({ valid: false });
factory.noDeployments();

await probot.receive(factory.push())
await probot.receive(factory.push());
expect(deploy.isDone()).toBe(false);
});

Expand Down
103 changes: 103 additions & 0 deletions test/deploy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import * as factory from "./factory";
import { deploy } from "../src/deploy";
import Octokit from "@octokit/rest";

describe("deploy", () => {
jest.setTimeout(30000);
let probot: factory.Probot;

afterEach(() => {
factory.cleanAll();
factory.store.clear();
});

beforeEach(() => {
probot = factory.probot();
factory.token();
factory.permission({ admin: true });
factory.repo().persist();
factory.gitCommit().persist();
factory.gitRef().persist();
});

test("creates a deployment", async () => {
const isDeployed = factory.deploy({
ref: "refs/heads/featureA",
task: "deploy",
transient_environment: false,
production_environment: true,
environment: "production",
auto_merge: true,
required_contexts: ["continuous-integration/travis-ci/push"],
description: "A test environment based on Docker",
payload: { target: "production" }
});
factory.config({ valid: true });

const github = new Octokit({ auth: "test" });

await deploy(github, probot.logger, factory.store, {
owner: "Codertocat",
repo: "Hello-World",
ref: "refs/heads/featureA",
sha: "0000000000000000000000000000000000000000",
target: "production"
});
expect(isDeployed.isDone()).toBe(true);
});

test("creates a deployment with a task", async () => {
const isDeployed = factory.deploy({
ref: "refs/heads/featureA",
task: "foobar",
transient_environment: false,
production_environment: true,
environment: "production",
auto_merge: true,
required_contexts: ["continuous-integration/travis-ci/push"],
description: "A test environment based on Docker",
payload: { target: "production" }
});
factory.config({ valid: true });

const github = new Octokit({ auth: "test" });

await deploy(github, probot.logger, factory.store, {
owner: "Codertocat",
repo: "Hello-World",
ref: "refs/heads/featureA",
sha: "0000000000000000000000000000000000000000",
target: "production",
task: "foobar"
});
expect(isDeployed.isDone()).toBe(true);
});

test("creates a deployment with force", async () => {
const isDeployed = factory.deploy({
ref: "refs/heads/featureA",
task: "foobar",
transient_environment: false,
production_environment: true,
environment: "production",
auto_merge: true,
required_contexts: [],
description: "A test environment based on Docker",
payload: { target: "production" }
});
factory.config({ valid: true });

const github = new Octokit({ auth: "test" });

await deploy(github, probot.logger, factory.store, {
owner: "Codertocat",
repo: "Hello-World",
ref: "refs/heads/featureA",
sha: "0000000000000000000000000000000000000000",
target: "production",
task: "foobar",
force: true,
});
expect(isDeployed.isDone()).toBe(true);
});
});
14 changes: 6 additions & 8 deletions test/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ export const store = new InMemStore();
export const deliverybot = (application: Application) => {
// Assign plain octokit to remove the plugins (like retries) the GitHub adds
// by default.
application['Octokit'] = Octokit;
application["Octokit"] = Octokit;
app(application, store, store, application.receive.bind(application));
};


export const probot = () => {
const probot = new Probot({ id: 123, cert: "test" });
const app = probot.load(deliverybot);
Expand All @@ -30,7 +29,7 @@ export const probot = () => {
getInstallationAccessToken: (option: any) => Promise.resolve("test")
};
return probot;
}
};

const fixtures = {
deployValid: require("./fixtures/deploy-valid"),
Expand Down Expand Up @@ -135,9 +134,9 @@ export const pr = () =>
.get("/repos/Codertocat/Hello-World/pulls/2")
.reply(200, fixtures.pullRequest);

export const deploy = () =>
export const deploy = (body?: any) =>
nock("https://api.github.com")
.post("/repos/Codertocat/Hello-World/deployments")
.post("/repos/Codertocat/Hello-World/deployments", body)
.reply(200, { id: 1 });

export const deployConflict = () =>
Expand Down Expand Up @@ -188,13 +187,13 @@ export const push = (): any => ({

export const status = (): any => ({
name: "status",
id: 'status-event',
id: "status-event",
payload: fixtures.status
});

export const checkRun = (): any => ({
name: "check_run",
id: 'check-run-event',
id: "check-run-event",
payload: fixtures.checkRunCreated
});

Expand All @@ -207,4 +206,3 @@ export const errorComment = (expected: string) =>
return true;
})
.reply(200);

0 comments on commit a627815

Please sign in to comment.