mirror of
https://github.com/renovatebot/renovate.git
synced 2025-01-12 06:56:24 +00:00
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:
parent
738408b728
commit
960648abbe
2 changed files with 32 additions and 2 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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'),
|
||||
|
|
Loading…
Reference in a new issue