fix(gh-96): fix git token parsing (#98)

This commit is contained in:
Andrea Lamparelli 2024-02-23 15:13:34 +01:00 committed by GitHub
parent 2c5c54654d
commit c57fca6bd6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 324 additions and 342 deletions

View file

@ -4,7 +4,7 @@ import PullRequestConfigsParser from "@bp/service/configs/pullrequest/pr-configs
import GitClientFactory from "@bp/service/git/git-client-factory";
import { GitClientType } from "@bp/service/git/git.types";
import { mockGitHubClient } from "../../../support/mock/git-client-mock-support";
import { resetEnvTokens, resetProcessArgs } from "../../../support/utils";
import { resetProcessArgs } from "../../../support/utils";
import { MERGED_PR_FIXTURE, REPO, TARGET_OWNER, MULT_COMMITS_PR_FIXTURE } from "../../../support/mock/github-data";
import GitHubMapper from "@bp/service/git/github/github-mapper";
import GitHubClient from "@bp/service/git/github/github-client";
@ -27,9 +27,6 @@ describe("github pull request config parser", () => {
beforeEach(() => {
// reset process.env variables
resetProcessArgs();
// reset env tokens
resetEnvTokens();
// mock octokit
mockGitHubClient("http://localhost/api/v3");

View file

@ -1,10 +1,10 @@
import { Args } from "@bp/service/args/args.types";
import { AuthTokenId, Configs } from "@bp/service/configs/configs.types";
import { Configs } from "@bp/service/configs/configs.types";
import PullRequestConfigsParser from "@bp/service/configs/pullrequest/pr-configs-parser";
import GitClientFactory from "@bp/service/git/git-client-factory";
import { GitClientType } from "@bp/service/git/git.types";
import { mockGitHubClient } from "../../../support/mock/git-client-mock-support";
import { addProcessArgs, createTestFile, removeTestFile, resetEnvTokens, resetProcessArgs } from "../../../support/utils";
import { addProcessArgs, createTestFile, removeTestFile, resetProcessArgs } from "../../../support/utils";
import { MERGED_PR_FIXTURE, OPEN_PR_FIXTURE, NOT_MERGED_PR_FIXTURE, REPO, TARGET_OWNER, MULT_COMMITS_PR_FIXTURE } from "../../../support/mock/github-data";
import CLIArgsParser from "@bp/service/args/cli/cli-args-parser";
import GitHubMapper from "@bp/service/git/github/github-mapper";
@ -66,9 +66,6 @@ describe("github pull request config parser", () => {
// reset process.env variables
resetProcessArgs();
// reset env tokens
resetEnvTokens();
// mock octokit
mockGitHubClient("http://localhost/api/v3");
@ -842,82 +839,4 @@ describe("github pull request config parser", () => {
comments: ["First comment", "Second comment"],
});
});
test("override token using auth arg", async () => {
process.env[AuthTokenId.GITHUB_TOKEN] = "mygithubtoken";
const args: Args = {
dryRun: true,
auth: "whatever",
pullRequest: mergedPRUrl,
targetBranch: "prod",
folder: "/tmp/test",
gitUser: "GitHub",
gitEmail: "noreply@github.com",
reviewers: [],
assignees: [],
inheritReviewers: true,
};
const configs: Configs = await configParser.parseAndValidate(args);
expect(configs.dryRun).toEqual(true);
expect(configs.auth).toEqual("whatever");
expect(configs.folder).toEqual("/tmp/test");
expect(configs.git).toEqual({
user: "GitHub",
email: "noreply@github.com"
});
});
test("auth using GITHUB_TOKEN has precedence over GIT_TOKEN env variable", async () => {
process.env[AuthTokenId.GIT_TOKEN] = "mygittoken";
process.env[AuthTokenId.GITHUB_TOKEN] = "mygithubtoken";
const args: Args = {
dryRun: true,
pullRequest: mergedPRUrl,
targetBranch: "prod",
folder: "/tmp/test",
gitUser: "GitHub",
gitEmail: "noreply@github.com",
reviewers: [],
assignees: [],
inheritReviewers: true,
};
const configs: Configs = await configParser.parseAndValidate(args);
expect(configs.dryRun).toEqual(true);
expect(configs.auth).toEqual("mygithubtoken");
expect(configs.folder).toEqual("/tmp/test");
expect(configs.git).toEqual({
user: "GitHub",
email: "noreply@github.com"
});
});
test("ignore env variables related to other git platforms", async () => {
process.env[AuthTokenId.GITLAB_TOKEN] = "mygitlabtoken";
process.env[AuthTokenId.CODEBERG_TOKEN] = "mycodebergtoken";
const args: Args = {
dryRun: true,
pullRequest: mergedPRUrl,
targetBranch: "prod",
folder: "/tmp/test",
gitUser: "GitHub",
gitEmail: "noreply@github.com",
reviewers: [],
assignees: [],
inheritReviewers: true,
};
const configs: Configs = await configParser.parseAndValidate(args);
expect(configs.dryRun).toEqual(true);
expect(configs.auth).toEqual(undefined);
expect(configs.folder).toEqual("/tmp/test");
expect(configs.git).toEqual({
user: "GitHub",
email: "noreply@github.com"
});
});
});

View file

@ -7,7 +7,6 @@ import { getAxiosMocked } from "../../../support/mock/git-client-mock-support";
import { MERGED_SQUASHED_MR } from "../../../support/mock/gitlab-data";
import GitLabClient from "@bp/service/git/gitlab/gitlab-client";
import GitLabMapper from "@bp/service/git/gitlab/gitlab-mapper";
import { resetEnvTokens } from "../../../support/utils";
jest.spyOn(GitLabMapper.prototype, "mapPullRequest");
jest.spyOn(GitLabClient.prototype, "getPullRequest");
@ -32,9 +31,6 @@ describe("gitlab merge request config parser", () => {
});
beforeEach(() => {
// reset env tokens
resetEnvTokens();
configParser = new PullRequestConfigsParser();
});

View file

@ -1,5 +1,5 @@
import { Args } from "@bp/service/args/args.types";
import { AuthTokenId, Configs } from "@bp/service/configs/configs.types";
import { Configs } from "@bp/service/configs/configs.types";
import PullRequestConfigsParser from "@bp/service/configs/pullrequest/pr-configs-parser";
import GitClientFactory from "@bp/service/git/git-client-factory";
import { GitClientType } from "@bp/service/git/git.types";
@ -798,97 +798,4 @@ describe("gitlab merge request config parser", () => {
comments: ["First comment", "Second comment"],
});
});
test("override token using auth arg", async () => {
process.env[AuthTokenId.GITLAB_TOKEN] = "mygitlabtoken";
const args: Args = {
dryRun: true,
auth: "whatever",
pullRequest: mergedPRUrl,
targetBranch: "prod",
folder: "/tmp/test",
gitUser: "Gitlab",
gitEmail: "noreply@gitlab.com",
reviewers: [],
assignees: [],
inheritReviewers: true,
};
const configs: Configs = await configParser.parseAndValidate(args);
expect(GitLabClient.prototype.getPullRequest).toBeCalledTimes(1);
expect(GitLabClient.prototype.getPullRequest).toBeCalledWith("superuser", "backporting-example", 1, true);
expect(GitLabMapper.prototype.mapPullRequest).toBeCalledTimes(1);
expect(GitLabMapper.prototype.mapPullRequest).toBeCalledWith(expect.anything(), []);
expect(configs.dryRun).toEqual(true);
expect(configs.auth).toEqual("whatever");
expect(configs.folder).toEqual("/tmp/test");
expect(configs.git).toEqual({
user: "Gitlab",
email: "noreply@gitlab.com"
});
});
test("auth using GITLAB_TOKEN has precedence over GIT_TOKEN env variable", async () => {
process.env[AuthTokenId.GIT_TOKEN] = "mygittoken";
process.env[AuthTokenId.GITLAB_TOKEN] = "mygitlabtoken";
const args: Args = {
dryRun: true,
pullRequest: mergedPRUrl,
targetBranch: "prod",
folder: "/tmp/test",
gitUser: "Gitlab",
gitEmail: "noreply@gitlab.com",
reviewers: [],
assignees: [],
inheritReviewers: true,
};
const configs: Configs = await configParser.parseAndValidate(args);
expect(GitLabClient.prototype.getPullRequest).toBeCalledTimes(1);
expect(GitLabClient.prototype.getPullRequest).toBeCalledWith("superuser", "backporting-example", 1, true);
expect(GitLabMapper.prototype.mapPullRequest).toBeCalledTimes(1);
expect(GitLabMapper.prototype.mapPullRequest).toBeCalledWith(expect.anything(), []);
expect(configs.dryRun).toEqual(true);
expect(configs.auth).toEqual("mygitlabtoken");
expect(configs.folder).toEqual("/tmp/test");
expect(configs.git).toEqual({
user: "Gitlab",
email: "noreply@gitlab.com"
});
});
test("ignore env variables related to other git platforms", async () => {
process.env[AuthTokenId.CODEBERG_TOKEN] = "mycodebergtoken";
process.env[AuthTokenId.GITHUB_TOKEN] = "mygithubtoken";
const args: Args = {
dryRun: true,
pullRequest: mergedPRUrl,
targetBranch: "prod",
folder: "/tmp/test",
gitUser: "Gitlab",
gitEmail: "noreply@gitlab.com",
reviewers: [],
assignees: [],
inheritReviewers: true,
};
const configs: Configs = await configParser.parseAndValidate(args);
expect(GitLabClient.prototype.getPullRequest).toBeCalledTimes(1);
expect(GitLabClient.prototype.getPullRequest).toBeCalledWith("superuser", "backporting-example", 1, true);
expect(GitLabMapper.prototype.mapPullRequest).toBeCalledTimes(1);
expect(GitLabMapper.prototype.mapPullRequest).toBeCalledWith(expect.anything(), []);
expect(configs.dryRun).toEqual(true);
expect(configs.auth).toEqual(undefined);
expect(configs.folder).toEqual("/tmp/test");
expect(configs.git).toEqual({
user: "Gitlab",
email: "noreply@gitlab.com"
});
});
});

