1 | import { setState, getState } from "windmill-client@1"; |
2 | import { Octokit } from "@octokit/[email protected]"; |
3 |
|
4 | const MAX_ITEMS = 500; |
5 | const PAGE_SIZE = 50; |
6 |
|
7 | |
8 | * @returns A list of issues in ascending order of creation date. |
9 | * |
10 | * The maximum number of returned items is 500 and therefore you'll miss |
11 | * the issues that are older than the 500th one. Please choose your |
12 | * scheduling accordingly. |
13 | */ |
14 | type Github = { |
15 | token: string; |
16 | }; |
17 | export async function main(auth: Github, owner: string, repo: string) { |
18 | const octokit = new Octokit({ |
19 | auth: auth.token, |
20 | }); |
21 | const newIssues: Issue[] = []; |
22 | const lastUpdate = await getState(); |
23 |
|
24 | let runLoop = true; |
25 | let page = 1; |
26 | do { |
27 | const result = await octokit.issues.listForRepo({ |
28 | owner, |
29 | repo, |
30 | filter: "all", |
31 | state: "open", |
32 | sort: "created", |
33 | direction: "desc", |
34 | per_page: PAGE_SIZE, |
35 | page: page++, |
36 | }); |
37 | const issues = result.data.filter((i) => i && !i.pull_request); |
38 |
|
39 | for (let i = 0; i < issues.length; i++) { |
40 | const issue = issues[i]; |
41 | if (new Date(issue.created_at).getTime() <= lastUpdate) { |
42 | runLoop = false; |
43 | break; |
44 | } |
45 | newIssues.push({ |
46 | title: issue.title, |
47 | url: issue.html_url, |
48 | created_at: issue.created_at, |
49 | user: { |
50 | login: issue.user?.login, |
51 | url: issue.user?.html_url, |
52 | }, |
53 | labels: <string[]>( |
54 | issue.labels |
55 | .map((l) => (typeof l === "string" ? l : l.name)) |
56 | .filter(Boolean) |
57 | ), |
58 | }); |
59 | if (newIssues.length >= MAX_ITEMS) { |
60 | runLoop = false; |
61 | break; |
62 | } |
63 | } |
64 |
|
65 | if (result.data.length < PAGE_SIZE) { |
66 | break; |
67 | } |
68 | } while (runLoop); |
69 |
|
70 | if (newIssues.length) { |
71 | await setState(new Date(newIssues[0].created_at).getTime()); |
72 | } |
73 | return newIssues.reverse(); |
74 | } |
75 |
|
76 | interface Issue { |
77 | title: string; |
78 | url: string; |
79 | created_at: string; |
80 | user: { |
81 | login?: string; |
82 | url?: string; |
83 | }; |
84 | labels: string[]; |
85 | } |
86 |
|