Skip to content

Commit

Permalink
Merge pull request #2389 from jBouyoud/protected-branch-on-old-versions
Browse files Browse the repository at this point in the history
fix: open release PR on current branch instead of base
  • Loading branch information
hipstersmoothie committed Sep 6, 2023
2 parents 97ed2c3 + ca628f7 commit 9f4bc81
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
16 changes: 14 additions & 2 deletions plugins/protected-branch/__tests__/protected-branch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ jest.mock(
exec(...args)
);

jest.mock("@auto-it/core/dist/utils/get-current-branch");

jest.mock("@auto-it/core/dist/git");
const mockAutoGit = Git as unknown as jest.SpyInstance;

Expand Down Expand Up @@ -57,6 +59,7 @@ describe("Protected-Branch Plugin", () => {

beforeEach(() => {
exec.mockReset();
(Auto.getCurrentBranch as jest.Mock).mockReset();
mockGetSha.mockReset().mockResolvedValueOnce("sha");
mockCreateCheck.mockReset();
mockCreatePr.mockReset().mockResolvedValueOnce({ data: { number: 42 } });
Expand Down Expand Up @@ -95,7 +98,7 @@ describe("Protected-Branch Plugin", () => {
const { hooks } = setupProtectedBranchPlugin(checkEnv);
await hooks.beforeRun.promise({
plugins: [["protected-branch", {}]],
// eslint-disable-next-line @typescript-eslint/no-explicit-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);
expect(checkEnv).toHaveBeenCalledWith("protected-branch", "PROTECTED_BRANCH_REVIEWER_TOKEN");
});
Expand All @@ -118,6 +121,10 @@ describe("Protected-Branch Plugin", () => {
repo: "my-repo",
};

beforeEach(() => {
(Auto.getCurrentBranch as jest.Mock).mockReturnValueOnce('current-branch-name');
});

function doMockPrApproval() {
mockAutoGit.mockReturnValueOnce({
github: {
Expand Down Expand Up @@ -150,7 +157,7 @@ describe("Protected-Branch Plugin", () => {

expect(mockCreatePr).toHaveBeenCalledWith({
...commonGitArgs,
base: "main",
base: "current-branch-name",
head: "automatic-release-sha",
title: "Automatic release",
});
Expand All @@ -173,6 +180,7 @@ describe("Protected-Branch Plugin", () => {

await expect(hooks.publish.promise(options)).resolves.toBeUndefined();

expect( (Auto.getCurrentBranch as jest.Mock)).toBeCalledTimes(1);
expect(exec).not.toHaveBeenCalled();
expect(mockGetSha).not.toHaveBeenCalled();
expect(mockCreateCheck).not.toHaveBeenCalled();
Expand All @@ -187,6 +195,7 @@ describe("Protected-Branch Plugin", () => {

await expect(hooks.publish.promise(options)).resolves.toBeUndefined();

expect( (Auto.getCurrentBranch as jest.Mock)).toBeCalledTimes(1);
expect(exec).not.toHaveBeenCalled();
expect(mockGetSha).not.toHaveBeenCalled();
expect(mockCreateCheck).not.toHaveBeenCalled();
Expand All @@ -200,6 +209,7 @@ describe("Protected-Branch Plugin", () => {

await expect(hooks.publish.promise(options)).resolves.toBeUndefined();

expect( (Auto.getCurrentBranch as jest.Mock)).toBeCalledTimes(1);
expect(exec).toHaveBeenCalledTimes(1);
expectCreateRemoteBranch();
expectHandleBranchProtections([]);
Expand All @@ -216,6 +226,7 @@ describe("Protected-Branch Plugin", () => {

await expect(hooks.publish.promise(options)).resolves.toBeUndefined();

expect( (Auto.getCurrentBranch as jest.Mock)).toBeCalledTimes(1);
expect(exec).toHaveBeenCalledTimes(1);
expectCreateRemoteBranch();
expectHandleBranchProtections(ciChecks);
Expand All @@ -232,6 +243,7 @@ describe("Protected-Branch Plugin", () => {

await expect(hooks.publish.promise(options)).resolves.toBeUndefined();

expect( (Auto.getCurrentBranch as jest.Mock)).toBeCalledTimes(1);
expect(exec).toHaveBeenCalledTimes(1);
expectCreateRemoteBranch();
expectHandleBranchProtections([]);
Expand Down
8 changes: 5 additions & 3 deletions plugins/protected-branch/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Auto, IPlugin, validatePluginConfiguration } from "@auto-it/core";
import { Auto, getCurrentBranch, IPlugin, validatePluginConfiguration } from '@auto-it/core';
import * as t from "io-ts";
import { GitOperator } from "./GitOperator";

Expand Down Expand Up @@ -60,7 +60,9 @@ export default class ProtectedBranchPlugin implements IPlugin {
stage: -1,
},
async () => {
if (!auto.git || !this.options.reviewerToken) {
const currentBranch = getCurrentBranch();

if (!auto.git || !this.options.reviewerToken || !currentBranch) {
return;
}

Expand Down Expand Up @@ -88,7 +90,7 @@ export default class ProtectedBranchPlugin implements IPlugin {
const prNumber = await gitOperator.createPr(
"Automatic release",
headBranch,
auto.baseBranch
currentBranch
);

// As reviewer, allowed in : `Restrict who can push to matching branches`
Expand Down

0 comments on commit 9f4bc81

Please sign in to comment.