Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fbbee91

Browse files
author
Dmitry Shibanov
committedJun 23, 2020
update setup go task to use releases
1 parent 0f551ac commit fbbee91

File tree

10 files changed

+11798
-14584
lines changed

10 files changed

+11798
-14584
lines changed
 

‎.github/workflows/versions.yml

+62-39
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,74 @@
1-
name: go-versions
1+
name: Validate 'setup-go'
22
on:
3-
push:
4-
branches:
5-
- master
6-
paths-ignore:
7-
- '**.md'
8-
pull_request:
9-
paths-ignore:
10-
- '**.md'
3+
push:
4+
branches:
5+
- master
6+
paths-ignore:
7+
- '**.md'
8+
pull_request:
9+
paths-ignore:
10+
- '**.md'
11+
schedule:
12+
- cron: 0 0 * * *
13+
1114
jobs:
12-
run:
13-
name: Go
14-
runs-on: ${{ matrix.operating-system }}
15+
local-cache:
16+
name: Setup local-cache version
17+
runs-on: ${{ matrix.os }}
1518
strategy:
19+
fail-fast: false
1620
matrix:
17-
operating-system: [ubuntu-latest, windows-latest, macos-latest]
21+
os: [macos-latest, windows-latest, ubuntu-latest]
22+
go: [1.12, 1.13, 1.14]
1823
steps:
19-
- name: Checkout
20-
uses: actions/checkout@v2
24+
- name: Checkout
25+
uses: actions/checkout@v2
2126

22-
- name: setup-go ^1.13.6
23-
uses: ./
24-
with:
25-
go-version: ^1.13.6
27+
- name: setup-go ${{ matrix.go }}
28+
uses: ./
29+
with:
30+
go-version: ${{ matrix.go }}
2631

27-
- name: validate version
28-
run: go version | grep "go1."
32+
- name: verify go
33+
run: __tests__/verify-go.sh ${{ matrix.go }}
34+
shell: bash
2935

30-
- name: setup-go 1.13
31-
uses: ./
32-
with:
33-
go-version: 1.13
34-
35-
- name: validate version
36-
run: go version | grep "go1.13."
36+
setup-versions-from-manifest:
37+
name: Setup ${{ matrix.go }} ${{ matrix.os }}
38+
runs-on: ${{ matrix.os }}
39+
strategy:
40+
fail-fast: false
41+
matrix:
42+
os: [macos-latest, windows-latest, ubuntu-latest]
43+
go: [1.12.16, 1.13.11, 1.14.3]
44+
steps:
45+
- name: Checkout
46+
uses: actions/checkout@v2
47+
48+
- name: setup-go ${{ matrix.go }}
49+
uses: ./
50+
with:
51+
go-version: ${{ matrix.go }}
52+
53+
- name: verify go
54+
run: __tests__/verify-go.sh ${{ matrix.go }}
55+
shell: bash
3756

38-
- name: setup-go 1.12.9
57+
setup-versions-from-dist:
58+
name: Setup ${{ matrix.go }} ${{ matrix.os }}
59+
runs-on: ${{ matrix.os }}
60+
strategy:
61+
fail-fast: false
62+
matrix:
63+
os: [macos-latest, windows-latest, ubuntu-latest]
64+
go: [1.7, 1.8.6]
65+
steps:
66+
- name: Checkout
67+
uses: actions/checkout@v2
68+
- name: setup-go ${{ matrix.go }}
3969
uses: ./
4070
with:
41-
go-version: 1.12.9
42-
43-
- name: validate version
44-
run: go version | grep "go1.12.9"
45-
46-
- name: dump env
71+
go-version: ${{ matrix.go }}
72+
- name: verify go
73+
run: __tests__/verify-go.sh ${{ matrix.go }}
4774
shell: bash
48-
run: |
49-
echo $PATH
50-
echo go versions in tool cache:
51-
echo $(ls $RUNNER_TOOL_CACHE/go)

