Skip to content

Commit 941f534

Browse files
Merge pull request #107 from useblacksmith/20250714.twirp-delete
feat: migrate cache deletion to Twirp backend
2 parents a995d81 + a98c8a2 commit 941f534

File tree

3 files changed

+79
-55
lines changed

3 files changed

+79
-55
lines changed

dist/index.js

Lines changed: 21 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__tests__/main.test.ts

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,34 @@ describe("deleteCache", () => {
2020
ok: true,
2121
status: 200,
2222
statusText: "OK",
23-
json: () => Promise.resolve({ deleted: 1 }),
23+
json: () => Promise.resolve({ count: 1 }),
2424
} as Response);
2525
});
2626

2727
const defaultParams = {
28-
baseUrl: "https://api.blacksmith.sh/cache",
29-
repoName: "test-repo",
28+
baseUrl: "http://baseurl/",
3029
cacheToken: "test-token",
31-
region: "eu-central",
3230
};
3331

34-
it("should send DELETE request for a specific cache key", async () => {
32+
it("should send request to delete a specific cache key", async () => {
3533
await deleteCache({
3634
cacheKey: "npm-cache",
3735
...defaultParams,
3836
});
3937

4038
expect(mockedFetch).toHaveBeenCalledWith(
41-
"https://api.blacksmith.sh/cache/caches/npm-cache",
39+
"http://baseurl/twirp/github.actions.results.api.v1.CacheService/DeleteCacheEntry",
4240
{
43-
method: "DELETE",
41+
method: "POST",
4442
headers: {
43+
"Content-Type": "application/json",
4544
Accept: "application/json; version=6.0-preview.1",
46-
"X-GitHub-Repo-Name": "test-repo",
4745
Authorization: "Bearer test-token",
48-
"X-Cache-Region": "eu-central",
4946
},
47+
body: JSON.stringify({
48+
key: "npm-cache",
49+
prefix: false,
50+
}),
5051
}
5152
);
5253
expect(mockedInfo).toHaveBeenCalledWith(
@@ -55,23 +56,27 @@ describe("deleteCache", () => {
5556
expect(mockedInfo).toHaveBeenCalledWith("Deleted 1 cache entries");
5657
});
5758

58-
it("should send DELETE request for a specific cache version", async () => {
59+
it("should send request to delete a specific cache version", async () => {
5960
await deleteCache({
6061
cacheKey: "npm-cache",
6162
cacheVersion: "v1.0",
6263
...defaultParams,
6364
});
6465

6566
expect(mockedFetch).toHaveBeenCalledWith(
66-
"https://api.blacksmith.sh/cache/caches/npm-cache/v1.0",
67+
"http://baseurl/twirp/github.actions.results.api.v1.CacheService/DeleteCacheEntry",
6768
{
68-
method: "DELETE",
69+
method: "POST",
6970
headers: {
71+
"Content-Type": "application/json",
7072
Accept: "application/json; version=6.0-preview.1",
71-
"X-GitHub-Repo-Name": "test-repo",
7273
Authorization: "Bearer test-token",
73-
"X-Cache-Region": "eu-central",
7474
},
75+
body: JSON.stringify({
76+
key: "npm-cache",
77+
version: "v1.0",
78+
prefix: false,
79+
}),
7580
}
7681
);
7782
expect(mockedInfo).toHaveBeenCalledWith(
@@ -80,12 +85,12 @@ describe("deleteCache", () => {
8085
expect(mockedInfo).toHaveBeenCalledWith("Deleted 1 cache entries");
8186
});
8287

83-
it("should send DELETE request with prefix parameter", async () => {
88+
it("should send request with prefix parameter", async () => {
8489
mockedFetch.mockResolvedValue({
8590
ok: true,
8691
status: 200,
8792
statusText: "OK",
88-
json: () => Promise.resolve({ deleted: 5 }),
93+
json: () => Promise.resolve({ count: 5 }),
8994
} as Response);
9095

9196
await deleteCache({
@@ -95,15 +100,18 @@ describe("deleteCache", () => {
95100
});
96101

97102
expect(mockedFetch).toHaveBeenCalledWith(
98-
"https://api.blacksmith.sh/cache/caches/npm-?prefix",
103+
"http://baseurl/twirp/github.actions.results.api.v1.CacheService/DeleteCacheEntry",
99104
{
100-
method: "DELETE",
105+
method: "POST",
101106
headers: {
107+
"Content-Type": "application/json",
102108
Accept: "application/json; version=6.0-preview.1",
103-
"X-GitHub-Repo-Name": "test-repo",
104109
Authorization: "Bearer test-token",
105-
"X-Cache-Region": "eu-central",
106110
},
111+
body: JSON.stringify({
112+
key: "npm-",
113+
prefix: true,
114+
}),
107115
}
108116
);
109117
expect(mockedInfo).toHaveBeenCalledWith(
@@ -112,12 +120,12 @@ describe("deleteCache", () => {
112120
expect(mockedInfo).toHaveBeenCalledWith("Deleted 5 cache entries");
113121
});
114122

115-
it("should send DELETE request with empty key and prefix parameter", async () => {
123+
it("should send request with empty key and prefix parameter", async () => {
116124
mockedFetch.mockResolvedValue({
117125
ok: true,
118126
status: 200,
119127
statusText: "OK",
120-
json: () => Promise.resolve({ deleted: 10 }),
128+
json: () => Promise.resolve({ count: 10 }),
121129
} as Response);
122130

123131
await deleteCache({
@@ -127,15 +135,18 @@ describe("deleteCache", () => {
127135
});
128136

129137
expect(mockedFetch).toHaveBeenCalledWith(
130-
"https://api.blacksmith.sh/cache/caches/?prefix",
138+
"http://baseurl/twirp/github.actions.results.api.v1.CacheService/DeleteCacheEntry",
131139
{
132-
method: "DELETE",
140+
method: "POST",
133141
headers: {
142+
"Content-Type": "application/json",
134143
Accept: "application/json; version=6.0-preview.1",
135-
"X-GitHub-Repo-Name": "test-repo",
136144
Authorization: "Bearer test-token",
137-
"X-Cache-Region": "eu-central",
138145
},
146+
body: JSON.stringify({
147+
key: "",
148+
prefix: true,
149+
}),
139150
}
140151
);
141152
expect(mockedInfo).toHaveBeenCalledWith(

src/main.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,20 @@ interface DeleteCacheParams {
2424
cacheVersion?: string;
2525
prefix?: boolean;
2626
baseUrl?: string;
27-
repoName?: string;
2827
cacheToken?: string;
29-
region?: string;
3028
}
3129

3230
export async function deleteCache({
3331
cacheKey,
3432
cacheVersion,
3533
prefix = false,
36-
baseUrl = process.env.BLACKSMITH_CACHE_URL ||
37-
(process.env.PETNAME?.includes("staging")
38-
? "https://stagingapi.blacksmith.sh/cache"
39-
: "https://api.blacksmith.sh/cache"),
40-
repoName = process.env["GITHUB_REPO_NAME"] ?? "",
34+
baseUrl = process.env.ACTIONS_RESULTS_URL ?? "",
4135
cacheToken = process.env["BLACKSMITH_CACHE_TOKEN"],
42-
region = process.env["BLACKSMITH_REGION"] ?? "eu-central",
4336
}: DeleteCacheParams): Promise<void> {
37+
if (!baseUrl) {
38+
throw new Error("ACTIONS_RESULTS_URL not set");
39+
}
40+
4441
if (!cacheKey && !prefix) {
4542
throw new Error("Cache key cannot be empty unless prefix is true");
4643
}
@@ -51,17 +48,25 @@ export async function deleteCache({
5148
throw new Error("Cannot specify version when using prefix");
5249
}
5350

54-
const resource = cacheVersion ? `${cacheKey}/${cacheVersion}` : cacheKey;
55-
const url = `${baseUrl}/caches/${resource}`;
51+
// The baseURL always has a trailing slash, but we still add it in case it is missing.
52+
if (!baseUrl.endsWith("/")) {
53+
baseUrl = `${baseUrl}/`;
54+
}
55+
const url = `${baseUrl}twirp/github.actions.results.api.v1.CacheService/DeleteCacheEntry`;
5656

57-
const response = await fetchWithRetry(prefix ? `${url}?prefix` : url, {
58-
method: "DELETE",
57+
const response = await fetchWithRetry(url, {
58+
// Twirp endpoints all use POST.
59+
method: "POST",
5960
headers: {
61+
"Content-Type": "application/json",
6062
Accept: "application/json; version=6.0-preview.1",
61-
"X-GitHub-Repo-Name": repoName,
6263
Authorization: `Bearer ${cacheToken}`,
63-
"X-Cache-Region": region,
6464
},
65+
body: JSON.stringify({
66+
key: cacheKey,
67+
version: cacheVersion,
68+
prefix,
69+
}),
6570
});
6671

6772
if (!response.ok && response.status !== 404) {
@@ -81,8 +86,8 @@ export async function deleteCache({
8186
cacheVersion ? " version" : ""
8287
}: ${cacheKey}${cacheVersion ? `@${cacheVersion}` : ""}`
8388
);
84-
if (data.deleted !== undefined) {
85-
info(`Deleted ${data.deleted} cache entries`);
89+
if (data.count !== undefined) {
90+
info(`Deleted ${data.count} cache entries`);
8691
}
8792
}
8893
}

0 commit comments

Comments
 (0)