Skip to content

Commit

Permalink
chore: added test for non-closing PR
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlementlegen committed Jul 6, 2024
1 parent 98e21d0 commit c6ba786
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
8 changes: 2 additions & 6 deletions src/helpers/update-pull-requests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { RequestError } from "@octokit/request-error";
import ms from "ms";
import { getAllTimelineEvents } from "../handlers/github-events";
import { Context } from "../types";
Expand All @@ -13,16 +12,13 @@ type IssueEvent = {

async function isPullMerged(context: Context, { repo, owner, issue_number: pullNumber }: IssueParams) {
try {
await context.octokit.pulls.checkIfMerged({
const res = await context.octokit.pulls.checkIfMerged({
repo,
owner,
pull_number: pullNumber,
});
return true;
return res.status === 204;
} catch (e) {
if (e instanceof RequestError) {
return e.status !== 204;
}
return false;
}
}
Expand Down
3 changes: 3 additions & 0 deletions tests/__mocks__/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ export const handlers = [
http.get("https://api.github.com/repos/:org/:repo/pulls/:id/merge", () => {
return HttpResponse.json();
}),
http.get("https://api.github.com/repos/:org/:repo/issues/:id/timeline", () => {
return HttpResponse.json();
}),
];
22 changes: 20 additions & 2 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { http, HttpResponse } from "msw";
import * as fs from "node:fs";
import { initializeDataSource } from "../src/adapters/sqlite/data-source";
import { PullRequest } from "../src/adapters/sqlite/entities/pull-request";
Expand All @@ -12,11 +13,12 @@ const htmlUrl = "https://github.com/ubiquibot/automated-merging/pull/1";
const actionsGithubPackage = "@actions/github";

describe("Action tests", () => {
const dbName = `database/tests/${expect.getState().currentTestName}.db`;
let dbName = `database/tests/test.db`;

beforeEach(() => {
jest.resetAllMocks();
jest.resetModules();
dbName = `database/tests/${expect.getState().currentTestName}.db`;
fs.rmSync(dbName, { force: true });
});

Expand Down Expand Up @@ -92,6 +94,20 @@ describe("Action tests", () => {
});

it("Should not close a PR that is not past the threshold", async () => {
const dataSource = await initializeDataSource(dbName);
const pr = new PullRequest();
pr.url = htmlUrl;
pr.lastActivity = new Date();
await pr.save();
server.use(
http.get(
"https://api.github.com/repos/:org/:repo/pulls/:id/merge",
() => {
return HttpResponse.json({}, { status: 404 });
},
{ once: true }
)
);
jest.mock(actionsGithubPackage, () => ({
context: {
repo: {
Expand All @@ -116,7 +132,9 @@ describe("Action tests", () => {
},
}));
const run = (await import("../src/action")).run;
await expect(run()).resolves.toReturn();
await expect(run()).resolves.toMatchObject({ status: 200 });
const pullRequests = await dataSource.getRepository(PullRequest).find();
expect(pullRequests).toHaveLength(1);
});

it("Should close a PR that is past the threshold", async () => {});
Expand Down

0 comments on commit c6ba786

Please sign in to comment.