mirror of
https://code.forgejo.org/actions/cache.git
synced 2025-04-16 16:01:22 +02:00
Send Content-Type: application/json and fix up some types
This commit is contained in:
parent
e9d35bb1ee
commit
866455018b
3 changed files with 38 additions and 33 deletions
18
package-lock.json
generated
18
package-lock.json
generated
|
@ -15,18 +15,11 @@
|
|||
"integrity": "sha512-nvFkxwiicvpzNiCBF4wFBDfnBvi7xp/as7LE1hBxBxKG2L29+gkIPBiLKMVORL+Hg3JNf07AKRfl0V5djoypjQ=="
|
||||
},
|
||||
"@actions/http-client": {
|
||||
"version": "1.0.3",
|
||||
"resolved": "https://registry.npmjs.org/@actions/http-client/-/http-client-1.0.3.tgz",
|
||||
"integrity": "sha512-wFwh1U4adB/Zsk4cc9kVqaBOHoknhp/pJQk+aWTocbAZWpIl4Zx/At83WFRLXvxB+5HVTWOACM6qjULMZfQSfw==",
|
||||
"version": "1.0.4",
|
||||
"resolved": "https://registry.npmjs.org/@actions/http-client/-/http-client-1.0.4.tgz",
|
||||
"integrity": "sha512-6EzXhqapKKtYr21ZnFQVBYwfrYPKPCivuSkUN/66/BDakkH2EPjUZH8tZ3MgHdI+gQIdcsY0ybbxw9ZEOmJB6g==",
|
||||
"requires": {
|
||||
"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": {
|
||||
|
@ -5947,6 +5940,11 @@
|
|||
"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": {
|
||||
"version": "0.6.0",
|
||||
"resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz",
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
"dependencies": {
|
||||
"@actions/core": "^1.2.0",
|
||||
"@actions/exec": "^1.0.1",
|
||||
"@actions/http-client": "^1.0.3",
|
||||
"@actions/http-client": "^1.0.4",
|
||||
"@actions/io": "^1.0.1",
|
||||
"uuid": "^3.3.3"
|
||||
},
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import * as core from "@actions/core";
|
||||
import * as fs from "fs";
|
||||
import { BearerCredentialHandler } from "@actions/http-client/auth";
|
||||
import { HttpClient, HttpCodes } from "@actions/http-client";
|
||||
import { HttpClient, HttpCodes, ITypedResponse } from "@actions/http-client";
|
||||
import {
|
||||
IHttpClientResponse,
|
||||
IRequestOptions
|
||||
|
@ -9,8 +9,7 @@ import {
|
|||
import {
|
||||
ArtifactCacheEntry,
|
||||
CommitCacheRequest,
|
||||
ReserveCacheRequest,
|
||||
ReserveCacheResponse
|
||||
ReserveCacheRequest
|
||||
} from "./contracts";
|
||||
import * as utils from "./utils/actionUtils";
|
||||
|
||||
|
@ -46,8 +45,9 @@ function getCacheApiUrl(resource: string): string {
|
|||
);
|
||||
}
|
||||
|
||||
core.debug(`Cache Url: ${baseUrl}`);
|
||||
return `${baseUrl}_apis/artifactcache/${resource}`;
|
||||
const url = `${baseUrl}_apis/artifactcache/${resource}`;
|
||||
core.debug(`Resource Url: ${url}`);
|
||||
return url;
|
||||
}
|
||||
|
||||
function createAcceptHeader(type: string, apiVersion: string): string {
|
||||
|
@ -81,19 +81,20 @@ export async function getCacheEntry(
|
|||
const httpClient = createHttpClient();
|
||||
const resource = `cache?keys=${encodeURIComponent(keys.join(","))}`;
|
||||
|
||||
const response = await httpClient.get(getCacheApiUrl(resource));
|
||||
if (response.message.statusCode === 204) {
|
||||
const response = await httpClient.getJson<ArtifactCacheEntry>(
|
||||
getCacheApiUrl(resource)
|
||||
);
|
||||
if (response.statusCode === 204) {
|
||||
return null;
|
||||
}
|
||||
if (!isSuccessStatusCode(response.message.statusCode)) {
|
||||
if (!isSuccessStatusCode(response.statusCode)) {
|
||||
throw new Error(
|
||||
`Cache service responded with ${response.message.statusCode}`
|
||||
`Cache service responded with ${response.statusCode}`
|
||||
);
|
||||
}
|
||||
|
||||
const body = await response.readBody();
|
||||
const cacheResult = JSON.parse(body) as ArtifactCacheEntry;
|
||||
const cacheDownloadUrl = cacheResult.archiveLocation;
|
||||
const cacheResult = response.result;
|
||||
const cacheDownloadUrl = cacheResult?.archiveLocation;
|
||||
if (!cacheDownloadUrl) {
|
||||
throw new Error("Cache not found.");
|
||||
}
|
||||
|
@ -132,13 +133,15 @@ export async function reserveCache(key: string): Promise<number> {
|
|||
const reserveCacheRequest: ReserveCacheRequest = {
|
||||
key
|
||||
};
|
||||
const response = await httpClient.post(
|
||||
const additionalHeaders = {
|
||||
"Content-Type": "application/json"
|
||||
};
|
||||
const response = await httpClient.postJson<any>(
|
||||
getCacheApiUrl("caches"),
|
||||
JSON.stringify(reserveCacheRequest)
|
||||
reserveCacheRequest,
|
||||
additionalHeaders
|
||||
);
|
||||
const body = await response.readBody();
|
||||
const cacheResult = JSON.parse(body) as ReserveCacheResponse;
|
||||
return cacheResult.cacheId || -1;
|
||||
return response?.result?.cacheId ?? -1;
|
||||
}
|
||||
|
||||
function getContentRange(start: number, end: number): string {
|
||||
|
@ -264,11 +267,15 @@ async function commitCache(
|
|||
httpClient: HttpClient,
|
||||
cacheId: number,
|
||||
filesize: number
|
||||
): Promise<IHttpClientResponse> {
|
||||
): Promise<ITypedResponse<any>> {
|
||||
const commitCacheRequest: CommitCacheRequest = { size: filesize };
|
||||
return await httpClient.post(
|
||||
const additionalHeaders = {
|
||||
"Content-Type": "application/json"
|
||||
};
|
||||
return await httpClient.postJson<any>(
|
||||
getCacheApiUrl(`caches/${cacheId.toString()}`),
|
||||
JSON.stringify(commitCacheRequest)
|
||||
commitCacheRequest,
|
||||
additionalHeaders
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -289,9 +296,9 @@ export async function saveCache(
|
|||
cacheId,
|
||||
cacheSize
|
||||
);
|
||||
if (!isSuccessStatusCode(commitCacheResponse.message.statusCode)) {
|
||||
if (!isSuccessStatusCode(commitCacheResponse.statusCode)) {
|
||||
throw new Error(
|
||||
`Cache service responded with ${commitCacheResponse.message.statusCode} during commit cache.`
|
||||
`Cache service responded with ${commitCacheResponse.statusCode} during commit cache.`
|
||||
);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue