Skip to content

Commit

Permalink
feat: Expose linked PR information as changeset metadata
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 f8aa012 commit e1efc60
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
"@babel/core": "^7.23.2",
"@babel/preset-env": "^7.22.20",
"@babel/preset-typescript": "^7.23.2",
"@fensak-io/front-matter": "^1.0.0",
"@octokit/rest": "^20.0.2",
"babel-preset-minify": "^0.5.2"
},
"devDependencies": {
"JS-Interpreter": "git://github.com/yorinasub17/JS-Interpreter.git#c1e9044f99041dead8c6a49a4f3784486df0fdd9",
"@jest/globals": "^29.7.0",
"@parcel/config-default": "2.9.3",
"@parcel/packager-ts": "2.9.3",
Expand All @@ -56,6 +56,7 @@
"@types/node": "^20.8.2",
"@typescript-eslint/eslint-plugin": "^6.7.2",
"@typescript-eslint/parser": "^6.7.2",
"JS-Interpreter": "git://github.com/yorinasub17/JS-Interpreter.git#c1e9044f99041dead8c6a49a4f3784486df0fdd9",
"eslint": "^8.50.0",
"eslint-config-prettier": "^9.0.0",
"jest": "^29.7.0",
Expand Down
19 changes: 19 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 25 additions & 1 deletion src/engine/from_github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@
import * as nodecrypto from "crypto";

import { Octokit } from "@octokit/rest";
import {
hasParsableFrontMatter,
extract as extractFrontMatter,
} from "@fensak-io/front-matter";

import { parseUnifiedDiff } from "./patch.ts";
import { IChangeSetMetadata, IPatch, PatchOp } from "./patch_types.ts";
import {
ILinkedPR,
IChangeSetMetadata,
IPatch,
PatchOp,
} from "./patch_types.ts";
import { SourcePlatform } from "./from.ts";

const crypto = nodecrypto.webcrypto;
Expand Down Expand Up @@ -71,6 +80,7 @@ export async function patchFromGitHubPullRequest(
metadata: {
sourceBranch: pullReq.head.ref,
targetBranch: pullReq.base.ref,
linkedPRs: extractLinkedPRs(pullReq.body),
},
patchList: [],
patchFetchMap: {},
Expand Down Expand Up @@ -152,6 +162,20 @@ 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 [];
}

interface IExpectedFrontMatter {
fensak: {
linked: ILinkedPR[];
};
}
const fm = extractFrontMatter<IExpectedFrontMatter>(prDescription);
return fm.attrs.fensak.linked;
}

function hexEncode(hb: Uint8Array): string {
const hashArray = Array.from(hb);
const hashHex = hashArray
Expand Down
1 change: 1 addition & 0 deletions src/engine/interpreter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { compileRuleFn, RuleFnSourceLang } from "./compile.ts";
const nullMeta = {
sourceBranch: "foo",
targetBranch: "bar",
linkedPRs: [],
};

test("sanity check", async () => {
Expand Down
24 changes: 24 additions & 0 deletions src/engine/patch_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,36 @@ export interface IPatch {
diff: IHunk[];
}

/**
* Represents another PR that this PR is linked to. A linked PR can be used to represent a dependency where the upstream PR needs to be merged before this PR should be merged.
* @property repo The name of the repository (in the same organization) where the linked PR is located. Blank if the
* same repo.
* @property prNum The PR number of the linked PR.
* @property isMerged Whether the linked PR has been merged.
* @property isClosed Whether the linked PR has been closed. Note that a merged PR is also closed.
*/
export interface ILinkedPR {
repo: string;
prNum: number;
isMerged: boolean;
isClosed: boolean;
}

/**
* Represents metadata about the change set that is under evaluation.
* @property sourceBranch The branch that the change set originates from.
* @property targetBranch The branch that the change set is merging into.
* @property linkedPRs The list of PRs that this PR is linked to.
*/
export interface IChangeSetMetadata {
sourceBranch: string;
targetBranch: string;
linkedPRs: ILinkedPR[];
}

// A convenient const for test cases to initialize a blank changeset metadata.
export const emptyChangeSetMetadata: IChangeSetMetadata = {
sourceBranch: "",
targetBranch: "",
linkedPRs: [],
};

0 comments on commit e1efc60

Please sign in to comment.