1- document . addEventListener ( "DOMContentLoaded" , function ( ) {
1+ document . addEventListener ( "DOMContentLoaded" , function ( ) {
22 setupFormHandler ( ) ;
33 setupNotificationSystem ( ) ;
44} ) ;
@@ -8,44 +8,44 @@ document.addEventListener("DOMContentLoaded", function() {
88// The show() method changes the element based on type and displays the message to the user
99// The hide() function makes sure that the notification fades away after 5 seconds
1010const notificationSystem = {
11- show : function ( message , type = 'error' ) {
11+ show : function ( message , type = 'error' ) {
1212 const notification = document . getElementById ( 'notification' ) ;
1313 const messageElement = document . getElementById ( 'notification-message' ) ;
14-
14+
1515 messageElement . textContent = message ;
16-
16+
1717 if ( type === 'error' ) {
1818 notification . style . backgroundColor = '#f8d7da' ;
1919 notification . style . color = '#721c24' ;
2020 notification . style . border = '1px solid #f5c6cb' ;
2121 } else {
2222 notification . style . backgroundColor = '#d4edda' ;
23- notification . style . color = '#155724' ;
23+ notification . style . color = '#155724' ;
2424 notification . style . border = '1px solid #c3e6cb' ;
2525 }
26-
26+
2727 notification . style . display = 'block' ;
2828 setTimeout ( ( ) => {
2929 notification . style . opacity = '1' ;
3030 } , 10 ) ;
31-
31+
3232 clearTimeout ( this . timeout ) ;
3333 this . timeout = setTimeout ( ( ) => this . hide ( ) , 5000 ) ;
3434 } ,
35-
36- hide : function ( ) {
35+
36+ hide : function ( ) {
3737 const notification = document . getElementById ( 'notification' ) ;
3838 notification . style . opacity = '0' ;
3939 setTimeout ( ( ) => {
4040 notification . style . display = 'none' ;
4141 } , 500 ) ;
4242 } ,
43-
44- error : function ( message ) {
43+
44+ error : function ( message ) {
4545 this . show ( message , 'error' ) ;
4646 } ,
47-
48- success : function ( message ) {
47+
48+ success : function ( message ) {
4949 this . show ( message , 'success' ) ;
5050 } ,
5151} ;
@@ -61,7 +61,7 @@ function setupNotificationSystem() {
6161function setupFormHandler ( ) {
6262 const form = document . getElementById ( "github-url-form" ) ;
6363
64- form . addEventListener ( "submit" , async function ( event ) {
64+ form . addEventListener ( "submit" , async function ( event ) {
6565 event . preventDefault ( ) ;
6666
6767 const submitButton = document . getElementById ( "repo-url-button" ) ;
@@ -71,30 +71,30 @@ function setupFormHandler() {
7171
7272 try {
7373 const repoURL = document . getElementById ( "repo-url" ) . value ;
74-
74+
7575 if ( repoURL . length == 0 ) {
7676 throw new Error ( "Please enter a GitHub repository URL" ) ;
7777 }
78-
78+
7979 const repoInfo = extractGitHubInfo ( repoURL ) ;
80-
80+
8181 if ( ! repoInfo ) {
8282 throw new Error ( "Invalid GitHub URL format. Please enter a valid GitHub repository URL ->(https://github.com/username/repository)" ) ;
8383 }
84-
84+
8585 const repositoryInfo = await getRepoInformation ( repoInfo ) ;
8686 const languages = await getRepoLanguages ( repoInfo )
87-
87+
8888 if ( repositoryInfo ) {
8989 preFillFields ( repositoryInfo , languages ) ;
9090 notificationSystem . success ( "Repository data loaded successfully!" ) ;
9191 } else {
9292 throw new Error ( "Could not fetch repository information. Please check the URL and try again." ) ;
9393 }
94-
94+
9595 } catch ( error ) {
9696 console . error ( error . message ) ;
97- notificationSystem . error ( error . message ) ;
97+ notificationSystem . error ( error . message ) ;
9898 } finally {
9999 submitButton . value = "Submit" ;
100100 submitButton . disabled = false ;
@@ -106,14 +106,14 @@ function extractGitHubInfo(url) {
106106 // Regex pattern to match GitHub URLs and extract org and repo
107107 const regex = / (?: h t t p s ? : \/ \/ ) ? (?: w w w \. ) ? g i t h u b \. c o m \/ ( [ ^ \/ ] + ) \/ ( [ ^ \/ \s ] + ) / ;
108108 const match = url . match ( regex ) ;
109-
109+
110110 if ( match && match . length === 3 ) {
111111 return {
112112 organization : match [ 1 ] ,
113113 repository : match [ 2 ]
114114 } ;
115115 }
116-
116+
117117 return null ;
118118}
119119
@@ -123,7 +123,7 @@ async function getRepoInformation(repoInfo) {
123123
124124 try {
125125 const response = await fetch ( endpoint ) ;
126-
126+
127127 if ( ! response . ok ) {
128128 throw new Error ( `GitHub API error (${ response . status } ): ${ response . statusText } ` ) ;
129129 }
@@ -139,7 +139,7 @@ async function getRepoLanguages(repoInfo) {
139139
140140 try {
141141 const response = await fetch ( endpoint ) ;
142-
142+
143143 if ( ! response . ok ) {
144144 throw new Error ( `GitHub API error (${ response . status } ): ${ response . statusText } ` ) ;
145145 }
@@ -157,65 +157,145 @@ function preFillFields(repoData, languages) {
157157 }
158158
159159 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
175196 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 = {
177203 name : repoData . license . spdx_id ,
178204 URL : repoData . html_url + "/blob/main/LICENSE"
179- } ) ;
205+ } ;
206+
207+ currentPermissions . licenses = [ licenseObj ] ;
208+ permissionsComp . setValue ( currentPermissions ) ;
180209 }
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 )
202217 }
203-
204- const mergedSubmission = { ...currentSubmission , ...newSubmission }
205218
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+
208288 } 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 )
211291 }
212292}
213293
214294// This is global so we could use this throughout the website!
215- window . showErrorNotification = function ( message ) {
295+ window . showErrorNotification = function ( message ) {
216296 notificationSystem . error ( message ) ;
217297} ;
218298
219- window . showSuccessNotification = function ( message ) {
299+ window . showSuccessNotification = function ( message ) {
220300 notificationSystem . success ( message ) ;
221301} ;
0 commit comments