@@ -3,7 +3,7 @@ const { simplifyObject } = require('@ulisesgascon/simplify-object')
3
3
const { github } = require ( '../providers' )
4
4
const { initializeStore } = require ( '../store' )
5
5
const { logger } = require ( '../utils' )
6
- const { validateGithubOrg } = require ( '../schemas' )
6
+ const { validateGithubOrg, validateGithubListOrgRepos , validateGithubRepository } = require ( '../schemas' )
7
7
8
8
const mapOrgData = ( data ) => {
9
9
const mappedData = simplifyObject ( data , {
@@ -37,6 +37,44 @@ const mapOrgData = (data) => {
37
37
return mappedData
38
38
}
39
39
40
+ const mapRepoData = ( data ) => {
41
+ const mappedData = simplifyObject ( data , {
42
+ include : [
43
+ 'node_id' , 'name' , 'full_name' ,
44
+ 'html_url' , 'description' , 'fork' , 'url' ,
45
+ 'git_url' , 'ssh_url' , 'clone_url' , 'svn_url' ,
46
+ 'homepage' , 'size' , 'stargazers_count' , 'watchers_count' ,
47
+ 'language' , 'has_issues' , 'has_projects' , 'has_downloads' ,
48
+ 'has_wiki' , 'has_pages' , 'has_discussions' , 'forks_count' ,
49
+ 'mirror_url' , 'archived' , 'disabled' , 'open_issues_count' ,
50
+ 'allow_forking' , 'is_template' , 'web_commit_signoff_required' ,
51
+ 'topics' , 'visibility' , 'default_branch' , 'allow_squash_merge' ,
52
+ 'allow_merge_commit' , 'allow_rebase_merge' , 'allow_auto_merge' ,
53
+ 'delete_branch_on_merge' , 'allow_update_branch' , 'use_squash_pr_title_as_default' ,
54
+ 'squash_merge_commit_message' , 'squash_merge_commit_title' , 'merge_commit_message' ,
55
+ 'merge_commit_title' , 'network_count' , 'subscribers_count'
56
+ ]
57
+ } )
58
+ // Additional fields that have different names in the database
59
+ mappedData . github_repo_id = data . id
60
+ mappedData . github_created_at = data . created_at
61
+ mappedData . github_updated_at = data . updated_at
62
+ mappedData . github_archived_at = data . archived_at
63
+ // license
64
+ mappedData . license_key = data . license ?. key
65
+ mappedData . license_name = data . license ?. name
66
+ mappedData . license_spdx_id = data . license ?. spdx_id
67
+ mappedData . license_url = data . license ?. url
68
+ mappedData . license_node_id = data . license ?. node_id
69
+ // Security and analysis
70
+ mappedData . secret_scanning_status = data . security_and_analysis ?. secret_scanning ?. status
71
+ mappedData . secret_scanning_push_protection_status = data . security_and_analysis ?. secret_scanning_push_protection ?. status
72
+ mappedData . dependabot_security_updates_status = data . security_and_analysis ?. dependabot_security_updates ?. status
73
+ mappedData . secret_scanning_non_provider_patterns_status = data . security_and_analysis ?. secret_scanning_non_provider_patterns ?. status
74
+ mappedData . secret_scanning_validity_checks_status = data . security_and_analysis ?. secret_scanning_validity_checks ?. status
75
+ return mappedData
76
+ }
77
+
40
78
const updateGithubOrgs = async ( knex ) => {
41
79
const { getAllGithubOrganizations, updateGithubOrganization } = initializeStore ( knex )
42
80
const organizations = await getAllGithubOrganizations ( )
@@ -58,6 +96,39 @@ const updateGithubOrgs = async (knex) => {
58
96
logger . log ( 'GitHub organizations updated successfully' )
59
97
}
60
98
99
+ const upsertGithubRepositories = async ( knex ) => {
100
+ const { getAllGithubOrganizations, upsertGithubRepository } = initializeStore ( knex )
101
+ const organizations = await getAllGithubOrganizations ( )
102
+ debug ( 'Checking stored organizations' )
103
+ if ( organizations . length === 0 ) {
104
+ throw new Error ( 'No organizations found. Please add organizations/projects before running this workflow.' )
105
+ }
106
+
107
+ debug ( 'Fetching repositories for organizations from GitHub API' )
108
+
109
+ await Promise . all ( organizations . map ( async ( org ) => {
110
+ debug ( `Fetching repositories for org (${ org . login } )` )
111
+ const repoList = await github . fetchOrgReposListByLogin ( org . login )
112
+ debug ( `Got ${ repoList . length } repositories for org (${ org . login } )` )
113
+ debug ( 'Validating data' )
114
+ validateGithubListOrgRepos ( repoList )
115
+ debug ( `Enriching all repositories for org (${ org . login } )` )
116
+
117
+ // Enrich and upsert each repository in parallel
118
+ await Promise . all ( repoList . map ( async ( repo ) => {
119
+ debug ( `Enriching repository (${ repo . full_name } )` )
120
+ const repoData = await github . fetchRepoByFullName ( repo . full_name )
121
+ debug ( `Validating repository (${ repo . full_name } )` )
122
+ validateGithubRepository ( repoData )
123
+ debug ( `Transforming repository (${ repo . full_name } ) data` )
124
+ const mappedData = mapRepoData ( repoData )
125
+ debug ( `Upserting repository (${ repo . full_name } )` )
126
+ await upsertGithubRepository ( mappedData , org . id )
127
+ } ) )
128
+ } ) )
129
+ }
130
+
61
131
module . exports = {
62
- updateGithubOrgs
132
+ updateGithubOrgs,
133
+ upsertGithubRepositories
63
134
}
0 commit comments