Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: force push to protected branch with explicit config #813

Merged
merged 1 commit into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/shipjs-lib/src/lib/config/defaultConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export default {
getTagName: ({ version }) => `v${version}`,
testCommandBeforeRelease: undefined, // ({ isYarn }) => isYarn ? 'yarn test' : 'npm run test',
appName: undefined,
forcePushBranches: [],
slack: {
default: {
username: 'Ship.js',
Expand Down
4 changes: 2 additions & 2 deletions packages/shipjs/src/flow/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ async function prepare({
const config = loadConfig(dir);
const { currentVersion, baseBranch } = validate({ config, dir });
validateMergeStrategy({ config });
const { remote } = config;
const { remote, forcePushBranches } = config;
pull({ remote, currentBranch: baseBranch, dir, dryRun });
fetchTags({ dir, dryRun });
push({ remote, currentBranch: baseBranch, dir, dryRun });
push({ remote, currentBranch: baseBranch, forcePushBranches, dir, dryRun });
const currentTag = config.getTagName({ version: currentVersion });
const { revisionRange } = await getRevisionRange({
yes,
Expand Down
26 changes: 26 additions & 0 deletions packages/shipjs/src/helper/__tests__/gitPush.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,30 @@ describe('gitPush', () => {
}
`);
});

it('force pushes', () => {
gitPush({
remote: 'origin',
refs: ['master', 'v1.2.3'],
forcePushBranches: ['master'],
dir: '.',
dryRun: false,
});
expect(print).toHaveBeenCalledTimes(0);
expect(run).toHaveBeenCalledTimes(2);
expect(run.mock.calls[0][0]).toMatchInlineSnapshot(`
Object {
"command": "git push -f origin master",
"dir": ".",
"dryRun": false,
}
`);
expect(run.mock.calls[1][0]).toMatchInlineSnapshot(`
Object {
"command": "git push origin v1.2.3",
"dir": ".",
"dryRun": false,
}
`);
});
});
18 changes: 15 additions & 3 deletions packages/shipjs/src/helper/gitPush.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import {
} from 'shipjs-lib';
import { print, run } from '../util';

export default function gitPush({ remote, refs, dir, dryRun }) {
export default function gitPush({
remote,
refs,
forcePushBranches = [],
dir,
dryRun,
}) {
const token = process.env.GITHUB_TOKEN;
if (token) {
if (!hasRemote('origin-with-token', dir)) {
Expand All @@ -20,7 +26,12 @@ export default function gitPush({ remote, refs, dir, dryRun }) {
});
}
refs.forEach((ref) => {
run({ command: `git push origin-with-token ${ref}`, dir, dryRun });
const force = forcePushBranches.includes(ref);
run({
command: `git push${force ? ' -f' : ''} origin-with-token ${ref}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

dir,
dryRun,
});
});
run({
command: `git remote remove origin-with-token`,
Expand All @@ -30,8 +41,9 @@ export default function gitPush({ remote, refs, dir, dryRun }) {
});
} else {
refs.forEach((ref) => {
const force = forcePushBranches.includes(ref);
run({
command: `git push ${remote} ${ref}`,
command: `git push${force ? ' -f' : ''} ${remote} ${ref}`,
dir,
dryRun,
});
Expand Down
10 changes: 8 additions & 2 deletions packages/shipjs/src/step/prepare/push.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import runStep from '../runStep';
import { gitPush } from '../../helper';

export default ({ remote, currentBranch, dir, dryRun }) =>
export default ({ remote, currentBranch, forcePushBranches, dir, dryRun }) =>
runStep({ title: 'Pushing to remote.' }, () => {
gitPush({ remote, refs: [currentBranch], dir, dryRun });
gitPush({
remote,
refs: [currentBranch],
forcePushBranches,
dir,
dryRun,
});
});
18 changes: 15 additions & 3 deletions packages/shipjs/src/step/release/gitPush.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,31 @@ import { run } from '../../util';
export default ({ tagName, config, dir, dryRun }) =>
runStep({ title: 'Pushing to the remote.' }, () => {
const currentBranch = getCurrentBranch(dir);
const { mergeStrategy, remote } = config;
const { mergeStrategy, remote, forcePushBranches } = config;
const destinationBranch = getBranchNameToMergeBack({
currentBranch,
mergeStrategy,
});
if (currentBranch === destinationBranch) {
gitPush({ remote, refs: [currentBranch, tagName], dir, dryRun });
gitPush({
remote,
refs: [currentBranch, tagName],
forcePushBranches,
dir,
dryRun,
});
} else {
// currentBranch: 'master'
// destinationBranch: 'develop'
// flow: develop -> master -> (here) develop
run({ command: `git checkout ${destinationBranch}`, dir, dryRun });
run({ command: `git merge ${currentBranch}`, dir, dryRun });
gitPush({ remote, refs: [destinationBranch, tagName], dir, dryRun });
gitPush({
remote,
refs: [destinationBranch, tagName],
forcePushBranches,
dir,
dryRun,
});
}
});