Skip to content

Commit

Permalink
feat: force push to protected branch with explicit config (#813)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eunjae Lee authored May 12, 2020
1 parent de2da75 commit 3647853
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 10 deletions.
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}`,
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,
});
}
});

0 comments on commit 3647853

Please sign in to comment.