1
- import { Github } from './github.js'
1
+ import { Github } from './github.js' ;
2
2
3
3
export const getGithubClient = ( ) => {
4
- if ( ! process . env . GITHUB_TOKEN ) {
5
- throw new Error ( 'GITHUB_TOKEN is not set' )
6
- }
7
- return new Github ( process . env . GITHUB_TOKEN )
8
- }
4
+ if ( ! process . env . GITHUB_TOKEN ) {
5
+ throw new Error ( 'GITHUB_TOKEN is not set' ) ;
6
+ }
7
+ return new Github ( process . env . GITHUB_TOKEN ) ;
8
+ } ;
9
9
10
10
export const PROJECT_ID = process . env . PROJECT_ID || '302' ;
11
11
12
12
export const syncIssue = async ( issue : string ) => {
13
- const github = getGithubClient ( )
14
-
15
- // 1. Fetch the issue details
16
- const issueData = await github . getIssue ( issue ) as Record < any , any > ;
17
-
18
- // Extract repository data
19
- const repoData = issueData ?. data ?. repository ;
20
- if ( ! repoData || ! repoData . issue ) {
21
- throw new Error ( `Issue ${ issue } not found` ) ;
13
+ const github = getGithubClient ( ) ;
14
+
15
+ // 1. Fetch the issue details
16
+ const issueData = await github . getIssue ( issue ) as Record < any , any > ;
17
+
18
+ // Extract repository data
19
+ const repoData = issueData ?. data ?. repository ;
20
+ if ( ! repoData || ! repoData . issue ) {
21
+ throw new Error ( `Issue ${ issue } not found` ) ;
22
+ }
23
+
24
+ const issueDetails = repoData . issue ;
25
+
26
+ // If issue is part of the project, set the issue's project properties
27
+ let projectItemId = undefined ;
28
+ if ( issueDetails . projectItems ?. nodes ) {
29
+ for ( const node of issueDetails . projectItems . nodes ) {
30
+ if ( `${ node . project ?. number } ` === PROJECT_ID ) {
31
+ projectItemId = node . id ;
32
+ break ;
33
+ }
22
34
}
35
+ }
23
36
24
- const issueDetails = repoData . issue ;
25
-
26
- // If issue is part of the project, set the issue's project properties
27
- let projectItemId = undefined ;
28
- if ( issueDetails . projectItems ?. nodes ) {
29
- for ( const node of issueDetails . projectItems . nodes ) {
30
- if ( `${ node . project ?. number } ` === PROJECT_ID ) {
31
- projectItemId = node . id ;
32
- break ;
33
- }
34
- }
35
- }
37
+ if ( projectItemId === undefined ) {
38
+ console . log ( 'Issue is not included in project, skipping.' ) ;
39
+ return ;
40
+ }
36
41
37
- if ( projectItemId === undefined ) {
38
- console . log ( 'Issue is not included in project, skipping.' )
39
- return ;
40
- }
42
+ const projectInfo = await github . getProjectInfo ( PROJECT_ID ) ;
43
+ const projectId = projectInfo . data . repository . projectV2 . id ! ;
41
44
42
- const projectInfo = await github . getProjectInfo ( PROJECT_ID ) ;
43
- const projectId = projectInfo . data . repository . projectV2 . id !
44
-
45
- let creationFieldId = undefined ;
46
- let updateFieldId = undefined ;
47
- for ( const field of projectInfo . data . repository . projectV2 . fields . nodes ) {
48
- if ( field . name === 'Creation date' ) {
49
- creationFieldId = field . id
50
- }
51
- if ( field . name === 'Update date' ) {
52
- updateFieldId = field . id
53
- }
45
+ let creationFieldId = undefined ;
46
+ let updateFieldId = undefined ;
47
+ for ( const field of projectInfo . data . repository . projectV2 . fields . nodes ) {
48
+ if ( field . name === 'Creation date' ) {
49
+ creationFieldId = field . id ;
54
50
}
55
-
56
- if ( creationFieldId === undefined ) {
57
- throw new Error ( 'Project field "Creation date" not found' )
51
+ if ( field . name === 'Update date' ) {
52
+ updateFieldId = field . id ;
58
53
}
54
+ }
59
55
60
- if ( updateFieldId === undefined ) {
61
- throw new Error ( 'Project field "Update date" not found' )
62
- }
56
+ if ( creationFieldId === undefined ) {
57
+ throw new Error ( 'Project field "Creation date" not found' ) ;
58
+ }
63
59
64
- // Get timeline items to determine the last update date (excluding github-actions)
65
- const timelineItems = issueDetails . timelineItems ?. nodes ?? [ ] ;
60
+ if ( updateFieldId === undefined ) {
61
+ throw new Error ( 'Project field "Update date" not found' ) ;
62
+ }
66
63
67
- // Get creation date from the first reaction or use current date
68
- const creationDate = new Date ( issueDetails . createdAt )
64
+ // Get timeline items to determine the last update date (excluding github-actions)
65
+ const timelineItems = issueDetails . timelineItems ?. nodes ?? [ ] ;
69
66
70
- // Get update date from the last timeline item or use creation date
71
- let updateDate = creationDate ;
67
+ // Get creation date from the first reaction or use current date
68
+ const creationDate = new Date ( issueDetails . createdAt ) ;
72
69
73
- for ( let index = 0 ; index < timelineItems . length ; index ++ ) {
74
- const item = timelineItems [ timelineItems . length - index - 1 ]
75
- if ( item ?. createdAt !== undefined && item . author ?. login !== 'github-actions' && item . actor ?. login !== 'github-actions' ) {
76
- updateDate = new Date ( item . createdAt )
77
- break
78
- }
79
- }
70
+ // Get update date from the last timeline item or use creation date
71
+ let updateDate = creationDate ;
80
72
81
- if ( issueDetails . reactions . nodes . length > 0 ) {
82
- const reactionDate = new Date ( issueDetails . reactions . nodes [ 0 ] . createdAt )
83
- if ( reactionDate > updateDate )
84
- updateDate = reactionDate
73
+ for ( let index = 0 ; index < timelineItems . length ; index ++ ) {
74
+ const item = timelineItems [ timelineItems . length - index - 1 ] ;
75
+ if ( item ?. createdAt !== undefined && item . author ?. login !== 'github-actions' && item . actor ?. login !== 'github-actions' ) {
76
+ updateDate = new Date ( item . createdAt ) ;
77
+ break ;
85
78
}
86
-
87
- const result = await github . setProjectItem ( projectId , projectItemId , {
88
- [ creationFieldId ] : { date : creationDate } ,
89
- [ updateFieldId ] : { date : updateDate }
90
- } ) ;
91
- console . log ( 'Result from mutation request: ' )
92
- console . dir ( JSON . stringify ( result ) )
93
- }
79
+ }
80
+
81
+ if ( issueDetails . reactions . nodes . length > 0 ) {
82
+ const reactionDate = new Date ( issueDetails . reactions . nodes [ 0 ] . createdAt ) ;
83
+ if ( reactionDate > updateDate ) { updateDate = reactionDate ; }
84
+ }
85
+
86
+ const result = await github . setProjectItem ( projectId , projectItemId , {
87
+ [ creationFieldId ] : { date : creationDate } ,
88
+ [ updateFieldId ] : { date : updateDate } ,
89
+ } ) ;
90
+ console . log ( 'Result from mutation request: ' ) ;
91
+ console . dir ( JSON . stringify ( result ) ) ;
92
+ } ;
0 commit comments