Skip to content

Commit

Permalink
fix(go): Improve go-import content parsing (#9022)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Mar 8, 2021
1 parent dda7514 commit ccdb09f
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 3 deletions.
50 changes: 50 additions & 0 deletions lib/datasource/go/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,56 @@ Array [
]
`;

exports[`datasource/go getReleases handles fyne.io 1`] = `
Object {
"releases": Array [
Object {
"gitRef": "v1.0.0",
"version": "v1.0.0",
},
Object {
"gitRef": "v2.0.0",
"version": "v2.0.0",
},
],
"sourceUrl": "https://github.com/fyne-io/fyne",
}
`;

exports[`datasource/go getReleases handles fyne.io 2`] = `
Array [
Object {
"headers": Object {
"accept-encoding": "gzip, deflate",
"host": "fyne.io",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://fyne.io/fyne?go-get=1",
},
Object {
"headers": Object {
"accept": "application/vnd.github.v3+json",
"accept-encoding": "gzip, deflate",
"host": "api.github.com",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://api.github.com/repos/fyne-io/fyne/tags?per_page=100",
},
Object {
"headers": Object {
"accept": "application/vnd.github.v3+json",
"accept-encoding": "gzip, deflate",
"host": "api.github.com",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://api.github.com/repos/fyne-io/fyne/releases?per_page=100",
},
]
`;

exports[`datasource/go getReleases processes real data 1`] = `
Object {
"releases": Array [
Expand Down
23 changes: 23 additions & 0 deletions lib/datasource/go/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,5 +445,28 @@ describe('datasource/go', () => {
expect(httpCalls).toMatchSnapshot();
httpMock.reset();
});
it('handles fyne.io', async () => {
httpMock
.scope('https://fyne.io/')
.get('/fyne?go-get=1')
.reply(
200,
'<meta name="go-import" content="fyne.io/fyne git https://github.com/fyne-io/fyne">'
);
httpMock
.scope('https://api.github.com/')
.get('/repos/fyne-io/fyne/tags?per_page=100')
.reply(200, [{ name: 'v1.0.0' }, { name: 'v2.0.0' }])
.get('/repos/fyne-io/fyne/releases?per_page=100')
.reply(200, []);
const res = await getPkgReleases({
datasource,
depName: 'fyne.io/fyne',
});
expect(res).toMatchSnapshot();
expect(res).not.toBeNull();
expect(res).toBeDefined();
expect(httpMock.getTrace()).toMatchSnapshot();
});
});
});
8 changes: 6 additions & 2 deletions lib/datasource/go/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { logger } from '../../logger';
import * as hostRules from '../../util/host-rules';
import { Http } from '../../util/http';
import { regEx } from '../../util/regex';
import { trimTrailingSlash } from '../../util/url';
import * as bitbucket from '../bitbucket-tags';
import * as github from '../github-tags';
import * as gitlab from '../gitlab-tags';
Expand Down Expand Up @@ -115,8 +116,11 @@ async function getDatasource(goModule: string): Promise<DataSource | null> {
const parsedUrl = URL.parse(goImportURL);

// split the go module from the URL: host/go/module -> go/module
const split = goModule.split('/');
const lookupName = split[1] + '/' + split[2];
const lookupName = trimTrailingSlash(parsedUrl.pathname)
.replace(/\.git$/, '')
.split('/')
.slice(-2)
.join('/');

return {
datasource: github.id,
Expand Down
8 changes: 7 additions & 1 deletion lib/util/url.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolveBaseUrl, validateUrl } from './url';
import { resolveBaseUrl, trimTrailingSlash, validateUrl } from './url';

describe('util/url', () => {
test.each([
Expand Down Expand Up @@ -53,4 +53,10 @@ describe('util/url', () => {
expect(validateUrl('http://github.com')).toBe(true);
expect(validateUrl('https://github.com')).toBe(true);
});
it('trimTrailingSlash', () => {
expect(trimTrailingSlash('foo')).toBe('foo');
expect(trimTrailingSlash('/foo/bar')).toBe('/foo/bar');
expect(trimTrailingSlash('foo/')).toBe('foo');
expect(trimTrailingSlash('foo//////')).toBe('foo');
});
});
4 changes: 4 additions & 0 deletions lib/util/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ export function ensureTrailingSlash(url: string): string {
return url.replace(/\/?$/, '/');
}

export function trimTrailingSlash(url: string): string {
return url.replace(/\/+$/, '');
}

export function resolveBaseUrl(baseUrl: string, input: string | URL): string {
const inputString = input.toString();

Expand Down

0 comments on commit ccdb09f

Please sign in to comment.