View file

@ -3,10 +3,11 @@ import Runner from "@bp/service/runner/runner";
import GitCLIService from "@bp/service/git/git-cli";
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 { addProcessArgs, createTestFile, removeTestFile, resetEnvTokens, resetProcessArgs } from "../../support/utils";
import { mockGitHubClient } from "../../support/mock/git-client-mock-support";
import GitClientFactory from "@bp/service/git/git-client-factory";
import { BackportPullRequest, GitClientType } from "@bp/service/git/git.types";
import { AuthTokenId } from "@bp/service/configs/configs.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 = {
@ -48,6 +49,9 @@ beforeEach(() => {
// reset process.env variables
resetProcessArgs();
// reset git env tokens
resetEnvTokens();
// mock octokit
mockGitHubClient();
@ -1104,4 +1108,59 @@ describe("cli runner", () => {
});
expect(GitHubClient.prototype.createPullRequest).toThrowError();
});
test("auth using GITHUB_TOKEN takes precedence over GIT_TOKEN env variable", async () => {
process.env[AuthTokenId.GIT_TOKEN] = "mygittoken";
process.env[AuthTokenId.GITHUB_TOKEN] = "mygithubtoken";
addProcessArgs([
"-tb",
"target",
"-pr",
"https://github.com/owner/reponame/pull/8632"
]);
await runner.execute();
expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITHUB, "mygithubtoken", "https://api.github.com");
// Not interested in all subsequent calls, already tested in other test cases
});
test("auth arg takes precedence over GITHUB_TOKEN", async () => {
process.env[AuthTokenId.GITHUB_TOKEN] = "mygithubtoken";
addProcessArgs([
"-tb",
"target",
"-pr",
"https://github.com/owner/reponame/pull/8632",
"-a",
"mytoken"
]);
await runner.execute();
expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITHUB, "mytoken", "https://api.github.com");
// Not interested in all subsequent calls, already tested in other test cases
});
test("ignore env variables related to other git platforms", async () => {
process.env[AuthTokenId.GITLAB_TOKEN] = "mygitlabtoken";
process.env[AuthTokenId.CODEBERG_TOKEN] = "mycodebergtoken";
addProcessArgs([
"-tb",
"target",
"-pr",
"https://github.com/owner/reponame/pull/8632"
]);
await runner.execute();
expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITHUB, undefined, "https://api.github.com");
// Not interested in all subsequent calls, already tested in other test cases
});
});

