-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-readme.js
169 lines (148 loc) · 6.5 KB
/
update-readme.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const fs = require('fs');
const fetch = require('node-fetch');
const username = 'jcabak';
const accessToken = process.env.GH_TOKEN;
const boldFavorites = true; // bold favoriteRepositories
const includePullRequestLinks = false; // make url to specific pull request
const favoriteRepositories = ['rails', 'microsoft', 'apple', 'home-assistant', 'google', 'raspberry', 'twitter', 'mozilla', 'facebook', 'googlechrome', 'nasa', 'w3c', 'basecamp'];
const ignoredRepositories = ['BinaryWorlds', 'LukasJoswiak'];
const show_open_pull_requests = true;
const show_closed_pull_requests = true;
async function fetchPullRequests() {
try {
const closedPullRequests = await fetchPullRequestsByState('closed');
const openPullRequests = await fetchPullRequestsByState('open');
const closedMarkdownContent = show_closed_pull_requests ? await generateMarkdownTable(closedPullRequests, 'Closed Pull Requests') : '';
const openMarkdownContent = show_open_pull_requests ? await generateMarkdownTable(openPullRequests, 'Open Pull Requests') : '';
const readmeContent = fs.readFileSync('README.md', 'utf8');
const newContent = readmeContent
.replace(/<!-- CLOSED_PULL_REQUESTS_START -->[\s\S]*<!-- CLOSED_PULL_REQUESTS_END -->/, `<!-- CLOSED_PULL_REQUESTS_START -->\n${closedMarkdownContent}\n<!-- CLOSED_PULL_REQUESTS_END -->`)
.replace(/<!-- OPEN_PULL_REQUESTS_START -->[\s\S]*<!-- OPEN_PULL_REQUESTS_END -->/, `<!-- OPEN_PULL_REQUESTS_START -->\n${openMarkdownContent}\n<!-- OPEN_PULL_REQUESTS_END -->`);
fs.writeFileSync('README.md', newContent);
} catch (error) {
console.error('Error fetching pull requests:', error);
}
}
async function fetchPullRequestsByState(state) {
try {
const response = await fetch(`https://api.github.com/search/issues?q=is:pr+is:${state}+author:${username}&per_page=150`, {
headers: {
'Authorization': `Bearer ${accessToken}`
}
});
const data = await response.json();
return data.items;
} catch (error) {
console.error(`Error fetching ${state} pull requests:`, error);
return [];
}
}
async function generateMarkdownTable(pullRequests, title) {
let markdownContent = `| Icon | User | Repository | Stars | Forks | Pull Request |\n|:----|:----|:----|:----|:----|:----|\n`;
for (const pullRequest of pullRequests) {
const repositoryOwnerAvatarUrl = await fetchRepositoryOwnerAvatar(pullRequest.repository_url);
const repositoryOwner = await fetchRepositoryOwner(pullRequest.repository_url);
// Ignore the repository if the owner is in the ignoredRepositories list
if (ignoredRepositories.includes(repositoryOwner)) {
continue;
}
const repositoryUrl = await fetchRepositoryUrl(pullRequest.repository_url);
const repositoryName = await fetchRepositoryName(pullRequest.repository_url);
const repositoryStars = await fetchRepositoryStars(pullRequest.repository_url);
const repositoryForks = await fetchRepositoryForks(pullRequest.repository_url);
const repositoryOwnerUrl = `https://github.com/${repositoryOwner}`;
const pullRequestLink = pullRequest.html_url; // Get the pull request link
const pullRequestColumnContent = includePullRequestLinks ? `[${pullRequest.title}](${pullRequestLink})` : pullRequest.title;
if (boldFavorites && favoriteRepositories.includes(repositoryOwner.toLowerCase())) {
markdownContent += `| <img src="${repositoryOwnerAvatarUrl}" alt="Logo ${repositoryOwner}" width="30" height="30"> | [**${repositoryOwner}**](${repositoryOwnerUrl}) | [**${repositoryName}**](${repositoryUrl}) | **${repositoryStars}** | **${repositoryForks}** | **${pullRequestColumnContent}** |\n`;
} else {
markdownContent += `| <img src="${repositoryOwnerAvatarUrl}" alt="Logo ${repositoryOwner}" width="30" height="30"> | [${repositoryOwner}](${repositoryOwnerUrl}) | [${repositoryName}](${repositoryUrl}) | ${repositoryStars} | ${repositoryForks} | ${pullRequestColumnContent} |\n`;
}
}
return markdownContent;
}
async function fetchRepositoryOwner(repoUrl) {
try {
const response = await fetch(repoUrl, {
headers: {
'Authorization': `Bearer ${accessToken}`
}
});
const data = await response.json();
return data.owner.login;
} catch (error) {
console.error('Error fetching repository owner:', error);
return '';
}
}
async function fetchRepositoryUrl(repoUrl) {
try {
const response = await fetch(repoUrl, {
headers: {
'Authorization': `Bearer ${accessToken}`
}
});
const data = await response.json();
return data.html_url;
} catch (error) {
console.error('Error fetching repository URL:', error);
return '';
}
}
async function fetchRepositoryName(repoUrl) {
try {
const response = await fetch(repoUrl, {
headers: {
'Authorization': `Bearer ${accessToken}`
}
});
const data = await response.json();
return data.name;
} catch (error) {
console.error('Error fetching repository name:', error);
return '';
}
}
async function fetchRepositoryOwnerAvatar(repoUrl) {
try {
const response = await fetch(repoUrl, {
headers: {
'Authorization': `Bearer ${accessToken}`
}
});
const data = await response.json();
return data.owner.avatar_url;
} catch (error) {
console.error('Error fetching repository owner avatar URL:', error);
return '';
}
}
async function fetchRepositoryStars(repoUrl) {
try {
const response = await fetch(repoUrl, {
headers: {
'Authorization': `Bearer ${accessToken}`
}
});
const data = await response.json();
return data.stargazers_count;
} catch (error) {
console.error('Error fetching repository stars:', error);
return 0;
}
}
async function fetchRepositoryForks(repoUrl) {
try {
const response = await fetch(repoUrl, {
headers: {
'Authorization': `Bearer ${accessToken}`
}
});
const data = await response.json();
return data.forks_count;
} catch (error) {
console.error('Error fetching repository forks:', error);
return 0;
}
}
fetchPullRequests();