mirror of https://github.com/actions/toolkit
Allow specifying arbitrary headers when downloading tools to the tool cache. (#530)
parent
15fef78171
commit
ff45a53422
|
@ -763,6 +763,61 @@ describe('@actions/tool-cache', function() {
|
||||||
expect(err.toString()).toContain('404')
|
expect(err.toString()).toContain('404')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('supports authorization headers', async function() {
|
||||||
|
nock('http://example.com', {
|
||||||
|
reqheaders: {
|
||||||
|
authorization: 'token abc123'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.get('/some-file-that-needs-authorization')
|
||||||
|
.reply(200, undefined)
|
||||||
|
|
||||||
|
await tc.downloadTool(
|
||||||
|
'http://example.com/some-file-that-needs-authorization',
|
||||||
|
undefined,
|
||||||
|
'token abc123'
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('supports custom headers', async function() {
|
||||||
|
nock('http://example.com', {
|
||||||
|
reqheaders: {
|
||||||
|
accept: 'application/octet-stream'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.get('/some-file-that-needs-headers')
|
||||||
|
.reply(200, undefined)
|
||||||
|
|
||||||
|
await tc.downloadTool(
|
||||||
|
'http://example.com/some-file-that-needs-headers',
|
||||||
|
undefined,
|
||||||
|
undefined,
|
||||||
|
{
|
||||||
|
accept: 'application/octet-stream'
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('supports authorization and custom headers', async function() {
|
||||||
|
nock('http://example.com', {
|
||||||
|
reqheaders: {
|
||||||
|
accept: 'application/octet-stream',
|
||||||
|
authorization: 'token abc123'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.get('/some-file-that-needs-authorization-and-headers')
|
||||||
|
.reply(200, undefined)
|
||||||
|
|
||||||
|
await tc.downloadTool(
|
||||||
|
'http://example.com/some-file-that-needs-authorization-and-headers',
|
||||||
|
undefined,
|
||||||
|
'token abc123',
|
||||||
|
{
|
||||||
|
accept: 'application/octet-stream'
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -32,12 +32,14 @@ const userAgent = 'actions/tool-cache'
|
||||||
* @param url url of tool to download
|
* @param url url of tool to download
|
||||||
* @param dest path to download tool
|
* @param dest path to download tool
|
||||||
* @param auth authorization header
|
* @param auth authorization header
|
||||||
|
* @param headers other headers
|
||||||
* @returns path to downloaded tool
|
* @returns path to downloaded tool
|
||||||
*/
|
*/
|
||||||
export async function downloadTool(
|
export async function downloadTool(
|
||||||
url: string,
|
url: string,
|
||||||
dest?: string,
|
dest?: string,
|
||||||
auth?: string
|
auth?: string,
|
||||||
|
headers?: IHeaders
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
dest = dest || path.join(_getTempDirectory(), uuidV4())
|
dest = dest || path.join(_getTempDirectory(), uuidV4())
|
||||||
await io.mkdirP(path.dirname(dest))
|
await io.mkdirP(path.dirname(dest))
|
||||||
|
@ -56,7 +58,7 @@ export async function downloadTool(
|
||||||
const retryHelper = new RetryHelper(maxAttempts, minSeconds, maxSeconds)
|
const retryHelper = new RetryHelper(maxAttempts, minSeconds, maxSeconds)
|
||||||
return await retryHelper.execute(
|
return await retryHelper.execute(
|
||||||
async () => {
|
async () => {
|
||||||
return await downloadToolAttempt(url, dest || '', auth)
|
return await downloadToolAttempt(url, dest || '', auth, headers)
|
||||||
},
|
},
|
||||||
(err: Error) => {
|
(err: Error) => {
|
||||||
if (err instanceof HTTPError && err.httpStatusCode) {
|
if (err instanceof HTTPError && err.httpStatusCode) {
|
||||||
|
@ -79,7 +81,8 @@ export async function downloadTool(
|
||||||
async function downloadToolAttempt(
|
async function downloadToolAttempt(
|
||||||
url: string,
|
url: string,
|
||||||
dest: string,
|
dest: string,
|
||||||
auth?: string
|
auth?: string,
|
||||||
|
headers?: IHeaders
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
if (fs.existsSync(dest)) {
|
if (fs.existsSync(dest)) {
|
||||||
throw new Error(`Destination file path ${dest} already exists`)
|
throw new Error(`Destination file path ${dest} already exists`)
|
||||||
|
@ -90,12 +93,12 @@ async function downloadToolAttempt(
|
||||||
allowRetries: false
|
allowRetries: false
|
||||||
})
|
})
|
||||||
|
|
||||||
let headers: IHeaders | undefined
|
|
||||||
if (auth) {
|
if (auth) {
|
||||||
core.debug('set auth')
|
core.debug('set auth')
|
||||||
headers = {
|
if (headers === undefined) {
|
||||||
authorization: auth
|
headers = {}
|
||||||
}
|
}
|
||||||
|
headers.authorization = auth
|
||||||
}
|
}
|
||||||
|
|
||||||
const response: httpm.HttpClientResponse = await http.get(url, headers)
|
const response: httpm.HttpClientResponse = await http.get(url, headers)
|
||||||
|
|
Loading…
Reference in New Issue