‎__tests__/data/versions-manifest.json

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
[
2+
{
3+
"version": "1.12.17",
4+
"stable": true,
5+
"release_url": "https://github.com/actions/go-versions/releases/tag/1.12.17-20200616.21",
6+
"files": [
7+
{
8+
"filename": "go-1.12.17-darwin-x64.tar.gz",
9+
"arch": "x64",
10+
"platform": "darwin",
11+
"download_url": "https://github.com/actions/go-versions/releases/download/1.12.17-20200616.21/go-1.12.17-darwin-x64.tar.gz"
12+
},
13+
{
14+
"filename": "go-1.12.17-linux-x64.tar.gz",
15+
"arch": "x64",
16+
"platform": "linux",
17+
"download_url": "https://github.com/actions/go-versions/releases/download/1.12.17-20200616.21/go-1.12.17-linux-x64.tar.gz"
18+
},
19+
{
20+
"filename": "go-1.12.17-win32-x64.zip",
21+
"arch": "x64",
22+
"platform": "win32",
23+
"download_url": "https://github.com/actions/go-versions/releases/download/1.12.17-20200616.21/go-1.12.17-win32-x64.zip"
24+
}
25+
]
26+
},
27+
{
28+
"version": "1.12.16",
29+
"stable": true,
30+
"release_url": "https://github.com/actions/go-versions/releases/tag/1.12.16-20200616.20",
31+
"files": [
32+
{
33+
"filename": "go-1.12.16-darwin-x64.tar.gz",
34+
"arch": "x64",
35+
"platform": "darwin",
36+
"download_url": "https://github.com/actions/go-versions/releases/download/1.12.16-20200616.20/go-1.12.16-darwin-x64.tar.gz"
37+
},
38+
{
39+
"filename": "go-1.12.16-linux-x64.tar.gz",
40+
"arch": "x64",
41+
"platform": "linux",
42+
"download_url": "https://github.com/actions/go-versions/releases/download/1.12.16-20200616.20/go-1.12.16-linux-x64.tar.gz"
43+
},
44+
{
45+
"filename": "go-1.12.16-win32-x64.zip",
46+
"arch": "x64",
47+
"platform": "win32",
48+
"download_url": "https://github.com/actions/go-versions/releases/download/1.12.16-20200616.20/go-1.12.16-win32-x64.zip"
49+
}
50+
]
51+
},
52+
{
53+
"version": "1.9.7",
54+
"stable": true,
55+
"release_url": "https://github.com/actions/go-versions/releases/tag/1.9.7-20200616.1",
56+
"files": [
57+
{
58+
"filename": "go-1.9.7-darwin-x64.tar.gz",
59+
"arch": "x64",
60+
"platform": "darwin",
61+
"download_url": "https://github.com/actions/go-versions/releases/download/1.9.7-20200616.1/go-1.9.7-darwin-x64.tar.gz"
62+
},
63+
{
64+
"filename": "go-1.9.7-linux-x64.tar.gz",
65+
"arch": "x64",
66+
"platform": "linux",
67+
"download_url": "https://github.com/actions/go-versions/releases/download/1.9.7-20200616.1/go-1.9.7-linux-x64.tar.gz"
68+
},
69+
{
70+
"filename": "go-1.9.7-win32-x64.zip",
71+
"arch": "x64",
72+
"platform": "win32",
73+
"download_url": "https://github.com/actions/go-versions/releases/download/1.9.7-20200616.1/go-1.9.7-win32-x64.zip"
74+
}
75+
]
76+
}
77+
]

‎__tests__/setup-go.test.ts

+58-79
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as im from '../src/installer';
1010

1111
let goJsonData = require('./data/golang-dl.json');
1212
let matchers = require('../matchers.json');
13+
let goTestManifest = require('./data/versions-manifest.json');
1314
let matcherPattern = matchers.problemMatcher[0].pattern[0];
1415
let matcherRegExp = new RegExp(matcherPattern.regexp);
1516

