Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove axios call #68

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions src/handlers/assign/check-pull-requests.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -105,6 +117,7 @@ export async function getIssueByNumber(context: Context, issueNumber: number) {
}
}
export interface GetLinkedParams {
context?: Context;
owner: string;
repository: string;
issue?: number;
Expand Down
55 changes: 24 additions & 31 deletions src/helpers/get-linked-pull-requests.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,37 +11,31 @@ export async function getLinkedPullRequests(
context: Context,
{ owner, repository, issue }: GetLinkedParams
): Promise<GetLinkedResults[]> {
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,
0x4007 marked this conversation as resolved.
Show resolved Hide resolved
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;
Expand Down
Loading