From 0d09a495b6bd8465b12972423790b9aaa2aef9e8 Mon Sep 17 00:00:00 2001 From: Yoriyasu Yano <430092+yorinasub17@users.noreply.github.com> Date: Wed, 18 Oct 2023 09:52:22 -0500 Subject: [PATCH] fix: Make the target branch for the PR also available in the metadata parameter Signed-off-by: Yoriyasu Yano <430092+yorinasub17@users.noreply.github.com> --- src/engine/from_github.ts | 1 + src/engine/interpreter.test.ts | 37 +++++++++++++++++++--------------- src/engine/patch_types.ts | 2 ++ 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/engine/from_github.ts b/src/engine/from_github.ts index 2eac296..9116ee3 100644 --- a/src/engine/from_github.ts +++ b/src/engine/from_github.ts @@ -70,6 +70,7 @@ export async function patchFromGitHubPullRequest( const out: IGitHubPullRequestPatches = { metadata: { sourceBranch: pullReq.head.ref, + targetBranch: pullReq.base.ref, }, patchList: [], patchFetchMap: {}, diff --git a/src/engine/interpreter.test.ts b/src/engine/interpreter.test.ts index b6b9196..49c06d6 100644 --- a/src/engine/interpreter.test.ts +++ b/src/engine/interpreter.test.ts @@ -7,6 +7,11 @@ import { PatchOp } from "./patch_types.ts"; import { runRule, RuleLogMode, RuleLogLevel } from "./interpreter.ts"; import { compileRuleFn, RuleFnSourceLang } from "./compile.ts"; +const nullMeta = { + sourceBranch: "foo", + targetBranch: "bar", +}; + test("sanity check", async () => { const ruleFn = `function main(inp, metadata) { return inp.length === 1 && metadata.sourceBranch === "foo"; @@ -24,7 +29,7 @@ test("sanity check", async () => { diff: [], }, ], - { sourceBranch: "foo" }, + nullMeta, ); expect(result.approve).toBe(true); }); @@ -47,7 +52,7 @@ test("sanity check old version", async () => { diff: [], }, ], - { sourceBranch: "foo" }, + nullMeta, ); expect(result.approve).toBe(true); }); @@ -70,7 +75,7 @@ test("ES5 minify", async () => { diff: [], }, ], - { sourceBranch: "foo" }, + nullMeta, ); expect(result.approve).toBe(true); }); @@ -95,7 +100,7 @@ test("ES6 support", async () => { diff: [], }, ], - { sourceBranch: "foo" }, + nullMeta, ); expect(result.approve).toBe(true); }); @@ -125,7 +130,7 @@ function main(inp: IPatch[], metadata: IChangeSetMetadata) { diff: [], }, ], - { sourceBranch: "foo" }, + nullMeta, ); expect(result.approve).toBe(true); }); @@ -139,7 +144,7 @@ test("basic logging", async () => { const opts = { logMode: RuleLogMode.Capture, }; - const result = await runRule(ruleFn, [], { sourceBranch: "foo" }, opts); + const result = await runRule(ruleFn, [], nullMeta, opts); expect(result.approve).toBe(false); expect(result.logs).toEqual([ { @@ -158,7 +163,7 @@ test("logging with multiple objects", async () => { const opts = { logMode: RuleLogMode.Capture, }; - const result = await runRule(ruleFn, [], { sourceBranch: "foo" }, opts); + const result = await runRule(ruleFn, [], nullMeta, opts); expect(result.approve).toBe(false); expect(result.logs).toEqual([ { @@ -178,7 +183,7 @@ test("logging order", async () => { const opts = { logMode: RuleLogMode.Capture, }; - const result = await runRule(ruleFn, [], { sourceBranch: "foo" }, opts); + const result = await runRule(ruleFn, [], nullMeta, opts); expect(result.approve).toBe(false); expect(result.logs).toEqual([ { @@ -201,7 +206,7 @@ test("logging warn level", async () => { const opts = { logMode: RuleLogMode.Capture, }; - const result = await runRule(ruleFn, [], { sourceBranch: "foo" }, opts); + const result = await runRule(ruleFn, [], nullMeta, opts); expect(result.approve).toBe(false); expect(result.logs).toEqual([ { @@ -220,7 +225,7 @@ test("logging error level", async () => { const opts = { logMode: RuleLogMode.Capture, }; - const result = await runRule(ruleFn, [], { sourceBranch: "foo" }, opts); + const result = await runRule(ruleFn, [], nullMeta, opts); expect(result.approve).toBe(false); expect(result.logs).toEqual([ { @@ -235,7 +240,7 @@ test("main return must be boolean", async () => { return "hello world"; } `; - await expect(runRule(ruleFn, [], { sourceBranch: "foo" })).rejects.toThrow( + await expect(runRule(ruleFn, [], nullMeta)).rejects.toThrow( "main function must return boolean", ); }); @@ -246,7 +251,7 @@ test("infinite loop", async () => { return "hello world"; } `; - await expect(runRule(ruleFn, [], { sourceBranch: "foo" })).rejects.toThrow( + await expect(runRule(ruleFn, [], nullMeta)).rejects.toThrow( "user defined rule timed out", ); }, 10000); @@ -277,7 +282,7 @@ test("XMLHTTPRequest not supported", async () => { diff: [], }, ], - { sourceBranch: "foo" }, + nullMeta, ), ).rejects.toThrow("XMLHttpRequest is not defined"); }); @@ -303,7 +308,7 @@ test("fetch is not supported", async () => { diff: [], }, ], - { sourceBranch: "foo" }, + nullMeta, ), ).rejects.toThrow("fetch is not defined"); }); @@ -327,7 +332,7 @@ test("process is not supported", async () => { diff: [], }, ], - { sourceBranch: "foo" }, + nullMeta, ), ).rejects.toThrow("process is not defined"); }); @@ -351,7 +356,7 @@ test("Deno is not supported", async () => { diff: [], }, ], - { sourceBranch: "foo" }, + nullMeta, ), ).rejects.toThrow("Deno is not defined"); }); diff --git a/src/engine/patch_types.ts b/src/engine/patch_types.ts index 4647df7..2f51981 100644 --- a/src/engine/patch_types.ts +++ b/src/engine/patch_types.ts @@ -80,7 +80,9 @@ export interface IPatch { /** * 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. */ export interface IChangeSetMetadata { sourceBranch: string; + targetBranch: string; }