-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrest.ts
More file actions
201 lines (182 loc) · 5.87 KB
/
rest.ts
File metadata and controls
201 lines (182 loc) · 5.87 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
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
import { Octokit } from "@octokit/rest";
import { sleep } from "bun";
import type {
RestParams,
Issue,
Label,
IssuesList,
IterateParams,
ActionCounters,
WorkflowInputs,
ActionResponse,
PaginateParams,
} from "./types";
export default function Rest({ owner, repo }: RestParams) {
const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN,
});
/**
* Get all Esri Product Labels (color: #006B75) from an issue
* @param issue - The GitHub issue object
* @returns An array of Esri Product Labels or an empty array if none found
*/
function getProductLabels(issue: Issue): Label[] {
return issue.labels.filter(
(label): label is Label =>
typeof label !== "string" && label.color === "006B75",
);
}
/**
* Remove pull requests from a list of issues
* @param issues - An array of GitHub issues
* @returns An array of issues excluding pull requests
*/
function removePullRequests(issues: IssuesList): Issue[] {
return issues.filter((issue) => !issue.pull_request);
}
/**
* Dispatch a GitHub Actions workflow for the given issue
* @param issue - The GitHub issue object
* @param inputs - The inputs for the workflow dispatch
* @returns ActionResponse indicating the result of the operation
*/
async function dispatchMondayWorkflow(
issue: Issue,
inputs: WorkflowInputs,
): Promise<ActionResponse> {
const defaultInputs = {
issue_number: issue.number.toString(),
event_type: "SyncActionChanges",
};
try {
await octokit.rest.actions.createWorkflowDispatch({
owner,
repo,
workflow_id: "issue-monday-sync.yml",
ref: "dev",
inputs: { ...defaultInputs, ...inputs },
});
console.log(`DISPATCHED: ${issue.html_url}`);
return "triggered";
} catch (error) {
console.error(
`FAILED: ${issue.html_url}. Error: ${String(error)}`,
error,
);
return "failed";
}
}
/**
* Sync Esri Product Labels to Monday.com via GitHub Actions workflow dispatch
* @param issue - The GitHub issue object
* @returns ActionResponse indicating the result of the operation
*/
async function syncEsriProductLabels(issue: Issue): Promise<ActionResponse> {
const productLabels = getProductLabels(issue);
if (productLabels.length === 0) return "skipped";
const firstLabel = productLabels[0];
if (!firstLabel?.name) {
console.warn(`FAILED: ${issue.html_url}. No valid label found.`);
return "failed";
}
const inputs: WorkflowInputs = {
label_name: firstLabel.name,
label_color: firstLabel.color,
label_action: "added",
};
return await dispatchMondayWorkflow(issue, inputs);
}
/**
* Iterate over all issues in the repository and perform the specified action
*
* @param action - Method to perform on each Issue, returns a promise of ActionResponse
* @param state - Filter issues by state: "open", "closed", or "all" (default: "open")
* @param assignee - Filter issues by assignee: Can be the name of a user, `"none"` for issues with no assigned user, and `"*"` for issues assigned to any user.
* @param milestone - Filter issues by milestone title
* @param label_filter - Filter issues that have all specified labels (comma-separated string?)
* @param per_page - Number of issues to fetch per page (default: 30)
* @param sleepMs - Milliseconds to sleep between page fetches (default: 0)
* @param onlyFirstPage - If true, only fetch and process the first page of results (default: false)
* @returns A promise that resolves when all issues have been processed
*/
async function iterateAllIssues({
action,
state = "open",
assignee,
milestone,
label_filter,
per_page = 30,
sleepMs = 0,
onlyFirstPage = false,
}: IterateParams): Promise<void> {
const counters: ActionCounters = {
total: 0,
triggered: 0,
skipped: 0,
failed: 0,
};
let iteratorOptions: PaginateParams = { owner, repo, per_page, state };
if (assignee) {
iteratorOptions = { ...iteratorOptions, assignee };
}
if (milestone) {
iteratorOptions = { ...iteratorOptions, milestone };
}
if (label_filter) {
iteratorOptions = { ...iteratorOptions, labels: label_filter };
}
for await (const page of octokit.paginate.iterator(
"GET /repos/{owner}/{repo}/issues",
iteratorOptions,
)) {
const issues = removePullRequests(page.data);
counters.total += issues.length;
for (const issue of issues) {
const result = await action(issue);
counters[result] += 1;
}
if (onlyFirstPage) break;
console.log(`Sleeping for ${sleepMs} ms before next page...`);
await sleep(sleepMs);
}
console.log(
`\n --- Issues processed --- \n
Total: ${counters.total} \n
Triggered: ${counters.triggered} \n
Skipped: ${counters.skipped} \n
Failed: ${counters.failed} \n`,
);
}
/**
* Sync assignees to Monday.com via GitHub Actions workflow dispatch
* @param issue - The GitHub issue object
* @returns ActionResponse indicating the result of the operation
*/
async function syncAssignees(issue: Issue): Promise<ActionResponse> {
if (issue?.assignees?.length === 0) return "skipped";
return await dispatchMondayWorkflow(issue, { assignee_updated: "true" });
}
/**
* Fetch a single issue by its number
* @param issue_number - The number of the issue to fetch
* @returns The GitHub issue object
*/
async function getIssue(issue_number: number): Promise<Issue> {
const { data: issue } = await octokit.rest.issues.get({
owner,
repo,
issue_number,
});
return issue;
}
return {
octokit,
getIssue,
syncAssignees,
getProductLabels,
dispatchMondayWorkflow,
removePullRequests,
syncEsriProductLabels,
iterateAllIssues,
};
}