This repository has been archived by the owner on Sep 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace axios with native fetch for GitHub requests
- Remove axios dependency in getLinkedIssues function - Implement error handling for fetch requests - Maintain existing functionality while improving code maintainability
- Loading branch information
1 parent
12b702d
commit eb6a6e3
Showing
3 changed files
with
59 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,62 @@ | ||
import axios from "axios"; | ||
import { HTMLElement, parse } from "node-html-parser"; | ||
import { GetLinkedParams } from "../handlers/assign/check-pull-requests"; | ||
import { Context } from "../types/context"; | ||
|
||
interface GetLinkedResults { | ||
organization: string; | ||
repository: string; | ||
number: number; | ||
href: string; | ||
} | ||
|
||
export async function getLinkedPullRequests( | ||
context: Context, | ||
{ owner, repository, issue }: GetLinkedParams | ||
): Promise<GetLinkedResults[]> { | ||
const logger = context.logger; | ||
const collection = [] as GetLinkedResults[]; | ||
const { data } = await axios.get(`https://github.com/${owner}/${repository}/issues/${issue}`); | ||
const dom = parse(data); | ||
const devForm = dom.querySelector("[data-target='create-branch.developmentForm']") as HTMLElement; | ||
const linkedList = devForm.querySelectorAll(".my-1"); | ||
if (linkedList.length === 0) { | ||
context.logger.info(`No linked pull requests found`); | ||
return []; | ||
} | ||
const collection: GetLinkedResults[] = []; | ||
|
||
for (const linked of linkedList) { | ||
const relativeHref = linked.querySelector("a")?.attrs?.href; | ||
if (!relativeHref) continue; | ||
const parts = relativeHref.split("/"); | ||
try { | ||
const response = await fetch(`https://github.com/${owner}/${repository}/issues/${issue}`); | ||
|
||
// check if array size is at least 4 | ||
if (parts.length < 4) continue; | ||
if (!response.ok) { | ||
throw new Error(`GitHub responded with status ${response.status}`); | ||
} | ||
|
||
const html = await response.text(); | ||
const dom = parse(html); | ||
|
||
// extract the organization name and repo name from the link:(e.g. " | ||
const organization = parts[parts.length - 4]; | ||
const repository = parts[parts.length - 3]; | ||
const number = Number(parts[parts.length - 1]); | ||
const href = `https://github.com${relativeHref}`; | ||
const devForm = dom.querySelector("[data-target='create-branch.developmentForm']") as HTMLElement; | ||
const linkedList = devForm?.querySelectorAll(".my-1") || []; | ||
|
||
if (`${organization}/${repository}` !== `${owner}/${repository}`) { | ||
logger.info("Skipping linked pull request from another repository", href); | ||
continue; | ||
if (linkedList.length === 0) { | ||
logger.info(`No linked pull requests found`); | ||
return []; | ||
} | ||
|
||
collection.push({ organization, repository, number, href }); | ||
} | ||
for (const linked of linkedList) { | ||
const relativeHref = linked.querySelector("a")?.attrs?.href; | ||
if (!relativeHref) continue; | ||
const parts = relativeHref.split("/"); | ||
|
||
return collection; | ||
if (parts.length < 4) continue; | ||
|
||
const organization = parts[parts.length - 4]; | ||
const repo = parts[parts.length - 3]; | ||
const number = Number(parts[parts.length - 1]); | ||
const href = `https://github.com${relativeHref}`; | ||
|
||
if (`${organization}/${repo}` !== `${owner}/${repository}`) { | ||
logger.info("Skipping linked pull request from another repository", href); | ||
continue; | ||
} | ||
|
||
collection.push({ organization, repository: repo, number, href }); | ||
} | ||
|
||
return collection; | ||
} catch (error) { | ||
logger.error("Error fetching linked pull requests:", error); | ||
throw error; | ||
} | ||
} |