Skip to content

Commit

Permalink
Merge pull request #59 from gentlementlegen/feat/error-reporting
Browse files Browse the repository at this point in the history
feat: errors are reported within the metadata of the Github comment
  • Loading branch information
gentlementlegen authored Jul 17, 2024
2 parents 97a1a38 + bc08034 commit e8474b3
Show file tree
Hide file tree
Showing 10 changed files with 699 additions and 11 deletions.
5 changes: 5 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Conversation rewards",
"description": "Generate rewards for on topic conversation for closing issues as complete.",
"ubiquity:listeners": [ "issues.closed" ]
}
5 changes: 5 additions & 0 deletions src/helpers/github-comment-module-instance.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import * as github from "@actions/github";
import { GithubCommentModule } from "../parser/github-comment-module";

export function getGithubWorkflowRunUrl() {
return `${github.context.payload.repository?.html_url}/actions/runs/${github.context.runId}`;
}

const githubCommentModule = new GithubCommentModule();

export default githubCommentModule;
15 changes: 12 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import * as core from "@actions/core";
import githubCommentModuleInstance, { getGithubWorkflowRunUrl } from "./helpers/github-comment-module-instance";
import logger from "./helpers/logger";
import { run } from "./run";

export default run()
.then((result) => {
core?.setOutput("result", result);
return result;
})
.catch((e) => {
console.error("Failed to run comment evaluation:", e);
core?.setFailed(e.toString());
.catch(async (e) => {
const errorMessage = logger.error(`Failed to run comment evaluation. ${e}`, e);
try {
await githubCommentModuleInstance.postComment(
`${errorMessage?.logMessage.diff}\n<!--\n${getGithubWorkflowRunUrl()}\n${JSON.stringify(errorMessage?.metadata, null, 2)}\n-->`
);
} catch (err) {
logger.error(`Failed to update Github comment: ${err}`);
}
core?.setFailed(e);
return e;
});
6 changes: 6 additions & 0 deletions src/parser/github-comment-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CommentType } from "../configuration/comment-types";
import configuration from "../configuration/config-reader";
import { GithubCommentConfiguration, githubCommentConfigurationType } from "../configuration/github-comment-config";
import { getOctokitInstance } from "../get-authentication-token";
import { getGithubWorkflowRunUrl } from "../helpers/github-comment-module-instance";
import logger from "../helpers/logger";
import { getERC20TokenSymbol } from "../helpers/web3";
import { IssueActivity } from "../issue-activity";
Expand Down Expand Up @@ -36,6 +37,11 @@ export class GithubCommentModule implements Module {
result[key].evaluationCommentHtml = await this._generateHtml(key, value);
bodyArray.push(result[key].evaluationCommentHtml);
}
// Add the workflow run url and the metadata in the GitHub's comment
bodyArray.push("\n<!--");
bodyArray.push(`\n${getGithubWorkflowRunUrl()}\n`);
bodyArray.push(JSON.stringify(result, null, 2));
bodyArray.push("\n-->");
const body = bodyArray.join("");
if (this._configuration.debug) {
fs.writeFileSync(this._debugFilePath, body);
Expand Down
133 changes: 132 additions & 1 deletion tests/__mocks__/results/output-reward-split.html

Large diffs are not rendered by default.

477 changes: 476 additions & 1 deletion tests/__mocks__/results/output.html

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions tests/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,42 @@ jest.mock("../src/parser/command-line", () => {
};
});

jest.mock("@actions/github", () => ({
context: {
runId: "1",
payload: {
repository: {
html_url: "https://github.com/ubiquibot/conversation-rewards",
},
},
},
}));

describe("Action tests", () => {
it("Should skip when the issue is closed without the completed status", async () => {
const result = await run();
expect(result).toEqual("Issue was not closed as completed. Skipping.");
});

it("Should link metadata to Github's comment", async () => {
jest.mock("../src/run", () => ({
run: jest.fn(() => {
return Promise.reject("Some error");
}),
}));
const githubCommentModule = require("../src/parser/github-comment-module");
const spy = jest.spyOn(githubCommentModule.GithubCommentModule.prototype, "postComment");
const run = (await import("../src/index")) as unknown as { default: Promise<string> };
await expect(run.default).resolves.toEqual("Some error");
expect(spy).toHaveBeenCalledWith(`\`\`\`diff
! Failed to run comment evaluation. Some error
\`\`\`
<!--
https://github.com/ubiquibot/conversation-rewards/actions/runs/1
{
"message": "Some error",
"caller": "error"
}
-->`);
});
});
15 changes: 14 additions & 1 deletion tests/process.issue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ jest.mock("../src/helpers/web3", () => ({
},
}));

