forked from danielweller-swp/gha-remove-artifacts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
136 lines (115 loc) · 3.38 KB
/
index.js
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
const core = require("@actions/core");
const { Octokit } = require("@octokit/action");
const { throttling } = require("@octokit/plugin-throttling");
const moment = require("moment");
const devEnv = process.env.NODE_ENV === "dev";
const inputKeys = {
AGE: devEnv ? "AGE" : "age",
};
if (devEnv) {
// eslint-disable-next-line global-require, import/no-extraneous-dependencies
require("dotenv-safe").config();
}
function readInput(key, isRequired = false) {
if (devEnv) {
return process.env[key];
}
return core.getInput(key, { required: isRequired });
}
function getConfig() {
const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/");
const [age, units] = readInput(inputKeys.AGE, true).split(" ");
const maxAge = moment().subtract(age, units);
console.log(
"Maximum artifact age:",
age,
units,
"( created before",
maxAge.format(),
")"
);
return {
repo: {
owner,
repo,
},
pagination: {
perPage: 100,
},
maxAge: moment().subtract(age, units),
retriesEnabled: true,
};
}
const ThrottledOctokit = Octokit.plugin(throttling);
async function run() {
const config = getConfig();
const octokit = new ThrottledOctokit({
throttle: {
onRateLimit: (retryAfter, options) => {
console.error(
`Request quota exhausted for request ${options.method} ${options.url}, number of total global retries: ${options.request.retryCount}`
);
console.log(`Retrying after ${retryAfter} seconds!`);
return config.retriesEnabled;
},
onAbuseLimit: (retryAfter, options) => {
console.error(
`Abuse detected for request ${options.method} ${options.url}, retry count: ${options.request.retryCount}`
);
console.log(`Retrying after ${retryAfter} seconds!`);
return config.retriesEnabled;
},
},
});
const artifactRequest = octokit.actions.listArtifactsForRepo.endpoint.merge({
...config.repo,
per_page: config.pagination.perPage,
});
const artifacts = await octokit.paginate(
artifactRequest,
// eslint-disable-next-line no-unused-vars
({ data }, done) => {
console.log("Received a page");
return data;
}
);
console.log(`Considering ${artifacts.length} artifacts...`);
const artifactPromises = artifacts
.filter(artifact => {
const createdAt = moment(artifact.created_at);
const toBeDeleted = createdAt.isBefore(config.maxAge);
if (!toBeDeleted) {
console.log(
` Keeping ${artifact.name} (id ${artifact.id}) from ${artifact.created_at}`
);
}
return toBeDeleted;
})
.map(artifact => {
console.log(
`About to remove id: ${artifact.id}, name: ${artifact.name}).`
);
return octokit.actions
.deleteArtifact({
...config.repo,
artifact_id: artifact.id,
})
.then(() => {
console.log(
`Successfully removed artifact (id: ${artifact.id}, name: ${artifact.name}).`
);
})
.catch(e => {
console.log(
`Error on deleting (id: ${artifact.id}, name: ${artifact.name}): ${e}.`
);
throw e;
});
});
return Promise.all(artifactPromises).then(artifactDeletePromises =>
Promise.all([].concat(...artifactDeletePromises))
);
}
run().catch(err => {
core.setFailed(err.toString());
});