This repository was archived by the owner on Jan 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (78 loc) · 2.28 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
const github = require("@actions/github");
const core = require("@actions/core");
const Axios = require("axios");
const path = require("path");
const ejs = require("ejs");
const user = core.getInput("username");
const pass = core.getInput("password");
const pat = core.getInput("pat");
try {
if (!user || !pass) throw new Error("Username or password missing");
if (!pat) throw new Error("Github PAT missing");
const kit = github.getOctokit(pat);
fetchToken(user, pass).then(async (token) => {
const data = await fetchData(token);
const rawPoops = data.poops;
const poops = {
max: 0,
};
for (let i = 0; i < rawPoops.length; i++) {
const poop = rawPoops[i];
const hour = new Date(poop.created_at).getHours();
if (!poops[hour]) poops[hour] = 1;
else poops[hour] += 1;
if (!poops.max || poops[hour] > poops.max) {
poops.max = poops[hour];
}
}
const html = await ejs.renderFile(path.join(__dirname, "template.ejs"), { poops });
const original = await kit.rest.repos
.getContent({
...github.context.repo,
path: "poop-metrics.svg",
})
.catch(() => {});
await kit.rest.repos.createOrUpdateFileContents({
...github.context.repo,
path: "poop-metrics.svg",
sha: original ? original.data.sha : undefined,
message: "Update Poop Metrics",
committer: {
name: "github-actions",
email: "actions@github.com",
},
content: Buffer.from(html).toString("base64"),
});
});
} catch (err) {
core.setFailed(err);
}
async function fetchToken(username, password) {
const {
data: { device },
} = await Axios.post("https://api.poopmap.net/api/v1/devices");
await Axios.post(
"https://api.poopmap.net/api/v1/sessions",
{
user: { username, password },
},
{
headers: { Authorization: `Token token=${device.token}` },
}
);
const {
data: { url },
} = await Axios.post(
"https://api.poopmap.net/api/v1/public_links",
{},
{
headers: { Authorization: `Token token=${device.token}` },
}
);
const token = url.split("=").pop();
return token;
}
async function fetchData(token) {
const { data } = await Axios.get(`https://api.poopmap.net/api/v1/public_links/${token}`);
return data;
}