jest.mock("@actions/github", () => ({
context: {
runId: "1",
payload: {
repository: {
html_url: "https://github.com/ubiquibot/conversation-rewards",
},
},
},
}));

jest.mock("../src/parser/command-line", () => {
// Require is needed because mock cannot access elements out of scope
// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand Down Expand Up @@ -239,7 +250,9 @@ describe("Modules tests", () => {
await processor.run(activity);
const result = JSON.parse(processor.dump());
expect(result).toEqual(githubCommentResults);
expect(fs.readFileSync("./output.html")).toEqual(fs.readFileSync("./tests/__mocks__/results/output.html"));
expect(fs.readFileSync("./output.html", "utf-8")).toEqual(
fs.readFileSync("./tests/__mocks__/results/output.html", "utf-8")
);
});

it("Should properly generate the configuration", () => {
Expand Down
19 changes: 15 additions & 4 deletions tests/rewards.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { drop } from "@mswjs/data";
import Decimal from "decimal.js";
import fs from "fs";
import { http, HttpResponse } from "msw";
import githubCommentModuleInstance from "../src/helpers/github-comment-module-instance";
import { IssueActivity } from "../src/issue-activity";
import { ContentEvaluatorModule } from "../src/parser/content-evaluator-module";
import { DataPurgeModule } from "../src/parser/data-purge-module";
import { FormattingEvaluatorModule } from "../src/parser/formatting-evaluator-module";
import { GithubCommentModule } from "../src/parser/github-comment-module";
import { PermitGenerationModule } from "../src/parser/permit-generation-module";
import { Processor } from "../src/parser/processor";
import { UserExtractorModule } from "../src/parser/user-extractor-module";
Expand All @@ -23,6 +23,17 @@ jest.spyOn(ContentEvaluatorModule.prototype, "_evaluateComments").mockImplementa
return Promise.resolve(comments.map(() => new Decimal(0.8)));
});

jest.mock("@actions/github", () => ({
context: {
runId: "1",
payload: {
repository: {
html_url: "https://github.com/ubiquibot/conversation-rewards",
},
},
},
}));

jest.mock("@ubiquibot/permit-generation/core", () => {
const originalModule = jest.requireActual("@ubiquibot/permit-generation/core");

Expand Down Expand Up @@ -136,7 +147,7 @@ describe("Rewards tests", () => {
new FormattingEvaluatorModule(),
new ContentEvaluatorModule(),
new PermitGenerationModule(),
new GithubCommentModule(),
githubCommentModuleInstance,
];
server.use(
http.post("https://*", () =>
Expand All @@ -157,8 +168,8 @@ describe("Rewards tests", () => {
await processor.run(activity);
const result = JSON.parse(processor.dump());
expect(result).toEqual(rewardSplitResult);
expect(fs.readFileSync("./output.html")).toEqual(
fs.readFileSync("./tests/__mocks__/results/output-reward-split.html")
expect(fs.readFileSync("./output.html", "utf-8")).toEqual(
fs.readFileSync("./tests/__mocks__/results/output-reward-split.html", "utf-8")
);
});
});
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3315,7 +3315,7 @@
ethers "6.11.1"
libsodium-wrappers "^0.7.13"

"@ubiquity-dao/rpc-handler@^1.1.0":
"@ubiquity-dao/[email protected]":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@ubiquity-dao/rpc-handler/-/rpc-handler-1.1.0.tgz#5a17e98de8c611ea19315ff946166b20d6f7f629"
integrity sha512-EzbwAoHx+jPEymAdJbKt1O7C9RB/R3IWcBPRbDzFs8iBOuFktClsR8e95xvFrV9n2jTmr2ZHxpXE/rOLOEWfPA==
Expand Down

0 comments on commit e8474b3

Please sign in to comment.