Repository navigation
203 lines (174 loc) · 7.8 KB
/
Copy pathdelete-github-packages.yml
File metadata and controls
203 lines (174 loc) · 7.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
name: Delete GitHub Packages
on:
workflow_dispatch:
inputs:
branch:
description: Branch whose published package versions should be removed
required: true
default: dev
type: string
channel:
description: Channel used when the versions were published
required: true
default: preview
type: string
dry_run:
description: When true, list matches only and do not delete anything
required: true
default: true
type: boolean
permissions:
contents: read
packages: write
concurrency:
group: delete-github-packages-${{ inputs.branch }}-${{ inputs.channel }}
cancel-in-progress: false
jobs:
delete_packages:
runs-on: codebuild-semoss-github-runner-${{ github.run_id }}-${{ github.run_attempt }}
container:
image: public.ecr.aws/docker/library/node:24-bullseye
env:
SOURCE_BRANCH: ${{ inputs.branch }}
PUBLISH_CHANNEL: ${{ inputs.channel }}
DRY_RUN: ${{ inputs.dry_run }}
PACKAGE_ADMIN_TOKEN: ${{ secrets.SEMOSS_GITHUB_PAT }}
FALLBACK_GITHUB_TOKEN: ${{ github.token }}
GITHUB_OWNER: ${{ github.repository_owner }}
steps:
- name: Delete matching package versions
run: |
node <<'NODE'
const owner = process.env.GITHUB_OWNER;
const ownerScope = `@${(owner || "").toLowerCase()}`;
const packages = [
{
displayName: `${ownerScope}/i18n`,
apiName: "i18n",
},
{
displayName: `${ownerScope}/sdk`,
apiName: "sdk",
},
{
displayName: `${ownerScope}/ui`,
apiName: "ui",
},
{
displayName: `${ownerScope}/shared`,
apiName: "shared",
},
{
displayName: `${ownerScope}/renderer`,
apiName: "renderer",
},
{
displayName: `${ownerScope}/chat`,
apiName: "chat",
},
];
const token =
process.env.PACKAGE_ADMIN_TOKEN || process.env.FALLBACK_GITHUB_TOKEN;
if (!token) {
throw new Error("Missing token for package deletion. Provide SEMOSS_GITHUB_PAT or allow github.token.");
}
const sanitize = (value) =>
value
.toLowerCase()
.replace(/[^a-z0-9-]+/g, "-")
.replace(/^-+|-+$/g, "")
.replace(/--+/g, "-") || "snapshot";
const branch = sanitize(process.env.SOURCE_BRANCH || "dev");
const channel = sanitize(process.env.PUBLISH_CHANNEL || "preview");
const dryRun = (process.env.DRY_RUN || "true") === "true";
const versionFragment = `${channel}.${branch}.`;
const headers = {
Authorization: `Bearer ${token}`,
Accept: "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
};
const getOwnerType = async () => {
const response = await fetch(`https://api.github.com/users/${owner}`, {
headers,
});
if (!response.ok) {
throw new Error(`Failed to resolve owner type for ${owner}: ${response.status}`);
}
const payload = await response.json();
return payload.type;
};
const ownerType = await getOwnerType();
const packageBaseUrl =
ownerType === "Organization"
? `https://api.github.com/orgs/${owner}/packages/npm`
: `https://api.github.com/users/${owner}/packages/npm`;
const listVersions = async ({ displayName, apiName }) => {
const versions = [];
for (let page = 1; page <= 10; page += 1) {
const response = await fetch(
`${packageBaseUrl}/${encodeURIComponent(apiName)}/versions?per_page=100&page=${page}`,
{ headers },
);
if (response.status === 404) {
console.log(`Package not found, skipping: ${displayName}`);
return [];
}
if (!response.ok) {
throw new Error(
`Failed to list versions for ${displayName}: ${response.status}`,
);
}
const payload = await response.json();
versions.push(...payload);
if (payload.length < 100) {
break;
}
}
return versions;
};
const deleteVersion = async ({ displayName, apiName }, versionId) => {
const response = await fetch(
`${packageBaseUrl}/${encodeURIComponent(apiName)}/versions/${versionId}`,
{
method: "DELETE",
headers,
},
);
if (!response.ok) {
throw new Error(
`Failed to delete ${displayName} version id ${versionId}: ${response.status}`,
);
}
};
const matches = [];
for (const pkg of packages) {
const versions = await listVersions(pkg);
const matchedVersions = versions
.filter((version) => version.name.includes(versionFragment))
.map((version) => ({
packageName: pkg.displayName,
packageApiName: pkg.apiName,
versionId: version.id,
versionName: version.name,
}));
matches.push(...matchedVersions);
}
if (matches.length === 0) {
console.log(`No package versions found matching ${versionFragment}`);
process.exit(0);
}
console.log(JSON.stringify({ dryRun, matches }, null, 2));
if (dryRun) {
process.exit(0);
}
for (const match of matches) {
console.log(`Deleting ${match.packageName}@${match.versionName}`);
await deleteVersion(
{
displayName: match.packageName,
apiName: match.packageApiName,
},
match.versionId,
);
}
NODE