1 | import { |
2 | setState, |
3 | getState, |
4 | } from "https://deno.land/x/windmill@v1.85.0/mod.ts"; |
5 | import { Octokit } from "npm:@octokit/rest@19.0.5"; |
6 |
|
7 | const MAX_ITEMS = 500; |
8 |
|
9 |
|
10 | * @returns A list of starrers in descending order of when they starred. |
11 | * The maximum number of returned items is 500. |
12 | */ |
13 | type Github = { |
14 | token: string; |
15 | }; |
16 | export async function main(auth: Github, owner: string, repo: string) { |
17 | const octokit = new Octokit({ |
18 | auth: auth.token, |
19 | }); |
20 | const lastUpdate = (await getState()) || 0; |
21 |
|
22 | const starCount = (await octokit.repos.get({ owner, repo })).data |
23 | .stargazers_count; |
24 | let lastPage = |
25 | +(starCount / 100).toFixed(0) + (starCount % 100 === 0 ? 0 : 1); |
26 | const newStarrers: Starrer[] = []; |
27 |
|
28 | await setState(Date.now()); |
29 | let runLoop = true; |
30 | do { |
31 | const response = await octokit.activity.listStargazersForRepo({ |
32 | owner, |
33 | repo, |
34 | per_page: 100, |
35 | page: lastPage--, |
36 | headers: { |
37 | accept: "application/vnd.github.star+json", |
38 | }, |
39 | }); |
40 | for (let i = response.data.length - 1; i >= 0; i--) { |
41 | const entry = response.data[i]; |
42 | if (!entry || !entry.user) continue; |
43 | const starredAt = new Date(entry.starred_at).getTime(); |
44 | if (starredAt < lastUpdate) { |
45 | runLoop = false; |
46 | break; |
47 | } |
48 | const { id, login, type, url, email, name } = entry.user; |
49 | newStarrers.push({ id, starredAt, login, type, url, email, name }); |
50 | if (newStarrers.length >= MAX_ITEMS) { |
51 | await setState(newStarrers.at(-1)?.starredAt); |
52 | runLoop = false; |
53 | break; |
54 | } |
55 | } |
56 | } while (runLoop); |
57 | console.log(newStarrers.length); |
58 | return newStarrers; |
59 | } |
60 |
|
61 | interface Starrer { |
62 | id: number; |
63 | login: string; |
64 | type: string; |
65 | url: string; |
66 | starredAt: number; |
67 | email?: string | null; |
68 | name?: string | null; |
69 | } |
70 |
|