-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwiki-helper.js
68 lines (57 loc) · 1.73 KB
/
wiki-helper.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
'use strict';
import Debug from 'debug';
const debug = new Debug('mozilla-github-watcher:Wiki');
const BASE_URL = 'https://wiki.mozilla.org/api.php';
const FIELDS = 'title|timestamp|user';
const LIST = 'recentchanges';
const ACTION = 'query';
const LIMIT = 500;
const FORMAT = 'json';
const LAST_EDITS_URL = `${BASE_URL}?action=${ACTION}&list=${LIST}&rcprop=${FIELDS}&rclimit=${LIMIT}&format=${FORMAT}`;
class WikiHelper {
constructor() {
this.apiUrl = LAST_EDITS_URL;
}
/**
* Queries the Mozilla Wiki API for the latest 500 edits.
*
* @return {Promise} Promise which resolves with all repositories found
*/
getLatestEdits() {
debug('getting Wiki edits (currently disabled)..', this.apiUrl);
return Promise.resolve([]);
// Wiki fetching is currently disabled due to GitHub Actions
// not being able to fetch from it. We are getting back a 403
// error in the pipeline, while locally it works.
return new Promise((resolve) => {
fetch(this.apiUrl).then((res) => res.json())
.then((data) => {
const edits = this.groupEdits(data.query.recentchanges);
resolve(edits);
});
});
}
/**
* Groups edits to get rid of duplication
*
* @param {Array} edits list of edits to group
* @return {Array} array of grouped edits
*/
groupEdits(edits) {
debug('grouping edits');
const groupedEdits = [];
edits.forEach((change) => {
let alreadyIncluded = false;
groupedEdits.forEach((groupedEdit) => {
if (groupedEdit.title === change.title) {
alreadyIncluded = true;
}
});
if (!alreadyIncluded) {
groupedEdits.push(change);
}
});
return groupedEdits;
}
}
export default WikiHelper;