1
- document . addEventListener ( "DOMContentLoaded" , function ( ) {
1
+ document . addEventListener ( "DOMContentLoaded" , function ( ) {
2
2
setupFormHandler ( ) ;
3
3
setupNotificationSystem ( ) ;
4
4
} ) ;
@@ -8,44 +8,44 @@ document.addEventListener("DOMContentLoaded", function() {
8
8
// The show() method changes the element based on type and displays the message to the user
9
9
// The hide() function makes sure that the notification fades away after 5 seconds
10
10
const notificationSystem = {
11
- show : function ( message , type = 'error' ) {
11
+ show : function ( message , type = 'error' ) {
12
12
const notification = document . getElementById ( 'notification' ) ;
13
13
const messageElement = document . getElementById ( 'notification-message' ) ;
14
-
14
+
15
15
messageElement . textContent = message ;
16
-
16
+
17
17
if ( type === 'error' ) {
18
18
notification . style . backgroundColor = '#f8d7da' ;
19
19
notification . style . color = '#721c24' ;
20
20
notification . style . border = '1px solid #f5c6cb' ;
21
21
} else {
22
22
notification . style . backgroundColor = '#d4edda' ;
23
- notification . style . color = '#155724' ;
23
+ notification . style . color = '#155724' ;
24
24
notification . style . border = '1px solid #c3e6cb' ;
25
25
}
26
-
26
+
27
27
notification . style . display = 'block' ;
28
28
setTimeout ( ( ) => {
29
29
notification . style . opacity = '1' ;
30
30
} , 10 ) ;
31
-
31
+
32
32
clearTimeout ( this . timeout ) ;
33
33
this . timeout = setTimeout ( ( ) => this . hide ( ) , 5000 ) ;
34
34
} ,
35
-
36
- hide : function ( ) {
35
+
36
+ hide : function ( ) {
37
37
const notification = document . getElementById ( 'notification' ) ;
38
38
notification . style . opacity = '0' ;
39
39
setTimeout ( ( ) => {
40
40
notification . style . display = 'none' ;
41
41
} , 500 ) ;
42
42
} ,
43
-
44
- error : function ( message ) {
43
+
44
+ error : function ( message ) {
45
45
this . show ( message , 'error' ) ;
46
46
} ,
47
-
48
- success : function ( message ) {
47
+
48
+ success : function ( message ) {
49
49
this . show ( message , 'success' ) ;
50
50
} ,
51
51
} ;
@@ -61,7 +61,7 @@ function setupNotificationSystem() {
61
61
function setupFormHandler ( ) {
62
62
const form = document . getElementById ( "github-url-form" ) ;
63
63
64
- form . addEventListener ( "submit" , async function ( event ) {
64
+ form . addEventListener ( "submit" , async function ( event ) {
65
65
event . preventDefault ( ) ;
66
66
67
67
const submitButton = document . getElementById ( "repo-url-button" ) ;
@@ -71,30 +71,30 @@ function setupFormHandler() {
71
71
72
72
try {
73
73
const repoURL = document . getElementById ( "repo-url" ) . value ;
74
-
74
+
75
75
if ( repoURL . length == 0 ) {
76
76
throw new Error ( "Please enter a GitHub repository URL" ) ;
77
77
}
78
-
78
+
79
79
const repoInfo = extractGitHubInfo ( repoURL ) ;
80
-
80
+
81
81
if ( ! repoInfo ) {
82
82
throw new Error ( "Invalid GitHub URL format. Please enter a valid GitHub repository URL ->(https://github.com/username/repository)" ) ;
83
83
}
84
-
84
+
85
85
const repositoryInfo = await getRepoInformation ( repoInfo ) ;
86
86
const languages = await getRepoLanguages ( repoInfo )
87
-
87
+
88
88
if ( repositoryInfo ) {
89
89
preFillFields ( repositoryInfo , languages ) ;
90
90
notificationSystem . success ( "Repository data loaded successfully!" ) ;
91
91
} else {
92
92
throw new Error ( "Could not fetch repository information. Please check the URL and try again." ) ;
93
93
}
94
-
94
+
95
95
} catch ( error ) {
96
96
console . error ( error . message ) ;
97
- notificationSystem . error ( error . message ) ;
97
+ notificationSystem . error ( error . message ) ;
98
98
} finally {
99
99
submitButton . value = "Submit" ;
100
100
submitButton . disabled = false ;
@@ -106,14 +106,14 @@ function extractGitHubInfo(url) {
106
106
// Regex pattern to match GitHub URLs and extract org and repo
107
107
const regex = / (?: h t t p s ? : \/ \/ ) ? (?: w w w \. ) ? g i t h u b \. c o m \/ ( [ ^ \/ ] + ) \/ ( [ ^ \/ \s ] + ) / ;
108
108
const match = url . match ( regex ) ;
109
-
109
+
110
110
if ( match && match . length === 3 ) {
111
111
return {
112
112
organization : match [ 1 ] ,
113
113
repository : match [ 2 ]
114
114
} ;
115
115
}
116
-
116
+
117
117
return null ;
118
118
}
119
119
@@ -123,7 +123,7 @@ async function getRepoInformation(repoInfo) {
123
123
124
124
try {
125
125
const response = await fetch ( endpoint ) ;
126
-
126
+
127
127
if ( ! response . ok ) {
128
128
throw new Error ( `GitHub API error (${ response . status } ): ${ response . statusText } ` ) ;
129
129
}
@@ -139,7 +139,7 @@ async function getRepoLanguages(repoInfo) {
139
139
140
140
try {
141
141
const response = await fetch ( endpoint ) ;
142
-
142
+
143
143
if ( ! response . ok ) {
144
144
throw new Error ( `GitHub API error (${ response . status } ): ${ response . statusText } ` ) ;
145
145
}
@@ -157,65 +157,145 @@ function preFillFields(repoData, languages) {
157
157
}
158
158
159
159
try {
160
- const currentSubmission = { }
161
-
162
- window . formIOInstance . components . forEach ( component => {
163
- if ( component . components ) {
164
- component . components . forEach ( nestedComp => {
165
- if ( nestedComp . key ) {
166
- currentSubmission [ nestedComp . key ] = nestedComp . getValue ( )
167
- }
168
- } ) ;
169
- } else if ( component . key ) {
170
- currentSubmission [ component . key ] = component . getValue ( )
171
- }
172
- } )
173
-
174
- let licenses = [ ] ;
160
+ const form = window . formIOInstance
161
+
162
+ // Updating VCS to git - typically always be git
163
+ form . getComponent ( 'vcs' ) . setValue ( 'git' )
164
+
165
+ // Updating organization - only option available
166
+ form . getComponent ( 'organization' ) . setValue ( 'Centers for Medicare & Medicaid Services' )
167
+
168
+ // Updating visibility
169
+ form . getComponent ( 'repositoryVisibility' ) . setValue ( repoData . private ? 'private' : 'public' )
170
+
171
+ // Updating name
172
+ if ( repoData . name ) {
173
+ form . getComponent ( 'name' ) . setValue ( repoData . name )
174
+ }
175
+
176
+ // Updating description
177
+ if ( repoData . description ) {
178
+ form . getComponent ( 'description' ) . setValue ( repoData . description )
179
+ }
180
+
181
+ // Updating URL
182
+ if ( repoData . html_url ) {
183
+ form . getComponent ( 'repositoryURL' ) . setValue ( repoData . html_url )
184
+ }
185
+
186
+ // Updating forks
187
+ if ( repoData . forks_count !== undefined ) {
188
+ const reuseFrequencyComp = form . getComponent ( 'reuseFrequency' )
189
+ const currentReuse = { }
190
+
191
+ currentReuse . forks = repoData . forks_count
192
+ reuseFrequencyComp . setValue ( currentReuse )
193
+ }
194
+
195
+ // Updating license object
175
196
if ( repoData . license && repoData . license . spdx_id ) {
176
- licenses . push ( {
197
+ const permissionsComp = form . getComponent ( 'permissions' ) ;
198
+ const currentPermissions = permissionsComp . getValue ( ) || { } ;
199
+
200
+ currentPermissions . licenses = currentPermissions . licenses || [ ] ;
201
+
202
+ const licenseObj = {
177
203
name : repoData . license . spdx_id ,
178
204
URL : repoData . html_url + "/blob/main/LICENSE"
179
- } ) ;
205
+ } ;
206
+
207
+ currentPermissions . licenses = [ licenseObj ] ;
208
+ permissionsComp . setValue ( currentPermissions ) ;
180
209
}
181
-
182
- const newSubmission = {
183
- name : repoData . name || '' ,
184
- description : repoData . description || '' ,
185
- repositoryURL : repoData . html_url || '' ,
186
- repositoryVisibility : repoData . private ? "private" : "public" ,
187
- vcs : 'git' ,
188
- permissions : {
189
- licenses : licenses
190
- } ,
191
- reuseFrequency : {
192
- forks : repoData . forks_count || 0
193
- } ,
194
- languages : Object . keys ( languages ) || [ ] ,
195
- date : {
196
- created : repoData . created_at || '' ,
197
- lastModified : repoData . updated_at || '' ,
198
- metaDataLastUpdated : new Date ( ) . toISOString ( )
199
- } ,
200
- tags : repoData . topics || [ ] ,
201
- feedbackMechanisms : [ repoData . html_url + "/issues" ]
210
+
211
+ // Update languages list by combining any the user has entered
212
+ if ( languages ) {
213
+ const languagesComp = form . getComponent ( 'languages' )
214
+ const newLanguages = Object . keys ( languages ) || [ ]
215
+
216
+ languagesComp . setValue ( newLanguages )
202
217
}
203
-
204
- const mergedSubmission = { ...currentSubmission , ...newSubmission }
205
218
206
- window . formIOInstance . setSubmission ( { data : mergedSubmission } )
207
-
219
+ // Update dates
220
+ if ( repoData . created_at && repoData . updated_at ) {
221
+ const dateComp = form . getComponent ( 'date' )
222
+ const currentDate = dateComp . getValue ( ) || { }
223
+
224
+ currentDate . created = repoData . created_at ;
225
+ currentDate . lastModified = repoData . updated_at
226
+ currentDate . metaDataLastUpdated = new Date ( ) . toISOString ( )
227
+
228
+ dateComp . setValue ( currentDate )
229
+ }
230
+
231
+ // Update tags
232
+ if ( repoData . topics ) {
233
+ const tagsComp = form . getComponent ( 'tags' )
234
+
235
+ const newTags = [ ...repoData . topics ] || [ ]
236
+ tagsComp . setValue ( newTags )
237
+ }
238
+
239
+ // Update feedback mechanisms
240
+ if ( repoData . html_url ) {
241
+ const feedbackComp = form . getComponent ( 'feedbackMechanisms' )
242
+ let currentFeedback = form . getComponent ( 'feedbackMechanisms' ) . getValue
243
+ currentFeedback = [ ]
244
+
245
+ const issuesUrl = repoData . html_url + "/issues"
246
+
247
+ currentFeedback . push ( issuesUrl )
248
+ feedbackComp . setValue ( currentFeedback )
249
+ }
250
+
251
+ // Update upstream
252
+ if ( repoData . html_url ) {
253
+ const upstreamComp = form . getComponent ( 'upstream' )
254
+ const urlParts = repoData . html_url . split ( '/' )
255
+
256
+ if ( urlParts . length >= 2 ) {
257
+ const org = urlParts [ urlParts . length - 2 ]
258
+ const repo = urlParts [ urlParts . length - 1 ]
259
+
260
+ const dependenciesUrl = `https://github.com/${ org } /${ repo } /network/dependencies`
261
+
262
+ upstreamComp . setValue ( dependenciesUrl )
263
+ }
264
+ }
265
+
266
+ // Update repositoryHost
267
+ if ( repoData . html_url ) {
268
+ if ( repoData . html_url . includes ( 'github.cms.gov' ) ) {
269
+ form . getComponent ( 'repositoryHost' ) . setValue ( 'github.cms.gov' )
270
+ } else if ( repoData . html_url . includes ( 'github.com/CMSgov' ) ) {
271
+ form . getComponent ( 'repositoryHost' ) . setValue ( 'github.com/CMSgov' )
272
+ } else if ( repoData . html_url . includes ( 'github.com/CMS-Enterprise' ) ) {
273
+ form . getComponent ( 'repositoryHost' ) . setValue ( 'github.com/CMS-Enterprise' )
274
+ } else if ( repoData . html_url . includes ( 'github.com/DSACMS' ) ) {
275
+ form . getComponent ( 'repositoryHost' ) . setValue ( 'github.com/DSACMS' )
276
+ }
277
+ }
278
+
279
+ // fields to potentially automate
280
+ // clones, but this is only tracked for every 14 days
281
+ // status, by checking if its public, we can assume its production and check if its archival
282
+ // laborHours, by running a script? this might be harder since we need SCC
283
+ // maturityModel, we could check to see if certain files / sections live within a repo and make a guess like that
284
+ // usageType, by assuming that if its public = openSource and if private = governmnetWideReuse
285
+
286
+ notificationSystem . success ( "Repository data loaded successfully!" )
287
+
208
288
} catch ( error ) {
209
- notificationSystem . error ( "Error filling form fields with repository data. Please refresh and try again" ) ;
210
- console . error ( "Form fill error:" , error ) ;
289
+ notificationSystem . error ( "Error filling form fields with repository data. Please refresh and try again" )
290
+ console . error ( "Form fill error:" , error )
211
291
}
212
292
}
213
293
214
294
// This is global so we could use this throughout the website!
215
- window . showErrorNotification = function ( message ) {
295
+ window . showErrorNotification = function ( message ) {
216
296
notificationSystem . error ( message ) ;
217
297
} ;
218
298
219
- window . showSuccessNotification = function ( message ) {
299
+ window . showSuccessNotification = function ( message ) {
220
300
notificationSystem . success ( message ) ;
221
301
} ;
0 commit comments