Skip to content

Commit

Permalink
fix(2185): Use default_branch (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkyi authored Aug 19, 2020
1 parent b30b47b commit 135c49e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function getRepoInfoByCheckoutUrl(checkoutUrl) {
return {
hostname: matched[MATCH_COMPONENT_HOSTNAME],
reponame: matched[MATCH_COMPONENT_REPONAME],
branch: matched[MATCH_COMPONENT_BRANCH].slice(1),
branch: matched[MATCH_COMPONENT_BRANCH] ? matched[MATCH_COMPONENT_BRANCH].slice(1) : null,
owner: matched[MATCH_COMPONENT_OWNER]
};
}
Expand Down Expand Up @@ -300,7 +300,9 @@ class GitlabScm extends Scm {
}).then((response) => {
checkResponseError(response, '_parseUrl');

return `${repoInfo.hostname}:${response.body.id}:${repoInfo.branch}`;
const branch = repoInfo.branch || response.body.default_branch;

return `${repoInfo.hostname}:${response.body.id}:${branch}`;
});
}

Expand Down Expand Up @@ -429,7 +431,7 @@ class GitlabScm extends Scm {
/**
* Decorate a given SCM URI with additional data to better display
* related information. If a branch suffix is not provided, it will default
* to the master branch
* to the default_branch
* @method _decorateUrl
* @param {Config} config Configuration object
* @param {String} config.scmUri The SCM URI the commit belongs to
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"chai": "^3.5.0",
"eslint": "^4.6.0",
"eslint-config-screwdriver": "^3.0.0",
"jenkins-mocha": "^6.0.0",
"jenkins-mocha": "^8.0.0",
"mockery": "^2.0.0",
"sinon": "^1.17.7",
"sinon-as-promised": "^4.0.2"
Expand Down
26 changes: 25 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ describe('index', function () {
fakeResponse = {
statusCode: 200,
body: {
id: '12345'
id: '12345',
default_branch: 'main'
}
};
expectedOptions = {
Expand Down Expand Up @@ -137,6 +138,29 @@ describe('index', function () {
});
});

it('resolves to the correct parsed url for ssh with default branch', () => {
const expected =
'gitlab.com:12345:main';

expectedOptions = {
url: apiUrl,
method: 'GET',
json: true,
auth: {
bearer: token
}
};

return scm.parseUrl({
checkoutUrl: '[email protected]:batman/test.git',
token,
scmContext
}).then((parsed) => {
assert.calledWith(requestMock, expectedOptions);
assert.equal(parsed, expected);
});
});

it('resolves to the correct parsed url for https', () => {
const expected =
'gitlab.com:12345:mynewbranch';
Expand Down

0 comments on commit 135c49e

Please sign in to comment.