-
Notifications
You must be signed in to change notification settings - Fork 657
Expand file tree
/
Copy pathgithub.js
More file actions
132 lines (116 loc) · 2.85 KB
/
github.js
File metadata and controls
132 lines (116 loc) · 2.85 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
require('dotenv').config();
const CacheAsset = require("@11ty/eleventy-cache-assets");
const fastglob = require("fast-glob");
const graymatter = require("gray-matter");
/*
Sample return data:
{
"data": {
"repository": {
"name": "repo-name",
"stargazers": {
"totalCount": 11
},
"forks": {
"totalCount": 3
},
"issues": {
"totalCount": 9
}
}
}
}
*/
async function githubRequest(user, repo) {
let errorData = {
stars: "",
forks: "",
issues: "",
};
if(process.env.ELEVENTY_ENV == 'dev' || !process.env.GITHUB_READ_TOKEN) {
return errorData;
}
const query = `
query {
repository(owner: "${user}", name: "${repo}") {
name
stargazers {
totalCount
}
forks {
totalCount
}
issues(states:[OPEN]) {
totalCount
}
}
}
`;
const url = `https://api.github.com/graphql?user=${user}&repo=${repo}`;
const fetchOptions = {
method: "POST",
headers: {
"Authorization": `bearer ${process.env.GITHUB_READ_TOKEN}`
},
body: JSON.stringify({ query })
};
const opts = {
duration: "1401m", // 23.5 hours
type: "json",
fetchOptions
};
let req;
try {
req = await CacheAsset(url, opts);
if(req.errors && req.errors.length) {
console.log( "GitHub Data Source Error from API", req.errors );
if(req.errors.filter(e => e.type === "RATE_LIMITED").length > 0) {
throw new Error("Failing the build due to GitHub API rate limiting.");
}
return errorData;
}
return {
stars: req.data.repository.stargazers.totalCount,
forks: req.data.repository.forks.totalCount,
issues: req.data.repository.issues.totalCount,
}
} catch(e) {
console.log( "GitHub Data Source Error", e );
return errorData;
}
}
async function getReposFromMarkdown(glob) {
// Starters
let ssgs = await fastglob(glob, {
caseSensitiveMatch: false
});
let repos = [];
for(let ssg of ssgs) {
let matter = graymatter.read(ssg);
let fullRepo = matter.data.repo;
if(fullRepo) {
let split = fullRepo.split("/");
let user = split[0];
let repo = split[1];
if(!matter.data.repohost || matter.data.repohost === "github") {
if(matter.data.disabled) {
continue;
}
repos.push({ user, repo });
}
}
}
return repos;
}
module.exports = async function() {
let data = {};
let ssgRepos = await getReposFromMarkdown("./src/site/generators/*.md");
for(let entry of ssgRepos) {
data[`${entry.user}/${entry.repo}`] = await githubRequest(entry.user, entry.repo);
}
let cmsRepos = await getReposFromMarkdown("./src/site/headless-cms/*.md");
for(let entry of cmsRepos) {
data[`${entry.user}/${entry.repo}`] = await githubRequest(entry.user, entry.repo);
}
return data;
};