Skip to content

Commit

Permalink
fix(issue-52): use pull request github api url as source (#53)
Browse files Browse the repository at this point in the history
fix #52
  • Loading branch information
lampajr committed Jul 10, 2023
1 parent fcc0167 commit a737aa7
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dist/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ exports.inferGitClient = inferGitClient;
const inferGitApiUrl = (prUrl, apiVersion = "v4") => {
const url = new URL(prUrl);
const baseUrl = `${url.protocol}//${url.host}`;
if (baseUrl.includes(PUBLIC_GITHUB_URL)) {
if (baseUrl.includes(PUBLIC_GITHUB_URL) || baseUrl.includes(PUBLIC_GITHUB_API)) {
return PUBLIC_GITHUB_API;
}
return `${baseUrl}/api/${apiVersion}`;
Expand Down
2 changes: 1 addition & 1 deletion dist/gha/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ exports.inferGitClient = inferGitClient;
const inferGitApiUrl = (prUrl, apiVersion = "v4") => {
const url = new URL(prUrl);
const baseUrl = `${url.protocol}//${url.host}`;
if (baseUrl.includes(PUBLIC_GITHUB_URL)) {
if (baseUrl.includes(PUBLIC_GITHUB_URL) || baseUrl.includes(PUBLIC_GITHUB_API)) {
return PUBLIC_GITHUB_API;
}
return `${baseUrl}/api/${apiVersion}`;
Expand Down
2 changes: 1 addition & 1 deletion src/service/git/git-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const inferGitApiUrl = (prUrl: string, apiVersion = "v4"): string => {
const url = new URL(prUrl);
const baseUrl = `${url.protocol}//${url.host}`;

if (baseUrl.includes(PUBLIC_GITHUB_URL)) {
if (baseUrl.includes(PUBLIC_GITHUB_URL) || baseUrl.includes(PUBLIC_GITHUB_API)) {
return PUBLIC_GITHUB_API;
}

Expand Down
12 changes: 10 additions & 2 deletions test/service/git/git-util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,22 @@ describe("check git utilities", () => {
});

test("check infer github client", ()=> {
expect(inferGitClient("https://github.com/superuser/backporting-example/-/merge_requests/4")).toStrictEqual(GitClientType.GITHUB);
expect(inferGitClient("https://github.com/superuser/backporting-example/pull/4")).toStrictEqual(GitClientType.GITHUB);
});

test("check infer gitlab client", ()=> {
expect(inferGitClient("https://my.gitlab.awesome.com/superuser/backporting-example/-/merge_requests/4")).toStrictEqual(GitClientType.GITLAB);
});

test("Not recognized git client type", ()=> {
test("not recognized git client type", ()=> {
expect(() => inferGitClient("https://not.recognized/superuser/backporting-example/-/merge_requests/4")).toThrowError("Remote git service not recognized from pr url: https://not.recognized/superuser/backporting-example/-/merge_requests/4");
});

test("check infer github client using github api", ()=> {
expect(inferGitClient("https://api.github.com/repos/owner/repo/pulls/1")).toStrictEqual(GitClientType.GITHUB);
});

test("check infer github api from github api url", ()=> {
expect(inferGitApiUrl("https://api.github.com/repos/owner/repo/pulls/1")).toStrictEqual("https://api.github.com");
});
});
82 changes: 82 additions & 0 deletions test/service/runner/cli-github-runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import GitHubClient from "@bp/service/git/github/github-client";
import CLIArgsParser from "@bp/service/args/cli/cli-args-parser";
import { addProcessArgs, createTestFile, removeTestFile, resetProcessArgs } from "../../support/utils";
import { mockGitHubClient } from "../../support/mock/git-client-mock-support";
import GitClientFactory from "@bp/service/git/git-client-factory";
import { GitClientType } from "@bp/service/git/git.types";

const GITHUB_MERGED_PR_W_OVERRIDES_CONFIG_FILE_CONTENT_PATHNAME = "./cli-github-runner-pr-merged-with-overrides.json";
const GITHUB_MERGED_PR_W_OVERRIDES_CONFIG_FILE_CONTENT = {
Expand All @@ -27,6 +29,7 @@ const GITHUB_MERGED_PR_W_OVERRIDES_CONFIG_FILE_CONTENT = {

jest.mock("@bp/service/git/git-cli");
jest.spyOn(GitHubClient.prototype, "createPullRequest");
jest.spyOn(GitClientFactory, "getOrCreate");

let parser: ArgsParser;
let runner: Runner;
Expand Down Expand Up @@ -73,6 +76,9 @@ describe("cli runner", () => {

const cwd = process.cwd() + "/bp";

expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITHUB, undefined, "https://api.github.com");

expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
expect(GitCLIService.prototype.clone).toBeCalledWith("https://github.com/owner/reponame.git", cwd, "target");

Expand Down Expand Up @@ -133,6 +139,9 @@ describe("cli runner", () => {

const cwd = process.cwd() + "/folder";

expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITHUB, undefined, "https://api.github.com");

expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
expect(GitCLIService.prototype.clone).toBeCalledWith("https://github.com/owner/reponame.git", cwd, "target");

Expand Down Expand Up @@ -167,6 +176,9 @@ describe("cli runner", () => {

const cwd = "/tmp/folder";

expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITHUB, undefined, "https://api.github.com");

expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
expect(GitCLIService.prototype.clone).toBeCalledWith("https://github.com/owner/reponame.git", cwd, "target");

Expand Down Expand Up @@ -195,6 +207,9 @@ describe("cli runner", () => {

const cwd = process.cwd() + "/bp";

expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITHUB, undefined, "https://api.github.com");

expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
expect(GitCLIService.prototype.clone).toBeCalledWith("https://github.com/owner/reponame.git", cwd, "target");

Expand Down Expand Up @@ -237,6 +252,9 @@ describe("cli runner", () => {

const cwd = process.cwd() + "/bp";

expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITHUB, undefined, "https://api.github.com");

expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
expect(GitCLIService.prototype.clone).toBeCalledWith("https://github.com/owner/reponame.git", cwd, "target");

Expand Down Expand Up @@ -289,6 +307,9 @@ describe("cli runner", () => {

const cwd = process.cwd() + "/bp";

expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITHUB, undefined, "https://api.github.com");

expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
expect(GitCLIService.prototype.clone).toBeCalledWith("https://github.com/owner/reponame.git", cwd, "target");

Expand Down Expand Up @@ -343,6 +364,9 @@ describe("cli runner", () => {

const cwd = process.cwd() + "/bp";

expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITHUB, undefined, "https://api.github.com");

expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
expect(GitCLIService.prototype.clone).toBeCalledWith("https://github.com/owner/reponame.git", cwd, "target");

Expand Down Expand Up @@ -396,6 +420,9 @@ describe("cli runner", () => {

const cwd = process.cwd() + "/bp";

expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITHUB, undefined, "https://api.github.com");

expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
expect(GitCLIService.prototype.clone).toBeCalledWith("https://github.com/owner/reponame.git", cwd, "target");

Expand Down Expand Up @@ -441,6 +468,9 @@ describe("cli runner", () => {

const cwd = process.cwd() + "/bp";

expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITHUB, undefined, "https://api.github.com");

expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
expect(GitCLIService.prototype.clone).toBeCalledWith("https://github.com/owner/reponame.git", cwd, "target");

Expand Down Expand Up @@ -485,6 +515,9 @@ describe("cli runner", () => {

const cwd = process.cwd() + "/bp";

expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITHUB, undefined, "https://api.github.com");

expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
expect(GitCLIService.prototype.clone).toBeCalledWith("https://github.com/owner/reponame.git", cwd, "target");

Expand Down Expand Up @@ -525,6 +558,9 @@ describe("cli runner", () => {

const cwd = process.cwd() + "/bp";

expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITHUB, "my-auth-token", "https://api.github.com");

expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
expect(GitCLIService.prototype.clone).toBeCalledWith("https://github.com/owner/reponame.git", cwd, "target");

Expand Down Expand Up @@ -554,4 +590,50 @@ describe("cli runner", () => {
}
);
});

// to check: https://github.com/kiegroup/git-backporting/issues/52
test("using github api url instead of html one", async () => {
addProcessArgs([
"-tb",
"target",
"-pr",
"https://api.github.com/repos/owner/reponame/pulls/2368"
]);

await runner.execute();

const cwd = process.cwd() + "/bp";

expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITHUB, undefined, "https://api.github.com");

expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
expect(GitCLIService.prototype.clone).toBeCalledWith("https://github.com/owner/reponame.git", cwd, "target");

expect(GitCLIService.prototype.createLocalBranch).toBeCalledTimes(1);
expect(GitCLIService.prototype.createLocalBranch).toBeCalledWith(cwd, "bp-target-28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc");

expect(GitCLIService.prototype.fetch).toBeCalledTimes(1);
expect(GitCLIService.prototype.fetch).toBeCalledWith(cwd, "pull/2368/head:pr/2368");

expect(GitCLIService.prototype.cherryPick).toBeCalledTimes(1);
expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc");

expect(GitCLIService.prototype.push).toBeCalledTimes(1);
expect(GitCLIService.prototype.push).toBeCalledWith(cwd, "bp-target-28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc");

expect(GitHubClient.prototype.createPullRequest).toBeCalledTimes(1);
expect(GitHubClient.prototype.createPullRequest).toBeCalledWith({
owner: "owner",
repo: "reponame",
head: "bp-target-28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc",
base: "target",
title: "[target] PR Title",
body: expect.stringContaining("**Backport:** https://github.com/owner/reponame/pull/2368"),
reviewers: ["gh-user", "that-s-a-user"],
assignees: [],
labels: [],
}
);
});
});
30 changes: 30 additions & 0 deletions test/service/runner/cli-gitlab-runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import CLIArgsParser from "@bp/service/args/cli/cli-args-parser";
import { addProcessArgs, createTestFile, removeTestFile, resetProcessArgs } from "../../support/utils";
import { getAxiosMocked } from "../../support/mock/git-client-mock-support";
import { MERGED_SQUASHED_MR } from "../../support/mock/gitlab-data";
import GitClientFactory from "@bp/service/git/git-client-factory";
import { GitClientType } from "@bp/service/git/git.types";

const GITLAB_MERGED_PR_COMPLEX_CONFIG_FILE_CONTENT_PATHNAME = "./cli-gitlab-runner-pr-merged-with-overrides.json";
const GITLAB_MERGED_PR_COMPLEX_CONFIG_FILE_CONTENT = {
Expand Down Expand Up @@ -41,6 +43,7 @@ jest.mock("axios", () => {

jest.mock("@bp/service/git/git-cli");
jest.spyOn(GitLabClient.prototype, "createPullRequest");
jest.spyOn(GitClientFactory, "getOrCreate");


let parser: ArgsParser;
Expand Down Expand Up @@ -86,6 +89,9 @@ describe("cli runner", () => {

const cwd = process.cwd() + "/bp";

expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITLAB, undefined, "https://my.gitlab.host.com/api/v4");

expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
expect(GitCLIService.prototype.clone).toBeCalledWith("https://my.gitlab.host.com/superuser/backporting-example.git", cwd, "target");

Expand Down Expand Up @@ -117,6 +123,9 @@ describe("cli runner", () => {

const cwd = process.cwd() + "/folder";

expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITLAB, undefined, "https://my.gitlab.host.com/api/v4");

expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
expect(GitCLIService.prototype.clone).toBeCalledWith("https://my.gitlab.host.com/superuser/backporting-example.git", cwd, "target");

Expand Down Expand Up @@ -148,6 +157,9 @@ describe("cli runner", () => {

const cwd = process.cwd() + "/bp";

expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITLAB, undefined, "https://my.gitlab.host.com/api/v4");

expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
expect(GitCLIService.prototype.clone).toBeCalledWith("https://my.gitlab.host.com/superuser/backporting-example.git", cwd, "target");

Expand Down Expand Up @@ -201,6 +213,9 @@ describe("cli runner", () => {

const cwd = process.cwd() + "/bp";

expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITLAB, undefined, "https://my.gitlab.host.com/api/v4");

expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
expect(GitCLIService.prototype.clone).toBeCalledWith("https://my.gitlab.host.com/superuser/backporting-example.git", cwd, "target");

Expand Down Expand Up @@ -257,6 +272,9 @@ describe("cli runner", () => {

const cwd = process.cwd() + "/bp";

expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITLAB, undefined, "https://my.gitlab.host.com/api/v4");

expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
expect(GitCLIService.prototype.clone).toBeCalledWith("https://my.gitlab.host.com/superuser/backporting-example.git", cwd, "target");

Expand Down Expand Up @@ -310,6 +328,9 @@ describe("cli runner", () => {

const cwd = process.cwd() + "/bp";

expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITLAB, undefined, "https://my.gitlab.host.com/api/v4");

expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
expect(GitCLIService.prototype.clone).toBeCalledWith("https://my.gitlab.host.com/superuser/backporting-example.git", cwd, "target");

Expand Down Expand Up @@ -355,6 +376,9 @@ describe("cli runner", () => {

const cwd = process.cwd() + "/bp";

expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITLAB, undefined, "https://my.gitlab.host.com/api/v4");

expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
expect(GitCLIService.prototype.clone).toBeCalledWith("https://my.gitlab.host.com/superuser/backporting-example.git", cwd, "target");

Expand Down Expand Up @@ -400,6 +424,9 @@ describe("cli runner", () => {

const cwd = process.cwd() + "/bp";

expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITLAB, undefined, "https://my.gitlab.host.com/api/v4");

expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
expect(GitCLIService.prototype.clone).toBeCalledWith("https://my.gitlab.host.com/superuser/backporting-example.git", cwd, "target");

Expand Down Expand Up @@ -441,6 +468,9 @@ describe("cli runner", () => {

const cwd = process.cwd() + "/bp";

expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITLAB, "my-token", "https://my.gitlab.host.com/api/v4");

expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
expect(GitCLIService.prototype.clone).toBeCalledWith("https://my.gitlab.host.com/superuser/backporting-example.git", cwd, "prod");

Expand Down

0 comments on commit a737aa7

Please sign in to comment.