Skip to content

Commit 8e2954d

Browse files
authored
Merge pull request #51 from screwdriver-cd/configDir
feat(1132): Checkout parent repo if child pipeline
2 parents 3a22a24 + c377924 commit 8e2954d

File tree

3 files changed

+63
-8
lines changed

3 files changed

+63
-8
lines changed

index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,36 @@ class BitbucketScm extends Scm {
682682
"else echo 'sd-step exec core/git'; fi)";
683683
const command = [];
684684

685+
// Checkout config pipeline if this is a child pipeline
686+
if (config.parentConfig) {
687+
const parentCheckoutUrl = `${config.parentConfig.host}/${config.parentConfig.org}/`
688+
+ `${config.parentConfig.repo}`; // URL for https
689+
const parentSshCheckoutUrl = `git@${config.parentConfig.host}:`
690+
+ `${config.parentConfig.org}/${config.parentConfig.repo}`; // URL for ssh
691+
const parentBranch = config.parentConfig.branch;
692+
const externalConfigDir = '$SD_ROOT_DIR/config';
693+
694+
command.push('if [ ! -z $SCM_CLONE_TYPE ] && [ $SCM_CLONE_TYPE = ssh ]; ' +
695+
`then export CONFIG_URL=${parentSshCheckoutUrl}; ` +
696+
'elif [ ! -z $SCM_USERNAME ] && [ ! -z $SCM_ACCESS_TOKEN ]; ' +
697+
'then export CONFIG_URL=https://$SCM_USERNAME:$SCM_ACCESS_TOKEN@'
698+
+ `${parentCheckoutUrl}; ` +
699+
`else export CONFIG_URL=https://${parentCheckoutUrl}; fi`);
700+
701+
command.push(`export SD_CONFIG_DIR=${externalConfigDir}`);
702+
703+
// Git clone
704+
command.push(`echo Cloning external config repo ${parentCheckoutUrl}`);
705+
command.push(`${gitWrapper} `
706+
+ `"git clone --quiet --progress --branch ${parentBranch} `
707+
+ '$CONFIG_URL $SD_CONFIG_DIR"');
708+
709+
// Reset to SHA
710+
command.push(`${gitWrapper} "git -C $SD_CONFIG_DIR reset --hard `
711+
+ `${config.parentConfig.sha}"`);
712+
command.push(`echo Reset external config repo to ${config.parentConfig.sha}`);
713+
}
714+
685715
// Git clone
686716
command.push(`echo Cloning ${checkoutUrl}, on branch ${branch}`);
687717
command.push('if [ ! -z $SCM_CLONE_TYPE ] && [ $SCM_CLONE_TYPE = ssh ]; ' +

test/data/childCommands.json

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": "if [ ! -z $SCM_CLONE_TYPE ] && [ $SCM_CLONE_TYPE = ssh ]; then export CONFIG_URL=git@github.com:screwdriver-cd/parent-repo; elif [ ! -z $SCM_USERNAME ] && [ ! -z $SCM_ACCESS_TOKEN ]; then export CONFIG_URL=https://$SCM_USERNAME:$SCM_ACCESS_TOKEN@github.com/screwdriver-cd/parent-repo; else export CONFIG_URL=https://github.com/screwdriver-cd/parent-repo; fi && export SD_CONFIG_DIR=$SD_ROOT_DIR/config && echo Cloning external config repo github.com/screwdriver-cd/parent-repo && $(if git --version > /dev/null 2>&1; then echo 'eval'; else echo 'sd-step exec core/git'; fi) \"git clone --quiet --progress --branch master $CONFIG_URL $SD_CONFIG_DIR\" && $(if git --version > /dev/null 2>&1; then echo 'eval'; else echo 'sd-step exec core/git'; fi) \"git -C $SD_CONFIG_DIR reset --hard 54321\" && echo Reset external config repo to 54321 && echo Cloning hostName/orgName/repoName, on branch branchName && 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 branchName $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: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const testCommands = require('./data/commands.json');
77
const testPrCommands = require('./data/prCommands.json');
88
const testCustomPrCommands = require('./data/customPrCommands.json');
99
const testCommitBranchCommands = require('./data/commitBranchCommands.json');
10+
const testChildCommands = require('./data/childCommands.json');
1011
const testPayloadOpen = require('./data/pr.opened.json');
1112
const testPayloadSync = require('./data/pr.sync.json');
1213
const testPayloadClose = require('./data/pr.closed.json');
@@ -1162,13 +1163,17 @@ describe('index', function () {
11621163
});
11631164

11641165
describe('getCheckoutCommand', () => {
1165-
const config = {
1166-
branch: 'branchName',
1167-
host: 'hostName',
1168-
org: 'orgName',
1169-
repo: 'repoName',
1170-
sha: 'shaValue'
1171-
};
1166+
let config;
1167+
1168+
beforeEach(() => {
1169+
config = {
1170+
branch: 'branchName',
1171+
host: 'hostName',
1172+
org: 'orgName',
1173+
repo: 'repoName',
1174+
sha: 'shaValue'
1175+
};
1176+
});
11721177

11731178
it('resolves checkout command without prRef', () =>
11741179
scm.getCheckoutCommand(config).then((command) => {
@@ -1185,6 +1190,8 @@ describe('index', function () {
11851190
});
11861191

11871192
it('resolves checkout command with custom username and email', () => {
1193+
config.prRef = 'prBranch';
1194+
11881195
scm = new BitbucketScm({
11891196
oauthClientId: 'myclientid',
11901197
oauthClientSecret: 'myclientsecret',
@@ -1200,12 +1207,26 @@ describe('index', function () {
12001207

12011208
it('resolves checkout command without prRef', () => {
12021209
config.commitBranch = 'commitBranch';
1203-
config.prRef = undefined;
12041210

12051211
return scm.getCheckoutCommand(config).then((command) => {
12061212
assert.deepEqual(command, testCommitBranchCommands);
12071213
});
12081214
});
1215+
1216+
it('promises to get the checkout command for a child pipeline', () => {
1217+
config.parentConfig = {
1218+
branch: 'master',
1219+
host: 'github.com',
1220+
org: 'screwdriver-cd',
1221+
repo: 'parent-repo',
1222+
sha: '54321'
1223+
};
1224+
1225+
return scm.getCheckoutCommand(config)
1226+
.then((command) => {
1227+
assert.deepEqual(command, testChildCommands);
1228+
});
1229+
});
12091230
});
12101231

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

0 commit comments

Comments
 (0)