Skip to content

Commit 969b48e

Browse files
committed
feat: add workflow upsert-github-repositories
1 parent 7445918 commit 969b48e

File tree

2 files changed

+84
-7
lines changed

2 files changed

+84
-7
lines changed

src/cli/workflows.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
const inquirer = require('inquirer').default
2-
const { updateGithubOrgs } = require('../workflows')
2+
const debug = require('debug')('cli:workflows')
3+
const { updateGithubOrgs, upsertGithubRepositories } = require('../workflows')
34
const { logger } = require('../utils')
45

56
const commandList = [{
67
name: 'update-github-orgs',
7-
description: 'Check the organizations stored and update the information with the GitHub API.'
8+
description: 'Check the organizations stored and update the information with the GitHub API.',
9+
workflow: updateGithubOrgs
10+
}, {
11+
name: 'upsert-github-repositories',
12+
description: 'Check the organizations stored and update/create the information related to the repositories with the GitHub API.',
13+
workflow: upsertGithubRepositories
814
}]
915

1016
const validCommandNames = commandList.map(({ name }) => name)
@@ -32,9 +38,9 @@ async function runWorkflowCommand (knex, options = {}) {
3238
}
3339
])
3440

35-
if (answers.name === 'update-github-orgs') {
36-
await updateGithubOrgs(knex)
37-
}
41+
const command = commandList.find(({ name }) => name === answers.name)
42+
debug(`Running workflow: ${command.name}`)
43+
await command.workflow(knex)
3844

3945
return answers
4046
}

src/workflows/index.js

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { simplifyObject } = require('@ulisesgascon/simplify-object')
33
const { github } = require('../providers')
44
const { initializeStore } = require('../store')
55
const { logger } = require('../utils')
6-
const { validateGithubOrg } = require('../schemas')
6+
const { validateGithubOrg, validateGithubListOrgRepos, validateGithubRepository } = require('../schemas')
77

88
const mapOrgData = (data) => {
99
const mappedData = simplifyObject(data, {
@@ -37,6 +37,44 @@ const mapOrgData = (data) => {
3737
return mappedData
3838
}
3939

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+
4078
const updateGithubOrgs = async (knex) => {
4179
const { getAllGithubOrganizations, updateGithubOrganization } = initializeStore(knex)
4280
const organizations = await getAllGithubOrganizations()
@@ -58,6 +96,39 @@ const updateGithubOrgs = async (knex) => {
5896
logger.log('GitHub organizations updated successfully')
5997
}
6098

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+
61131
module.exports = {
62-
updateGithubOrgs
132+
updateGithubOrgs,
133+
upsertGithubRepositories
63134
}

0 commit comments

Comments
 (0)