Skip to content

Commit

Permalink
fix: push to git with GITHUB_TOKEN (#234)
Browse files Browse the repository at this point in the history
* fix: push to git with GITHUB_TOKEN

* fix: validate hub with token
  • Loading branch information
Eunjae Lee authored Sep 6, 2019
1 parent bd3d6c9 commit a39e7ee
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 19 deletions.
1 change: 1 addition & 0 deletions packages/shipjs-lib/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export {
default as getLatestCommitMessage,
} from './lib/git/getLatestCommitMessage';
export { default as getRepoURL } from './lib/git/getRepoURL';
export { default as getRepoURLWithToken } from './lib/git/getRepoURLWithToken';
export { default as getLatestCommitHash } from './lib/git/getLatestCommitHash';
export { default as getCommitUrl } from './lib/git/getCommitUrl';
export { default as isWorkingTreeClean } from './lib/git/isWorkingTreeClean';
Expand Down
4 changes: 2 additions & 2 deletions packages/shipjs-lib/src/lib/git/getCommitUrl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import getRepoURL from './getRepoURL';

export default function getCommitUrl(hash, dir = '.') {
const repoURL = getRepoURL(dir);
export default function getCommitUrl(remote, hash, dir = '.') {
const repoURL = getRepoURL(remote, dir);
return `${repoURL}/commit/${hash}`;
}
8 changes: 8 additions & 0 deletions packages/shipjs-lib/src/lib/git/getRemoteOriginUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import silentExec from '../shell/silentExec';

export default function getRemoteOriginUrl(remote, dir) {
const url = silentExec(`git remote get-url ${remote}`, { dir })
.toString()
.trim();
return url;
}
8 changes: 3 additions & 5 deletions packages/shipjs-lib/src/lib/git/getRepoURL.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import silentExec from '../shell/silentExec';
import getRemoteOriginUrl from './getRemoteOriginUrl';
import gh from 'parse-github-url';

export default function getRepoURL(dir) {
const url = silentExec('git config --get remote.origin.url', { dir })
.toString()
.trim();
export default function getRepoURL(remote, dir) {
const url = getRemoteOriginUrl(remote, dir);
const { repo } = gh(url);
return `https://github.com/${repo}`;
}
8 changes: 8 additions & 0 deletions packages/shipjs-lib/src/lib/git/getRepoURLWithToken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import getRemoteOriginUrl from './getRemoteOriginUrl';
import gh from 'parse-github-url';

export default function getRepoURLWithToken(token, remote, dir) {
const url = getRemoteOriginUrl(remote, dir);
const { repo } = gh(url);
return `https://${token}@github.com/${repo}`;
}
3 changes: 2 additions & 1 deletion packages/shipjs/src/flow/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ async function release({ help = false, dir = '.', dryRun = false }) {
printDryRunBanner();
}
const config = loadConfig(dir);
const { remote } = config;
const { currentVersion: version } = validate({ config, dir });
const {
appName,
latestCommitHash,
latestCommitUrl,
repoURL,
releaseTag,
} = gatherRepoInfo({ version, dir });
} = gatherRepoInfo({ remote, version, dir });
await notifyReleaseStart({
config,
appName,
Expand Down
2 changes: 1 addition & 1 deletion packages/shipjs/src/helper/validateBeforePrepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function validateBeforePrepare({
if (baseBranches.indexOf(getCurrentBranch(dir)) === -1) {
result.push(CURRENT_BRANCH_INCORRECT);
}
if (!hasTag(currentTagName)) {
if (!hasTag(currentTagName, dir)) {
result.push(NO_TAG_FOR_CURRENT_VERSION);
}
return result.length === 0 ? true : result;
Expand Down
5 changes: 4 additions & 1 deletion packages/shipjs/src/step/checkHub.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export default () =>
exitProcess(1);
}
const configured =
silentExec('yes "" | hub api user', { ignoreError: true }).code === 0;
silentExec(
`yes "" | GITHUB_TOKEN=${process.env.GITHUB_TOKEN} hub api user`,
{ ignoreError: true }
).code === 0;
if (!configured) {
print(error('You need to configure `hub`.'));
print(
Expand Down
2 changes: 1 addition & 1 deletion packages/shipjs/src/step/prepare/createPullRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default ({
run(`git branch -D ${stagingBranch}`, dir, dryRun);
exitProcess(0);
}
const repoURL = getRepoURL(dir);
const repoURL = getRepoURL(remote, dir);
const message = formatPullRequestMessage({
repoURL,
baseBranch,
Expand Down
11 changes: 10 additions & 1 deletion packages/shipjs/src/step/prepare/push.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { getRepoURLWithToken } from 'shipjs-lib'; // eslint-disable-line import/no-unresolved
import runStep from '../runStep';

export default ({ config, currentBranch, dir, dryRun }) =>
runStep({ title: 'Pushing to remote.' }, ({ run }) => {
const { remote } = config;
run(`git push ${remote} ${currentBranch}`, dir, dryRun);

const token = process.env.GITHUB_TOKEN;
if (token) {
const url = getRepoURLWithToken(token, remote, dir);
run(`git remote add origin-with-token ${url}`, dir, dryRun);
run(`git push origin-with-token ${currentBranch}`, dir, dryRun);
} else {
run(`git push ${remote} ${currentBranch}`, dir, dryRun);
}
});
6 changes: 3 additions & 3 deletions packages/shipjs/src/step/release/gatherRepoInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import {
} from 'shipjs-lib'; // eslint-disable-line import/no-unresolved
import runStep from '../runStep';

export default ({ version, dir }) =>
export default ({ remote, version, dir }) =>
runStep({ title: 'Gathering repository information.' }, () => {
const appName = getAppName(dir);
const latestCommitHash = getLatestCommitHash(dir);
const latestCommitUrl = getCommitUrl(latestCommitHash, dir);
const repoURL = getRepoURL(dir);
const latestCommitUrl = getCommitUrl(remote, latestCommitHash, dir);
const repoURL = getRepoURL(remote, dir);
const releaseTag = getReleaseTag(version);

return {
Expand Down
22 changes: 18 additions & 4 deletions packages/shipjs/src/step/release/gitPush.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import { getCurrentBranch } from 'shipjs-lib'; // eslint-disable-line import/no-unresolved
import { getCurrentBranch, getRepoURLWithToken } from 'shipjs-lib'; // eslint-disable-line import/no-unresolved
import runStep from '../runStep';
import getBranchNameToMergeBack from '../../helper/getBranchNameToMergeBack';

function getPushCommands({ remote, tagName, dir }) {
const token = process.env.GITHUB_TOKEN;
if (token) {
const url = getRepoURLWithToken(token, remote, dir);
return [
`git remote add origin-with-token ${url}`,
`git push origin-with-token`,
`git push origin-with-token ${tagName}`,
];
} else {
return [`git push && git push ${remote} ${tagName}`];
}
}

export default ({ tagName, config, dir, dryRun }) =>
runStep({ title: 'Pushing to the remote.' }, ({ run }) => {
const currentBranch = getCurrentBranch(dir);
Expand All @@ -10,15 +24,15 @@ export default ({ tagName, config, dir, dryRun }) =>
currentBranch,
mergeStrategy,
});
const pushCommand = `git push && git push ${remote} ${tagName}`;
const pushCommands = getPushCommands({ remote, tagName, dir });
if (currentBranch === destinationBranch) {
run(pushCommand, dir, dryRun);
pushCommands.forEach(command => run(command, dir, dryRun));
} else {
// currentBranch: 'master'
// destinationBranch: 'develop'
// flow: develop -> master -> (here) develop
run(`git checkout ${destinationBranch}`, dir, dryRun);
run(`git merge ${currentBranch}`, dir, dryRun);
run(pushCommand, dir, dryRun);
pushCommands.forEach(command => run(command, dir, dryRun));
}
});

0 comments on commit a39e7ee

Please sign in to comment.