Skip to content

Commit

Permalink
fix: Use default branch as default instead of hardcoding (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkyi authored Aug 19, 2020
1 parent c4d8694 commit a24090d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
27 changes: 15 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ function getInfo(scmUrl) {
throw new Error(`Invalid scmUrl: ${scmUrl}`);
}

const branch = matched[MATCH_COMPONENT_BRANCH_NAME] || '#master';
const branch = matched[MATCH_COMPONENT_BRANCH_NAME];

return {
owner: matched[MATCH_COMPONENT_USER_NAME],
repo: matched[MATCH_COMPONENT_REPO_NAME],
host: matched[MATCH_COMPONENT_HOST_NAME],
branch: branch.slice(1)
branch: branch ? branch.slice(1) : undefined
};
}

Expand Down Expand Up @@ -169,11 +169,13 @@ class GithubScm extends Scm {
const [scmHost, scmId, scmBranch, rootDir] = scmUri.split(':');

let repoFullName;
let defaultBranch;

if (scmRepo) {
repoFullName = scmRepo.name;
} else {
try {
// https://github.com/octokit/rest.js/issues/163
const repo = await this.breaker.runCommand({
scopeType: 'request',
route: 'GET /repositories/:id',
Expand All @@ -182,6 +184,7 @@ class GithubScm extends Scm {
});

repoFullName = repo.data.full_name;
defaultBranch = repo.data.default_branch;
} catch (err) {
logger.error('Failed to lookupScmUri: ', err);
throw err;
Expand All @@ -191,7 +194,7 @@ class GithubScm extends Scm {
const [repoOwner, repoName] = repoFullName.split('/');

return {
branch: scmBranch,
branch: scmBranch || defaultBranch,
host: scmHost,
repo: repoName,
owner: repoOwner,
Expand Down Expand Up @@ -1033,22 +1036,22 @@ class GithubScm extends Scm {
}

/**
* Get id of a specific repo
* @async _getRepoId
* Get repo id and default branch of specific repo
* @async _getRepoInfo
* @param {Object} scmInfo The result of getScmInfo
* @param {String} token The token used to authenticate to the SCM
* @param {String} checkoutUrl The checkoutUrl to parse
* @return {Promise} Resolves to the id of the repo
* @return {Promise} Resolves an object with repo id and default branch
*/
async _getRepoId(scmInfo, token, checkoutUrl) {
async _getRepoInfo(scmInfo, token, checkoutUrl) {
try {
const repo = await this.breaker.runCommand({
action: 'get',
token,
params: scmInfo
});

return repo.data.id;
return { repoId: repo.data.id, defaultBranch: repo.data.default_branch };
} catch (err) {
if (err.status === 404) {
throw new Error(`Cannot find repository ${checkoutUrl}`);
Expand Down Expand Up @@ -1159,7 +1162,7 @@ class GithubScm 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
* @async _decorateUrl
* @param {Config} config
* @param {String} config.scmUri The SCM URI the commit belongs to
Expand Down Expand Up @@ -1373,7 +1376,7 @@ class GithubScm extends Scm {

return {
action: 'tag',
branch: hoek.reach(webhookPayload, 'master_branch'),
branch: hoek.reach(webhookPayload, 'repository.default_branch'),
checkoutUrl,
type: 'repo',
username: hoek.reach(webhookPayload, 'sender.login'),
Expand Down Expand Up @@ -1413,8 +1416,8 @@ class GithubScm extends Scm {
throw new Error(message);
}

const repoId = await this._getRepoId(scmInfo, token, checkoutUrl);
const scmUri = `${scmInfo.host}:${repoId}:${scmInfo.branch}`;
const { repoId, defaultBranch } = await this._getRepoInfo(scmInfo, token, checkoutUrl);
const scmUri = `${scmInfo.host}:${repoId}:${scmInfo.branch || defaultBranch}`;

return rootDir ? `${scmUri}:${rootDir}` : scmUri;
}
Expand Down
36 changes: 17 additions & 19 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1483,17 +1483,19 @@ jobs:
let checkoutUrl;
const repoData = {
id: 8675309,
full_name: 'iAm/theCaptain'
full_name: 'iAm/theCaptain',
default_branch: 'main'
};
const token = 'mygithubapitoken';
const repoInfo = {
host: 'github.com',
repo: 'theCaptain',
owner: 'iAm'
};
let repoInfo;

beforeEach(() => {
checkoutUrl = 'git@github.com:iAm/theCaptain.git#boat';
repoInfo = {
host: 'github.com',
repo: 'theCaptain',
owner: 'iAm'
};
});

it('parses a complete ssh url', () => {
Expand Down Expand Up @@ -1528,21 +1530,17 @@ jobs:
});
});

it('parses a ssh url, defaulting the branch to master', () => {
it('parses a ssh url, defaulting the branch to default branch', () => {
checkoutUrl = 'git@github.com:iAm/theCaptain.git';

repoInfo.branch = undefined;
githubMock.repos.get.resolves({ data: repoData });

return scm.parseUrl({
checkoutUrl,
token
}).then((result) => {
assert.strictEqual(result, 'github.com:8675309:master');

assert.strictEqual(result, 'github.com:8675309:main');
assert.calledWith(githubMock.repos.get, sinon.match(repoInfo));
assert.calledWith(githubMock.repos.get, sinon.match({
branch: 'master'
}));
});
});

Expand Down Expand Up @@ -1917,13 +1915,12 @@ jobs:
});

describe('generateDeployKey', () => {
it('returns a public and private key pair object', () => {
it('returns a public and private key pair object', () =>
scm.generateDeployKey().then((keys) => {
assert.isObject(keys);
assert.property(keys, 'pubKey');
assert.property(keys, 'key');
});
});
}));
});

describe('addDeployKey', () => {
Expand All @@ -1946,10 +1943,11 @@ jobs:
it('returns a private key', async () => {
generateDeployKeyStub.returns(Promise.resolve({ pubKey, key: privKey }));
githubMock.request.resolves({ data: pubKey });
const privateKey = await scm.addDeployKey(addDepKeyConfig);

assert.isString(privateKey);
assert.deepEqual(privateKey, privKey);
return scm.addDeployKey(addDepKeyConfig).then((privateKey) => {
assert.isString(privateKey);
assert.deepEqual(privateKey, privKey);
});
});
});

Expand Down

0 comments on commit a24090d

Please sign in to comment.