@@ -32,6 +33,8 @@ describe('setup-go', () => {
3233
let existsSpy: jest.SpyInstance;
3334
let mkdirpSpy: jest.SpyInstance;
3435
let execSpy: jest.SpyInstance;
36+
let getManifestSpy: jest.SpyInstance;
37+
let getInfoManifestSpy: jest.SpyInstance;
3538

3639
beforeEach(() => {
3740
// @actions/core
@@ -52,17 +55,26 @@ describe('setup-go', () => {
5255
dlSpy = jest.spyOn(tc, 'downloadTool');
5356
exSpy = jest.spyOn(tc, 'extractTar');
5457
cacheSpy = jest.spyOn(tc, 'cacheDir');
55-
getSpy = jest.spyOn(im, 'getVersions');
58+
getSpy = jest.spyOn(im, 'getVersionsDist');
59+
getManifestSpy = jest.spyOn(tc, 'getManifestFromRepo');
60+
getInfoManifestSpy = jest.spyOn(im, 'getInfoFromManifest');
5661

5762
// io
5863
whichSpy = jest.spyOn(io, 'which');
5964
existsSpy = jest.spyOn(fs, 'existsSync');
6065
mkdirpSpy = jest.spyOn(io, 'mkdirP');
6166

67+
// gets
68+
getManifestSpy.mockImplementation(() => <tc.IToolRelease[]>goTestManifest);
69+
70+
// getInfoManifestSpy.mockImplementation(
71+
// () => <im.IGoVersionInfo | null>
72+
// )
73+
6274
// writes
6375
cnSpy = jest.spyOn(process.stdout, 'write');
6476
logSpy = jest.spyOn(console, 'log');
65-
dbgSpy = jest.spyOn(main, '_debug');
77+
dbgSpy = jest.spyOn(core, 'debug');
6678
getSpy.mockImplementation(() => <im.IGoVersion[]>goJsonData);
6779
cnSpy.mockImplementation(line => {
6880
// uncomment to debug
@@ -87,94 +99,77 @@ describe('setup-go', () => {
8799
afterAll(async () => {}, 100000);
88100

89101
it('can query versions', async () => {
90-
let versions: im.IGoVersion[] | null = await im.getVersions(
102+
let versions: im.IGoVersion[] | null = await im.getVersionsDist(
91103
'https://non.existant.com/path'
92104
);
93105
expect(versions).toBeDefined();
94106
let l: number = versions ? versions.length : 0;
95107
expect(l).toBe(91);
96108
});
97109

98-
it('finds stable match for exact version', async () => {
99-
os.platform = 'win32';
100-
os.arch = 'x64';
101-
102-
// get request is already mocked
103-
// spec: 1.13.7 => 1.13.7 (exact)
104-
let match: im.IGoVersion | undefined = await im.findMatch('1.13.7', true);
105-
expect(match).toBeDefined();
106-
let version: string = match ? match.version : '';
107-
expect(version).toBe('go1.13.7');
108-
let fileName = match ? match.files[0].filename : '';
109-
expect(fileName).toBe('go1.13.7.windows-amd64.zip');
110+
it('can mock manifest versions', async () => {
111+
let versions: tc.IToolRelease[] | null = await tc.getManifestFromRepo(
112+
'actions',
113+
'go-versions',
114+
'mocktoken'
115+
);
116+
expect(versions).toBeDefined();
117+
expect(versions.length).toBe(3);
110118
});
111119

112-
it('finds stable match for exact dot zero version', async () => {
120+
it('can find 1.12.17 from manifest on osx', async () => {
113121
os.platform = 'darwin';
114122
os.arch = 'x64';
115-
116-
// spec: 1.13.0 => 1.13
117-
let match: im.IGoVersion | undefined = await im.findMatch('1.13.0', true);
118-
expect(match).toBeDefined();
119-
let version: string = match ? match.version : '';
120-
expect(version).toBe('go1.13');
121-
let fileName = match ? match.files[0].filename : '';
122-
expect(fileName).toBe('go1.13.darwin-amd64.tar.gz');
123-
});
124-
125-
it('finds latest patch version for minor version spec', async () => {
126-
os.platform = 'linux';
127-
os.arch = 'x64';
128-
129-
// spec: 1.13 => 1.13.7 (latest)
130-
let match: im.IGoVersion | undefined = await im.findMatch('1.13', true);
123+
let versions: tc.IToolRelease[] | null = await tc.getManifestFromRepo(
124+
'actions',
125+
'go-versions',
126+
'mocktoken'
127+
);
128+
expect(versions).toBeDefined();
129+
let match = await tc.findFromManifest('1.12.17', true, versions);
131130
expect(match).toBeDefined();
132-
let version: string = match ? match.version : '';
133-
expect(version).toBe('go1.13.7');
134-
let fileName = match ? match.files[0].filename : '';
135-
expect(fileName).toBe('go1.13.7.linux-amd64.tar.gz');
131+
expect(match!.version).toBe('1.12.17');
136132
});
137133

138-
it('finds latest patch version for caret version spec', async () => {
134+
it('can find 12 from manifest on linux', async () => {
139135
os.platform = 'linux';
140136
os.arch = 'x64';
141-
142-
// spec: ^1.13.6 => 1.13.7
143-
let match: im.IGoVersion | undefined = await im.findMatch('^1.13.6', true);
137+
let versions: tc.IToolRelease[] | null = await tc.getManifestFromRepo(
138+
'actions',
139+
'go-versions',
140+
'mocktoken'
141+
);
142+
expect(versions).toBeDefined();
143+
let match = await tc.findFromManifest('1.12.17', true, versions);
144144
expect(match).toBeDefined();
145-
let version: string = match ? match.version : '';
146-
expect(version).toBe('go1.13.7');
147-
let fileName = match ? match.files[0].filename : '';
148-
expect(fileName).toBe('go1.13.7.linux-amd64.tar.gz');
145+
expect(match!.version).toBe('1.12.17');
149146
});
150147

151-
it('finds latest version for major version spec', async () => {
148+
it('can find 10 from manifest on windows', async () => {
152149
os.platform = 'win32';
153-
os.arch = 'x32';
154-
155-
// spec: 1 => 1.13.7 (latest)
156-
let match: im.IGoVersion | undefined = await im.findMatch('1', true);
150+
os.arch = 'x64';
151+
let versions: tc.IToolRelease[] | null = await tc.getManifestFromRepo(
152+
'actions',
153+
'go-versions',
154+
'mocktoken'
155+
);
156+
expect(versions).toBeDefined();
157+
let match = await tc.findFromManifest('1.12', true, versions);
157158
expect(match).toBeDefined();
158-
let version: string = match ? match.version : '';
159-
expect(version).toBe('go1.13.7');
160-
let fileName = match ? match.files[0].filename : '';
161-
expect(fileName).toBe('go1.13.7.windows-386.zip');
159+
expect(match!.version).toBe('1.12.17');
162160
});
163161

164-
it('finds unstable pre-release version', async () => {
165-
os.platform = 'linux';
162+
it('finds stable match for exact dot zero version', async () => {
163+
os.platform = 'darwin';
166164
os.arch = 'x64';
167165

168-
// spec: 1.14, stable=false => go1.14rc1
169-
let match: im.IGoVersion | undefined = await im.findMatch(
170-
'1.14.0-rc1',
171-
false
172-
);
166+
// spec: 1.13.0 => 1.13
167+
let match: im.IGoVersion | undefined = await im.findMatch('1.13.0', true);
173168
expect(match).toBeDefined();
174169
let version: string = match ? match.version : '';
175-
expect(version).toBe('go1.14rc1');
170+
expect(version).toBe('go1.13');
176171
let fileName = match ? match.files[0].filename : '';
177-
expect(fileName).toBe('go1.14rc1.linux-amd64.tar.gz');
172+
expect(fileName).toBe('go1.13.darwin-amd64.tar.gz');
178173
});
179174

180175
it('evaluates to stable with input as true', async () => {
@@ -261,7 +256,7 @@ describe('setup-go', () => {
261256
await main.run();
262257

263258
expect(cnSpy).toHaveBeenCalledWith(
264-
`::error::Could not find a version that satisfied version spec: 9.99.9${osm.EOL}`
259+
`::error::Unable to find Go version '9.99.9' for platform linux and architecture x64.${osm.EOL}`
265260
);
266261
});
267262

@@ -283,22 +278,6 @@ describe('setup-go', () => {
283278
);
284279
});
285280

286-
it('reports empty query results', async () => {
287-
let errMsg = 'unhandled download message';
288-
os.platform = 'linux';
289-
os.arch = 'x64';
290-
291-
inputs['go-version'] = '1.13.1';
292-
293-
findSpy.mockImplementation(() => '');
294-
getSpy.mockImplementation(() => null);
295-
await main.run();
296-
297-
expect(cnSpy).toHaveBeenCalledWith(
298-
`::error::Failed to download version 1.13.1: Error: golang download url did not return results${osm.EOL}`
299-
);
300-
});
301-
302281
it('does not add BIN if go is not in path', async () => {
303282
whichSpy.mockImplementation(async () => {
304283
return '';

‎__tests__/verify-go.sh

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
#!/bin/sh
3+
4+
if [ -z "$1" ]; then
5+
echo "Must supply go version argument"
6+
exit 1
7+
fi
8+
9+
go_version="$(go version)"
10+
echo "Found go version '$go_version'"
11+
if [ -z "$(echo $go_version | grep $1)" ]; then
12+
echo "Unexpected version"
13+
exit 1
14+
fi

‎action.yml

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ inputs:
77
stable:
88
description: 'Whether to download only stable versions'
99
default: 'true'
10+
token:
11+
description: Used to pull node distributions from go-versions. Since there's a default, this is typically not supplied by the user.
12+
default: ${{ github.token }}
1013
runs:
1114
using: 'node12'
1215
main: 'dist/index.js'

‎dist/index.js

+6,752-3,323
Large diffs are not rendered by default.

‎package-lock.json

+4,642-11,070
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
"author": "GitHub",
2424
"license": "MIT",
2525
"dependencies": {
26-
"@actions/core": "^1.2.2",
26+
"@actions/core": "^1.2.3",
2727
"@actions/http-client": "^1.0.6",
2828
"@actions/io": "^1.0.2",
29-
"@actions/tool-cache": "^1.3.3",
29+
"@actions/tool-cache": "^1.5.5",
3030
"semver": "^6.1.1"
3131
},
3232
"devDependencies": {
@@ -39,6 +39,6 @@
3939
"nock": "^10.0.6",
4040
"prettier": "^1.17.1",
4141
"ts-jest": "^24.0.2",
42-
"typescript": "^3.5.1"
42+
"typescript": "^3.8.3"
4343
}
4444
}

‎src/installer.ts

+167-43
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,12 @@
11
import * as tc from '@actions/tool-cache';
2+
import * as core from '@actions/core';
23
import * as path from 'path';
34
import * as semver from 'semver';
45
import * as httpm from '@actions/http-client';
56
import * as sys from './system';
6-
import {debug} from '@actions/core';
7+
import os from 'os';
78

8-
export async function downloadGo(
9-
versionSpec: string,
10-
stable: boolean
11-
): Promise<string | undefined> {
12-
let toolPath: string | undefined;
13-
14-
try {
15-
let match: IGoVersion | undefined = await findMatch(versionSpec, stable);
16-
17-
if (match) {
18-
// download
19-
debug(`match ${match.version}`);
20-
let downloadUrl: string = `https://storage.googleapis.com/golang/${match.files[0].filename}`;
21-
console.log(`Downloading from ${downloadUrl}`);
22-
23-
let downloadPath: string = await tc.downloadTool(downloadUrl);
24-
debug(`downloaded to ${downloadPath}`);
25-
26-
// extract
27-
console.log('Extracting ...');
28-
let extPath: string =
29-
sys.getPlatform() == 'windows'
30-
? await tc.extractZip(downloadPath)
31-
: await tc.extractTar(downloadPath);
32-
debug(`extracted to ${extPath}`);
33-
34-
// extracts with a root folder that matches the fileName downloaded
35-
const toolRoot = path.join(extPath, 'go');
36-
toolPath = await tc.cacheDir(toolRoot, 'go', makeSemver(match.version));
37-
}
38-
} catch (error) {
39-
throw new Error(`Failed to download version ${versionSpec}: ${error}`);
40-
}
41-
42-
return toolPath;
43-
}
9+
type InstallationType = 'dist' | 'manifest';
4410

4511
export interface IGoVersionFile {
4612
filename: string;
@@ -55,6 +21,157 @@ export interface IGoVersion {
5521
files: IGoVersionFile[];
5622
}
5723

24+
export interface IGoVersionInfo {
25+
type: InstallationType;
26+
downloadUrl: string;
27+
resolvedVersion: string;
28+
fileName: string;
29+
}
30+
31+
export async function getGo(
32+
versionSpec: string,
33+
stable: boolean,
34+
auth: string | undefined
35+
) {
36+
let osPlat: string = os.platform();
37+
let osArch: string = os.arch();
38+
39+
// check cache
40+
let toolPath: string;
41+
toolPath = tc.find('go', versionSpec);
42+
// If not found in cache, download
43+
if (toolPath) {
44+
console.log(`Found in cache @ ${toolPath}`);
45+
const toolcachePath = await tc.cacheDir(
46+
toolPath,
47+
'go',
48+
makeSemver(versionSpec)
49+
);
50+
return toolcachePath;
51+
}
52+
console.log(`Attempting to download ${versionSpec}...`);
53+
let downloadPath = '';
54+
let info: IGoVersionInfo | null = null;
55+
56+
//
57+
// Try download from internal distribution (popular versions only)
58+
//
59+
try {
60+
info = await getInfoFromManifest(versionSpec, stable, auth);
61+
if (info) {
62+
downloadPath = await installGoVersion(info, auth);
63+
} else {
64+
console.log(
65+
'Not found in manifest. Falling back to download directly from Go'
66+
);
67+
}
68+
} catch (err) {
69+
if (
70+
err instanceof tc.HTTPError &&
71+
(err.httpStatusCode === 403 || err.httpStatusCode === 429)
72+
) {
73+
console.log(
74+
`Received HTTP status code ${err.httpStatusCode}. This usually indicates the rate limit has been exceeded`
75+
);
76+
} else {
77+
console.log(err.message);
78+
}
79+
core.debug(err.stack);
80+
console.log('Falling back to download directly from Go');
81+
}
82+
83+
//
84+
// Download from storage.googleapis.com
85+
//
86+
if (!downloadPath) {
87+
info = await getInfoFromDist(versionSpec, stable);
88+
if (!info) {
89+
throw new Error(
90+
`Unable to find Go version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`
91+
);
92+
}
93+
94+
try {
95+
downloadPath = await installGoVersion(info, undefined);
96+
} catch (err) {
97+
throw new Error(`Failed to download version ${versionSpec}: ${err}`);
98+
}
99+
}
100+
101+
return downloadPath;
102+
}
103+
104+
async function installGoVersion(
105+
info: IGoVersionInfo,
106+
auth: string | undefined
107+
): Promise<string> {
108+
console.log(`Acquiring ${info.resolvedVersion} from ${info.downloadUrl}`);
109+
const downloadPath = await tc.downloadTool(info.downloadUrl, undefined, auth);
110+
111+
console.log('Extracting Go...');
112+
let extPath = await extractGoArchive(downloadPath);
113+
if (info.type === 'dist') {
114+
extPath = path.join(extPath, 'go');
115+
}
116+
117+
console.log('Adding to the cache ...');
118+
return await tc.cacheDir(extPath, 'go', makeSemver(info.resolvedVersion));
119+
}
120+
121+
export async function extractGoArchive(archivePath: string): Promise<string> {
122+
const arch = os.arch();
123+
let extPath: string;
124+
125+
if (arch === 'win32') {
126+
extPath = await tc.extractZip(archivePath);
127+
} else {
128+
extPath = await tc.extractTar(archivePath);
129+
}
130+
131+
return extPath;
132+
}
133+
134+
export async function getInfoFromManifest(
135+
versionSpec: string,
136+
stable: boolean,
137+
auth: string | undefined
138+
): Promise<IGoVersionInfo | null> {
139+
let info: IGoVersionInfo | null = null;
140+
const releases = await tc.getManifestFromRepo('actions', 'go-versions', auth);
141+
console.log(`matching ${versionSpec}...`);
142+
const rel = await tc.findFromManifest(versionSpec, stable, releases);
143+
144+
if (rel && rel.files.length > 0) {
145+
info = <IGoVersionInfo>{};
146+
info.type = 'manifest';
147+
info.resolvedVersion = rel.version;
148+
info.downloadUrl = rel.files[0].download_url;
149+
info.fileName = rel.files[0].filename;
150+
}
151+
152+
return info;
153+
}
154+
155+
async function getInfoFromDist(
156+
versionSpec: string,
157+
stable: boolean
158+
): Promise<IGoVersionInfo | null> {
159+
let version: IGoVersion | undefined;
160+
version = await findMatch(versionSpec, stable);
161+
if (!version) {
162+
return null;
163+
}
164+
165+
let downloadUrl: string = `https://storage.googleapis.com/golang/${version.files[0].filename}`;
166+
167+
return <IGoVersionInfo>{
168+
type: 'dist',
169+
downloadUrl: downloadUrl,
170+
resolvedVersion: version.version,
171+
fileName: version.files[0].filename
172+
};
173+
}
174+
58175
export async function findMatch(
59176
versionSpec: string,
60177
stable: boolean
@@ -66,7 +183,7 @@ export async function findMatch(
66183
let match: IGoVersion | undefined;
67184

68185
const dlUrl: string = 'https://golang.org/dl/?mode=json&include=all';
69-
let candidates: IGoVersion[] | null = await module.exports.getVersions(dlUrl);
186+
let candidates: IGoVersion[] | null = await getVersionsDist(dlUrl);
70187
if (!candidates) {
71188
throw new Error(`golang download url did not return results`);
72189
}
@@ -83,18 +200,20 @@ export async function findMatch(
83200
version = version + '.0';
84201
}
85202

86-
debug(`check ${version} satisfies ${versionSpec}`);
203+
core.debug(`check ${version} satisfies ${versionSpec}`);
87204
if (
88205
semver.satisfies(version, versionSpec) &&
89206
(!stable || candidate.stable === stable)
90207
) {
91208
goFile = candidate.files.find(file => {
92-
debug(`${file.arch}===${archFilter} && ${file.os}===${platFilter}`);
209+
core.debug(
210+
`${file.arch}===${archFilter} && ${file.os}===${platFilter}`
211+
);
93212
return file.arch === archFilter && file.os === platFilter;
94213
});
95214

96215
if (goFile) {
97-
debug(`matched ${candidate.version}`);
216+
core.debug(`matched ${candidate.version}`);
98217
match = candidate;
99218
break;
100219
}
@@ -110,9 +229,14 @@ export async function findMatch(
110229
return result;
111230
}
112231

113-
export async function getVersions(dlUrl: string): Promise<IGoVersion[] | null> {
232+
export async function getVersionsDist(
233+
dlUrl: string
234+
): Promise<IGoVersion[] | null> {
114235
// this returns versions descending so latest is first
115-
let http: httpm.HttpClient = new httpm.HttpClient('setup-go');
236+
let http: httpm.HttpClient = new httpm.HttpClient('setup-go', [], {
237+
allowRedirects: true,
238+
maxRedirects: 3
239+
});
116240
return (await http.getJson<IGoVersion[]>(dlUrl)).result;
117241
}
118242

‎src/main.ts

+20-27
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import * as core from '@actions/core';
22
import * as io from '@actions/io';
3-
import * as tc from '@actions/tool-cache';
43
import * as installer from './installer';
54
import path from 'path';
65
import cp from 'child_process';
76
import fs from 'fs';
7+
import {URL} from 'url';
88

99
export async function run() {
1010
try {
@@ -23,28 +23,18 @@ export async function run() {
2323
);
2424

2525
if (versionSpec) {
26-
let installDir: string | undefined = tc.find('go', versionSpec);
26+
let token = core.getInput('token');
27+
let auth = !token || isGhes() ? undefined : `token ${token}`;
2728

28-
if (!installDir) {
29-
console.log(
30-
`A version satisfying ${versionSpec} not found locally, attempting to download ...`
31-
);
32-
installDir = await installer.downloadGo(versionSpec, stable);
33-
console.log('Installed');
34-
}
29+
const installDir = await installer.getGo(versionSpec, stable, auth);
3530

36-
if (installDir) {
37-
core.exportVariable('GOROOT', installDir);
38-
core.addPath(path.join(installDir, 'bin'));
39-
console.log('Added go to the path');
31+
core.exportVariable('GOROOT', installDir);
32+
core.addPath(path.join(installDir, 'bin'));
33+
console.log('Added go to the path');
4034

41-
let added = addBinToPath();
42-
core.debug(`add bin ${added}`);
43-
} else {
44-
throw new Error(
45-
`Could not find a version that satisfied version spec: ${versionSpec}`
46-
);
47-
}
35+
let added = addBinToPath();
36+
core.debug(`add bin ${added}`);
37+
console.log('Done');
4838
}
4939

5040
// add problem matchers
@@ -68,25 +58,25 @@ export async function run() {
6858
export async function addBinToPath(): Promise<boolean> {
6959
let added = false;
7060
let g = await io.which('go');
71-
_debug(`which go :${g}:`);
61+
core.debug(`which go :${g}:`);
7262
if (!g) {
73-
_debug('go not in the path');
63+
core.debug('go not in the path');
7464
return added;
7565
}
7666

7767
let buf = cp.execSync('go env GOPATH');
7868
if (buf) {
7969
let gp = buf.toString().trim();
80-
_debug(`go env GOPATH :${gp}:`);
70+
core.debug(`go env GOPATH :${gp}:`);
8171
if (!fs.existsSync(gp)) {
8272
// some of the hosted images have go install but not profile dir
83-
_debug(`creating ${gp}`);
73+
core.debug(`creating ${gp}`);
8474
io.mkdirP(gp);
8575
}
8676

8777
let bp = path.join(gp, 'bin');
8878
if (!fs.existsSync(bp)) {
89-
_debug(`creating ${bp}`);
79+
core.debug(`creating ${bp}`);
9080
io.mkdirP(bp);
9181
}
9282

@@ -96,6 +86,9 @@ export async function addBinToPath(): Promise<boolean> {
9686
return added;
9787
}
9888

99-
export function _debug(message: string) {
100-
core.debug(message);
89+
function isGhes(): boolean {
90+
const ghUrl = new URL(
91+
process.env['GITHUB_SERVER_URL'] || 'https://github.com'
92+
);
93+
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
10194
}

0 commit comments

Comments
 (0)
Please sign in to comment.