-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
106 lines (97 loc) · 3.82 KB
/
index.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
const core = require("@actions/core");
const { Octokit } = require('@octokit/core')
const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN });
(async () => {
try {
const repoCount = parseInt(core.getInput('repoCount'))
const repoPerRow = parseInt(core.getInput('reposPerRow'))
const imageSize = parseInt(core.getInput('imageSize'))
const ref = core.getInput('ref')
const username = process.env.GITHUB_REPOSITORY.split("/")[0]
const repo = process.env.GITHUB_REPOSITORY.split("/")[1]
const getReadme = await octokit.request('GET /repos/{owner}/{repo}/contents/{path}', {
owner: username,
repo: repo,
path: core.getInput('path'),
}).catch(e => {
console.error("Failed: ", e)
core.setFailed("Failed: ", e.message)
})
const sha = getReadme.data.sha
let recentReposHaveImage = []
let recentRepos = new Set()
/** GitHub Activity pagination is limited at 100 records x 3 pages */
for (let i = 0; recentRepos.size < repoCount && i < 3; i++) {
console.log(i)
const getActivity = await octokit.request(`GET /users/{username}/events/public?per_page=100&page=${i}`, {
username: username,
}).catch(e => {
console.error("Failed: ", e)
core.setFailed("Failed: ", e.message)
})
console.log(getActivity)
console.log(getActivity.data)
for (const value of getActivity.data) {
console.log(value)
let activityRepo = value.repo.name
if (value.type === "ForkEvent") activityRepo = value.payload.forkee.full_name
if (!JSON.parse(core.getInput('excludeActivity')).includes(value.type) && !JSON.parse(core.getInput('excludeRepo')).includes(activityRepo)) {
recentRepos.add(activityRepo)
}
if (recentRepos.size >= repoCount) break
}
}
for (const repo of recentRepos) {
await octokit.request('GET /repos/{owner}/{repo}/contents/{path}', {
owner: repo.split("/")[0],
repo: repo.split("/")[1],
path: 'DISPLAY.jpg',
}).then(() => {
recentReposHaveImage.push(true)
}).catch(e => {
recentReposHaveImage.push(false)
})
}
const data = core.getInput("customReadmeFile").replace(/\${\w{0,}}/g, (match) => {
switch (match) {
case "${repoTable}": return chunkArray(Array.from(recentRepos), repoPerRow).map((value, row) => {
return `|${value.map(value => ` [${value}](https://github.com/${value}) |`)}
|${value.map(() => ` :-: |`)}
|${value.map((value, col) => ` <a href="https://github.com/${value}"><img src="https://github.com/${recentReposHaveImage[row * repoPerRow + col] ? value : `${username}/${repo}`}/raw/${ref}/DISPLAY.jpg" alt="${value}" title="${value}" width="${imageSize}" height="${imageSize}"></a> |`
)}\n\n`
}).toString().replace(/,/g, "");
case "${header}": return core.getInput('header')
case "${subhead}": return core.getInput('subhead')
case "${footer}": return core.getInput('footer')
default:
console.error(`${match} is not recognised`)
return ""
}
})
await octokit.request('PUT /repos/{owner}/{repo}/contents/{path}', {
owner: username,
repo: repo,
path: core.getInput('path'),
message: '(Automated) Update README.md',
content: Buffer.from(data, "utf8").toString('base64'),
sha: sha,
}).then(() => {
core.setOutput("repositories", Array.from(recentRepos))
}).catch((e) => {
console.error("Failed: ", e)
core.setFailed("Failed: ", e.message)
})
} catch (e) {
console.error("Failed: ", e)
core.setFailed("Failed: ", e.message)
}
})()
const chunkArray = (array, size) => {
let chunked = []
let index = 0
while (index < array.length) {
chunked.push(array.slice(index, size + index))
index += size
}
return chunked
}