View file

@ -3,11 +3,12 @@ import Runner from "@bp/service/runner/runner";
import GitCLIService from "@bp/service/git/git-cli";
import GitLabClient from "@bp/service/git/gitlab/gitlab-client";
import CLIArgsParser from "@bp/service/args/cli/cli-args-parser";
import { addProcessArgs, createTestFile, removeTestFile, resetProcessArgs } from "../../support/utils";
import { addProcessArgs, createTestFile, removeTestFile, resetEnvTokens, 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";
import { AuthTokenId } from "@bp/service/configs/configs.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 = {
@ -63,6 +64,9 @@ beforeEach(() => {
// reset process.env variables
resetProcessArgs();
// reset git env tokens
resetEnvTokens();
// create CLI arguments parser
parser = new CLIArgsParser();
@ -595,4 +599,59 @@ describe("cli runner", () => {
}
);
});
test("auth using GITLAB_TOKEN takes precedence over GIT_TOKEN env variable", async () => {
process.env[AuthTokenId.GIT_TOKEN] = "mygittoken";
process.env[AuthTokenId.GITLAB_TOKEN] = "mygitlabtoken";
addProcessArgs([
"-tb",
"target",
"-pr",
"https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/2"
]);
await runner.execute();
expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITLAB, "mygitlabtoken", "https://my.gitlab.host.com/api/v4");
// Not interested in all subsequent calls, already tested in other test cases
});
test("auth arg takes precedence over GITLAB_TOKEN", async () => {
process.env[AuthTokenId.GITLAB_TOKEN] = "mygitlabtoken";
addProcessArgs([
"-tb",
"target",
"-pr",
"https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/2",
"-a",
"mytoken"
]);
await runner.execute();
expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITLAB, "mytoken", "https://my.gitlab.host.com/api/v4");
// Not interested in all subsequent calls, already tested in other test cases
});
test("ignore env variables related to other git platforms", async () => {
process.env[AuthTokenId.GITHUB_TOKEN] = "mygithubtoken";
process.env[AuthTokenId.CODEBERG_TOKEN] = "mycodebergtoken";
addProcessArgs([
"-tb",
"target",
"-pr",
"https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/2"
]);
await runner.execute();
expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITLAB, undefined, "https://my.gitlab.host.com/api/v4");
// Not interested in all subsequent calls, already tested in other test cases
});
});

