Skip to content

Commit

Permalink
Add Octokit error handling utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
willsawyerrrr committed Jan 19, 2024
1 parent ec3985e commit 54c992c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
23 changes: 8 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { EmitterWebhookEvent } from "@octokit/webhooks";
import { APIGatewayEvent, APIGatewayProxyResult } from "aws-lambda";
import { App } from "octokit";
import { safeOctokitRequest } from "./utils";

const app = new App({
appId: process.env.APP_ID!,
Expand Down Expand Up @@ -34,7 +35,7 @@ async function run({ payload }: EmitterWebhookEvent<"pull_request">) {
const octokit = await app.getInstallationOctokit(payload.installation!.id);

if (!payload.pull_request.merged) {
await octokit.rest.issues.createComment({
await safeOctokitRequest(octokit.rest.issues.createComment, {
body: "Pull request was closed without merging.",
issue_number: payload.pull_request.number,
owner: payload.repository.owner.login,
Expand All @@ -44,23 +45,19 @@ async function run({ payload }: EmitterWebhookEvent<"pull_request">) {
return;
}

const { data, status } = await octokit.rest.issues.createComment({
const { id: commentId } = await safeOctokitRequest(octokit.rest.issues.createComment, {
body: "Running `multi-gitter`...",
issue_number: payload.pull_request.number,
owner: payload.repository.owner.login,
repo: payload.repository.name,
});

if (!(status >= 200 && status < 300)) {
return;
}

await octokit.rest.issues.updateComment({
await safeOctokitRequest(octokit.rest.issues.updateComment, {
body: "Done running `multi-gitter`.",
issue_number: payload.pull_request.number,
owner: payload.repository.owner.login,
repo: payload.repository.name,
comment_id: data.id,
comment_id: commentId,
});

// check verification status
Expand All @@ -71,22 +68,18 @@ async function run({ payload }: EmitterWebhookEvent<"pull_request">) {
async function verify({ payload }: EmitterWebhookEvent<"pull_request">) {
const octokit = await app.getInstallationOctokit(payload.installation!.id);

const { data, status } = await octokit.rest.issues.createComment({
const { id: commentId } = await safeOctokitRequest(octokit.rest.issues.createComment, {
body: "Verifying pull request contents...",
issue_number: payload.pull_request.number,
owner: payload.repository.owner.login,
repo: payload.repository.name,
});

if (!(status >= 200 && status < 300)) {
return;
}

await octokit.rest.issues.updateComment({
await safeOctokitRequest(octokit.rest.issues.updateComment, {
body: "Done verifying.",
issue_number: payload.pull_request.number,
owner: payload.repository.owner.login,
repo: payload.repository.name,
comment_id: data.id,
comment_id: commentId,
});
}
41 changes: 41 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { RequestError } from "octokit";

function camelCaseToSentenceCase(str: string) {
return str.replace(
/([a-z])([A-Z])/g,
(_, lower: string, upper: string) => `${lower} ${upper.toLowerCase()}`
);
}

// TODO: figure out how to type this to restrict the method to only
// those which are available on the `octokit.rest` instance

/**
* Calls an Octokit method, logging any errors that occur.
*
* @param method Octokit method to call
* @param params parameters to pass to the method
*
* @returns the `data` property of the response
*
* @todo figure out how to type this to restrict the method to only those which are available on the `octokit.rest` instance
*/
export async function safeOctokitRequest<Method extends (...args: any[]) => any>(
method: Method,
...params: Parameters<Method>
): Promise<Awaited<ReturnType<Method>>["data"]> {
try {
const response = await method(params);
return response.data;
} catch (error) {
if (error instanceof RequestError) {
console.error(
"Failed to %s: got HTTP %s response.",
camelCaseToSentenceCase(method.name),
error.status
);
}
console.error(error);
throw error;
}
}

0 comments on commit 54c992c

Please sign in to comment.