-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.ts
More file actions
140 lines (123 loc) · 3.36 KB
/
handler.ts
File metadata and controls
140 lines (123 loc) · 3.36 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
136
137
138
139
140
import { APIGatewayProxyHandler } from "aws-lambda";
import axios from "axios";
import debug from "debug";
import * as dayjs from "dayjs";
import * as LocalizedFormat from "dayjs/plugin/localizedFormat";
import { readFileSync } from "fs";
import "source-map-support/register";
dayjs.extend(LocalizedFormat);
const logError = debug("ghas:error");
const GitHubEndpoint = "https://api.github.com/graphql";
interface Emoji {
emoji: string;
name: string;
}
interface Config {
token: string;
emojis: Emoji[];
messages: string[];
busy: boolean;
}
function loadConfig(): Config | null {
try {
let configBytes = readFileSync(".config.json", "utf8");
return JSON.parse(configBytes.toString());
} catch (error) {
logError(error);
return null;
}
}
function getYearProgress() {
const yearStart = dayjs().startOf("year");
const yearEnd = dayjs().endOf("year");
return dayjs().diff(yearStart) / yearEnd.diff(yearStart);
}
function buildProgressBar(progress: number, length: number = 10) {
// ██░░░░░░░░ XX%
let progressLength = Math.round(length * progress);
let trackLength = length - progressLength;
let track = Buffer.concat([
Buffer.alloc(progressLength * 3, "█"),
Buffer.alloc(trackLength * 3, "░"),
]);
progress = 100 * progress;
const precision = progress < 1 ? 2 : 0;
return `${track.toString()} ${progress.toFixed(precision)}%`;
}
async function queryUserStatus() {
let query = `
query {
viewer {
status { emoji, emojiHTML, message, indicatesLimitedAvailability }
}
}
`;
const response = await axios.post(GitHubEndpoint, {
query,
});
const result = response.data;
return result.data.viewer.status;
}
async function mutateUserStatus(
emoji: Emoji,
message: string,
busy: boolean
): Promise<any> {
const input = `
emoji: ":${emoji.name}:",
message: "${message}",
limitedAvailability: ${busy.toString()}
`;
let query = `
mutation {
changeUserStatus(input:{${input}}) {
status { id }
}
}
`;
let response = await axios.post(GitHubEndpoint, {
query,
});
return response.data;
}
async function getIndex(config: Config) {
const status = await queryUserStatus();
return (
config.emojis.findIndex((emoji) => `:${emoji.name}:` === status.emoji) || 0
);
}
async function setNextStatus(config: Config) {
let index = await getIndex(config);
index = index + 1;
const yearStartDate = dayjs().startOf("year");
const messateText = yearStartDate.format(
config.messages[index % config.messages.length]
);
const message = `${messateText} ${buildProgressBar(getYearProgress())}`;
return await mutateUserStatus(
config.emojis[index % config.emojis.length],
message,
config.busy
);
}
async function updateStatus() {
const config = loadConfig();
axios.defaults.headers.common["Authorization"] = `bearer ${config.token}`;
return await setNextStatus(config);
}
export const test = () =>
updateStatus().then((result) => console.log(JSON.stringify(result, null, 2)));
export const update: APIGatewayProxyHandler = async (event, _context) => {
await updateStatus();
return {
statusCode: 200,
body: JSON.stringify(
{
message: "Status Updated",
input: event,
},
null,
2
),
};
};