backport
Version:
A CLI tool that automates the process of backporting commits
253 lines • 10.3 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const getTargetBranches_1 = require("./getTargetBranches");
const prompts = __importStar(require("./prompts"));
describe('getTargetBranches', () => {
let promptSpy;
beforeEach(() => {
jest.clearAllMocks();
promptSpy = jest
.spyOn(prompts, 'promptForTargetBranches')
.mockResolvedValueOnce(['branchA']);
});
describe('when `options.targetBranches` is empty', () => {
let branches;
beforeEach(async () => {
const options = {
targetBranches: [],
targetBranchChoices: [{ name: 'branchA' }, { name: 'branchB' }],
multipleBranches: false,
interactive: true,
};
const commits = [
{
author: { email: 'soren.louv@elastic.co', name: 'Søren Louv-Jansen' },
sourceCommit: {
branchLabelMapping: {},
committedDate: 'aaa',
message: 'hey',
sha: 'abcd',
},
sourcePullRequest: {
labels: [],
url: 'foo',
title: 'hey',
number: 1337,
mergeCommit: {
message: 'hey',
sha: 'abcd',
},
},
sourceBranch: '7.x',
suggestedTargetBranches: [],
targetPullRequestStates: [],
},
];
branches = await (0, getTargetBranches_1.getTargetBranches)(options, commits);
});
it('should return branches from prompt', () => {
expect(branches).toEqual(['branchA']);
});
it('should call prompt with correct args', () => {
expect(promptSpy).toHaveBeenLastCalledWith({
targetBranchChoices: [{ name: 'branchA' }, { name: 'branchB' }],
isMultipleChoice: false,
});
});
});
describe('when `options.targetBranches` is not empty', () => {
let branches;
beforeEach(async () => {
branches = await (0, getTargetBranches_1.getTargetBranches)({
targetBranches: ['branchA', 'branchB'],
targetBranchChoices: [],
multipleBranches: false,
}, []);
});
it('should return branches from `options.branches`', () => {
expect(branches).toEqual(['branchA', 'branchB']);
});
it('should not call prompt', () => {
expect(promptSpy).not.toHaveBeenCalled();
});
});
describe('when interactive=false', () => {
it('should throw when there are no missing backports', async () => {
const commits = [];
await expect(() => {
return (0, getTargetBranches_1.getTargetBranches)({
interactive: false,
}, commits);
}).rejects.toThrow('There are no branches to backport to. Aborting.');
});
it('should return missing backports', async () => {
const commits = [
{
suggestedTargetBranches: ['7.x'],
targetPullRequestStates: [
{ branch: '7.2', state: 'MERGED' },
{ branch: '7.1', state: 'OPEN' },
{ branch: '7.x', state: 'NOT_CREATED' },
],
},
];
const targetBranches = await (0, getTargetBranches_1.getTargetBranches)({ interactive: false }, commits);
expect(targetBranches).toEqual(['7.x']);
});
});
describe('when `targetPullRequestStates` is missing a backport to 7.x', () => {
let targetBranchChoices;
beforeEach(async () => {
const options = {
interactive: true,
targetBranches: [],
multipleBranches: true,
targetBranchChoices: [
{ name: 'master' },
{ name: '7.x' },
{ name: '7.7' },
{ name: '7.6' },
{ name: '7.5' },
],
branchLabelMapping: {},
sourceBranch: 'master',
};
const commits = [
{
author: { email: 'soren.louv@elastic.co', name: 'Søren Louv-Jansen' },
sourceCommit: {
branchLabelMapping: {},
committedDate: 'bbb',
message: '[backport] Bump to 5.1.3 (#62286)',
sha: 'my-sha',
},
sourcePullRequest: {
labels: [],
url: 'foo',
title: '[backport] Bump to 5.1.3',
number: 62286,
mergeCommit: {
message: '[backport] Bump to 5.1.3 (#62286)',
sha: 'my-sha',
},
},
sourceBranch: 'master',
suggestedTargetBranches: ['7.x'],
targetPullRequestStates: [],
},
];
await (0, getTargetBranches_1.getTargetBranches)(options, commits);
targetBranchChoices = promptSpy.mock.calls[0][0].targetBranchChoices;
});
it('should list the correct branches', async () => {
expect(targetBranchChoices).toEqual([
{ checked: true, name: '7.x' },
{ checked: false, name: '7.7' },
{ checked: false, name: '7.6' },
{ checked: false, name: '7.5' },
]);
});
it('should not list the sourceBranch (master)', async () => {
expect(targetBranchChoices).not.toContainEqual(expect.objectContaining({ name: 'master' }));
});
it('should select 7.x', async () => {
expect(targetBranchChoices).toContainEqual({
name: '7.x',
checked: true,
});
});
});
describe('when `commit.suggestedTargetBranches` is not empty and non-interactive mode', () => {
let branches;
beforeEach(async () => {
const options = {
interactive: false,
};
const commits = [
{
author: { email: 'soren.louv@elastic.co', name: 'Søren Louv-Jansen' },
sourceCommit: {
branchLabelMapping: {},
committedDate: 'aaa',
message: 'hey',
sha: 'abcd',
},
sourceBranch: '7.x',
suggestedTargetBranches: ['release/targetBranch'],
targetPullRequestStates: [],
},
{
author: { email: 'soren.louv@elastic.co', name: 'Søren Louv-Jansen' },
sourceCommit: {
branchLabelMapping: {},
committedDate: 'aaa',
message: 'hey',
sha: 'abcd',
},
sourceBranch: '7.x',
suggestedTargetBranches: ['release/targetBranch'],
targetPullRequestStates: [],
},
];
branches = await (0, getTargetBranches_1.getTargetBranches)(options, commits);
});
it('should return branches from commit', () => {
expect(branches).toEqual(['release/targetBranch']);
});
});
});
describe('getTargetBranchChoices', () => {
const options = {
interactive: true,
targetBranchChoices: [
{ name: 'master', checked: true },
{ name: '7.x', checked: true },
{ name: '7.8', checked: false },
{ name: '7.7', checked: false },
],
branchLabelMapping: {},
};
const sourceBranch = 'master';
it('should not check any branches if no labels match', () => {
const targetBranchesFromLabels = [];
const branches = (0, getTargetBranches_1.getTargetBranchChoices)(options, targetBranchesFromLabels, sourceBranch);
expect(branches).toEqual([
{ checked: false, name: '7.x' },
{ checked: false, name: '7.8' },
{ checked: false, name: '7.7' },
]);
});
it('should pre-select branches based on labels', () => {
const targetBranchesFromLabels = ['7.7'];
const branches = (0, getTargetBranches_1.getTargetBranchChoices)(options, targetBranchesFromLabels, sourceBranch);
expect(branches).toEqual([
{ checked: false, name: '7.x' },
{ checked: false, name: '7.8' },
{ checked: true, name: '7.7' },
]);
});
});
//# sourceMappingURL=getTargetBranches.test.js.map