-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathissues.ts
More file actions
161 lines (144 loc) · 5.2 KB
/
Copy pathissues.ts
File metadata and controls
161 lines (144 loc) · 5.2 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
import { createSync, type ProxyConfiguration } from 'nango';
import { z } from 'zod';
const GitLabProjectSchema = z.object({
id: z.number(),
path_with_namespace: z.string().optional()
});
const GitLabIssueSchema = z.object({
id: z.number(),
project_id: z.number(),
iid: z.number(),
title: z.string(),
description: z.string().nullable().optional(),
state: z.string(),
created_at: z.string(),
updated_at: z.string(),
closed_at: z.string().nullable().optional(),
author: z
.object({
id: z.number(),
username: z.string()
})
.optional(),
labels: z.array(z.string()).optional(),
web_url: z.string().optional()
});
const IssueSchema = z.object({
id: z.string(),
project_id: z.number(),
iid: z.number(),
title: z.string(),
description: z.string().optional(),
state: z.string(),
created_at: z.string(),
updated_at: z.string(),
closed_at: z.string().optional(),
author_id: z.number().optional(),
author_username: z.string().optional(),
labels: z.array(z.string()).optional(),
web_url: z.string().optional()
});
const CheckpointSchema = z.object({
updated_after: z.string()
});
const sync = createSync({
description: 'Sync issues from GitLab',
version: '1.0.0',
endpoints: [{ method: 'POST', path: '/syncs/issues' }],
frequency: 'every hour',
autoStart: true,
checkpoint: CheckpointSchema,
models: {
Issue: IssueSchema
},
exec: async (nango) => {
const checkpoint = await nango.getCheckpoint();
const runStart = new Date().toISOString();
const projectsConfig: ProxyConfiguration = {
// https://docs.gitlab.com/api/projects/#list-all-projects
endpoint: '/api/v4/projects',
params: {
membership: 'true'
},
paginate: {
type: 'offset',
offset_name_in_request: 'page',
offset_calculation_method: 'per-page',
offset_start_value: 1,
limit_name_in_request: 'per_page',
limit: 100
},
retries: 3
};
const projects: Array<z.infer<typeof GitLabProjectSchema>> = [];
for await (const projectBatch of nango.paginate(projectsConfig)) {
for (const raw of projectBatch) {
const parsed = GitLabProjectSchema.safeParse(raw);
if (parsed.success) {
projects.push(parsed.data);
}
}
}
let anySaved = false;
for (const project of projects) {
const params: Record<string, string | number> = {
state: 'all',
order_by: 'updated_at',
sort: 'asc'
};
if (checkpoint != null && checkpoint['updated_after'].length > 0) {
params['updated_after'] = checkpoint['updated_after'];
}
const issuesConfig: ProxyConfiguration = {
// https://docs.gitlab.com/api/issues/#list-all-project-issues
endpoint: `/api/v4/projects/${encodeURIComponent(String(project.id))}/issues`,
params,
paginate: {
type: 'offset',
offset_name_in_request: 'page',
offset_calculation_method: 'per-page',
offset_start_value: 1,
limit_name_in_request: 'per_page',
limit: 100
},
retries: 3
};
for await (const issueBatch of nango.paginate(issuesConfig)) {
const issues: Array<z.infer<typeof IssueSchema>> = [];
for (const raw of issueBatch) {
const parsed = GitLabIssueSchema.safeParse(raw);
if (!parsed.success) {
continue;
}
const issue = parsed.data;
issues.push({
id: String(issue.id),
project_id: issue.project_id,
iid: issue.iid,
title: issue.title,
...(issue.description != null && { description: issue.description }),
state: issue.state,
created_at: issue.created_at,
updated_at: issue.updated_at,
...(issue.closed_at != null && { closed_at: issue.closed_at }),
...(issue.author != null && {
author_id: issue.author.id,
author_username: issue.author.username
}),
labels: issue.labels,
web_url: issue.web_url
});
}
if (issues.length > 0) {
await nango.batchSave(issues, 'Issue');
anySaved = true;
}
}
}
if (anySaved) {
await nango.saveCheckpoint({ updated_after: runStart });
}
}
});
export type NangoSyncLocal = Parameters<(typeof sync)['exec']>[0];
export default sync;