forked from wdw21/songbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistChanges.js
More file actions
198 lines (176 loc) · 6.08 KB
/
Copy pathlistChanges.js
File metadata and controls
198 lines (176 loc) · 6.08 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
import {newUserOctokit, htmlPrefix, htmlSuffix, HandleError} from './common.js'
import util from "util";
const SONGEDITOR_BRANCH_REGEXP=/^se-.*/g;
export async function cleanupChanges(req, res) {
const {refs, diffs, user, octokit} = await getRefs(req, res);
// Let's delete empty & merged branches.
for (let i = 0; i < refs.length; ++i) {
let branch = refs[i];
if (branch.name.match(SONGEDITOR_BRANCH_REGEXP)) {
const diff = await diffs.get(branch.name);
if (diff.data.files.length === 0) {
octokit.rest.git.deleteRef({owner: user, repo: 'songbook', "ref": "heads/" + branch.name}).then();
continue;
}
if (branch.associatedPullRequests.edges.length > 0) {
let merged = true;
for (const edge of branch.associatedPullRequests.edges) {
if (!edge.node.closed || !edge.node.merged) {
merged = false;
}
}
if (merged) {
octokit.rest.git.deleteRef({owner: user, repo: 'songbook', "ref": "heads/" + branch.name}).then();
}
}
}
}
res.redirect(`/users/${user}/changes`);
}
async function getRefs(req, res) {
const {octokit, user, mygraphql} = await newUserOctokit(req, res);
if (!octokit) {
return null;
}
let r = await mygraphql(`query ($user:String!, $repo:String!){
repository(owner:$user,name:$repo) {
url
parent {
id
name
owner {
id
login
}
}
refs(last: 100, refPrefix:"refs/heads/", orderBy: {field:TAG_COMMIT_DATE, direction:DESC}) {
nodes {
id
name
associatedPullRequests(last:5) {
edges {
node {
id
title
number
url
closed
closedAt
changedFiles
merged
}
}
}
target {
... on Commit {
changedFilesIfAvailable,
commitSHA: oid
committedDate,
commitUrl,
url,
history(first: 2) {
nodes {
id
oid
commitUrl
messageHeadline
committedDate
}
}
}
}
}
}
}
}`,
{
user: user,
repo: "songbook"
});
const diffs = new Map();
const refs = r.repository.refs.nodes;
console.log("Refs size before sort", refs.length);
refs.sort((a, b) => {
if (!a || !a.target || !a.target.committedDate) {
return 1;
}
if (!b || !b.target || !a.target.committedDate) {
return -1;
}
return -a.target.committedDate.localeCompare(b.target.committedDate)
});
console.log("Refs size after sort", refs.length);
for (let i = 0; i < refs.length; ++i) {
let branch = refs[i];
console.log("Considering: " + branch.name, i, branch.name.match(SONGEDITOR_BRANCH_REGEXP));
if (branch.name.match(SONGEDITOR_BRANCH_REGEXP)) {
console.log("Processing: " + branch.name, i);
diffs.set(branch.name, octokit.rest.repos.compareCommitsWithBasehead({
owner: 'wdw21',
repo: 'songbook',
basehead: `main...${user}:songbook:${branch.name}`
}));
}
}
return {refs, diffs, user, octokit, r};
}
export async function listChanges(req, res) {
try {
const {refs, diffs, user, r} = await getRefs(req, res);
console.log('2',refs);
htmlPrefix(res);
res.write(`
<h2>Rozpoczęte edycje</h2>
<table>
<tr><th>Plik</th> <th>Ostatnia zmiana</th> <th>Akcje</th> <th>Status recenzji</th> <th>Zaawansowane</th> </tr>
`);
for (let i = 0; i < refs.length; ++i) {
let branch = refs[i];
if (branch.name.match(SONGEDITOR_BRANCH_REGEXP)) {
const diff = await diffs.get(branch.name);
if (!diff || diff.data.files.length < 1) {
continue;
}
res.write(`<td>${diff.data.files[0].status === 'added' ? 'Nowy:' : ''} ${diff.data.files[0].filename.replaceAll("songs/", "")}</td>`)
res.write(`<td>${new Date(branch.target.committedDate).toLocaleString("pl-PL")}</td>`)
res.write(`<td>
<a href="/users/${user}/changes/${branch.name}:edit">[Edytuj]</a>
<button onclick="deleteBranch('${user}','${branch.name}')">Usuń</button></td>`);
res.write(`<td>`);
if (branch.associatedPullRequests.edges.length > 0) {
let pr = branch.associatedPullRequests.edges[0].node;
res.write(`<a href="${pr.url}">[W recenzji]</a>`);
} else {
res.write(`<a href="/users/${user}/changes/${branch.name}:publish">[Wyślij do recenzji]</a>`);
}
res.write(`</td>`);
res.write(`<td>
${branch.name}<br/>
<a href="${r.repository.url}/tree/${branch.name}">[branch]</a><br/>
<a href="${branch.target.commitUrl}">[commit]</a>
</td>`);
res.write(`</tr>`);
}
}
res.write(`
</table>
<a id="newChange" href="/users/${user}/changes:new"><span class="material-icons">add_circle</span>[Nowa zmiana]</a>
<form method="post" action="/users/${user}/changes:cleanup">
<input type="submit" value="Usuń puste edycje"></input>
</form>
<details>
<summary>[Magia pod spodem]</summary>
<pre>${util.inspect(r, false, null, false)}</pre>`);
for (let [key, value] of diffs) {
res.write(`<hr/><h3>diff: ${key}</h3><pre>${util.inspect(await value, false, null, false)}</pre>\n`);
}
res.write(`
</details>`);
} catch (e) {
HandleError(e, res);
} finally {
if (!res.responsesSent) {
htmlSuffix(res);
}
}
}