Skip to content

Commit b56ae93

Browse files
Copilotneilime
andcommitted
Refactor prune-pull-requests-image-tags to use clean-images action
Co-authored-by: neilime <314088+neilime@users.noreply.github.com>
1 parent 23ff483 commit b56ae93

2 files changed

Lines changed: 26 additions & 69 deletions

File tree

actions/docker/prune-pull-requests-image-tags/action.yml

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ outputs:
1414
```json
1515
["1.0.0", "1.0.1"]
1616
```
17-
value: ${{ steps.delete-image-tags.outputs.deleted-image-tags }}
17+
value: ${{ steps.clean-images.outputs.deleted-package-versions }}
1818

1919
inputs:
2020
image:
@@ -41,15 +41,10 @@ runs:
4141
with:
4242
image: ${{ inputs.image }}
4343

44-
- shell: bash
45-
# FIXME: workaround until will be merged: https://github.com/actions/runner/pull/1684
46-
run: |
47-
rm -fr ./self-actions
48-
4944
- id: is-organization-or-user
5045
uses: hoverkraft-tech/ci-github-common/actions/repository-owner-is-organization@b7dd413209df265bef8d7eb0efb117eaabc684c4 # 0.27.0
5146

52-
- id: delete-image-tags
47+
- id: get-tags-to-delete
5348
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
5449
with:
5550
github-token: ${{ inputs.github-token }}
@@ -80,7 +75,7 @@ runs:
8075
const isOrganization = `${{ steps.is-organization-or-user.outputs.is-organization }}` === 'true';
8176
8277
const script = require(`${{ github.action_path }}/index.js`)
83-
const deletedTags = await script({
78+
const tagsToDelete = await script({
8479
github,
8580
context,
8681
core,
@@ -89,4 +84,20 @@ runs:
8984
isOrganization,
9085
});
9186
92-
core.setOutput('deleted-image-tags', JSON.stringify(deletedTags));
87+
// Convert tags to comma-separated string for delete-tags input
88+
const tagsString = tagsToDelete.join(',');
89+
core.setOutput('tags-to-delete', tagsString);
90+
core.info(`Tags to delete: ${tagsString}`);
91+
92+
- id: clean-images
93+
uses: ./self-actions/docker/clean-images
94+
if: steps.get-tags-to-delete.outputs.tags-to-delete != ''
95+
with:
96+
token: ${{ inputs.github-token }}
97+
package: ${{ steps.image-name.outputs.image-name }}
98+
delete-tags: ${{ steps.get-tags-to-delete.outputs.tags-to-delete }}
99+
100+
- shell: bash
101+
# FIXME: workaround until will be merged: https://github.com/actions/runner/pull/1684
102+
run: |
103+
rm -fr ./self-actions

actions/docker/prune-pull-requests-image-tags/index.js

Lines changed: 6 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = async ({
1010
const packageName = imageName.replace(`${repositoryOwner}/`, "");
1111

1212
let currentPage = 1;
13-
const allDeletedTags = [];
13+
const allTagsToDelete = [];
1414
for (;;) {
1515
const data = await getAllPackageVersions({
1616
github,
@@ -26,23 +26,21 @@ module.exports = async ({
2626
break;
2727
}
2828

29-
const deletedTags = await Promise.all(
29+
const tagsToDelete = await Promise.all(
3030
data.map((packageVersion) =>
31-
processPackageVersion({
31+
getTagsToDeleteFromPackageVersion({
3232
github,
3333
context,
3434
core,
35-
isOrganization,
36-
packageName,
3735
pullRequestTagFilter,
3836
packageVersion,
3937
}),
4038
),
4139
);
42-
allDeletedTags.push(...deletedTags.flat());
40+
allTagsToDelete.push(...tagsToDelete.flat());
4341
}
4442

45-
return [...new Set(allDeletedTags)];
43+
return [...new Set(allTagsToDelete)];
4644
};
4745

4846
async function getAllPackageVersions({
@@ -81,12 +79,10 @@ async function getAllPackageVersions({
8179
return data;
8280
}
8381

84-
async function processPackageVersion({
82+
async function getTagsToDeleteFromPackageVersion({
8583
github,
8684
context,
8785
core,
88-
packageName,
89-
isOrganization,
9086
pullRequestTagFilter,
9187
packageVersion,
9288
}) {
@@ -127,15 +123,6 @@ async function processPackageVersion({
127123
return [];
128124
}
129125

130-
await deletePackageVersion({
131-
github,
132-
core,
133-
context,
134-
packageName,
135-
isOrganization,
136-
packageVersion,
137-
});
138-
139126
return tags;
140127
}
141128

@@ -171,47 +158,6 @@ async function isPullRequestClosed({ github, context, pullRequestNumber }) {
171158
return closedPullRequests.get(pullRequestNumber);
172159
}
173160

174-
async function deletePackageVersion({
175-
github,
176-
core,
177-
context,
178-
packageName,
179-
isOrganization,
180-
packageVersion,
181-
}) {
182-
try {
183-
if (isOrganization) {
184-
return await rateLimitRetryCall(
185-
github.rest.packages.deletePackageVersionForOrg,
186-
{
187-
package_type: "container",
188-
package_name: packageName,
189-
org: context.repo.owner,
190-
package_version_id: packageVersion.id,
191-
},
192-
);
193-
}
194-
195-
return await rateLimitRetryCall(
196-
github.rest.packages.deletePackageVersionForUser,
197-
{
198-
package_type: "container",
199-
package_name: packageName,
200-
username: context.repo.owner,
201-
package_version_id: packageVersion.id,
202-
},
203-
);
204-
} catch (error) {
205-
if (error.status === 404) {
206-
core.warning(
207-
`Package version ${packageVersion.name} (${packageVersion.id}) cannot be deleted as it is not found`,
208-
);
209-
} else {
210-
throw error;
211-
}
212-
}
213-
}
214-
215161
async function rateLimitRetryCall(fn, ...args) {
216162
try {
217163
return await fn(...args);

0 commit comments

Comments
 (0)