forked from achasveachas/GitHub-cleaner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·155 lines (142 loc) · 3.95 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
let repoList = [];
function handleGetLabs() {
if (getToken() && getUsername()) {
getRepoList(1);
addStatus("Getting Repos");
} else {
reportErrors(
`Please provide your GitHub valid token and username.<br /> See <a href="https://github.com/achasveachas/GitHub-cleaner/blob/master/README.md">HERE</a> for details.`
);
}
}
function getRepoList(page) {
fetch(`https://api.github.com/user/repos?page=${page}&per_page=100`, {
method: "get",
headers: {
Authorization: `token ${getToken()}`
}
})
.then(res => res.json())
.then(json => {
if (json.length < 1) {
filterRepoList();
} else {
repoList = repoList.concat(json);
console.log("Retrieved " + repoList.length + "repos.");
getRepoList(page + 1);
}
});
}
function filterRepoList() {
const SEARCH_TERM = document.getElementById("search").value;
addStatus("Filtering Repos");
repoList = repoList.filter(
repo =>
repo.name.includes(SEARCH_TERM) && repo.owner.login === getUsername()
);
addStatus(`${repoList.length} Repos found`);
document.getElementById("make_private").disabled = false;
document.getElementById("transfer_repos").disabled = false;
}
function handlePrivatizeRepos() {
if (
confirm(
`Are you sure you want to mark these ${repoList.length} repos private?`
)
) {
privatize();
}
}
function privatize() {
for (var i = 0; i < repoList.length; i++) {
var repo = repoList[i];
var url = repo.url;
var name = repo.name;
var requestBody = {
name: name,
private: true
};
fetch(url, {
method: "PATCH",
headers: {
Authorization: `token ${getToken()}`
},
body: JSON.stringify(requestBody)
})
.then(
res =>
(document.getElementById("progress").innerHTML = `Lab ${i + 1}/${
repoList.length
} has been marked private.`)
)
.catch(er => reportErrors(er));
}
addStatus("All labs have been marked private!");
}
function handleTransferRepos() {
const ORG_ID = document.getElementById("organization").value;
if (
confirm(
`Are you sure you want to transfer these ${
repoList.length
} repos to "${ORG_ID}"?`
)
) {
transferRepos(ORG_ID);
}
}
function transferRepos(ORG_ID) {
for (var i = 0; i < repoList.length; i++) {
var repo = repoList[i];
var url = repo.url + "/transfer";
var name = repo.name;
var requestBody = { new_owner: ORG_ID };
fetch(url, {
method: "POST",
headers: {
Accept: "application/vnd.github.nightshade-preview+json",
"Content-Type": "application/vnd.github.nightshade-preview+json",
Authorization: `token ${getToken()}`
},
body: JSON.stringify(requestBody)
})
.then(
res =>
(document.getElementById("progress").innerHTML = `Repo(s) ${i}/${
repoList.length
} has been transfered to ${ORG_ID}.`)
)
.catch(er => reportErrors(er));
}
addStatus("All labs have been transfered!");
}
function addStatus(status) {
var li = document.createElement("li");
li.innerHTML = status;
document.getElementById("progress").append(li);
}
function reportErrors(error) {
document.getElementById("errors").innerHTML = error;
}
function getUsername() {
return document.getElementById("username").value;
}
function getToken() {
// Go to https://github.com/settings/tokens and get a token.
return document.getElementById("token").value;
}
function validateToken() {
const username = getUsername();
const token = getToken();
fetch(`https://api.github.com/applications/${username}/tokens/${token}`)
.then(res => {
if (res.ok) {
document.getElementById("progress").innerHTML = `Your token is valid`;
return true;
} else {
document.getElementById("progress").innerHTML = `Your token is invalid`;
return false;
}
})
.catch(er => reportErrors(er));
}