better octokit error handling
This commit is contained in:
parent
a1ad90c511
commit
a84370e3c2
1 changed files with 35 additions and 24 deletions
|
|
@ -1,15 +1,18 @@
|
|||
import { getInstallationOctokitByOwner } from "../github/github-utils"
|
||||
import { githubApp } from "../github/github-app"
|
||||
|
||||
function updateOptimizationsDict(dict: { [key: string]: [string] }, path: string, funcName: string) {
|
||||
function updateOptimizationsDict(
|
||||
dict: { [key: string]: [string] },
|
||||
path: string,
|
||||
funcName: string,
|
||||
) {
|
||||
if (path in dict) {
|
||||
dict[path].push(funcName);
|
||||
dict[path].push(funcName)
|
||||
} else {
|
||||
dict[path] = [funcName];
|
||||
dict[path] = [funcName]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export async function verifyExistingOptimizations(req, res) {
|
||||
const { repo_owner, repo_name, pr_number } = req.body
|
||||
|
||||
|
|
@ -19,16 +22,23 @@ export async function verifyExistingOptimizations(req, res) {
|
|||
}
|
||||
console.log(`Got installation Octokit for ${repo_owner}/${repo_name}`)
|
||||
|
||||
const pr = await octokit.rest.pulls.get({
|
||||
owner: repo_owner,
|
||||
repo: repo_name,
|
||||
pull_number: pr_number,
|
||||
})
|
||||
if (pr.status !== 200) {
|
||||
let pr
|
||||
try {
|
||||
pr = await octokit.rest.pulls.get({
|
||||
owner: repo_owner,
|
||||
repo: repo_name,
|
||||
pull_number: pr_number,
|
||||
})
|
||||
} catch (error: any) {
|
||||
if (error.status === 404) {
|
||||
return res.status(404).send(`PR ${pr_number} not found for ${repo_owner}/${repo_name}`)
|
||||
}
|
||||
console.error("Error getting PR:", error)
|
||||
return res
|
||||
.status(500)
|
||||
.send({ error: `Error getting PR ${pr_number} for ${repo_owner}/${repo_name}` })
|
||||
}
|
||||
|
||||
const optimizations_dict: { [key: string]: [string] } = {}
|
||||
|
||||
if (pr.data.body.includes("This pull request contains optimizations for PR")) {
|
||||
|
|
@ -36,21 +46,22 @@ export async function verifyExistingOptimizations(req, res) {
|
|||
for (const line of pr_body) {
|
||||
if (line.includes("📄")) {
|
||||
const split = line.split(" ")
|
||||
const function_name: string = split[2].replace("`", "").replace("`", "")
|
||||
const file_path: string = split[4].replace("`", "").replace("`", "")
|
||||
updateOptimizationsDict(optimizations_dict, file_path, function_name);
|
||||
|
||||
const function_name: string = split[2].replace(/`/g, "").trim()
|
||||
const file_path: string = split[4].replace(/`/g, "").trim()
|
||||
updateOptimizationsDict(optimizations_dict, file_path, function_name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const pr_messages = await octokit.rest.issues.listComments({
|
||||
owner: repo_owner,
|
||||
repo: repo_name,
|
||||
issue_number: pr_number,
|
||||
})
|
||||
|
||||
if (pr_messages.status !== 200) {
|
||||
let pr_messages
|
||||
try {
|
||||
pr_messages = await octokit.rest.issues.listComments({
|
||||
owner: repo_owner,
|
||||
repo: repo_name,
|
||||
issue_number: pr_number,
|
||||
})
|
||||
} catch (error: any) {
|
||||
console.error("Error getting PR messages:", error)
|
||||
return res
|
||||
.status(500)
|
||||
.send({ error: `Error getting PR messages for ${repo_owner}/${repo_name}` })
|
||||
|
|
@ -62,9 +73,9 @@ export async function verifyExistingOptimizations(req, res) {
|
|||
for (const line of pr_body) {
|
||||
if (line.includes("📄")) {
|
||||
const split = line.split(" ")
|
||||
const function_name: string = split[2].replace("`", "").replace("`", "")
|
||||
const file_path: string = split[4].replace("`", "").replace("`", "")
|
||||
updateOptimizationsDict(optimizations_dict, file_path, function_name);
|
||||
const function_name: string = split[2].replace(/`/g, "").trim()
|
||||
const file_path: string = split[4].replace(/`/g, "").trim()
|
||||
updateOptimizationsDict(optimizations_dict, file_path, function_name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue