Skip to content

Commit

Permalink
refactor(github): Convert datasources to class form (#14124)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
Co-authored-by: Jamie Magee <jamie.magee@gmail.com>
  • Loading branch information
3 people committed Feb 13, 2022
1 parent 91f36b7 commit 8a7abfd
Show file tree
Hide file tree
Showing 57 changed files with 594 additions and 650 deletions.
12 changes: 8 additions & 4 deletions lib/constants/platform.spec.ts
@@ -1,6 +1,6 @@
import { BitBucketTagsDatasource } from '../datasource/bitbucket-tags';
import { id as GH_RELEASES_DS } from '../datasource/github-releases';
import { id as GH_TAGS_DS } from '../datasource/github-tags';
import { GithubReleasesDatasource } from '../datasource/github-releases';
import { GithubTagsDatasource } from '../datasource/github-tags';
import { GitlabPackagesDatasource } from '../datasource/gitlab-packages';
import { GitlabReleasesDatasource } from '../datasource/gitlab-releases';
import { GitlabTagsDatasource } from '../datasource/gitlab-tags';
Expand Down Expand Up @@ -36,8 +36,12 @@ describe('constants/platform', () => {
});

it('should be part of the GITHUB_API_USING_HOST_TYPES ', () => {
expect(GITHUB_API_USING_HOST_TYPES.includes(GH_TAGS_DS)).toBeTrue();
expect(GITHUB_API_USING_HOST_TYPES.includes(GH_RELEASES_DS)).toBeTrue();
expect(
GITHUB_API_USING_HOST_TYPES.includes(GithubTagsDatasource.id)
).toBeTrue();
expect(
GITHUB_API_USING_HOST_TYPES.includes(GithubReleasesDatasource.id)
).toBeTrue();
expect(GITHUB_API_USING_HOST_TYPES.includes(PodDatasource.id)).toBeTrue();
expect(
GITHUB_API_USING_HOST_TYPES.includes(GITHUB_CHANGELOG_ID)
Expand Down
8 changes: 4 additions & 4 deletions lib/datasource/api.ts
Expand Up @@ -12,8 +12,8 @@ import { GalaxyDatasource } from './galaxy';
import { GalaxyCollectionDatasource } from './galaxy-collection';
import { GitRefsDatasource } from './git-refs';
import { GitTagsDatasource } from './git-tags';
import * as githubReleases from './github-releases';
import * as githubTags from './github-tags';
import { GithubReleasesDatasource } from './github-releases';
import { GithubTagsDatasource } from './github-tags';
import { GitlabPackagesDatasource } from './gitlab-packages';
import { GitlabReleasesDatasource } from './gitlab-releases';
import { GitlabTagsDatasource } from './gitlab-tags';
Expand Down Expand Up @@ -57,8 +57,8 @@ api.set(GalaxyDatasource.id, new GalaxyDatasource());
api.set(GalaxyCollectionDatasource.id, new GalaxyCollectionDatasource());
api.set(GitRefsDatasource.id, new GitRefsDatasource());
api.set(GitTagsDatasource.id, new GitTagsDatasource());
api.set('github-releases', githubReleases);
api.set('github-tags', githubTags);
api.set(GithubReleasesDatasource.id, new GithubReleasesDatasource());
api.set(GithubTagsDatasource.id, new GithubTagsDatasource());
api.set(GitlabPackagesDatasource.id, new GitlabPackagesDatasource());
api.set(GitlabReleasesDatasource.id, new GitlabReleasesDatasource());
api.set(GitlabTagsDatasource.id, new GitlabTagsDatasource());
Expand Down
17 changes: 1 addition & 16 deletions lib/datasource/github-releases/common.spec.ts
@@ -1,5 +1,4 @@
import { GitHubReleaseMocker } from './test';
import { getApiBaseUrl, getGithubRelease, getSourceUrlBase } from '.';
import { getApiBaseUrl, getSourceUrlBase } from './common';

describe('datasource/github-releases/common', () => {
describe('getSourceUrlBase', () => {
Expand All @@ -24,18 +23,4 @@ describe('datasource/github-releases/common', () => {
expect(apiUrl).toBe('https://gh.my-company.com/api/v3/');
});
});

describe('getGithubRelease', () => {
const apiUrl = 'https://github.com/';
const lookupName = 'someDep';
const releaseMock = new GitHubReleaseMocker(apiUrl, lookupName);

it('returns release', async () => {
const version = 'v1.0.0';
releaseMock.release(version);

const release = await getGithubRelease(apiUrl, lookupName, version);
expect(release.tag_name).toBe(version);
});
});
});
20 changes: 20 additions & 0 deletions lib/datasource/github-releases/common.ts
@@ -0,0 +1,20 @@
import { ensureTrailingSlash } from '../../util/url';

const defaultSourceUrlBase = 'https://github.com/';

export function getSourceUrlBase(registryUrl: string): string {
// default to GitHub.com if no GHE host is specified.
return ensureTrailingSlash(registryUrl ?? defaultSourceUrlBase);
}

export function getApiBaseUrl(registryUrl: string): string {
const sourceUrlBase = getSourceUrlBase(registryUrl);
return sourceUrlBase === defaultSourceUrlBase
? `https://api.github.com/`
: `${sourceUrlBase}api/v3/`;
}

export function getSourceUrl(lookupName: string, registryUrl?: string): string {
const sourceUrlBase = getSourceUrlBase(registryUrl);
return `${sourceUrlBase}${lookupName}`;
}
51 changes: 40 additions & 11 deletions lib/datasource/github-releases/digest.spec.ts
Expand Up @@ -2,14 +2,16 @@ import hasha from 'hasha';
import * as httpMock from '../../../test/http-mock';
import { GitHubReleaseMocker } from './test';
import type { DigestAsset } from './types';
import { findDigestAsset, mapDigestAssetToRelease } from '.';

import { GithubReleasesDatasource } from '.';

describe('datasource/github-releases/digest', () => {
const lookupName = 'some/dep';
const releaseMock = new GitHubReleaseMocker(
'https://api.github.com',
lookupName
);
const githubReleases = new GithubReleasesDatasource();

describe('findDigestAsset', () => {
it('finds SHASUMS.txt file containing digest', async () => {
Expand All @@ -19,7 +21,10 @@ describe('datasource/github-releases/digest', () => {
'another-digest linux-arm64.tar.gz'
);

const digestAsset = await findDigestAsset(release, 'test-digest');
const digestAsset = await githubReleases.findDigestAsset(
release,
'test-digest'
);
expect(digestAsset.assetName).toBe('SHASUMS.txt');
expect(digestAsset.digestedFileName).toBe('linux-amd64.tar.gz');
});
Expand All @@ -35,7 +40,10 @@ describe('datasource/github-releases/digest', () => {
.get(`/repos/${lookupName}/releases/download/v1.0.0/SHASUMS.txt`)
.reply(200, '');

const digestAsset = await findDigestAsset(release, 'test-digest');
const digestAsset = await githubReleases.findDigestAsset(
release,
'test-digest'
);
expect(digestAsset).toBeNull();
});

Expand All @@ -49,14 +57,20 @@ describe('datasource/github-releases/digest', () => {
});
const contentDigest = await hasha.async(content, { algorithm: 'sha256' });

const digestAsset = await findDigestAsset(release, contentDigest);
const digestAsset = await githubReleases.findDigestAsset(
release,
contentDigest
);
expect(digestAsset.assetName).toBe('asset.zip');
expect(digestAsset.digestedFileName).toBeUndefined();
});

it('returns null when no assets available', async () => {
const release = releaseMock.release('v1.0.0');
const digestAsset = await findDigestAsset(release, 'test-digest');
const digestAsset = await githubReleases.findDigestAsset(
release,
'test-digest'
);
expect(digestAsset).toBeNull();
});
});
Expand All @@ -75,7 +89,10 @@ describe('datasource/github-releases/digest', () => {
'v1.0.1',
'updated-digest asset.zip'
);
const digest = await mapDigestAssetToRelease(digestAsset, release);
const digest = await githubReleases.mapDigestAssetToRelease(
digestAsset,
release
);
expect(digest).toBe('updated-digest');
});

Expand All @@ -89,7 +106,7 @@ describe('datasource/github-releases/digest', () => {
'v1.0.1',
'updated-digest asset-1.0.1.zip'
);
const digest = await mapDigestAssetToRelease(
const digest = await githubReleases.mapDigestAssetToRelease(
digestAssetWithVersion,
release
);
Expand All @@ -101,13 +118,19 @@ describe('datasource/github-releases/digest', () => {
'v1.0.1',
'moot-digest asset.tar.gz'
);
const digest = await mapDigestAssetToRelease(digestAsset, release);
const digest = await githubReleases.mapDigestAssetToRelease(
digestAsset,
release
);
expect(digest).toBeNull();
});

it('returns null when digest file not found', async () => {
const release = releaseMock.release('v1.0.1');
const digest = await mapDigestAssetToRelease(digestAsset, release);
const digest = await githubReleases.mapDigestAssetToRelease(
digestAsset,
release
);
expect(digest).toBeNull();
});
});
Expand All @@ -128,13 +151,19 @@ describe('datasource/github-releases/digest', () => {
algorithm: 'sha256',
});

const digest = await mapDigestAssetToRelease(digestAsset, release);
const digest = await githubReleases.mapDigestAssetToRelease(
digestAsset,
release
);
expect(digest).toEqual(contentDigest);
});

it('returns null when not found', async () => {
const release = releaseMock.release('v1.0.1');
const digest = await mapDigestAssetToRelease(digestAsset, release);
const digest = await githubReleases.mapDigestAssetToRelease(
digestAsset,
release
);
expect(digest).toBeNull();
});
});
Expand Down
20 changes: 12 additions & 8 deletions lib/datasource/github-releases/index.spec.ts
Expand Up @@ -2,8 +2,7 @@ import { getDigest, getPkgReleases } from '..';
import * as httpMock from '../../../test/http-mock';
import * as _hostRules from '../../util/host-rules';
import { GitHubReleaseMocker } from './test';
import { id as datasource } from '.';
import * as github from '.';
import { GithubReleasesDatasource } from '.';

