Send Content-Type: application/json and fix up some types

This commit is contained in:
David Kale 2020-02-03 15:22:05 -05:00
parent e9d35bb1ee
commit 866455018b
3 changed files with 38 additions and 33 deletions

18
package-lock.json generated
View file

@ -15,18 +15,11 @@
"integrity": "sha512-nvFkxwiicvpzNiCBF4wFBDfnBvi7xp/as7LE1hBxBxKG2L29+gkIPBiLKMVORL+Hg3JNf07AKRfl0V5djoypjQ==" "integrity": "sha512-nvFkxwiicvpzNiCBF4wFBDfnBvi7xp/as7LE1hBxBxKG2L29+gkIPBiLKMVORL+Hg3JNf07AKRfl0V5djoypjQ=="
}, },
"@actions/http-client": { "@actions/http-client": {
"version": "1.0.3", "version": "1.0.4",
"resolved": "https://registry.npmjs.org/@actions/http-client/-/http-client-1.0.3.tgz", "resolved": "https://registry.npmjs.org/@actions/http-client/-/http-client-1.0.4.tgz",
"integrity": "sha512-wFwh1U4adB/Zsk4cc9kVqaBOHoknhp/pJQk+aWTocbAZWpIl4Zx/At83WFRLXvxB+5HVTWOACM6qjULMZfQSfw==", "integrity": "sha512-6EzXhqapKKtYr21ZnFQVBYwfrYPKPCivuSkUN/66/BDakkH2EPjUZH8tZ3MgHdI+gQIdcsY0ybbxw9ZEOmJB6g==",
"requires": { "requires": {
"tunnel": "0.0.6" "tunnel": "0.0.6"
},
"dependencies": {
"tunnel": {
"version": "0.0.6",
"resolved": "https://registry.npmjs.org/tunnel/-/tunnel-0.0.6.tgz",
"integrity": "sha512-1h/Lnq9yajKY2PEbBadPXj3VxsDDu844OnaAo52UVmIzIvwwtBPIuNvkjuzBlTWpfJyUbG3ez0KSBibQkj4ojg=="
}
} }
}, },
"@actions/io": { "@actions/io": {
@ -5947,6 +5940,11 @@
"tslib": "^1.8.1" "tslib": "^1.8.1"
} }
}, },
"tunnel": {
"version": "0.0.6",
"resolved": "https://registry.npmjs.org/tunnel/-/tunnel-0.0.6.tgz",
"integrity": "sha512-1h/Lnq9yajKY2PEbBadPXj3VxsDDu844OnaAo52UVmIzIvwwtBPIuNvkjuzBlTWpfJyUbG3ez0KSBibQkj4ojg=="
},
"tunnel-agent": { "tunnel-agent": {
"version": "0.6.0", "version": "0.6.0",
"resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz", "resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz",

View file

@ -26,7 +26,7 @@
"dependencies": { "dependencies": {
"@actions/core": "^1.2.0", "@actions/core": "^1.2.0",
"@actions/exec": "^1.0.1", "@actions/exec": "^1.0.1",
"@actions/http-client": "^1.0.3", "@actions/http-client": "^1.0.4",
"@actions/io": "^1.0.1", "@actions/io": "^1.0.1",
"uuid": "^3.3.3" "uuid": "^3.3.3"
}, },

View file

@ -1,7 +1,7 @@
import * as core from "@actions/core"; import * as core from "@actions/core";
import * as fs from "fs"; import * as fs from "fs";
import { BearerCredentialHandler } from "@actions/http-client/auth"; import { BearerCredentialHandler } from "@actions/http-client/auth";
import { HttpClient, HttpCodes } from "@actions/http-client"; import { HttpClient, HttpCodes, ITypedResponse } from "@actions/http-client";
import { import {
IHttpClientResponse, IHttpClientResponse,
IRequestOptions IRequestOptions
@ -9,8 +9,7 @@ import {
import { import {
ArtifactCacheEntry, ArtifactCacheEntry,
CommitCacheRequest, CommitCacheRequest,
ReserveCacheRequest, ReserveCacheRequest
ReserveCacheResponse
} from "./contracts"; } from "./contracts";
import * as utils from "./utils/actionUtils"; import * as utils from "./utils/actionUtils";
@ -46,8 +45,9 @@ function getCacheApiUrl(resource: string): string {
); );
} }
core.debug(`Cache Url: ${baseUrl}`); const url = `${baseUrl}_apis/artifactcache/${resource}`;
return `${baseUrl}_apis/artifactcache/${resource}`; core.debug(`Resource Url: ${url}`);
return url;
} }
function createAcceptHeader(type: string, apiVersion: string): string { function createAcceptHeader(type: string, apiVersion: string): string {
@ -81,19 +81,20 @@ export async function getCacheEntry(
const httpClient = createHttpClient(); const httpClient = createHttpClient();
const resource = `cache?keys=${encodeURIComponent(keys.join(","))}`; const resource = `cache?keys=${encodeURIComponent(keys.join(","))}`;
const response = await httpClient.get(getCacheApiUrl(resource)); const response = await httpClient.getJson<ArtifactCacheEntry>(
if (response.message.statusCode === 204) { getCacheApiUrl(resource)
);
if (response.statusCode === 204) {
return null; return null;
} }
if (!isSuccessStatusCode(response.message.statusCode)) { if (!isSuccessStatusCode(response.statusCode)) {
throw new Error( throw new Error(
`Cache service responded with ${response.message.statusCode}` `Cache service responded with ${response.statusCode}`
); );
} }
const body = await response.readBody(); const cacheResult = response.result;
const cacheResult = JSON.parse(body) as ArtifactCacheEntry; const cacheDownloadUrl = cacheResult?.archiveLocation;
const cacheDownloadUrl = cacheResult.archiveLocation;
if (!cacheDownloadUrl) { if (!cacheDownloadUrl) {
throw new Error("Cache not found."); throw new Error("Cache not found.");
} }
@ -132,13 +133,15 @@ export async function reserveCache(key: string): Promise<number> {
const reserveCacheRequest: ReserveCacheRequest = { const reserveCacheRequest: ReserveCacheRequest = {
key key
}; };
const response = await httpClient.post( const additionalHeaders = {
"Content-Type": "application/json"
};
const response = await httpClient.postJson<any>(
getCacheApiUrl("caches"), getCacheApiUrl("caches"),
JSON.stringify(reserveCacheRequest) reserveCacheRequest,
additionalHeaders
); );
const body = await response.readBody(); return response?.result?.cacheId ?? -1;
const cacheResult = JSON.parse(body) as ReserveCacheResponse;
return cacheResult.cacheId || -1;
} }
function getContentRange(start: number, end: number): string { function getContentRange(start: number, end: number): string {
@ -264,11 +267,15 @@ async function commitCache(
httpClient: HttpClient, httpClient: HttpClient,
cacheId: number, cacheId: number,
filesize: number filesize: number
): Promise<IHttpClientResponse> { ): Promise<ITypedResponse<any>> {
const commitCacheRequest: CommitCacheRequest = { size: filesize }; const commitCacheRequest: CommitCacheRequest = { size: filesize };
return await httpClient.post( const additionalHeaders = {
"Content-Type": "application/json"
};
return await httpClient.postJson<any>(
getCacheApiUrl(`caches/${cacheId.toString()}`), getCacheApiUrl(`caches/${cacheId.toString()}`),
JSON.stringify(commitCacheRequest) commitCacheRequest,
additionalHeaders
); );
} }
@ -289,9 +296,9 @@ export async function saveCache(
cacheId, cacheId,
cacheSize cacheSize
); );
if (!isSuccessStatusCode(commitCacheResponse.message.statusCode)) { if (!isSuccessStatusCode(commitCacheResponse.statusCode)) {
throw new Error( throw new Error(
`Cache service responded with ${commitCacheResponse.message.statusCode} during commit cache.` `Cache service responded with ${commitCacheResponse.statusCode} during commit cache.`
); );
} }