Skip to content

Commit

Permalink
feat(*): update architecture, semantic-release
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberhck committed Aug 24, 2019
1 parent 60fa6cf commit c9cdf9e
Show file tree
Hide file tree
Showing 17 changed files with 289 additions and 489 deletions.
9 changes: 7 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 11 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
{
"name": "@fossapps/handlebars.templates",
"description": "",
"name": "pull_request_label_enforcer",
"description": "A github app to enforce labels on pull request",
"main": "./lib/index.js",
"typings": "./lib/index",
"keywords": [],
"author": "@cyberhck",
"keywords": [
"probot",
"github",
"pull requests",
"enforce labels on pull requests"
],
"author": "Nishchal Gautam <gautam.nishchal@gmail.com>",
"license": "GPL",
"repository": {
"type": "git",
"url": "https://github.com/fossapps/Handlebars-Issue-and-Pull-Requests.git"
"url": "git@github.com:fossapps/pr_label_enforcer.git"
},
"homepage": "https://github.com/fossapps/Handlebars-Issue-and-Pull-Requests",
"homepage": "https://cyberhck.serveo.net/",
"dependencies": {
"dotenv": "^8.0.0",
"handlebars": "^4.1.2",
"hbs": "^4.0.4",
"probot": "^9.2.19"
},
Expand Down
94 changes: 0 additions & 94 deletions src/App.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {App} from "./App";
import {IGithubIssueHelper, IPayload} from "./GithubIssueHelper";
import {IPayloadHelper} from "./PayloadHelper";

describe("App", () => {
it("should be defined", () => {
Expand All @@ -13,96 +11,4 @@ describe("App", () => {
App.handle({on: spy} as any);
expect(spy).toHaveBeenCalled();
});

it("should be able to edit pull request body correctly", async () => {
const payloadHelperMock: IPayloadHelper = {
getNewBody: () => "new body with different data"
};
const updateMock = jest.fn();
const ghHelper: IGithubIssueHelper = {
comment: () => undefined as any,
updateBody: updateMock
};
const log = jest.fn();
const mockContext = {
payload: {
pull_request: {}
},
issue: (payload: { body: string }): IPayload => ({
number: 1,
repo: "",
owner: "",
body: payload.body
}),
log: {
debug: log
}
};
const app = new App(ghHelper, payloadHelperMock, mockContext as any);
await app.handleEvent();
expect(log).toHaveBeenCalled();
expect(updateMock).toHaveBeenCalledWith({body: "new body with different data", number: 1, owner: "", repo: ""});
});

it("should be able to edit issue body correctly", async () => {
const payloadHelperMock: IPayloadHelper = {
getNewBody: () => "new body with different data"
};
const updateMock = jest.fn();
const ghHelper: IGithubIssueHelper = {
comment: () => undefined as any,
updateBody: updateMock
};
const log = jest.fn();
const mockContext = {
payload: {
issue: {}
},
issue: (payload: { body: string }): IPayload => ({
number: 1,
repo: "",
owner: "",
body: payload.body
}),
log: {
debug: log
}
};
const app = new App(ghHelper, payloadHelperMock, mockContext as any);
await app.handleEvent();
expect(log).toHaveBeenCalled();
expect(updateMock).toHaveBeenCalledWith({body: "new body with different data", number: 1, owner: "", repo: ""});
});

it("should create a issue comment if there's error with handlebar", async () => {
const payloadHelperMock: IPayloadHelper = {
getNewBody: () => {
throw new Error("Error");
}
};
const commentMock = jest.fn();
const ghHelper: IGithubIssueHelper = {
comment: commentMock,
updateBody: () => undefined as any
};
const log = jest.fn();
const mockContext = {
payload: {
issue: {}
},
issue: (payload: { body: string }): IPayload => ({
number: 1,
repo: "",
owner: "",
body: payload.body
}),
log: {
debug: log,
info: () => undefined as any
}
};
const app = new App(ghHelper, payloadHelperMock, mockContext as any);
await app.handleEvent();
expect(commentMock).toHaveBeenCalled();
});
});
62 changes: 32 additions & 30 deletions src/App.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
import * as Webhooks from "@octokit/webhooks";
import {Application, Context} from "probot";
import {GithubIssueHelper, IGithubIssueHelper} from "./GithubIssueHelper";
import {IPayloadHelper, PayloadHelper} from "./PayloadHelper";

export type TData = Webhooks.WebhookPayloadPullRequest | Webhooks.WebhookPayloadIssues;
import {getAppConfig} from "./AppConfig";
import {IGithubConfig, LabelMatcher} from "./LabelMatcher";
import {CheckStatus, StatusChecksManager} from "./StatusChecksManager";

export class App {
constructor(private ghHelper: IGithubIssueHelper, private payloadHelper: IPayloadHelper, private context: Context<TData>) {
}

public static handle(context: Application): void {
context.on(["pull_request.opened", "issues.opened", "issues.edited", "pull_request.edited"], App.handleEvent);
context.on(["pull_request.opened", "pull_request.reopened", "pull_request.labeled", "pull_request.unlabeled"], App.handleEvent);
}

private static handleEvent(context: Context<TData>): Promise<void> {
private static handleEvent(context: Context<Webhooks.WebhookPayloadPullRequest>): Promise<void> {
context.log.info(`handling ${context.event} event`);
const app = new App(new GithubIssueHelper(context.github, PayloadHelper.isPr(context.payload)), new PayloadHelper(context), context);
return app.handleEvent();
const app = new App();
return app.handleEvent(context);
}

private static getErrorComment(error: Error): string {
const message = (error.stack) ? error.stack!.split("\n").join("\n>") : error.toString();
return `## There was error processing your body
The exact error message is the following
${message}
This body won't be processed any further, please fix your template.
`;
}

public async handleEvent(): Promise<void> {
try {
const newBody = this.payloadHelper.getNewBody();
await this.ghHelper.updateBody(this.context.issue({body: newBody}));
this.context.log.debug(`updated ${PayloadHelper.isPr(this.context.payload) ? "PR" : "issue"} body`);
} catch (e) {
this.context.log.info(`Error: ${e.toString()}`);
await this.ghHelper.comment(this.context.issue({body: App.getErrorComment(e)}));
public async handleEvent(context: Context<Webhooks.WebhookPayloadPullRequest>): Promise<void> {
const data: IGithubConfig = await context.config<IGithubConfig>("pr_labels.yml");
if (!data) {
return;
}
const matcher = new LabelMatcher(context.payload.pull_request.labels);
const targetStatus = matcher.matches(data) ? CheckStatus.SUCCESS : data.invalidStatus === "failed" ? CheckStatus.FAILED : CheckStatus.PENDING;
const statusChecksManager = new StatusChecksManager(context, getAppConfig().checkName);
const status = await statusChecksManager.getCheck();
if (targetStatus === status) {
return;
}
switch (targetStatus) {
case CheckStatus.PENDING:
await statusChecksManager.setPending();
break;
case CheckStatus.FAILED:
await statusChecksManager.setFailed();
break;
case CheckStatus.SUCCESS:
await statusChecksManager.setSuccess();
break;
default:
const errorMessage = `not able to handle ${targetStatus}`;
throw new Error(errorMessage);
}
}
}
4 changes: 3 additions & 1 deletion src/AppConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export interface IAppConfig {
id: number;
cert: string;
port: number;
checkName: string;
}
export const getAppConfig = (): IAppConfig => {
if (!process.env.APP_ID) {
Expand All @@ -19,6 +20,7 @@ export const getAppConfig = (): IAppConfig => {
id,
port: 3000,
secret: process.env.WEBHOOK_SECRET,
cert: Buffer.from(process.env.PRIVATE_KEY_ENCODED, "base64").toString("ascii")
cert: Buffer.from(process.env.PRIVATE_KEY_ENCODED, "base64").toString("ascii"),
checkName: process.env.CHECK_NAME ? process.env.CHECK_NAME : "pull_request_label"
};
};
45 changes: 0 additions & 45 deletions src/GithubIssueHelper.test.ts

This file was deleted.

32 changes: 0 additions & 32 deletions src/GithubIssueHelper.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/HandlebarCompiler.test.ts

This file was deleted.

17 changes: 0 additions & 17 deletions src/HandlebarCompiler.ts

This file was deleted.

Loading

0 comments on commit c9cdf9e

Please sign in to comment.