Skip to content

Commit

Permalink
Merge pull request #9 from TheCleric/feature/enhance-debugging
Browse files Browse the repository at this point in the history
Added enhanced debugging and the ability to use changes to the config file within the PR itself
  • Loading branch information
ffittschen committed Aug 27, 2020
2 parents a902b20 + 179127b commit b3000eb
Show file tree
Hide file tree
Showing 15 changed files with 306 additions and 4,056 deletions.
28 changes: 14 additions & 14 deletions .github/workflows/checkin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ jobs:
check_pr:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v1

- name: "npm ci"
run: npm ci
- name: "npm ci"
run: npm ci

- name: "npm run build"
run: npm run build
- name: "npm run build"
run: npm run build

- name: "npm run test"
run: npm run test
- name: "npm run test"
run: npm run test

- name: "check for uncommitted changes"
# Ensure no changes, but ignore node_modules dir since dev/fresh ci deps installed.
run: |
git diff --exit-code --stat -- . ':!node_modules' \
|| (echo "##[error] found changed files after build. please 'npm run build && npm run format'" \
"and check in all changes" \
&& exit 1)
- name: "check for uncommitted changes"
# Ensure no changes, but ignore node_modules dir since dev/fresh ci deps installed.
run: |
git diff --exit-code --stat -- . ':!node_modules' \
|| (echo "##[error] found changed files after build. please 'npm run build && npm run format'" \
"and check in all changes" \
&& exit 1)
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,6 @@ Temporary Items
.apdisk

# End of https://www.gitignore.io/api/macos

lib/src
lib/__tests__
95 changes: 95 additions & 0 deletions __tests__/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import * as github from "@actions/github";
import * as Context from '@actions/github/lib/context';
import "jest-extended";
import nock from "nock";
import path from "path";
import { getConfig } from '../src/config';
import { configFixture, emptyConfigFixture } from './shared';


nock.disableNetConnect();

