From ccdb09fe3caad37c9cec67d5d88af30c77710d31 Mon Sep 17 00:00:00 2001 From: Sergei Zharinov <zharinov@users.noreply.github.com> Date: Mon, 8 Mar 2021 17:12:19 +0400 Subject: [PATCH] fix(go): Improve go-import content parsing (#9022) --- .../go/__snapshots__/index.spec.ts.snap | 50 +++++++++++++++++++ lib/datasource/go/index.spec.ts | 23 +++++++++ lib/datasource/go/index.ts | 8 ++- lib/util/url.spec.ts | 8 ++- lib/util/url.ts | 4 ++ 5 files changed, 90 insertions(+), 3 deletions(-) diff --git a/lib/datasource/go/__snapshots__/index.spec.ts.snap b/lib/datasource/go/__snapshots__/index.spec.ts.snap index 625877a6e8..8ef004e2c8 100644 --- a/lib/datasource/go/__snapshots__/index.spec.ts.snap +++ b/lib/datasource/go/__snapshots__/index.spec.ts.snap @@ -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 [ diff --git a/lib/datasource/go/index.spec.ts b/lib/datasource/go/index.spec.ts index ea5993f520..442f20e38a 100644 --- a/lib/datasource/go/index.spec.ts +++ b/lib/datasource/go/index.spec.ts @@ -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(); + }); }); }); diff --git a/lib/datasource/go/index.ts b/lib/datasource/go/index.ts index ba341aea2b..37e931c6f0 100644 --- a/lib/datasource/go/index.ts +++ b/lib/datasource/go/index.ts @@ -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'; @@ -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, diff --git a/lib/util/url.spec.ts b/lib/util/url.spec.ts index 58461fff12..6e9665ceb3 100644 --- a/lib/util/url.spec.ts +++ b/lib/util/url.spec.ts @@ -1,4 +1,4 @@ -import { resolveBaseUrl, validateUrl } from './url'; +import { resolveBaseUrl, trimTrailingSlash, validateUrl } from './url'; describe('util/url', () => { test.each([ @@ -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'); + }); }); diff --git a/lib/util/url.ts b/lib/util/url.ts index f18a9d46f0..6e61b1eeee 100644 --- a/lib/util/url.ts +++ b/lib/util/url.ts @@ -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(); -- GitLab