This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (72 loc) · 2.27 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
const { getInput, setFailed, setOutput } = require("@actions/core");
const github = require("@actions/github");
const { default: fetch } = require("node-fetch");
const job = async () => {
try {
const PAT = getInput('pat')
const { owner, repo } = github.context.repo
const headers = { Authorization: `token ${PAT}` }
const BASE_URI = `https://api.github.com/repos/${owner}/${repo}`
let response = await fetch(`${BASE_URI}/contents/hits.json`, {headers})
if (response.status === 403) {
throw new Error(`${response.status}: ${response.statusText}`)
}
const initialDate = new Date().toISOString().split("T")[0]
const defaultObject = {
Total: 0,
Unique: 0,
initialDate
}
let { content, sha } = await response.json()
if (!content) {
content = {
views: {...defaultObject},
clones: {...defaultObject}
}
}
else {
content = JSON.parse(Buffer.from(content, 'base64').toString('utf8'))
}
await Promise.allSettled(
["views", "clones"].map(async type => {
let result = (await (await fetch(`${BASE_URI}/traffic/${type}`, {headers})).json())[type]
result.forEach(({ count, uniques, timestamp }) => {
content[type][timestamp.split("T")[0]] = {
Total: count,
Unique: uniques
}
})
content[type].Total = 0
content[type].Unique = 0
Object.values(content[type]).forEach(v => {
if(!!v.Total && !!v.Unique) {
content[type].Total += v.Total
content[type].Unique += v.Unique
}
})
})
)
let body = {
message: `Update hits.json on ${new Date().toISOString().split("T")[0]}`,
content: Buffer.from(JSON.stringify(content)).toString("base64"),
}
if (!!sha) {
body.sha = sha
}
response = await fetch(`${BASE_URI}/contents/hits.json`, {
method: "PUT",
headers: {
...headers,
"accept": "application/vnd.github.v3+json"
},
body: JSON.stringify(body)
})
if (response.status > 400) {
throw new Error(`${response.status}: ${response.statusText}`)
}
setOutput("status", `${response.status}: ${response.statusText}`)
} catch (error) {
setFailed(error.message)
}
}
job()