Skip to content

Commit

Permalink
fix: Fix bug where the linked PR was not pulling in merge info correctly
Browse files Browse the repository at this point in the history
Signed-off-by: Yoriyasu Yano <430092+yorinasub17@users.noreply.github.com>
  • Loading branch information
yorinasub17 committed Oct 19, 2023
1 parent 3890118 commit 63f50da
Showing 1 changed file with 55 additions and 6 deletions.
61 changes: 55 additions & 6 deletions src/engine/from_github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ export async function patchFromGitHubPullRequest(
metadata: {
sourceBranch: pullReq.head.ref,
targetBranch: pullReq.base.ref,
linkedPRs: extractLinkedPRs(pullReq.body),
linkedPRs: await extractLinkedPRs(
clt,
repo.owner,
repo.name,
prNum,
pullReq.body,
),
},
patchList: [],
patchFetchMap: {},
Expand Down Expand Up @@ -162,18 +168,61 @@ async function getGitHubPRFileID(salt: string, url: URL): Promise<string> {
return hexEncode(new Uint8Array(digest));
}

function extractLinkedPRs(prDescription: string | null): ILinkedPR[] {
if (!prDescription || !hasParsableFrontMatter(prDescription)) {
return [];
async function extractLinkedPRs(
clt: Octokit,
owner: string,
repo: string,
prNum: number,
prDescription: string | null,
): Promise<ILinkedPR[]> {
interface IFrontMatterLinkedPR {
repo?: string;
prNum: number;
}

interface IExpectedFrontMatter {
fensak: {
linked: ILinkedPR[];
linked: IFrontMatterLinkedPR[];
};
}

if (!prDescription || !hasParsableFrontMatter(prDescription)) {
return [];
}

const fm = extractFrontMatter<IExpectedFrontMatter>(prDescription);
return fm.attrs.fensak.linked;
if (!fm.attrs.fensak) {
return [];
}
if (!fm.attrs.fensak.linked) {
throw new TypeError(
`PR ${owner}/${repo}#${prNum} has front matter, but it is not in the expected format`,
);
}

const out: ILinkedPR[] = await Promise.all(
fm.attrs.fensak.linked.map(async (l): Promise<ILinkedPR> => {
let outR = "";
let r = repo;
if (l.repo) {
outR = l.repo;
r = l.repo;
}
const { data: pullReq } = await clt.pulls.get({
owner: owner,
repo: r,
pull_number: l.prNum,
});

return {
repo: outR,
prNum: l.prNum,
isMerged: pullReq.merged,
isClosed: pullReq.state === "closed",
};
}),
);
return out;
}

function hexEncode(hb: Uint8Array): string {
Expand Down

0 comments on commit 63f50da

Please sign in to comment.