feat: Detect GitLab default branch for presets (#3015)

Usually master will be used, but just in case - let's find which branch is default first, before retrieving the file.

Closes #2991
This commit is contained in:
rtaum 2019-01-04 06:48:12 +01:00 committed by Rhys Arkins
parent 738408b728
commit 960648abbe
2 changed files with 32 additions and 2 deletions

View file

@ -5,6 +5,8 @@ module.exports = {
getPkgReleases,
};
const GitLabApiUrl = 'https://gitlab.com/api/v4/projects';
async function getPreset(pkgName, presetName = 'default') {
if (presetName !== 'default') {
throw new Error(
@ -15,8 +17,13 @@ async function getPreset(pkgName, presetName = 'default') {
let res;
try {
const urlEncodedPkgName = encodeURIComponent(pkgName);
const url = `https://gitlab.com/api/v4/projects/${urlEncodedPkgName}/repository/files/renovate.json?ref=master`;
res = Buffer.from((await glGot(url)).body.content, 'base64').toString();
const defautlBranchName = await getDefaultBranchName(urlEncodedPkgName);
const presetUrl = `${GitLabApiUrl}/${urlEncodedPkgName}/repository/files/renovate.json?ref=${defautlBranchName}`;
res = Buffer.from(
(await glGot(presetUrl)).body.content,
'base64'
).toString();
} catch (err) {
logger.debug('Failed to retrieve renovate.json from repo');
throw new Error('dep not found');
@ -32,3 +39,18 @@ async function getPreset(pkgName, presetName = 'default') {
/* eslint-disable no-unused-vars */
function getPkgReleases(purl) {}
/* eslint-enable no-unused-vars */
async function getDefaultBranchName(urlEncodedPkgName) {
const branchesUrl = `${GitLabApiUrl}/${urlEncodedPkgName}/repository/branches`;
const res = await glGot(branchesUrl);
const branches = res.body;
let defautlBranchName = 'master';
for (const branch of branches) {
if (branch.default) {
defautlBranchName = branch.name;
break;
}
}
return defautlBranchName;
}

View file

@ -31,6 +31,14 @@ describe('datasource/gitlab', () => {
await expect(gitlab.getPreset('some/repo')).rejects.toThrow();
});
it('should return the preset', async () => {
glGot.mockImplementationOnce(() => ({
body: [
{
name: 'master',
default: true,
},
],
}));
glGot.mockImplementationOnce(() => ({
body: {
content: Buffer.from('{"foo":"bar"}').toString('base64'),