Skip to content

Commit 3a22a24

Browse files
yuichi10tkyi
authored andcommitted
feat(1082): Use commit branch to clone (#50)
1 parent 7ed3389 commit 3a22a24

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

index.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -663,33 +663,35 @@ class BitbucketScm extends Scm {
663663
* Checkout the source code from a repository; resolves as an object with checkout commands
664664
* @method getCheckoutCommand
665665
* @param {Object} config
666-
* @param {String} config.branch Pipeline branch
667-
* @param {String} config.host Scm host to checkout source code from
668-
* @param {String} config.org Scm org name
669-
* @param {String} config.repo Scm repo name
670-
* @param {String} config.sha Commit sha
671-
* @param {String} [config.prRef] PR reference (can be a PR branch or reference)
666+
* @param {String} config.branch Pipeline branch
667+
* @param {String} config.host Scm host to checkout source code from
668+
* @param {String} config.org Scm org name
669+
* @param {String} config.repo Scm repo name
670+
* @param {String} config.sha Commit sha
671+
* @param {String} [config.commitBranch] Commit branch
672+
* @param {String} [config.prRef] PR reference (can be a PR branch or reference)
672673
* @return {Promise}
673674
*/
674675
_getCheckoutCommand(config) {
675676
const checkoutUrl = `${config.host}/${config.org}/${config.repo}`;
676677
const sshCheckoutUrl = `git@${config.host}:${config.org}/${config.repo}`;
677-
const checkoutRef = config.prRef ? config.branch : config.sha;
678+
const branch = config.commitBranch ? config.commitBranch : config.branch;
679+
const checkoutRef = config.prRef ? branch : config.sha;
678680
const gitWrapper = '$(if git --version > /dev/null 2>&1; ' +
679681
"then echo 'eval'; " +
680682
"else echo 'sd-step exec core/git'; fi)";
681683
const command = [];
682684

683685
// Git clone
684-
command.push(`echo Cloning ${checkoutUrl}, on branch ${config.branch}`);
686+
command.push(`echo Cloning ${checkoutUrl}, on branch ${branch}`);
685687
command.push('if [ ! -z $SCM_CLONE_TYPE ] && [ $SCM_CLONE_TYPE = ssh ]; ' +
686688
`then export SCM_URL=${sshCheckoutUrl}; ` +
687689
'elif [ ! -z $SCM_USERNAME ] && [ ! -z $SCM_ACCESS_TOKEN ]; ' +
688690
`then export SCM_URL=https://$SCM_USERNAME:$SCM_ACCESS_TOKEN@${checkoutUrl}; ` +
689691
`else export SCM_URL=https://${checkoutUrl}; fi`
690692
);
691693
command.push(`${gitWrapper} `
692-
+ `"git clone --quiet --progress --branch ${config.branch} $SCM_URL $SD_SOURCE_DIR"`);
694+
+ `"git clone --quiet --progress --branch ${branch} $SCM_URL $SD_SOURCE_DIR"`);
693695
// Reset to Sha
694696
command.push(`echo Reset to SHA ${checkoutRef}`);
695697
command.push(`${gitWrapper} "git reset --hard ${checkoutRef}"`);
@@ -702,7 +704,7 @@ class BitbucketScm extends Scm {
702704
if (config.prRef) {
703705
const prRef = config.prRef.replace('merge', 'head:pr');
704706

705-
command.push(`echo Fetching PR and merging with ${config.branch}`);
707+
command.push(`echo Fetching PR and merging with ${branch}`);
706708
command.push(`${gitWrapper} "git fetch origin ${prRef}"`);
707709
command.push(`${gitWrapper} "git merge --no-edit ${config.sha}"`);
708710
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "sd-checkout-code",
3+
"command": "echo Cloning hostName/orgName/repoName, on branch commitBranch && if [ ! -z $SCM_CLONE_TYPE ] && [ $SCM_CLONE_TYPE = ssh ]; then export SCM_URL=git@hostName:orgName/repoName; elif [ ! -z $SCM_USERNAME ] && [ ! -z $SCM_ACCESS_TOKEN ]; then export SCM_URL=https://$SCM_USERNAME:$SCM_ACCESS_TOKEN@hostName/orgName/repoName; else export SCM_URL=https://hostName/orgName/repoName; fi && $(if git --version > /dev/null 2>&1; then echo 'eval'; else echo 'sd-step exec core/git'; fi) \"git clone --quiet --progress --branch commitBranch $SCM_URL $SD_SOURCE_DIR\" && echo Reset to SHA shaValue && $(if git --version > /dev/null 2>&1; then echo 'eval'; else echo 'sd-step exec core/git'; fi) \"git reset --hard shaValue\" && echo Setting user name and user email && $(if git --version > /dev/null 2>&1; then echo 'eval'; else echo 'sd-step exec core/git'; fi) \"git config user.name sd-buildbot\" && $(if git --version > /dev/null 2>&1; then echo 'eval'; else echo 'sd-step exec core/git'; fi) \"git config user.email dev-null@screwdriver.cd\""
4+
}

test/index.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const sinon = require('sinon');
66
const testCommands = require('./data/commands.json');
77
const testPrCommands = require('./data/prCommands.json');
88
const testCustomPrCommands = require('./data/customPrCommands.json');
9+
const testCommitBranchCommands = require('./data/commitBranchCommands.json');
910
const testPayloadOpen = require('./data/pr.opened.json');
1011
const testPayloadSync = require('./data/pr.sync.json');
1112
const testPayloadClose = require('./data/pr.closed.json');
@@ -1196,6 +1197,15 @@ describe('index', function () {
11961197
assert.deepEqual(command, testCustomPrCommands);
11971198
});
11981199
});
1200+
1201+
it('resolves checkout command without prRef', () => {
1202+
config.commitBranch = 'commitBranch';
1203+
config.prRef = undefined;
1204+
1205+
return scm.getCheckoutCommand(config).then((command) => {
1206+
assert.deepEqual(command, testCommitBranchCommands);
1207+
});
1208+
});
11991209
});
12001210

12011211
describe('stats', () => {

0 commit comments

Comments
 (0)