Skip to content

Commit 3ba3340

Browse files
authored
feat(2676): Support passing in full path for getFile (#84)
1 parent 6e23532 commit 3ba3340

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

index.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const Path = require('path');
1010
const Url = require('url');
1111
const request = require('screwdriver-request');
1212
const schema = require('screwdriver-data-schema');
13+
const CHECKOUT_URL_REGEX = schema.config.regex.CHECKOUT_URL;
1314
const API_URL_V2 = 'https://api.bitbucket.org/2.0';
1415
const REPO_URL = `${API_URL_V2}/repositories`;
1516
const USER_URL = `${API_URL_V2}/users`;
@@ -49,8 +50,7 @@ function throwError(errorReason, errorCode = 500) {
4950
* @return {Object} An object with hostname, repo, branch, and username
5051
*/
5152
function getRepoInfo(checkoutUrl, rootDir) {
52-
const regex = schema.config.regex.CHECKOUT_URL;
53-
const matched = regex.exec(checkoutUrl);
53+
const matched = CHECKOUT_URL_REGEX.exec(checkoutUrl);
5454

5555
// Check if regex did not pass
5656
if (!matched) {
@@ -582,17 +582,35 @@ class BitbucketScm extends Scm {
582582
* @async getFile
583583
* @param {Object} config Configuration
584584
* @param {String} config.scmUri The scmUri
585-
* @param {String} config.path The file in the repo to fetch
585+
* @param {String} config.path The file in the repo to fetch or full checkout URL
586586
* @param {String} config.token The token used to authenticate to the SCM
587587
* @param {String} [config.ref] The reference to the SCM, either branch or sha
588588
* @return {Promise} Resolves to the content of the file
589589
*/
590590
async _getFile({ scmUri, ref, path }) {
591-
const { branch: branchFromScmUri, repoId, rootDir } = getScmUriParts(scmUri);
592-
const branch = ref || branchFromScmUri;
593-
const fullPath = rootDir ? Path.join(rootDir, path) : path;
594-
const fileUrl = `${REPO_URL}/${repoId}/src/${branch}/${fullPath}`;
591+
let fullPath = path;
592+
let username;
593+
let repo;
594+
let branch;
595+
let rootDir;
596+
let repoId;
595597
const token = await this._getToken();
598+
599+
// If full path to a file is provided, e.g. git@github.com:screwdriver-cd/scm-github.git:path/to/a/file.yaml
600+
if (CHECKOUT_URL_REGEX.test(path)) {
601+
({ username, repo, branch, rootDir } = getRepoInfo(fullPath));
602+
fullPath = rootDir;
603+
repoId = `${username}/${repo}`;
604+
} else {
605+
const scmUriParts = getScmUriParts(scmUri);
606+
607+
({ repoId, rootDir } = scmUriParts);
608+
branch = ref || scmUriParts.branch;
609+
610+
fullPath = rootDir ? Path.join(rootDir, path) : path;
611+
}
612+
613+
const fileUrl = `${REPO_URL}/${repoId}/src/${branch}/${fullPath}`;
596614
const options = {
597615
url: fileUrl,
598616
method: 'GET',

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
"debug": false
3636
},
3737
"devDependencies": {
38-
"chai": "^4.3.4",
39-
"eslint": "^7.5.0",
38+
"chai": "^4.3.6",
39+
"eslint": "^7.32.0",
4040
"eslint-config-screwdriver": "^5.0.1",
4141
"mocha": "^8.4.0",
4242
"mocha-multi-reporters": "^1.5.1",
@@ -46,11 +46,11 @@
4646
"sinon": "^9.0.0"
4747
},
4848
"dependencies": {
49-
"@hapi/hoek": "^9.2.0",
50-
"circuit-fuses": "^4.0.6",
51-
"joi": "^17.2.1",
52-
"screwdriver-data-schema": "^21.6.1",
53-
"screwdriver-request": "^1.0.0",
54-
"screwdriver-scm-base": "^7.2.1"
49+
"@hapi/hoek": "^9.2.1",
50+
"circuit-fuses": "^4.1.2",
51+
"joi": "^17.6.0",
52+
"screwdriver-data-schema": "^21.22.2",
53+
"screwdriver-request": "^1.0.3",
54+
"screwdriver-scm-base": "^7.4.0"
5555
}
5656
}

test/index.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,22 @@ describe('index', function() {
10041004
});
10051005
});
10061006

1007+
it('resolves to correct commit sha with fullPath', () => {
1008+
scmUri = 'hostName:repoId:branchName:src/app/component';
1009+
params = {
1010+
scmUri,
1011+
token,
1012+
path: 'git@bitbucket.org:screwdriver-cd/reponame.git#main:path/to/file.txt'
1013+
};
1014+
expectedOptions.url =
1015+
'https://api.bitbucket.org/2.0/repositories/screwdriver-cd/reponame/src/main/path/to/file.txt';
1016+
1017+
return scm.getFile(params).then(content => {
1018+
assert.calledWith(requestMock, expectedOptions);
1019+
assert.deepEqual(content, 'dataValue');
1020+
});
1021+
});
1022+
10071023
it('rejects if status code is not 200', () => {
10081024
fakeResponse = {
10091025
statusCode: 404,

0 commit comments

Comments
 (0)