Skip to content

Commit

Permalink
Merge pull request #42 from Meniole/fix/permissions
Browse files Browse the repository at this point in the history
fix: permissions
  • Loading branch information
gentlementlegen authored Nov 2, 2024
2 parents a8c6232 + 11529fb commit 91783e0
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

22 changes: 19 additions & 3 deletions src/helpers/remind-and-remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,28 @@ async function remindAssignees(context: ContextPlugin, issue: ListIssueForRepo)
});
} else {
const pullRequests = await collectLinkedPullRequests(context, { repo, owner, issue_number });
let shouldPostToMainIssue = false;
for (const pullRequest of pullRequests) {
const { owner: prOwner, repo: prRepo, issue_number: prNumber } = parseIssueUrl(pullRequest.url);
try {
await octokit.rest.issues.createComment({
owner: prOwner,
repo: prRepo,
issue_number: prNumber,
body: [logMessage.logMessage.raw, metadata].join("\n"),
});
} catch (e) {
logger.error(`Could not post to ${pullRequest.url} will post to the issue instead.`, { e });
shouldPostToMainIssue = true;
}
}
// This is a fallback if we failed to post the reminder to a pull-request, which can happen when posting cross
// organizations, so we post to the parent issue instead, to make sure the user got a reminder.
if (shouldPostToMainIssue) {
await octokit.rest.issues.createComment({
owner: prOwner,
repo: prRepo,
issue_number: prNumber,
owner,
repo,
issue_number,
body: [logMessage.logMessage.raw, metadata].join("\n"),
});
}
Expand Down
91 changes: 91 additions & 0 deletions tests/comment.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { beforeEach, describe, expect, it, jest } from "@jest/globals";
import { RestEndpointMethodTypes } from "@octokit/rest";
import { Logs } from "@ubiquity-os/ubiquity-os-logger";
import { ListIssueForRepo } from "../src/types/github-types";
import { ContextPlugin } from "../src/types/plugin-input";

jest.unstable_mockModule("../src/helpers/collect-linked-pulls", () => []);
jest.unstable_mockModule("../src/helpers/github-url", () => ({
parseIssueUrl: jest.fn(() => ({
repo: "repo",
owner: "owner",
issue_number: 1,
})),
}));
jest.unstable_mockModule("../src/helpers/structured-metadata", () => ({
createStructuredMetadata: jest.fn(() => ""),
}));

describe("remindAssigneesForIssue", () => {
let context: ContextPlugin;
let issue: ListIssueForRepo;

beforeEach(() => {
context = {
logger: new Logs("debug"),
octokit: {
rest: {
issues: {
createComment: jest.fn(),
removeAssignees: jest.fn(),
},
},
},
config: {
warning: 1,
disqualification: 1,
pullRequestRequired: false,
},
} as unknown as ContextPlugin;

issue = {
html_url: "https://github.com/owner/repo/issues/1",
assignees: [{ login: "ubiquity-os", id: 1 }],
} as unknown as ListIssueForRepo;
});

it("should post a comment to the parent issue if posting to the pull request fails", async () => {
context.config.pullRequestRequired = true;
jest.unstable_mockModule("../src/helpers/collect-linked-pulls", () => {
return {
collectLinkedPullRequests: jest.fn(() => [
{
url: "https://github.com/owner/repo/pull/1",
body: "",
id: "1",
login: "ubiquity-os",
number: 1,
state: "OPEN",
title: "title",
},
]),
};
});

const mockedError = new Error("Failed to post comment");

(context.octokit.rest.issues.createComment as jest.MockedFunction<typeof context.octokit.rest.issues.createComment>)
.mockRejectedValueOnce(mockedError)
.mockResolvedValueOnce({} as unknown as RestEndpointMethodTypes["issues"]["createComment"]["response"]);

const { remindAssigneesForIssue } = await import("../src/helpers/remind-and-remove");
await remindAssigneesForIssue(context, issue);

expect(context.octokit.rest.issues.createComment).toHaveBeenCalledWith(
expect.objectContaining({
owner: "owner",
repo: "repo",
issue_number: 1,
})
);

expect(context.octokit.rest.issues.createComment).toHaveBeenCalledWith(
expect.objectContaining({
owner: "owner",
repo: "repo",
issue_number: 1,
body: expect.stringContaining("this task has been idle for a while"),
})
);
});
});

0 comments on commit 91783e0

Please sign in to comment.