describe("Config file loader", () => {
let context;

beforeEach(() => {
const repoToken = "token";
process.env["INPUT_REPO-TOKEN"] = repoToken;
process.env["GITHUB_REPOSITORY"] = "Codertocat/Hello-World";
process.env["GITHUB_EVENT_PATH"] = path.join(__dirname, "fixtures", "payload.json");

context = new Context.Context();
});

afterEach(() => {
nock.cleanAll();
});

it("succeeds", async () => {
// Arrange
const getConfigScope = nock("https://api.github.com")
.persist()
.get("/repos/Codertocat/Hello-World/contents/.github/pr-branch-labeler.yml?ref=0123456")
.reply(200, configFixture());

const octokit = new github.GitHub("token");

const config = await getConfig(octokit, "pr-branch-labeler.yml", context);

// Assert
expect(getConfigScope.isDone()).toBeTrue();
expect(config.length).toEqual(6);
expect.assertions(2);
});

it("throws an error for an invalid config file", async () => {

// Arrange
const getConfigScope = nock("https://api.github.com")
.persist()
.get("/repos/Codertocat/Hello-World/contents/.github/pr-branch-labeler.yml?ref=0123456")
.reply(200, configFixture("invalid-config.yml"));

const octokit = new github.GitHub("token");

await expect(getConfig(octokit, "pr-branch-labeler.yml", context)).rejects.toThrow(new Error("config.yml has invalid structure."));

// Assert
expect(getConfigScope.isDone()).toBeTrue();
expect.assertions(2);
});

it("throws an error for a directory", async () => {

// Arrange
const getConfigScope = nock("https://api.github.com")
.persist()
.get("/repos/Codertocat/Hello-World/contents/.github/test?ref=0123456")
.reply(200, []);

const octokit = new github.GitHub("token");

await expect(getConfig(octokit, "test", context)).rejects.toThrow(new Error("test is not a file."));

// Assert
expect(getConfigScope.isDone()).toBeTrue();
expect.assertions(2);
});

it("throws an error for no contents", async () => {

// Arrange
const getConfigScope = nock("https://api.github.com")
.persist()
.get("/repos/Codertocat/Hello-World/contents/.github/pr-branch-labeler.yml?ref=0123456")
.reply(200, emptyConfigFixture());

const octokit = new github.GitHub("token");

await expect(getConfig(octokit, "pr-branch-labeler.yml", context)).rejects.toThrow(new Error("pr-branch-labeler.yml is empty."));

// Assert
expect(getConfigScope.isDone()).toBeTrue();
expect.assertions(2);
});
});
3 changes: 2 additions & 1 deletion __tests__/fixtures/payload.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"pull_request": {
"number": 1,
"head": {
"ref": "feature/awesome-stuff"
"ref": "feature/awesome-stuff",
"sha": "0123456"
},
"base": {
"ref": "master"
Expand Down
64 changes: 26 additions & 38 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "jest-extended";
import nock from "nock";
import path from "path";
import { readFileSync } from "fs";
import "jest-extended";
import { configFixture } from './shared';

nock.disableNetConnect();

Expand All @@ -13,6 +13,7 @@ describe("PR Branch Labeler", () => {
process.env["INPUT_REPO-TOKEN"] = repoToken;
process.env["GITHUB_REPOSITORY"] = "Codertocat/Hello-World";
process.env["GITHUB_EVENT_PATH"] = path.join(__dirname, "fixtures", "payload.json");

main = require("../src/main");
});

Expand All @@ -24,15 +25,15 @@ describe("PR Branch Labeler", () => {
// Arrange
const getConfigScope = nock("https://api.github.com")
.persist()
.get("/repos/Codertocat/Hello-World/contents/.github/pr-branch-labeler.yml")
.get(`/repos/Codertocat/Hello-World/contents/.github/pr-branch-labeler.yml?ref=${main.context.payload.pull_request.head.sha}`)
.reply(200, configFixture());

const postLabelsScope = nock("https://api.github.com")
.persist()
.post("/repos/Codertocat/Hello-World/issues/42/labels")
.reply(200);

main.context.payload = createPullRequestOpenedFixture("feature/FOO-42-awesome-stuff", "master");
main.context.payload = createPullRequestOpenedFixture("feature/FOO-42-awesome-stuff", "master", main.context.payload.pull_request.head.sha);

// Act
await main.run();
Expand All @@ -48,7 +49,7 @@ describe("PR Branch Labeler", () => {
// Arrange
const getConfigScope = nock("https://api.github.com")
.persist()
.get("/repos/Codertocat/Hello-World/contents/.github/pr-branch-labeler.yml")
.get(`/repos/Codertocat/Hello-World/contents/.github/pr-branch-labeler.yml?ref=${main.context.payload.pull_request.head.sha}`)
.reply(200, configFixture());

const postLabelsScope = nock("https://api.github.com")
Expand All @@ -61,7 +62,7 @@ describe("PR Branch Labeler", () => {
})
.reply(200);

main.context.payload = createPullRequestOpenedFixture("feature/FOO-42-awesome-stuff", "master");
main.context.payload = createPullRequestOpenedFixture("feature/FOO-42-awesome-stuff", "master", main.context.payload.pull_request.head.sha);

// Act
await main.run();
Expand All @@ -78,7 +79,7 @@ describe("PR Branch Labeler", () => {
// Arrange
const getConfigScope = nock("https://api.github.com")
.persist()
.get("/repos/Codertocat/Hello-World/contents/.github/pr-branch-labeler.yml")
.get(`/repos/Codertocat/Hello-World/contents/.github/pr-branch-labeler.yml?ref=${main.context.payload.pull_request.head.sha}`)
.reply(200, configFixture());

const postLabelsScope = nock("https://api.github.com")
Expand All @@ -91,7 +92,7 @@ describe("PR Branch Labeler", () => {
})
.reply(200);

main.context.payload = createPullRequestOpenedFixture("support/FOO-42-assisting", "master");
main.context.payload = createPullRequestOpenedFixture("support/FOO-42-assisting", "master", main.context.payload.pull_request.head.sha);

// Act
await main.run();
Expand All @@ -108,7 +109,7 @@ describe("PR Branch Labeler", () => {
// Arrange
const getConfigScope = nock("https://api.github.com")
.persist()
.get("/repos/Codertocat/Hello-World/contents/.github/pr-branch-labeler.yml")
.get(`/repos/Codertocat/Hello-World/contents/.github/pr-branch-labeler.yml?ref=${main.context.payload.pull_request.head.sha}`)
.reply(200, configFixture());

const postLabelsScope = nock("https://api.github.com")
Expand All @@ -121,7 +122,7 @@ describe("PR Branch Labeler", () => {
})
.reply(200);

main.context.payload = createPullRequestOpenedFixture("bugfix/FOO-42-squash-bugs", "master");
main.context.payload = createPullRequestOpenedFixture("bugfix/FOO-42-squash-bugs", "master", main.context.payload.pull_request.head.sha);

// Act
await main.run();
Expand All @@ -136,7 +137,7 @@ describe("PR Branch Labeler", () => {
// Arrange
const getConfigScope = nock("https://api.github.com")
.persist()
.get("/repos/Codertocat/Hello-World/contents/.github/pr-branch-labeler.yml")
.get(`/repos/Codertocat/Hello-World/contents/.github/pr-branch-labeler.yml?ref=${main.context.payload.pull_request.head.sha}`)
.reply(200, configFixture());

const postLabelsScope = nock("https://api.github.com")
Expand All @@ -149,7 +150,7 @@ describe("PR Branch Labeler", () => {
})
.reply(200);

main.context.payload = createPullRequestOpenedFixture("hotfix/FOO-42-squash-bugs", "master");
main.context.payload = createPullRequestOpenedFixture("hotfix/FOO-42-squash-bugs", "master", main.context.payload.pull_request.head.sha);

// Act
await main.run();
Expand All @@ -164,7 +165,7 @@ describe("PR Branch Labeler", () => {
// Arrange
const getConfigScope = nock("https://api.github.com")
.persist()
.get("/repos/Codertocat/Hello-World/contents/.github/pr-branch-labeler.yml")
.get(`/repos/Codertocat/Hello-World/contents/.github/pr-branch-labeler.yml?ref=${main.context.payload.pull_request.head.sha}`)
.reply(200, configFixture());

const postLabelsScope = nock("https://api.github.com")
Expand All @@ -175,7 +176,7 @@ describe("PR Branch Labeler", () => {
})
.reply(200);

main.context.payload = createPullRequestOpenedFixture("bugfix/FOO-42-changes", "release/1.0.0");
main.context.payload = createPullRequestOpenedFixture("bugfix/FOO-42-changes", "release/1.0.0", main.context.payload.pull_request.head.sha);

// Act
await main.run();
Expand All @@ -190,7 +191,7 @@ describe("PR Branch Labeler", () => {
// Arrange
const getConfigScope = nock("https://api.github.com")
.persist()
.get("/repos/Codertocat/Hello-World/contents/.github/pr-branch-labeler.yml")
.get(`/repos/Codertocat/Hello-World/contents/.github/pr-branch-labeler.yml?ref=${main.context.payload.pull_request.head.sha}`)
.reply(200, configFixture());

const postLabelsScope = nock("https://api.github.com")
Expand All @@ -201,7 +202,7 @@ describe("PR Branch Labeler", () => {
})
.reply(200);

main.context.payload = createPullRequestOpenedFixture("feature/FOO-42-part", "feature/FOO-42-whole");
main.context.payload = createPullRequestOpenedFixture("feature/FOO-42-part", "feature/FOO-42-whole", main.context.payload.pull_request.head.sha);

// Act
await main.run();
Expand All @@ -217,7 +218,7 @@ describe("PR Branch Labeler", () => {
// Arrange
const getConfigScope = nock("https://api.github.com")
.persist()
.get("/repos/Codertocat/Hello-World/contents/.github/pr-branch-labeler.yml")
.get(`/repos/Codertocat/Hello-World/contents/.github/pr-branch-labeler.yml?ref=${main.context.payload.pull_request.head.sha}`)
.reply(404);

const postLabelsScope = nock("https://api.github.com")
Expand All @@ -230,7 +231,7 @@ describe("PR Branch Labeler", () => {
})
.reply(200);

main.context.payload = createPullRequestOpenedFixture("feature/FOO-42-awesome-stuff", "master");
main.context.payload = createPullRequestOpenedFixture("feature/FOO-42-awesome-stuff", "master", main.context.payload.pull_request.head.sha);

// Act
await main.run();
Expand All @@ -245,7 +246,7 @@ describe("PR Branch Labeler", () => {
// Arrange
const getConfigScope = nock("https://api.github.com")
.persist()
.get("/repos/Codertocat/Hello-World/contents/.github/pr-branch-labeler.yml")
.get(`/repos/Codertocat/Hello-World/contents/.github/pr-branch-labeler.yml?ref=${main.context.payload.pull_request.head.sha}`)
.reply(200, configFixture());

const postLabelsScope = nock("https://api.github.com")
Expand All @@ -255,7 +256,7 @@ describe("PR Branch Labeler", () => {
})
.reply(200);

main.context.payload = createPullRequestOpenedFixture("fix-the-build", "master");
main.context.payload = createPullRequestOpenedFixture("fix-the-build", "master", main.context.payload.pull_request.head.sha);

// Act
await main.run();
Expand All @@ -270,7 +271,7 @@ describe("PR Branch Labeler", () => {
// Arrange
const getConfigScope = nock("https://api.github.com")
.persist()
.get("/repos/Codertocat/Hello-World/contents/.github/pr-branch-labeler.yml")
.get(`/repos/Codertocat/Hello-World/contents/.github/pr-branch-labeler.yml?ref=${main.context.payload.pull_request.head.sha}`)
.reply(200, configFixture("invalid-config.yml"));

const postLabelsScope = nock("https://api.github.com")
Expand All @@ -280,7 +281,7 @@ describe("PR Branch Labeler", () => {
})
.reply(200);

main.context.payload = createPullRequestOpenedFixture("feature/FOO-42-awesome-stuff", "master");
main.context.payload = createPullRequestOpenedFixture("feature/FOO-42-awesome-stuff", "master", main.context.payload.pull_request.head.sha);

// Act
await expect(main.run()).rejects.toThrow(new Error("config.yml has invalid structure."));
Expand All @@ -292,27 +293,14 @@ describe("PR Branch Labeler", () => {
});
});

function encodeContent(content: Buffer | ArrayBuffer | SharedArrayBuffer) {
return Buffer.from(content).toString("base64");
}

function configFixture(fileName = "config.yml") {
return {
type: "file",
encoding: "base64",
name: fileName,
path: `.github/${fileName}`,
content: encodeContent(readFileSync(`./__tests__/fixtures/${fileName}`))
};
}

function createPullRequestOpenedFixture(headRef: string, baseRef: string) {
function createPullRequestOpenedFixture(headRef: string, baseRef: string, sha: string) {
return {
action: "opened",
pull_request: {
number: 42,
head: {
ref: headRef
ref: headRef,
sha
},
base: {
ref: baseRef
Expand Down
Loading

0 comments on commit b3000eb

Please sign in to comment.