-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathgithub.js
More file actions
executable file
·135 lines (125 loc) · 4.67 KB
/
github.js
File metadata and controls
executable file
·135 lines (125 loc) · 4.67 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
133
134
135
#!/usr/bin/env node
const axios = require('axios');
const allSettled = require('promise.allsettled');
const rateLimit = require('axios-rate-limit');
const isBefore = require('date-fns/isBefore');
const parseISO = require('date-fns/parseISO');
const formatDistanceToNow = require('date-fns/formatDistanceToNow')
const ora = require('ora');
const {errorLog} = require('./errors');
const {getThemeKey, getRepoName} = require('./utils');
const {updateFrontmatter} = require('./markdown')
const config = require('./config');
if (!process.env.GITHUB_TOKEN) {
throw new Error(
'Cannot access Github API - environment variable "GITHUB_TOKEN" is missing'
)
}
const token = process.env.GITHUB_TOKEN;
const axiosLimit = rateLimit(axios.create(), {maxRequests: 2, perMilliseconds: 200})
const spinner = ora('Loading')
const fetchRepoData = async (frontmatter) => {
const themeKey = getThemeKey(frontmatter.github)
const repoName = getRepoName(frontmatter.github)
try {
const res = await axiosLimit.get(
`https://api.github.com/repos/${repoName}`,
{
headers: {
Authorization: `Token ${token}`,
},
})
spinner.text = `${frontmatter.file} => ${res.data.html_url} - ${res.status}`
const lastCommit = await fetchBranchData(repoName, res.data.default_branch);
updateFrontmatter(frontmatter.file, {
stale: isBefore(parseISO(lastCommit), config.staleBeforeDate)
})
if (frontmatter.disabled) {
updateFrontmatter(frontmatter.file, {
disabled: false,
disabled_reason: ""
})
}
return {
theme_key: themeKey,
file: frontmatter.file,
name: res.data.name,
title: frontmatter.title,
github_username: res.data.owner.login,
repo: res.data.full_name,
branch: res.data.default_branch,
default_branch: res.data.default_branch,
github_url: res.data.html_url,
demo_url: frontmatter.demo,
stars: res.data.stargazers_count,
forks: res.data.forks_count,
open_issues: res.data.open_issues_count,
last_commit: lastCommit,
created_at: res.data.created_at,
description: res.data.description,
images: {
hires: `https://www.jamstackthemes.dev/capture/${themeKey}.png`,
thumbnail: `https://www.jamstackthemes.dev/images/theme/thumbnail/${themeKey}.jpg`,
screenshot: `https://www.jamstackthemes.dev/images/theme/thumbnail/2x/${themeKey}-2x.jpg`
}
}
} catch (err) {
const status = err.response?.status;
let error = `Github repo not found, status: ${status}`;
if (status === 404) {
updateFrontmatter(frontmatter.file, {
disabled: true,
disabled_reason: error
})
}
spinner.text = `${frontmatter.file} => ${error}`
errorLog.push({
theme_key: themeKey,
file: frontmatter.file,
repoUrl: frontmatter.github,
error
})
throw err
}
}
const fetchBranchData = (repo, branch) => {
return axiosLimit.get(
`https://api.github.com/repos/${repo}/branches/${branch}`,
{
headers: {
Authorization: `Token ${token}`,
},
}).then((res) => {
const lastCommit = res.data.commit.commit.author.date;
const lastCommitToNow = formatDistanceToNow(parseISO(lastCommit));
spinner.text = `${repo} => last commit to branch '${branch}' ${lastCommitToNow}`
return lastCommit;
}).catch((err) => {
throw err
});
}
const generateGithubData = async (markdownData, themesJsonData) => {
spinner.start("Fetching Github Data");
const initalThemes = config.themesJsonData;
const update = await allSettled(markdownData.map(frontmatter => fetchRepoData(frontmatter)))
const updatedThemesArray = update.filter(res => res.status === 'fulfilled').map(res => res.value)
const mergedThemesMap = updatedThemesArray.reduce((accumulator, theme) => {
if (theme === undefined) {
// TODO find root cause
console.log("undefined theme detected");
return {
...accumulator
};
}
const themeKey = getThemeKey(theme.github_url)
return {
...accumulator,
[themeKey]: theme
};
}, initalThemes);
spinner.succeed("Success - Fetching Github Data");
return mergedThemesMap
}
module.exports = {
generateGithubData
}