Skip to content

Commit a4cf077

Browse files
authored
feat(2437): Support read-only SCM (#77)
1 parent ee5e3a5 commit a4cf077

File tree

5 files changed

+83
-15
lines changed

5 files changed

+83
-15
lines changed

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ npm install screwdriver-scm-bitbucket
1313

1414
The class has a variety of knobs to tweak when interacting with Bitbucket.org.
1515

16-
| Parameter | Type | Description |
17-
| :------------- | :---- | :-------------|
18-
| config | Object | Configuration Object |
19-
| config.oauthClientId | String | OAuth Client ID provided by Bitbucket application |
20-
| config.oauthClientSecret | String | OAuth Client Secret provided by Bitbucket application |
21-
| config.username (sd-buildbot) | String | Bitbucket username for checkout |
22-
| config.email (dev-null@screwdriver.cd) | String | Bitbucket user email for checkout |
23-
| config.https (false) | Boolean | Is the Screwdriver API running over HTTPS |
24-
| config.fusebox ({}) | Object | [Circuit Breaker configuration][circuitbreaker] |
16+
| Parameter | Type | Default | Description |
17+
| :------------- | :---- | :------ | :-------------|
18+
| config | Object | | Configuration Object |
19+
| config.oauthClientId | String | | OAuth Client ID provided by Bitbucket application |
20+
| config.oauthClientSecret | String | | OAuth Client Secret provided by Bitbucket application |
21+
| config.username | String | sd-buildbot | Bitbucket username for checkout |
22+
| config.email | String | dev-null@screwdriver.cd | Bitbucket user email for checkout |
23+
| config.https | Boolean | false | Is the Screwdriver API running over HTTPS |
24+
| config.readOnly | Object | {} | Config with readOnly info: enabled, username, accessToken, cloneType |
25+
| config.fusebox | Object | {} | [Circuit Breaker configuration][circuitbreaker] |
26+
2527
```js
2628
const scm = new BitbucketScm({
2729
oauthClientId: 'your-client-id',

index.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class BitbucketScm extends Scm {
108108
* @param {String} options.oauthClientSecret OAuth Client Secret provided by Bitbucket application
109109
* @param {String} [options.username=sd-buildbot] Bitbucket username for checkout
110110
* @param {String} [options.email=dev-null@screwdriver.cd] Bitbucket user email for checkout
111+
* @param {Object} [options.readOnly={}] Read-only SCM instance config with: enabled, username, accessToken, cloneType
111112
* @param {Boolean} [options.https=false] Is the Screwdriver API running over HTTPS
112113
* @param {Object} [options.fusebox={}] Circuit Breaker configuration
113114
* @return {BitbucketScm}
@@ -118,6 +119,12 @@ class BitbucketScm extends Scm {
118119
this.config = joi.attempt(config, joi.object().keys({
119120
username: joi.string().optional().default('sd-buildbot'),
120121
email: joi.string().optional().default('dev-null@screwdriver.cd'),
122+
readOnly: joi.object().keys({
123+
enabled: joi.boolean().optional(),
124+
username: joi.string().optional(),
125+
accessToken: joi.string().optional(),
126+
cloneType: joi.string().valid('https', 'ssh').optional().default('https')
127+
}).optional().default({}),
121128
https: joi.boolean().optional().default(false),
122129
oauthClientId: joi.string().required(),
123130
oauthClientSecret: joi.string().required(),
@@ -794,19 +801,31 @@ class BitbucketScm extends Scm {
794801

795802
// Git clone
796803
command.push(`echo 'Cloning ${checkoutUrl}, on branch ${branch}'`);
797-
command.push('if [ ! -z $SCM_CLONE_TYPE ] && [ $SCM_CLONE_TYPE = ssh ]; ' +
798-
`then export SCM_URL=${sshCheckoutUrl}; ` +
799-
'elif [ ! -z $SCM_USERNAME ] && [ ! -z $SCM_ACCESS_TOKEN ]; ' +
800-
`then export SCM_URL=https://$SCM_USERNAME:$SCM_ACCESS_TOKEN@${checkoutUrl}; ` +
801-
`else export SCM_URL=https://${checkoutUrl}; fi`
802-
);
804+
805+
// Use read-only clone type
806+
if (hoek.reach(this.config, 'readOnly.enabled')) {
807+
if (hoek.reach(this.config, 'readOnly.cloneType') === 'ssh') {
808+
command.push(`export SCM_URL=${sshCheckoutUrl}`);
809+
} else {
810+
command.push('if [ ! -z $SCM_USERNAME ] && [ ! -z $SCM_ACCESS_TOKEN ]; ' +
811+
`then export SCM_URL=https://$SCM_USERNAME:$SCM_ACCESS_TOKEN@${checkoutUrl}; ` +
812+
`else export SCM_URL=https://${checkoutUrl}; fi`);
813+
}
814+
} else {
815+
command.push('if [ ! -z $SCM_CLONE_TYPE ] && [ $SCM_CLONE_TYPE = ssh ]; ' +
816+
`then export SCM_URL=${sshCheckoutUrl}; ` +
817+
'elif [ ! -z $SCM_USERNAME ] && [ ! -z $SCM_ACCESS_TOKEN ]; ' +
818+
`then export SCM_URL=https://$SCM_USERNAME:$SCM_ACCESS_TOKEN@${checkoutUrl}; ` +
819+
`else export SCM_URL=https://${checkoutUrl}; fi`);
820+
}
803821
command.push('if [ ! -z $GIT_SHALLOW_CLONE ] && [ $GIT_SHALLOW_CLONE = false ]; '
804822
+ `then ${gitWrapper} `
805823
+ `"git clone --recursive --quiet --progress --branch '${branch}' `
806824
+ '$SCM_URL $SD_SOURCE_DIR"; '
807825
+ `else ${gitWrapper} `
808826
+ '"git clone --depth=50 --no-single-branch --recursive --quiet --progress '
809827
+ `--branch '${branch}' $SCM_URL $SD_SOURCE_DIR"; fi`);
828+
810829
// Reset to Sha
811830
command.push(`echo 'Reset to SHA ${checkoutRef}'`);
812831
command.push(`${gitWrapper} "git reset --hard '${checkoutRef}'"`);
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 branchName' && if [ ! -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 [ ! -z $GIT_SHALLOW_CLONE ] && [ $GIT_SHALLOW_CLONE = false ]; then $(if git --version > /dev/null 2>&1; then echo 'eval'; else echo 'sd-step exec core/git'; fi) \"git clone --recursive --quiet --progress --branch 'branchName' $SCM_URL $SD_SOURCE_DIR\"; else $(if git --version > /dev/null 2>&1; then echo 'eval'; else echo 'sd-step exec core/git'; fi) \"git clone --depth=50 --no-single-branch --recursive --quiet --progress --branch 'branchName' $SCM_URL $SD_SOURCE_DIR\"; fi && 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 abcd\" && $(if git --version > /dev/null 2>&1; then echo 'eval'; else echo 'sd-step exec core/git'; fi) \"git config user.email dev-null@my.email.com\""
4+
}

