-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
110 lines (93 loc) · 3.11 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
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
{
'use strict';
console.log('init…');
const PER_PAGE = 100;
let startPage = 1;
document.addEventListener('submit', (e) => {
e.preventDefault();
backupRepo(e.target);
});
let allIssues = [];
let fetchIssues = (opt) => {
let issueUrl = `https://api.github.com/repos/${opt.user}/${opt.repo}/issues?per_page=${PER_PAGE}&page=${startPage}&state=all&access_token=${window.ACCESS_TOKEN}`;
window.fetch(issueUrl)
.then((response) => {
if (response.status !== 200) {
console.log('Looks like there was a problem. Status Code: ' + response.status);
return;
}
return response.json();
})
.then((issues) => {
// console.log('issues.length', issues.length, issues);
allIssues = allIssues.concat(issues);
if (issues.length === PER_PAGE) {
startPage += 1;
console.log('+startPage', startPage, allIssues.length);
fetchIssues(fetchIssuesObj);
return false;
} else {
startPage = 1;
return allIssues;
}
console.log(allIssues.length);
})
.then((issues) => {
if (!issues) { return; }
// re-sort from last
let sortFn = (a, b) => b.number - a.number;
issues = issues.sort(sortFn);
window.githubIssues = issues;
// console.log('Backup issues success!', issues);
console.log('Successfully backup issues!');
console.log('all issues:', issues.length);
Promise.all(issues.map((issue, index) => {
if (!issue.comments) { return; }
return window.fetch(issue.comments_url + `?access_token=${ACCESS_TOKEN}&per_page=${PER_PAGE}`)
.then((resp) => resp.text());
}))
.then((comments) => {
issues.forEach((issue, index) => {
if (comments[index]) {
issue.comments_content = comments[index];
}
});
return issues;
})
.then((issues) => {
console.log('final issues', issues);
let date = new Date().toISOString().split(/T/)[0];
let downloadLink;
if (opt.form.getElementsByTagName('a').length) {
downloadLink = opt.form.getElementsByTagName('a')[0];
} else {
downloadLink = document.createElement('a');
opt.form.appendChild(downloadLink);
}
downloadLink.innerHTML = `Download ${opt.repo}-latest.json`;
let file = new window.Blob([JSON.stringify(issues, null, 2)], {type: 'application/json'});
downloadLink.href = window.URL.createObjectURL(file);
downloadLink.onclick = () => {
downloadLink.download = `${opt.repo}-latest.json`;
};
});
})
.catch((err) => {
console.log('Fetch Error :-S', err);
});
};
var fetchIssuesObj;
let backupRepo = ($form) => {
targetForm = $form;
let $input = $form.getElementsByTagName('input')[0];
let inputValue = $input.value.trim();
allIssues = []; // reset
if (!inputValue.match(/[^/]+?\/[^/]+?/)) { return; }
fetchIssuesObj = {
user: inputValue.split('/')[0],
repo: inputValue.split('/')[1],
form: $form
};
fetchIssues(fetchIssuesObj);
};
}