View file

@ -3,7 +3,7 @@ import Runner from "@bp/service/runner/runner";
import GitCLIService from "@bp/service/git/git-cli";
import GitHubClient from "@bp/service/git/github/github-client";
import GHAArgsParser from "@bp/service/args/gha/gha-args-parser";
import { createTestFile, removeTestFile, spyGetInput } from "../../support/utils";
import { createTestFile, removeTestFile, resetEnvTokens, spyGetInput } 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";
@ -46,6 +46,9 @@ afterAll(() => {
});
beforeEach(() => {
// reset git env tokens
resetEnvTokens();
mockGitHubClient();
// create GHA arguments parser

View file

@ -3,7 +3,7 @@ import Runner from "@bp/service/runner/runner";
import GitCLIService from "@bp/service/git/git-cli";
import GitLabClient from "@bp/service/git/gitlab/gitlab-client";
import GHAArgsParser from "@bp/service/args/gha/gha-args-parser";
import { createTestFile, removeTestFile, spyGetInput } from "../../support/utils";
import { createTestFile, removeTestFile, resetEnvTokens, spyGetInput } 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";
@ -59,6 +59,9 @@ afterAll(() => {
});
beforeEach(() => {
// reset git env tokens
resetEnvTokens();
// create GHA arguments parser
parser = new GHAArgsParser();