Skip to content

Commit

Permalink
Merge pull request #2 from TheCleric/feature/enhance-debugging
Browse files Browse the repository at this point in the history
Feature/enhance debugging
  • Loading branch information
TheCleric authored Aug 24, 2020
2 parents 1949133 + a428e02 commit 9adbbed
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 3,989 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)
40 changes: 19 additions & 21 deletions __tests__/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
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/TheCleric/Hello-World/contents/.github/pr-branch-labeler.yml")
.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", {
repo: "Hello-World",
owner: "TheCleric"
});
const config = await getConfig(octokit, "pr-branch-labeler.yml", context);

// Assert
expect(getConfigScope.isDone()).toBeTrue();
Expand All @@ -40,15 +47,12 @@ describe("Config file loader", () => {
// Arrange
const getConfigScope = nock("https://api.github.com")
.persist()
.get("/repos/TheCleric/Hello-World/contents/.github/pr-branch-labeler.yml")
.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", {
repo: "Hello-World",
owner: "TheCleric"
})).rejects.toThrow(new Error("config.yml has invalid structure."));
await expect(getConfig(octokit, "pr-branch-labeler.yml", context)).rejects.toThrow(new Error("config.yml has invalid structure."));

// Assert
expect(getConfigScope.isDone()).toBeTrue();
Expand All @@ -60,15 +64,12 @@ describe("Config file loader", () => {
// Arrange
const getConfigScope = nock("https://api.github.com")
.persist()
.get("/repos/TheCleric/Hello-World/contents/.github/test")
.get("/repos/Codertocat/Hello-World/contents/.github/test?ref=0123456")
.reply(200, []);

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

await expect(getConfig(octokit, "test", {
repo: "Hello-World",
owner: "TheCleric"
})).rejects.toThrow(new Error("test is not a file."));
await expect(getConfig(octokit, "test", context)).rejects.toThrow(new Error("test is not a file."));

// Assert
expect(getConfigScope.isDone()).toBeTrue();
Expand All @@ -80,15 +81,12 @@ describe("Config file loader", () => {
// Arrange
const getConfigScope = nock("https://api.github.com")
.persist()
.get("/repos/TheCleric/Hello-World/contents/.github/pr-branch-labeler.yml")
.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", {
repo: "Hello-World",
owner: "TheCleric"
})).rejects.toThrow(new Error("pr-branch-labeler.yml is empty."));
await expect(getConfig(octokit, "pr-branch-labeler.yml", context)).rejects.toThrow(new Error("pr-branch-labeler.yml is empty."));

// Assert
expect(getConfigScope.isDone()).toBeTrue();
Expand Down
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
46 changes: 24 additions & 22 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,13 +293,14 @@ describe("PR Branch Labeler", () => {
});
});

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
6 changes: 6 additions & 0 deletions __tests__/shared.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import * as core from "@actions/core";
import { readFileSync } from "fs";
import "jest-extended";

// Dump core debug/error to console debug so that --silent works
jest.spyOn(core, 'debug').mockImplementation(console.debug);
jest.spyOn(core, 'error').mockImplementation(console.debug);

function encodeContent(content: Buffer | ArrayBuffer | SharedArrayBuffer) {
return Buffer.from(content).toString("base64");
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ module.exports = {
transform: {
"^.+\\.ts$": "ts-jest"
},
verbose: true
verbose: true,
};
Loading

0 comments on commit 9adbbed

Please sign in to comment.