-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgithubCommentParser.js
89 lines (78 loc) · 2.24 KB
/
githubCommentParser.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
function loadIssues(options) {
const owner = options.owner;
const repo = options.repo;
const issue = options.issue;
const render = options.render;
const headers = {
'Content-Type': 'application/json',
Accept: 'application/vnd.github.v3.html+json'
};
const data = {
error: null,
comments: [],
};
function parseLinkHeader(link) {
var entries = link.split(',');
var links = {};
for (var i in entries) {
var entry = entries[i];
var l = {};
l.name = entry.match(/rel=\"([^\"]*)/)[1];
l.url = entry.match(/<([^>]*)/)[1];
l.page = parseInt(entry.match(/page=(\d+).*$/)[1], 10);
links[l.name] = l;
}
return links;
}
function normalizeComment(item) {
return {
id: item.id,
url: item.html_url,
author: {
login: item.user.login,
avatarUrl: item.user.avatar_url,
url: item.user.html_url
},
body: item.body_html,
createdAt: item.created_at,
updatedAt: item.updated_at
};
}
function getIssueComments(page) {
const url = `https://api.github.com/repos/${owner}/${repo}/issues/${issue}/comments?page=${page || 1}`;
return fetch(url, { headers: headers })
.then((response, err) => {
if (err || !response.ok) {
if (err) {
data.error = `${err}`;
} else if (response.status === 401) {
data.error = 'Unauthorized.';
} else if (response.status === 403) {
data.error = 'Rate limit exceeded.';
} else {
data.error = `HTTP error ${response.status}`;
}
return
}
const link = response.headers.get('Link');
let nextPage = null;
if (link) {
linkData = parseLinkHeader(link);
nextPage = linkData.page;
}
return response
.json()
.then((items) => {
const newComments = items.map(normalizeComment);
data.comments = data.comments.concat(newComments);
if (nextPage) {
return getIssueComments(nextPage);
}
})
.catch((error) => {
data.error = `${error}`;
});
});
}
getIssueComments().then(() => render(data));
}