Skip to content

Commit

Permalink
fix(gomod): GOPROXY with slash doesnt work (#32255)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipAbed authored Nov 4, 2024
1 parent 7729740 commit 80dd936
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
37 changes: 37 additions & 0 deletions lib/modules/datasource/go/releases-goproxy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,43 @@ describe('modules/datasource/go/releases-goproxy', () => {
});
});

it('handles baseURL with slash at the end', async () => {
process.env.GOPROXY = `${baseUrl}/`;

httpMock
.scope(`${baseUrl}/gopkg.in/foo`)
.get('.v0/@v/list')
.reply(200, ['v0.1.0', 'v0.2.0', ' \n'].join('\n'))
.get('.v0/@v/v0.1.0.info')
.reply(200, { Version: 'v0.1.0', Time: '2017-01-01T00:00:00Z' })
.get('.v0/@v/v0.2.0.info')
.reply(200, { Version: 'v0.2.0', Time: '2017-02-01T00:00:00Z' })
.get('.v0/@latest')
.reply(200, { Version: 'v0.2.0' })
.get('.v1/@v/list')
.reply(200, ['v1.0.0', '\n'].join('\n'))
.get('.v1/@v/v1.0.0.info')
.reply(200, { Version: 'v1.0.0', Time: '2018-01-01T00:00:00Z' })
.get('.v1/@latest')
.reply(200, { Version: 'v1.0.0' })
.get('.v2/@v/list')
.reply(404);

const res = await datasource.getReleases({
packageName: 'gopkg.in/foo.v0',
});

expect(res).toEqual({
releases: [
{ releaseTimestamp: '2017-01-01T00:00:00Z', version: 'v0.1.0' },
{ releaseTimestamp: '2017-02-01T00:00:00Z', version: 'v0.2.0' },
{ releaseTimestamp: '2018-01-01T00:00:00Z', version: 'v1.0.0' },
],
sourceUrl: 'https://github.com/go-foo/foo',
tags: { latest: 'v1.0.0' },
});
});

it('continues if package returns no releases', async () => {
process.env.GOPROXY = baseUrl;

Expand Down
21 changes: 18 additions & 3 deletions lib/modules/datasource/go/releases-goproxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { filterMap } from '../../../util/filter-map';
import { HttpError } from '../../../util/http';
import * as p from '../../../util/promises';
import { newlineRegex, regEx } from '../../../util/regex';
import { joinUrlParts } from '../../../util/url';
import goVersioning from '../../versioning/go-mod-directive';
import { Datasource } from '../datasource';
import type { GetReleasesConfig, Release, ReleaseResult } from '../types';
Expand Down Expand Up @@ -129,7 +130,12 @@ export class GoProxyDatasource extends Datasource {
}

async listVersions(baseUrl: string, packageName: string): Promise<Release[]> {
const url = `${baseUrl}/${this.encodeCase(packageName)}/@v/list`;
const url = joinUrlParts(
baseUrl,
this.encodeCase(packageName),
'@v',
'list',
);
const { body } = await this.http.get(url);
return filterMap(body.split(newlineRegex), (str) => {
if (!is.nonEmptyStringAndNotWhitespace(str)) {
Expand All @@ -152,7 +158,12 @@ export class GoProxyDatasource extends Datasource {
packageName: string,
version: string,
): Promise<Release> {
const url = `${baseUrl}/${this.encodeCase(packageName)}/@v/${version}.info`;
const url = joinUrlParts(
baseUrl,
this.encodeCase(packageName),
'@v',
`${version}.info`,
);
const res = await this.http.getJson<VersionInfo>(url);

const result: Release = {
Expand All @@ -171,7 +182,11 @@ export class GoProxyDatasource extends Datasource {
packageName: string,
): Promise<string | null> {
try {
const url = `${baseUrl}/${this.encodeCase(packageName)}/@latest`;
const url = joinUrlParts(
baseUrl,
this.encodeCase(packageName),
'@latest',
);
const res = await this.http.getJson<VersionInfo>(url);
return res.body.Version;
} catch (err) {
Expand Down

0 comments on commit 80dd936

Please sign in to comment.