-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
254 lines (213 loc) · 6.01 KB
/
index.ts
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import { Octokit, RestEndpointMethodTypes } from '@octokit/rest';
import { config } from 'dotenv';
import Jimp from 'jimp';
import { resolve } from 'path';
import { GHAPICallError, ImageEditorError } from './errors';
config();
const BASE_IMAGE_PATH = resolve('behold-no-bg.png');
const gh = new Octokit({
auth: process.env.GITHUB_TOKEN as string,
});
type GHActivityResponseList =
RestEndpointMethodTypes['activity']['listEventsForAuthenticatedUser']['response']['data'];
type Commit = {
repo: string;
commitSha: string;
message: string;
pushedAt: string;
};
type WatchedRepo = {
name: string;
starredAt: string;
};
async function getRecentActivity(): Promise<GHActivityResponseList> {
try {
const result = await gh.rest.activity.listEventsForAuthenticatedUser({
username: 'thassiov',
});
return result.data;
} catch (error) {
throw new GHAPICallError('Could not get latests activity from GitHub', {
cause: error as Error,
});
}
}
/**
* Gets my latest commit
*/
function getLatestCommit(response: GHActivityResponseList): Commit | string {
const latestCommit = response.find(
(item) => item.type === 'PushEvent' && item.public === true
);
if (!latestCommit) {
return "Looks like I've been AFK. I'm probably sim racing.";
}
return {
repo: latestCommit!.repo.name,
commitSha: (latestCommit!.payload as any).commits[0].sha as string,
message: (latestCommit!.payload as any).commits[0].message as string,
pushedAt: latestCommit.created_at as string,
};
}
/**
* Gets the last 4 repos I watched/starred
*/
function getLatestWatchedRepos(
response: GHActivityResponseList
): WatchedRepo[] | string {
const watched = response
.filter(
(item) =>
item.type === 'WatchEvent' &&
item.payload.action === 'started' &&
!item.repo.name.startsWith('thassiov/')
)
.map(
(item) =>
({
name: item.repo.name,
starredAt: item.created_at,
}) as WatchedRepo
);
if (!watched.length) {
return "Didn't starred any repo in a while...";
}
return watched.slice(0, 5);
}
async function readBaseImage(): Promise<Jimp> {
try {
const image = await Jimp.read(BASE_IMAGE_PATH);
return image;
} catch (error) {
throw new ImageEditorError('Could not load base image', {
cause: error as Error,
});
}
}
async function addLatestCommitInfoToImage(
image: Jimp,
commit: Commit | string
): Promise<Jimp> {
const MAX_WIDTH_COMMIT_MESSAGE = 300;
const MAX_WIDTH_COMMIT_INFO = 265;
const sans32black = await Jimp.loadFont(Jimp.FONT_SANS_32_BLACK);
const sans32white = await Jimp.loadFont(Jimp.FONT_SANS_32_WHITE);
const sans16white = await Jimp.loadFont(Jimp.FONT_SANS_16_WHITE);
image.print(sans32black, 360, 500, 'My latest commit');
if (typeof commit === 'string') {
image.print(sans32white, 360, 567, `"${commit}"`, MAX_WIDTH_COMMIT_MESSAGE);
return image;
}
// commit message
image.print(
sans32white,
360,
567,
`"${commit.message}"`,
MAX_WIDTH_COMMIT_MESSAGE
);
// commit info - sha
image.print(
sans16white,
395,
747,
`${commit.commitSha.slice(0, 6)}`,
MAX_WIDTH_COMMIT_INFO
);
// commit info - time
image.print(
sans16white,
395,
768,
`${commit.pushedAt}`,
MAX_WIDTH_COMMIT_INFO
);
// commit info - repo
image.print(sans16white, 395, 788, `${commit.repo}`, MAX_WIDTH_COMMIT_INFO);
return image;
}
async function addStarredReposToImage(
image: Jimp,
watched: WatchedRepo[] | string
): Promise<Jimp> {
const MAX_WIDTH_REPO_INFO = 265;
const sans16black = await Jimp.loadFont(Jimp.FONT_SANS_16_BLACK);
const sans16white = await Jimp.loadFont(Jimp.FONT_SANS_16_WHITE);
image.print(sans16black, 20, 567, "I've been liking this repos also");
if (typeof watched === 'string') {
image.print(sans16white, 14, 612, watched, MAX_WIDTH_REPO_INFO);
return image;
}
let textStartAt = 610;
watched.forEach((repo) => {
if (repo.name.length > 30) {
const [username, repoName] = repo.name.split('/');
image.print(sans16white, 30, textStartAt, username, MAX_WIDTH_REPO_INFO);
textStartAt += 20;
image.print(
sans16white,
30,
textStartAt,
`/${repoName}`,
MAX_WIDTH_REPO_INFO
);
} else {
image.print(sans16white, 30, textStartAt, repo.name, MAX_WIDTH_REPO_INFO);
}
textStartAt += 25;
image.print(
sans16white,
30,
textStartAt,
repo.starredAt,
MAX_WIDTH_REPO_INFO
);
textStartAt += 40;
});
return image;
}
async function addFontSizeNotice(image: Jimp): Promise<Jimp> {
const MAX_WIDTH_FONT_NOTICE = 320;
const sans16white = await Jimp.loadFont(Jimp.FONT_SANS_16_WHITE);
image.print(
sans16white,
15,
490,
"tHiS fOnT iS tOo SmAlL i CaN't Re- I know the font is not good. I'll fix it later",
MAX_WIDTH_FONT_NOTICE
);
return image;
}
async function saveNewimage(image: Jimp): Promise<void> {
try {
// lol
const newFilename =
(BASE_IMAGE_PATH.split('/').reverse()[0]!.split('.')[0] as string) +
'-latest.png';
const filePath =
BASE_IMAGE_PATH.split('/').slice(0, -1).join('/') + `/${newFilename}`;
await image.writeAsync(filePath);
} catch (error) {
throw new ImageEditorError('Could not write edited image', {
cause: error as Error,
});
}
}
(async () => {
try {
const resp = await getRecentActivity();
let image = await readBaseImage();
image = await addFontSizeNotice(image);
image = await addLatestCommitInfoToImage(image, getLatestCommit(resp));
image = await addStarredReposToImage(image, getLatestWatchedRepos(resp));
await saveNewimage(image);
} catch (error) {
console.error(
'A problem happened during the creation of the new image and the operation could not finish. More details below.'
);
console.error((error as Error).message);
console.error((error as Error).stack);
console.error('Exiting...');
process.exit(1);
}
})();