-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgetAllRepoEvents.js
More file actions
51 lines (48 loc) · 1.32 KB
/
getAllRepoEvents.js
File metadata and controls
51 lines (48 loc) · 1.32 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
const getGitHubUsername = require('./getGitHubUsername');
const axios = require('axios');
require('dotenv').config();
async function getEvents(nextUrl, results, owner, repo, username) {
const response = await axios.get(
nextUrl || `https://api.github.com/repos/${owner}/${repo}/events`,
{
headers: {
Accept: 'application/vnd.github+json',
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
'X-GitHub-Api-Version': '2022-11-28',
},
}
);
console.log(response.data);
const myEvents = response.data.filter(
(event) => event.actor.login === username
);
results.push(...myEvents);
const next = response.headers.link
? response.headers.link
.split(', ')
.find((link) => link.includes('rel="next"'))
: null;
const nextUrl2 = next ? next.match(/<(.+)>/)[1] : null;
return { nextUrl: nextUrl2, results };
}
async function getAllRepoEventsByMe(owner, repo) {
const username = await getGitHubUsername();
let nextUrl = null;
let results = [];
do {
try {
const { nextUrl: next } = await getEvents(
nextUrl,
results,
owner,
repo,
username
);
nextUrl = next;
} catch (error) {
console.error(error);
}
} while (nextUrl);
return results;
}
module.exports = getAllRepoEventsByMe;