-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
62 lines (56 loc) · 1.73 KB
/
script.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
// PARAMETERS
const TOKEN = ""; // GitHub Token `repo_deployments` authorized
const REPO = "your-repo"; // e.g. "my-repository"
const USER_OR_ORG = "your-name"; // e.g. "your-name"
// GLOBAL VARS
const URL = `https://api.github.com/repos/${USER_OR_ORG}/${REPO}/deployments`;
const AUTH_HEADER = `token ${TOKEN}`;
// UTILITY FUNCTIONS
const getAllDeployments = () =>
fetch(`${URL}`, { headers: { authorization: AUTH_HEADER } }).then(val => val.json());
const makeDeploymentInactive = id =>
fetch(`${URL}/${id}/statuses`, {
method: "POST",
body: JSON.stringify({ state: "inactive" }),
headers: {
"Content-Type": "application/json",
Accept: "application/vnd.github.ant-man-preview+json",
authorization: AUTH_HEADER
}
}).then(() => id);
const deleteDeployment = id =>
fetch(`${URL}/${id}`, {
method: "DELETE",
headers: { authorization: AUTH_HEADER }
}).then(() => id);
// MAIN
getAllDeployments()
.catch(console.error)
.then(res => {
console.log(`${res.length} enviroments found`);
return res;
})
.then(val => val.map(({ id }) => id))
.then(ids => Promise.all(ids.map(id => makeDeploymentInactive(id))))
.then(res => {
console.log(`${res.length} enviroments marked as "inactive"`);
return res;
})
.then(ids => Promise.all(ids.map(id => deleteDeployment(id))))
.then(res => {
console.log(`${res.length} enviroments deleted`);
return res;
})
.then(finalResult => {
const appDiv = document.getElementById("app");
appDiv.innerHTML = `
<h1>CLEANUP RESULT</h1>
<br>
Removed enviroments: ${finalResult.length}
<br>
<br>Ids:<br>
${JSON.stringify(finalResult)}
<br><br><br><br><br><br>
<p>(Open up the console)</p>
`;
});