-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.ts
696 lines (619 loc) · 27.6 KB
/
main.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
import { LinearClient, Issue, Project, LinearDocument, Connection, IssueRelation, User, LinearFetch } from '@linear/sdk'
import { env } from 'process';
import { readFileSync, writeFileSync, existsSync, unlinkSync } from 'fs';
import { inspect } from 'util';
import { Version3Client, Version3Models } from 'jira.js';
import _, { orderBy } from 'lodash';
import adf2md from 'adf-to-md';
import { JSONTransformer } from '@atlaskit/editor-json-transformer';
import { MarkdownTransformer } from '@atlaskit/editor-markdown-transformer';
import { DateComparator, Maybe, PaginationOrderBy } from '@linear/sdk/dist/_generated_documents';
const jsonTransformer = new JSONTransformer();
const markdownTransformer = new MarkdownTransformer();
const jiraProject = env.JIRA_PROJECT!;
const linearTeam = env.LINEAR_TEAM!;
const checkpointFile = env.CHECKPOINT_FILE ?? "checkpoint.txt";
const lastAttemptFile = env.LAST_ATTEMPT_FILE ?? "last-attempt.txt";
const LINEAR_LINK_TITLE = 'Linear';
const linearClient = new LinearClient({
apiKey: env.LINEAR_API_KEY,
})
const jiraClient = new Version3Client({
host: 'https://dfinity.atlassian.net',
authentication: {
basic: {
email: env.JIRA_USER_EMAIL!,
apiToken: env.JIRA_USER_API_KEY!,
},
},
});
const JIRA_ISSUE_LINK_IMPORTED_TITLE = 'Original issue in Jira';
const JIRA_ISSUE_LINK_TITLE = 'Jira';
enum JiraIssueType {
Epic = "Epic",
Task = "Task",
}
interface JiraSyncableLinearEntity {
id: string;
description?: string;
url: string;
summary: () => string;
jiraIssueType: () => JiraIssueType;
stateName: () => Promise<string | undefined>;
jiraKey: () => Promise<string | undefined>;
createLinearLink: (jiraIssueKey: string) => Promise<void>;
jiraEpic: () => Promise<string | undefined>;
assigneeEmail: () => Promise<string | undefined>;
creatorEmail: () => Promise<string | undefined>;
parentJiraKey: () => Promise<string | undefined>;
userCommentsUntil: (until: Date) => Promise<({ id: string, body: string, authorDisplayName: string })[]>;
}
declare module '@linear/sdk' {
interface Issue extends JiraSyncableLinearEntity { }
interface Project extends JiraSyncableLinearEntity { }
}
function jiraIssueUrl(jiraIssueKey: string) {
return `https://dfinity.atlassian.net/browse/${jiraIssueKey}`
}
Issue.prototype.summary = function (this: Issue): string {
return this.title
}
Issue.prototype.jiraIssueType = function (this: Issue): JiraIssueType {
return JiraIssueType.Task
}
Issue.prototype.stateName = async function (this: Issue): Promise<string | undefined> {
return (this.state)?.then(s => {
switch (s.name) {
case "Todo":
return "To Do"
default:
return s.name
}
})
}
Issue.prototype.jiraKey = async function (this: Issue): Promise<string | undefined> {
return this.attachments().then(a => a.nodes.find(a => a.title === JIRA_ISSUE_LINK_TITLE || a.title === JIRA_ISSUE_LINK_IMPORTED_TITLE)?.url?.split('/').pop())
}
Issue.prototype.createLinearLink = async function (this: Issue, jiraIssueKey: string): Promise<void> {
await linearClient.createAttachment({
groupBySource: false,
iconUrl: "https://dfinity.atlassian.net/favicon-family.ico",
title: JIRA_ISSUE_LINK_TITLE,
url: jiraIssueUrl(jiraIssueKey),
issueId: this.id,
})
}
Issue.prototype.jiraEpic = async function (this: Issue): Promise<string | undefined> {
return (await (await this.project)?.links())?.nodes.find(a => a.label === JIRA_ISSUE_LINK_TITLE || a.label === JIRA_ISSUE_LINK_IMPORTED_TITLE)?.url?.split('/').pop()
}
Issue.prototype.assigneeEmail = async function (this: Issue): Promise<string | undefined> {
return (await this.assignee)?.email
}
Issue.prototype.creatorEmail = async function (this: Issue): Promise<string | undefined> {
return (await this.creator)?.email
}
Issue.prototype.parentJiraKey = async function (this: Issue): Promise<string | undefined> {
return await (await this.parent)?.jiraKey()
}
Issue.prototype.userCommentsUntil = async function (this: Issue, until: Date): Promise<({ id: string, body: string, authorDisplayName: string })[]> {
return await Promise.all(await this.comments({
filter: {
user: {
isMe: {
eq: false
}
},
updatedAt: {
gt: until,
}
}
}).then(c => loadAllPagedNodes(c)).then(c => c.map(c => c.user!.then(u => ({ ...c, authorDisplayName: u.name })))))
.then(c => c.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime()))
}
Project.prototype.summary = function (this: Project): string {
return this.name
}
Project.prototype.jiraIssueType = function (this: Project): JiraIssueType {
return JiraIssueType.Epic
}
Project.prototype.stateName = async function (this: Project): Promise<string | undefined> {
let state = this.state.charAt(0).toUpperCase() + this.state.slice(1);
switch (state) {
case "Planned":
return "To Do"
case "Completed":
return "Done"
case "Paused":
return "Blocked"
case "Started":
return "In Progress"
default:
return state
}
}
Project.prototype.jiraKey = async function (this: Project): Promise<string | undefined> {
return this.links().then(a => a.nodes.find(a => a.label === JIRA_ISSUE_LINK_TITLE || a.label === JIRA_ISSUE_LINK_IMPORTED_TITLE)?.url?.split('/').pop())
}
Project.prototype.createLinearLink = async function (this: Project, jiraIssueKey: string): Promise<void> {
await linearClient.createProjectLink({
label: JIRA_ISSUE_LINK_TITLE,
url: jiraIssueUrl(jiraIssueKey),
projectId: this.id,
})
}
Project.prototype.jiraEpic = async function (this: Project): Promise<string | undefined> {
return undefined
}
Project.prototype.assigneeEmail = async function (this: Project): Promise<string | undefined> {
return (await this.lead)?.email
}
Project.prototype.creatorEmail = async function (this: Project): Promise<string | undefined> {
return (await this.creator)?.email
}
Project.prototype.parentJiraKey = async function (this: Project): Promise<string | undefined> {
return undefined
}
Project.prototype.userCommentsUntil = async function (this: Project, _: Date): Promise<({ id: string, body: string, authorDisplayName: string })[]> {
return []
}
function jiraToLinearStateName(state: string, resolution?: string): string {
if (resolution == "Done") {
return "Done"
} else if (resolution) {
return "Canceled"
} else if (state == "To Do") {
return "Todo"
} else {
return state
}
}
function jiraResolutionForState(state: string): string | undefined {
switch (state) {
case "Canceled":
return "Won't Do";
case "Done":
return "Done";
default:
return undefined
}
}
function descriptionLinearIdTag(linearId: string): string {
return `Linear ID: ${linearId}`
}
async function loadAllPagedNodes<T>(pagedResults: Connection<T>): Promise<T[]> {
return pagedResults.nodes.concat(
pagedResults.pageInfo.hasNextPage
? await loadAllPagedNodes(await pagedResults.fetchNext())
: []
);
}
async function syncLinearEntities<T extends JiraSyncableLinearEntity>(
jiraUsers: Version3Models.User[],
linearUsers: User[],
getEntities: (variables?: LinearDocument.Exact<{
orderBy?: LinearDocument.Maybe<PaginationOrderBy>;
filter: {
updatedAt?: Maybe<DateComparator>;
}
}>) => LinearFetch<Connection<T>>,
checkpoint: Date,
) {
let paged = (await getEntities({
orderBy: LinearDocument.PaginationOrderBy.UpdatedAt,
filter: {
updatedAt: {
gt: checkpoint
}
}
}));
let entities = await loadAllPagedNodes(paged);
let results = await Promise.all(entities.map(async (entity) => {
console.log(`Syncing ${entity.url}`);
try {
let [
linkedJiraKey,
entityEpic,
jiraAssignee,
] = await Promise.all([
entity.jiraKey(),
entity.jiraEpic(),
entity.assigneeEmail().then(email => jiraUsers.find(u => u.emailAddress === email)?.accountId),
]);
let jiraFields: any = {
summary: entity.summary(),
project: {
key: jiraProject,
},
assignee: {
id: jiraAssignee,
},
description: jsonTransformer.encode(markdownTransformer.parse(`${entity.description || ""}\n\n${descriptionLinearIdTag(entity.id)}`)),
...(entity.jiraIssueType() === JiraIssueType.Epic ? { customfield_10010: entity.summary() } : {}),
...(entity.jiraIssueType() === JiraIssueType.Task && entityEpic ? { customfield_10013: entityEpic } : {})
};
let jiraIssueKey = (
linkedJiraKey ||
(
(await jiraClient.issueSearch.searchForIssuesUsingJql({
jql: `summary ~ ${JSON.stringify(entity.summary())} AND description ~ "${descriptionLinearIdTag(entity.id)}" AND type = "${entity.jiraIssueType()}" AND project = "${jiraProject}" ORDER BY created DESC`,
})).issues?.find(_ => true)?.key
) || (
await jiraClient.issues.createIssue({
fields: {
reporter: {
id: await entity.creatorEmail().then(email => jiraUsers.find(u => u.emailAddress === email)?.accountId),
},
issuetype: {
name: entity.jiraIssueType(),
},
...jiraFields,
}
}).then(i => i.key)
)
)!;
if (!jiraAssignee) {
const jiraIssue = await jiraClient.issues.getIssue({
issueIdOrKey: jiraIssueKey,
});
if (!linearUsers.some(u => u.email === jiraIssue.fields.assignee?.emailAddress)) {
delete jiraFields['assignee'];
}
}
// Link Linear issue back to Jira
if (!linkedJiraKey) {
await entity.createLinearLink(jiraIssueKey);
}
// Link the Jira issue back to Linear if not set already
let remoteLink = (await jiraClient.issueRemoteLinks.getRemoteIssueLinks({
issueIdOrKey: jiraIssueKey,
}) as Version3Models.RemoteIssueLink[]).find(l => l.object?.title === LINEAR_LINK_TITLE);
const linearEntityUrlNoDescription = encodeURI(entity.url);
if (!remoteLink) {
await jiraClient.issueRemoteLinks.createOrUpdateRemoteIssueLink({
issueIdOrKey: jiraIssueKey,
object: {
url: linearEntityUrlNoDescription,
title: LINEAR_LINK_TITLE,
}
});
}
// Update comments
let linearComments = await entity?.userCommentsUntil(checkpoint);
if (linearComments) {
let jiraComments = await jiraClient.issueComments.getComments({ issueIdOrKey: jiraIssueKey, expand: "renderedBody" }).then(r => r.comments?.filter(c => c.author?.emailAddress === env.JIRA_USER_EMAIL));
for (let linearComment of linearComments) {
let fields: any = {
body: jsonTransformer.encode(markdownTransformer.parse(`${linearComment.authorDisplayName} commented on Linear:\n\n${linearComment.body || ""}\n\n${descriptionLinearIdTag(linearComment.id)}`)),
issueIdOrKey: jiraIssueKey,
}
let existingJiraComment = jiraComments?.find(c => c.renderedBody?.includes(descriptionLinearIdTag(linearComment.id)));
if (existingJiraComment) {
await jiraClient.issueComments.updateComment({
...fields,
id: existingJiraComment.id!,
})
} else {
await jiraClient.issueComments.addComment({
...fields,
});
}
}
}
// Update summary and description
await jiraClient.issues.editIssue({
issueIdOrKey: jiraIssueKey,
fields: {
...jiraFields,
description: jsonTransformer.encode(markdownTransformer.parse(entity.description || " ")),
}
})
// Update the state of the ticket
let stateName = await entity.stateName();
if (stateName) {
let jiraIssue = await jiraClient.issues.getIssue({
issueIdOrKey: jiraIssueKey,
})
if (jiraIssue.fields.status.name !== stateName) {
let transitions = await jiraClient.issues.getTransitions({
issueIdOrKey: jiraIssueKey,
});
let transition = transitions.transitions?.find(t => t.name == (stateName == "Canceled" ? "Done" : stateName));
if (transition) {
let resolution = jiraResolutionForState(stateName);
await jiraClient.issues.doTransition({
issueIdOrKey: jiraIssueKey,
transition: {
id: transition.id,
},
...(resolution ? {
fields: {
resolution: {
name: resolution
},
}
} : {})
})
} else {
console.log(`Unable to update Jira ticket "${jiraIssueKey}" to state "${stateName}": transition not found (available: ${transitions.transitions?.map(t => t.name)})`)
}
}
}
let parentJiraKey = await entity.parentJiraKey();
if (parentJiraKey) {
await jiraClient.issueLinks.linkIssues({
inwardIssue: {
key: parentJiraKey,
},
outwardIssue: {
key: jiraIssueKey,
},
type: {
name: "Issue split",
},
})
}
} catch (e) {
console.log(`Failed to update entity ${entity.url}`, inspect(e, { showHidden: false, depth: 2, colors: false }));
return e
}
}));
if (results.find(r => r != undefined)) {
throw "Some entities failed to sync"
}
}
function capitalizeFirstLetter(string: string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function linearJiraRelationTypeMapping(type: string) {
switch (type) {
case "related":
return "Relates"
}
return capitalizeFirstLetter(type)
}
async function syncLinearRelations(relations: IssueRelation[]) {
let results = await Promise.all(relations.map(async (r) => {
try {
let [issue, relatedIssue] = await Promise.all([
r.issue?.then(async (i) => ({ ...i, team: await i.team, jiraKey: await i.jiraKey() })),
r.relatedIssue?.then(async (i) => ({ ...i, team: await i.team, jiraKey: await i.jiraKey() })),
]);
// Only sync relations completely within the syncing team
if (issue?.team?.key !== linearTeam || relatedIssue?.team?.key !== linearTeam) {
return
}
await jiraClient.issueLinks.linkIssues({
inwardIssue: {
key: issue.jiraKey,
},
outwardIssue: {
key: relatedIssue.jiraKey,
},
type: {
name: linearJiraRelationTypeMapping(r.type),
},
})
} catch (e) {
return e
}
}));
if (results.find(r => r !== undefined)) {
throw "Some relations failed to sync";
}
}
function descriptionJiraIdTag(jiraId: string): string {
return `Jira ID: ${jiraId}`
}
async function syncJiraIssuesToLinear(checkpoint: Date, until: Date, linearUsers: User[]) {
const jiraSearchMinutesBuffer = 5;
const jiraCreatedIssuesQuery = `type != Epic AND (description is empty or description !~ "${descriptionLinearIdTag("")}") AND updated >= -${Math.ceil((Math.abs((new Date()).getTime() - checkpoint.getTime()) / 1000) / 60) + jiraSearchMinutesBuffer}m AND project = "${jiraProject}" ORDER BY created DESC`;
const linearTeamClient = await linearClient.team(linearTeam);
const [linearTeamId, linearProjects, linearTeamStates] = await Promise.all([
(await linearClient.teams({ filter: { key: { eq: linearTeam } } })).nodes.find(_ => true)?.id,
linearClient.projects().then(p => loadAllPagedNodes(p)),
linearClient.workflowStates({ filter: { team: { key: { eq: linearTeam } } } }).then(loadAllPagedNodes),
])
if (!linearTeamId) {
throw `Linear Team ${linearTeam} not found`;
}
let errors = (await Promise.all((await jiraClient.issueSearch.searchForIssuesUsingJql({
jql: jiraCreatedIssuesQuery,
})).issues!.map(async (i) => {
try {
let jiraUpdatedAt = new Date(i.fields.updated || i.fields.created || 0);
if (jiraUpdatedAt < checkpoint) {
console.log(`Skip syncing Jira issue ${i.key}, already synced`);
return
}
if (jiraUpdatedAt > until) {
console.log(`Skip syncing Jira issue ${i.key}: too fresh, skipping because Linear sync likely failed. Waiting for Linear to sync first.`);
return
}
console.log(`Syncing issue created in Jira: ${i.key}`);
let transitions = (await jiraClient.issues.getTransitions({ issueIdOrKey: i.key })).transitions?.map(t => t.name);
let invalidTransition = transitions?.filter(t => !linearTeamStates.some(s => s.name === jiraToLinearStateName(t || "")));
if ((invalidTransition?.length || 0) > 0) {
console.log(`Skipping Jira ticket ${i.key}, unknown transitions: ${invalidTransition?.join(", ")}`)
return
}
let inlineCardToUrl = (doc?: Omit<Version3Models.Document, 'version'>) => {
if (!doc) {
return
}
if (doc.type === "inlineCard" && doc.attrs?.url) {
doc.type = "text"
doc.text = doc.attrs?.url
doc.marks = [{
type: "link",
attrs: {
href: doc.attrs?.url,
}
}]
delete doc.attrs
}
if (doc.type === "mediaSingle" && doc.content?.[0]?.attrs?.url) {
doc.type = "paragraph"
doc.content = [{
type: 'text', text: '!', // prefix the link with `!` to turn into picture link
}, {
type: "text",
text: doc.content?.[0]?.attrs?.url,
marks: [{
type: "link",
attrs: {
href: doc.content?.[0]?.attrs?.url,
}
}]
}]
delete doc.attrs
}
if (doc.content) {
doc.content.forEach(c => inlineCardToUrl(c))
}
return
}
inlineCardToUrl(i.fields.description)
// TODO: replace: Not implemented as time of writing
// const description = markdownTransformer.encode(jsonTransformer.parse(i.fields.description as JSONDocNode));
let description: string = i.fields.description ? adf2md.convert(i.fields.description).result : "";
// Force update
if (description === "") {
description = " ";
}
let linearIssue = await linearTeamClient.issues({
filter: {
searchableContent: {
contains: descriptionJiraIdTag(i.id),
}
}
}).then(r => r.nodes.find(_ => true));
if (!linearIssue) {
linearIssue = (await linearTeamClient.issues({
filter: {
attachments: {
url: {
eq: jiraIssueUrl(i.key),
},
}
}
})).nodes.find(_ => true);
}
let projectLink = i.fields.customfield_10013 ? await jiraClient.issueRemoteLinks.getRemoteIssueLinks({
issueIdOrKey: i.fields.customfield_10013,
}).then(links => (links as Version3Models.RemoteIssueLink[]).find(l => l.object?.title === LINEAR_LINK_TITLE)?.object?.url) : undefined;
const linearIssueFields = {
teamId: linearTeamId,
title: i.fields.summary,
description: description,
assigneeId: linearUsers.find(u => u.email === i.fields.assignee?.emailAddress)?.id,
projectId: linearProjects.find(p => p.url === projectLink)?.id,
stateId: linearTeamStates.find(state => state.name === jiraToLinearStateName(i.fields.status.name!, i.fields.resolution?.name))?.id,
}
if (!linearIssue) {
linearIssue = (await linearClient.createIssue({
...linearIssueFields,
description: `${description}\n${descriptionJiraIdTag(i.id)}`,
}).then(p => p.issue))!;
}
const linearIssueLinkedJira = await linearIssue?.jiraKey();
if (!linearIssueLinkedJira) {
await linearIssue?.createLinearLink(i.key);
}
// Update comments
let jiraComments = await jiraClient.issueComments.getComments({ issueIdOrKey: i.key })
.then(r => r.comments?.filter(c => c.author?.emailAddress !== env.JIRA_USER_EMAIL && new Date(c.updated || c.created || 0) > checkpoint))
.then(comments => comments?.sort((a, b) => new Date(a.created || a.updated || 0).getTime() - new Date(b.created || b.updated || 0).getTime()));
if (jiraComments) {
let linearComments = await linearIssue?.comments({
filter: {
user: {
isMe: {
eq: true,
}
}
}
}).then(r => loadAllPagedNodes(r));
for (let jiraComment of jiraComments) {
let linearCommentBody = `${jiraComment.author?.displayName} commented on Jira:\n\n${adf2md.convert(jiraComment.body).result}\n\n${descriptionJiraIdTag(jiraComment.id!)}`;
let existingLinearComment = linearComments.find(c => c.body?.includes(descriptionJiraIdTag(jiraComment.id!)));
if (existingLinearComment) {
await existingLinearComment.update({
body: linearCommentBody,
})
} else {
await linearClient.createComment({
body: linearCommentBody,
issueId: linearIssue?.id,
});
}
}
}
// Update Linear ticket if needed
let linearUpdatedAt = (linearIssue?.updatedAt || linearIssue?.createdAt || new Date(0));
// Markdown conversion is inconsistent so let's try to approximate that ticket description is unchanged by comparing only word characters
let withNormalizedDescription = ({ description, ...props }: { description?: string } & { [x: string]: any }) => ({
...props,
description: description?.replace(/[^a-zA-Z0-9]/g, ""),
})
let linearIssueMatchable = withNormalizedDescription({
...linearIssue,
teamId: linearIssue["_team"]?.id,
projectId: linearIssue["_project"]?.id,
stateId: linearIssue["_state"]?.id,
assigneeId: linearIssue["_assignee"]?.id,
});
if (_.isMatch(linearIssueMatchable, withNormalizedDescription(linearIssueFields))) {
console.log(`Skipping Linear ticket update ${linearIssue?.identifier} - already up-to-date`);
return
}
console.log(`Syncing Jira to Linear - update times: Linear ${linearUpdatedAt} --- Jira ${jiraUpdatedAt}`)
if (jiraUpdatedAt > linearUpdatedAt || linearIssue.description?.endsWith(descriptionJiraIdTag(i.id))) {
console.log(`Updating Linear ticket ${linearIssue?.identifier}`);
await linearIssue?.update(linearIssueFields);
} else {
console.log(`Skipping Linear ticket update ${linearIssue?.identifier} - Linear ticket is newer`);
return
}
} catch (e) {
console.log(`Failed to sync issue ${i.key}`)
return e
}
return null
}))).filter(e => e != null);
if (errors.length > 0) {
for (let error of errors) {
console.error(error);
}
throw new Error(`Failed to sync ${errors.length} issues`);
}
}
async function main() {
let checkpointNew = new Date();
const lastAttemptExists = existsSync(lastAttemptFile);
if (lastAttemptExists) {
checkpointNew = new Date(readFileSync(lastAttemptFile, 'utf8'));
} else {
writeFileSync(lastAttemptFile, checkpointNew.toISOString())
}
const checkpointExists = existsSync(checkpointFile);
const checkpointLast = checkpointExists ? new Date(readFileSync(checkpointFile, 'utf8')) : new Date(0);
console.log(`Syncing since last checkpoint ${checkpointLast.toISOString()}`)
const linearUsers = await linearClient.users().then(u => loadAllPagedNodes(u))
await syncJiraIssuesToLinear(checkpointLast, checkpointNew, linearUsers);
let jiraUsers = (await jiraClient.userSearch.findAssignableUsers({
project: jiraProject,
maxResults: 1000,
}));
const linearTeamClient = await linearClient.team(linearTeam);
await syncLinearEntities(jiraUsers, linearUsers, (v) => linearTeamClient.projects(v), checkpointLast);
await syncLinearEntities(jiraUsers, linearUsers, (v) => linearTeamClient.issues(v), checkpointLast);
await syncLinearRelations(
await loadAllPagedNodes(
await linearClient.issueRelations({
orderBy: LinearDocument.PaginationOrderBy.UpdatedAt,
}))
);
writeFileSync(checkpointFile, checkpointNew.toISOString())
unlinkSync(lastAttemptFile)
}
main();