Skip to content

Commit

Permalink
Merge pull request #179 from gentlementlegen/fix/help-menu
Browse files Browse the repository at this point in the history
fix: skip posting help comment when no commands found
  • Loading branch information
gentlementlegen authored Oct 31, 2024
2 parents f352dcf + 1593145 commit 686761e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/github/handlers/help-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ export async function postHelpCommand(context: GitHubContext<"issue_comment.crea
const { plugin } = pluginElement.uses[0];
commands.push(...(await parseCommandsFromManifest(context, plugin)));
}
await context.octokit.rest.issues.createComment({
body: comments.concat(commands.sort()).join("\n"),
issue_number: context.payload.issue.number,
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
});
if (!commands.length) {
console.warn("No commands found, will not post the help command message.");
} else {
await context.octokit.rest.issues.createComment({
body: comments.concat(commands.sort()).join("\n"),
issue_number: context.payload.issue.number,
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
});
}
}

/**
Expand Down
59 changes: 58 additions & 1 deletion tests/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe("Event related tests", () => {
data: {
content: btoa(
JSON.stringify({
name: "plugin",
name: "plugin A",
commands: {
action: {
description: "action",
Expand Down Expand Up @@ -134,4 +134,61 @@ describe("Event related tests", () => {
],
]);
});
it("Should not post the help menu when /help command if there is no available command", async () => {
const issues = {
createComment(params?: RestEndpointMethodTypes["issues"]["createComment"]["parameters"]) {
return params;
},
};
const spy = jest.spyOn(issues, "createComment");
const getContent = jest.fn((params?: RestEndpointMethodTypes["repos"]["getContent"]["parameters"]) => {
if (params?.path === CONFIG_FULL_PATH) {
return {
data: `
plugins:
- name: "Some Action plugin"
uses:
- id: plugin-c
plugin: ubiquity-os/plugin-c
`,
};
} else if (params?.path === "manifest.json") {
return {
data: {
content: btoa(
JSON.stringify({
name: "plugin c",
})
),
},
};
} else {
throw new Error("Not found");
}
});
await issueCommentCreated({
id: "",
key: "issue_comment.created",
octokit: {
rest: {
issues,
repos: {
getContent: getContent,
},
},
},
eventHandler: eventHandler,
payload: {
repository: {
owner: { login: "ubiquity" },
name,
},
issue: { number: 1 },
comment: {
body: "/help",
},
} as unknown as GitHubContext<"issue_comment.created">["payload"],
} as unknown as GitHubContext);
expect(spy).not.toBeCalled();
});
});

0 comments on commit 686761e

Please sign in to comment.