jest.mock('../../util/host-rules');
const hostRules: any = _hostRules;
Expand All @@ -25,6 +24,8 @@ const responseBody = [
];

describe('datasource/github-releases/index', () => {
const githubReleases = new GithubReleasesDatasource();

beforeEach(() => {
hostRules.hosts.mockReturnValue([]);
hostRules.find.mockReturnValue({
Expand All @@ -40,7 +41,7 @@ describe('datasource/github-releases/index', () => {
.reply(200, responseBody);

const res = await getPkgReleases({
datasource,
datasource: GithubReleasesDatasource.id,
depName: 'some/dep',
});
expect(res).toMatchSnapshot();
Expand All @@ -62,7 +63,7 @@ describe('datasource/github-releases/index', () => {
.scope(githubEnterpriseApiHost)
.get(`/api/v3/repos/${lookupName}/releases?per_page=100`)
.reply(200, responseBody);
const res = await github.getReleases({
const res = await githubReleases.getReleases({
registryUrl: 'https://git.enterprise.com',
lookupName,
});
Expand All @@ -80,14 +81,17 @@ describe('datasource/github-releases/index', () => {
const releaseMock = new GitHubReleaseMocker(githubApiHost, depName);

it('requires currentDigest', async () => {
const digest = await getDigest({ datasource, depName }, currentValue);
const digest = await getDigest(
{ datasource: GithubReleasesDatasource.id, depName },
currentValue
);
expect(digest).toBeNull();
});

it('defaults to currentDigest when currentVersion is missing', async () => {
const digest = await getDigest(
{
datasource,
datasource: GithubReleasesDatasource.id,
depName,
currentDigest,
},
Expand All @@ -106,7 +110,7 @@ describe('datasource/github-releases/index', () => {
releaseMock.withDigestFileAsset(nextValue, `${nextDigest} asset.zip`);
const digest = await getDigest(
{
datasource,
datasource: GithubReleasesDatasource.id,
depName,
currentValue,
currentDigest,
Expand All @@ -122,7 +126,7 @@ describe('datasource/github-releases/index', () => {
releaseMock.release(currentValue);
const digest = await getDigest(
{
datasource,
datasource: GithubReleasesDatasource.id,
depName,
currentValue,
currentDigest,
Expand Down

0 comments on commit 8a7abfd

Please sign in to comment.