Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: rename github/gitlab datasources #5581

Merged
merged 6 commits into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/constants/data-binary-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ export const DATASOURCE_DART = 'dart';
export const DATASOURCE_DOCKER = 'docker';
export const DATASOURCE_GIT_SUBMODULES = 'git-submodules';
export const DATASOURCE_GIT_TAGS = 'git-tags';
export const DATASOURCE_GITHUB = 'github';
export const DATASOURCE_GITLAB = 'gitlab';
export const DATASOURCE_GITHUB_RELEASES = 'github-releases';
export const DATASOURCE_GITHUB_TAGS = 'github-tags';
export const DATASOURCE_GITLAB_TAGS = 'gitlab-tags';
export const DATASOURCE_GO = 'go';
export const DATASOURCE_GRADLE_VERSION = 'gradle-version';
export const DATASOURCE_HELM = 'helm';
Expand Down
1 change: 0 additions & 1 deletion lib/datasource/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export interface Config {
export interface PkgReleaseConfig extends Config {
compatibility?: Record<string, string>;
depType?: string;
lookupType?: string;
npmrc?: string;
versioning?: string;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`datasource/github getPkgReleases returns releases 1`] = `
exports[`datasource/github-releases getPkgReleases returns releases 1`] = `
Object {
"releases": Array [
Object {
Expand All @@ -23,19 +23,3 @@ Object {
"sourceUrl": "https://github.com/some/dep",
}
`;

exports[`datasource/github getPkgReleases returns tags 1`] = `
Object {
"releases": Array [
Object {
"gitRef": "v1.0.0",
"version": "v1.0.0",
},
Object {
"gitRef": "v1.1.0",
"version": "v1.1.0",
},
],
"sourceUrl": "https://github.com/some/dep2",
}
`;
33 changes: 33 additions & 0 deletions lib/datasource/github-releases/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { api } from '../../platform/github/gh-got-wrapper';

import * as github from '.';

jest.mock('../../platform/github/gh-got-wrapper');
jest.mock('../../util/got');
jest.mock('../../util/host-rules');

const ghGot: any = api.get;

describe('datasource/github-releases', () => {
beforeEach(() => global.renovateCache.rmAll());
describe('getPkgReleases', () => {
beforeAll(() => global.renovateCache.rmAll());
it('returns releases', async () => {
const body = [
{ tag_name: 'a' },
{ tag_name: 'v' },
{ tag_name: '1.0.0' },
{ tag_name: 'v1.1.0' },
];
ghGot.mockReturnValueOnce({ headers: {}, body });
const res = await github.getPkgReleases({
lookupName: 'some/dep',
});
expect(res).toMatchSnapshot();
expect(res.releases).toHaveLength(4);
expect(
res.releases.find(release => release.version === 'v1.1.0')
).toBeDefined();
});
});
});
60 changes: 60 additions & 0 deletions lib/datasource/github-releases/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { api } from '../../platform/github/gh-got-wrapper';
import { ReleaseResult, PkgReleaseConfig } from '../common';
import { logger } from '../../logger';

const { get: ghGot } = api;

const cacheNamespace = 'datasource-github-releases';

/**
* github.getPkgReleases
*
* This function can be used to fetch releases with a customisable versioning (e.g. semver) and with releases.
*
* This function will:
* - Fetch all releases
* - Sanitize the versions if desired (e.g. strip out leading 'v')
* - Return a dependency object containing sourceUrl string and releases array
*/
export async function getPkgReleases({
lookupName: repo,
}: PkgReleaseConfig): Promise<ReleaseResult | null> {
let versions: string[];
const cachedResult = await renovateCache.get<ReleaseResult>(
cacheNamespace,
repo
);
// istanbul ignore if
if (cachedResult) {
return cachedResult;
}
try {
const url = `https://api.github.com/repos/${repo}/releases?per_page=100`;
type GitHubRelease = {
tag_name: string;
}[];

versions = (
await ghGot<GitHubRelease>(url, {
paginate: true,
})
).body.map(o => o.tag_name);
} catch (err) /* istanbul ignore next */ {
logger.debug({ repo, err }, 'Error retrieving from github');
}
// istanbul ignore if
if (!versions) {
return null;
}
const dependency: ReleaseResult = {
sourceUrl: 'https://github.com/' + repo,
releases: null,
};
dependency.releases = versions.map(version => ({
version,
gitRef: version,
}));
const cacheMinutes = 10;
await renovateCache.set(cacheNamespace, repo, dependency, cacheMinutes);
return dependency;
}
17 changes: 17 additions & 0 deletions lib/datasource/github-tags/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`datasource/github-tags getPkgReleases returns tags 1`] = `
Object {
"releases": Array [
Object {
"gitRef": "v1.0.0",
"version": "v1.0.0",
},
Object {
"gitRef": "v1.1.0",
"version": "v1.1.0",
},
],
"sourceUrl": "https://github.com/some/dep2",
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jest.mock('../../util/host-rules');
const ghGot: any = api.get;
const hostRules: any = _hostRules;

describe('datasource/github', () => {
describe('datasource/github-tags', () => {
beforeEach(() => global.renovateCache.rmAll());
describe('getDigest', () => {
beforeEach(() => {
Expand Down Expand Up @@ -61,24 +61,6 @@ describe('datasource/github', () => {
});
describe('getPkgReleases', () => {
beforeAll(() => global.renovateCache.rmAll());
it('returns releases', async () => {
const body = [
{ tag_name: 'a' },
{ tag_name: 'v' },
{ tag_name: '1.0.0' },
{ tag_name: 'v1.1.0' },
];
ghGot.mockReturnValueOnce({ headers: {}, body });
const res = await github.getPkgReleases({
lookupName: 'some/dep',
lookupType: 'releases',
});
expect(res).toMatchSnapshot();
expect(res.releases).toHaveLength(4);
expect(
res.releases.find(release => release.version === 'v1.1.0')
).toBeDefined();
});
it('returns tags', async () => {
const body = [{ name: 'v1.0.0' }, { name: 'v1.1.0' }];
ghGot.mockReturnValueOnce({ headers: {}, body });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { logger } from '../../logger';

const { get: ghGot } = api;

const cacheNamespace = 'datasource-github';
const cacheNamespace = 'datasource-github-tags';
function getCacheKey(repo: string, type: string): string {
return `${repo}:${type}`;
}
Expand Down Expand Up @@ -108,42 +108,28 @@ export async function getDigest(
*/
export async function getPkgReleases({
lookupName: repo,
lookupType,
}: PkgReleaseConfig): Promise<ReleaseResult | null> {
let versions: string[];
const cachedResult = await renovateCache.get<ReleaseResult>(
cacheNamespace,
getCacheKey(repo, lookupType || 'tags')
getCacheKey(repo, 'tags')
);
// istanbul ignore if
if (cachedResult) {
return cachedResult;
}
try {
if (lookupType === 'releases') {
const url = `https://api.github.com/repos/${repo}/releases?per_page=100`;
type GitHubRelease = {
tag_name: string;
}[];
// tag
const url = `https://api.github.com/repos/${repo}/tags?per_page=100`;
type GitHubTag = {
name: string;
}[];

versions = (
await ghGot<GitHubRelease>(url, {
paginate: true,
})
).body.map(o => o.tag_name);
} else {
// tag
const url = `https://api.github.com/repos/${repo}/tags?per_page=100`;
type GitHubTag = {
name: string;
}[];

versions = (
await ghGot<GitHubTag>(url, {
paginate: true,
})
).body.map(o => o.name);
}
versions = (
await ghGot<GitHubTag>(url, {
paginate: true,
})
).body.map(o => o.name);
} catch (err) {
logger.debug({ repo, err }, 'Error retrieving from github');
}
Expand All @@ -161,7 +147,7 @@ export async function getPkgReleases({
const cacheMinutes = 10;
await renovateCache.set(
cacheNamespace,
getCacheKey(repo, lookupType),
getCacheKey(repo, 'tags'),
dependency,
cacheMinutes
);
Expand Down
17 changes: 17 additions & 0 deletions lib/datasource/gitlab-tags/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`datasource/gitlab-tags getPkgReleases returns tags 1`] = `
Object {
"releases": Array [
Object {
"gitRef": "v1.0.0",
"version": "v1.0.0",
},
Object {
"gitRef": "v1.1.0",
"version": "v1.1.0",
},
],
"sourceUrl": "https://gitlab.company.com/api/v4/undefined",
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,13 @@ jest.mock('../../util/got');

const glGot: any = api.get;

describe('datasource/gitlab', () => {
describe('datasource/gitlab-tags', () => {
beforeEach(() => {
global.repoCache = {};
return global.renovateCache.rmAll();
});
describe('getPkgReleases', () => {
beforeAll(() => global.renovateCache.rmAll());
it('returns releases', async () => {
const body = [
{ tag_name: 'a' },
{ tag_name: 'v' },
{ tag_name: '1.0.0' },
{ tag_name: 'v1.1.0' },
];
glGot.mockReturnValueOnce({ headers: {}, body });
const res = await gitlab.getPkgReleases({
depName: 'some/dep',
lookupType: 'releases',
});
expect(res).toMatchSnapshot();
expect(res.releases).toHaveLength(4);
expect(
res.releases.find(release => release.version === 'v1.1.0')
).toBeDefined();
});
it('returns tags', async () => {
const body = [{ name: 'v1.0.0' }, { name: 'v1.1.0' }];
glGot.mockReturnValueOnce({ headers: {}, body });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,14 @@ import { PkgReleaseConfig, ReleaseResult } from '../common';
const { get: glGot } = api;

const cacheNamespace = 'datasource-gitlab';
function getCacheKey(
depHost: string,
repo: string,
lookupType: string
): string {
const type = lookupType || 'tags';
function getCacheKey(depHost: string, repo: string): string {
const type = 'tags';
return `${depHost}:${repo}:${type}`;
}

export async function getPkgReleases({
registryUrls,
lookupName: repo,
lookupType,
}: PkgReleaseConfig): Promise<ReleaseResult | null> {
// Use registryUrls if present, otherwise default to publid gitlab.com
const depHost = is.nonEmptyArray(registryUrls)
Expand All @@ -27,7 +22,7 @@ export async function getPkgReleases({
let versions: string[];
const cachedResult = await renovateCache.get<ReleaseResult>(
cacheNamespace,
getCacheKey(depHost, repo, lookupType)
getCacheKey(depHost, repo)
);
// istanbul ignore if
if (cachedResult) {
Expand All @@ -37,30 +32,17 @@ export async function getPkgReleases({
const urlEncodedRepo = encodeURIComponent(repo);

try {
if (lookupType === 'releases') {
const url = `${depHost}/api/v4/projects/${urlEncodedRepo}/releases?per_page=100`;
type GlRelease = {
tag_name: string;
}[];
// tag
const url = `${depHost}/api/v4/projects/${urlEncodedRepo}/repository/tags?per_page=100`;
type GlTag = {
name: string;
}[];

versions = (
await glGot<GlRelease>(url, {
paginate: true,
})
).body.map(o => o.tag_name);
} else {
// tag
const url = `${depHost}/api/v4/projects/${urlEncodedRepo}/repository/tags?per_page=100`;
type GlTag = {
name: string;
}[];

versions = (
await glGot<GlTag>(url, {
paginate: true,
})
).body.map(o => o.name);
}
versions = (
await glGot<GlTag>(url, {
paginate: true,
})
).body.map(o => o.name);
} catch (err) {
// istanbul ignore next
logger.debug({ repo, err }, 'Error retrieving from Gitlab');
Expand All @@ -83,7 +65,7 @@ export async function getPkgReleases({
const cacheMinutes = 10;
await renovateCache.set(
cacheNamespace,
getCacheKey(depHost, repo, lookupType),
getCacheKey(depHost, repo),
dependency,
cacheMinutes
);
Expand Down