diff --git a/src/handlers/assign/check-pull-requests.ts b/src/handlers/assign/check-pull-requests.ts index 4625aedd4..f5f0b3c14 100644 --- a/src/handlers/assign/check-pull-requests.ts +++ b/src/handlers/assign/check-pull-requests.ts @@ -1,5 +1,3 @@ -import axios from "axios"; -import { HTMLElement, parse } from "node-html-parser"; import { getAllPullRequests, addAssignees } from "../../helpers/issue"; import { Context } from "../../types/context"; @@ -15,6 +13,7 @@ export async function checkPullRequests(context: Context) { // Loop through the pull requests and assign them to their respective issues if needed for (const pull of pulls) { const linkedIssue = await getLinkedIssues({ + context, owner: payload.repository.owner.login, repository: payload.repository.name, pull: pull.number, @@ -60,18 +59,31 @@ export async function checkPullRequests(context: Context) { return logger.debug(`Checking pull requests done!`); } -export async function getLinkedIssues({ owner, repository, pull }: GetLinkedParams) { - const { data } = await axios.get(`https://github.com/${owner}/${repository}/pull/${pull}`); - const dom = parse(data); - const devForm = dom.querySelector("[data-target='create-branch.developmentForm']") as HTMLElement; - const linkedIssues = devForm.querySelectorAll(".my-1"); +export async function getLinkedIssues({ context, owner, repository, pull }: GetLinkedParams) { + if (!pull || !context) return null; + const { data } = await context.octokit.pulls.get({ + owner, + repo: repository, + pull_number: pull, + }); - if (linkedIssues.length === 0) { + const body = data.body; + if (!body) return null; + + const match = body.match(/#(\d+)/); + const issueNumber = match ? match[1] : null; + + if (!issueNumber) { return null; } - const issueUrl = linkedIssues[0].querySelector("a")?.attrs?.href || null; - return issueUrl; + const issue = await context.octokit.issues.get({ + owner, + repo: repository, + issue_number: Number(issueNumber), + }); + + return issue.data.html_url; } export async function getPullByNumber(context: Context, pull: number) { @@ -105,6 +117,7 @@ export async function getIssueByNumber(context: Context, issueNumber: number) { } } export interface GetLinkedParams { + context?: Context; owner: string; repository: string; issue?: number; diff --git a/src/helpers/get-linked-pull-requests.ts b/src/helpers/get-linked-pull-requests.ts index c265d81c8..7c9d89b8b 100644 --- a/src/helpers/get-linked-pull-requests.ts +++ b/src/helpers/get-linked-pull-requests.ts @@ -1,7 +1,6 @@ -import axios from "axios"; -import { HTMLElement, parse } from "node-html-parser"; -import { GetLinkedParams } from "../handlers/assign/check-pull-requests"; +import { GetLinkedParams, getLinkedIssues } from "../handlers/assign/check-pull-requests"; import { Context } from "../types/context"; +import { getAllPullRequests } from "./issue"; interface GetLinkedResults { organization: string; repository: string; @@ -12,37 +11,31 @@ export async function getLinkedPullRequests( context: Context, { owner, repository, issue }: GetLinkedParams ): Promise { - const logger = context.logger; + if (!issue) return []; + // 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 []; - } - - for (const linked of linkedList) { - const relativeHref = linked.querySelector("a")?.attrs?.href; - if (!relativeHref) continue; - const parts = relativeHref.split("/"); - - // check if array size is at least 4 - if (parts.length < 4) continue; + const pulls = await getAllPullRequests(context); + const currentIssue = await context.octokit.issues.get({ + owner, + repo: repository, + issue_number: issue, + }); + for (const pull of pulls) { + const linkedIssue = await getLinkedIssues({ + context, + owner: owner, + repository: repository, + pull: pull.number, + }); - // 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}`; - - if (`${organization}/${repository}` !== `${owner}/${repository}`) { - logger.info("Skipping linked pull request from another repository", href); - continue; + if (linkedIssue === currentIssue.data.html_url) { + collection.push({ + organization: owner, + repository, + number: pull.number, + href: pull.html_url, + }); } - - collection.push({ organization, repository, number, href }); } return collection;