test/data/readOnlyCommandsSsh.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": "echo 'Cloning hostName/orgName/repoName, on branch branchName' && export SCM_URL=git@hostName:orgName/repoName && if [ ! -z $GIT_SHALLOW_CLONE ] && [ $GIT_SHALLOW_CLONE = false ]; then $(if git --version > /dev/null 2>&1; then echo 'eval'; else echo 'sd-step exec core/git'; fi) \"git clone --recursive --quiet --progress --branch 'branchName' $SCM_URL $SD_SOURCE_DIR\"; else $(if git --version > /dev/null 2>&1; then echo 'eval'; else echo 'sd-step exec core/git'; fi) \"git clone --depth=50 --no-single-branch --recursive --quiet --progress --branch 'branchName' $SCM_URL $SD_SOURCE_DIR\"; fi && 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 abcd\" && $(if git --version > /dev/null 2>&1; then echo 'eval'; else echo 'sd-step exec core/git'; fi) \"git config user.email dev-null@my.email.com\""
4+
}

test/index.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const assert = require('chai').assert;
44
const mockery = require('mockery');
55
const sinon = require('sinon');
66
const testCommands = require('./data/commands.json');
7+
const testReadOnlyCommandsHttps = require('./data/readOnlyCommandsHttps.json');
8+
const testReadOnlyCommandsSsh = require('./data/readOnlyCommandsSsh.json');
79
const testPrCommands = require('./data/prCommands.json');
810
const testCustomPrCommands = require('./data/customPrCommands.json');
911
const testCommitBranchCommands = require('./data/commitBranchCommands.json');
@@ -92,6 +94,7 @@ describe('index', function () {
9294
username: 'abcd',
9395
email: 'dev-null@my.email.com',
9496
fusebox: {},
97+
readOnly: {},
9598
https: false
9699
});
97100
});
@@ -1539,6 +1542,42 @@ describe('index', function () {
15391542
})
15401543
);
15411544

1545+
it('gets the checkout command with https clone type when read-only is enabled', () => {
1546+
scm = new BitbucketScm({
1547+
oauthClientId: 'myclientid',
1548+
oauthClientSecret: 'myclientsecret',
1549+
username: 'abcd',
1550+
email: 'dev-null@my.email.com',
1551+
readOnly: {
1552+
enabled: true,
1553+
cloneType: 'https'
1554+
}
1555+
});
1556+
1557+
return scm.getCheckoutCommand(config)
1558+
.then((command) => {
1559+
assert.deepEqual(command, testReadOnlyCommandsHttps);
1560+
});
1561+
});
1562+
1563+
it('gets the checkout command with ssh clone type when read-only is enabled', () => {
1564+
scm = new BitbucketScm({
1565+
oauthClientId: 'myclientid',
1566+
oauthClientSecret: 'myclientsecret',
1567+
username: 'abcd',
1568+
email: 'dev-null@my.email.com',
1569+
readOnly: {
1570+
enabled: true,
1571+
cloneType: 'ssh'
1572+
}
1573+
});
1574+
1575+
return scm.getCheckoutCommand(config)
1576+
.then((command) => {
1577+
assert.deepEqual(command, testReadOnlyCommandsSsh);
1578+
});
1579+
});
1580+
15421581
it('resolves checkout command with prRef', () => {
15431582
config.prRef = 'prBranch';
15441583

0 commit comments

Comments
 (0)