From e4553e55f116ea0d4c783f64ec6ca478ec6c2507 Mon Sep 17 00:00:00 2001 From: telekosmos Date: Thu, 19 Dec 2024 11:44:00 +0100 Subject: [PATCH 1/4] chore: refactor getAll* functions --- src/store/index.js | 41 +++++++++++------------------------------ 1 file changed, 11 insertions(+), 30 deletions(-) diff --git a/src/store/index.js b/src/store/index.js index b55d1e9..885a9ed 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -1,5 +1,10 @@ const debug = require('debug')('store') +const getAllFn = (knex) => async (table) => { + debug(`Fetching all records from ${table}...`) + return knex(table).select('*') +} + const upsertGithubRepository = knex => async (repository, orgId) => { debug(`Upserting repository (${repository.full_name})...`) @@ -11,11 +16,6 @@ const upsertGithubRepository = knex => async (repository, orgId) => { } } -const getAllGithubOrganizations = knex => async () => { - debug('Getting all GitHub organizations...') - return knex('github_organizations').select() -} - const updateGithubOrganization = knex => async (organization) => { const { login } = organization debug(`Updating organization (${login})...`) @@ -46,21 +46,11 @@ const addProject = knex => async (project) => { }).returning('*') } -const getAllComplianceChecks = knex => async () => { - debug('Getting all checks...') - return knex('compliance_checks').select().returning('*') -} - const getCheckByCodeName = knex => async (codeName) => { debug(`Getting check by code name (${codeName})...`) return knex('compliance_checks').where({ code_name: codeName }).first() } -const getAllProjects = knex => async () => { - debug('Getting all projects...') - return knex('projects').select().returning('*') -} - const deleteAlertsByComplianceCheckId = knex => async (complianceCheckId) => { debug(`Deleting alerts by compliance_check_id (${complianceCheckId})...`) return knex('compliance_checks_alerts').where({ compliance_check_id: complianceCheckId }).delete() @@ -90,16 +80,6 @@ const upsertComplianceCheckResult = knex => async (result) => { } } -const getAllSSoftwareDesignTrainings = knex => async () => { - debug('Getting all software design trainings...') - return knex('software_design_training').select().returning('*') -} - -const getAllGithubRepositories = knex => async () => { - debug('Getting all GitHub repositories...') - return knex('github_repositories').select().returning('*') -} - const upsertOSSFScorecard = knex => async (scorecard) => { // IMPORTANT: Check for repo_id and commit hash as multiple results can exist for the same repo const query = { github_repository_id: scorecard.github_repository_id, scorecard_commit: scorecard.scorecard_commit } @@ -113,22 +93,23 @@ const upsertOSSFScorecard = knex => async (scorecard) => { const initializeStore = (knex) => { debug('Initializing store...') + const getAll = getAllFn(knex) return { addProject: addProject(knex), addGithubOrganization: addGithubOrganization(knex), - getAllGithubOrganizations: getAllGithubOrganizations(knex), + getAllGithubOrganizations: async () => getAll('github_organizations'), updateGithubOrganization: updateGithubOrganization(knex), upsertGithubRepository: upsertGithubRepository(knex), - getAllComplianceChecks: getAllComplianceChecks(knex), + getAllComplianceChecks: async () => getAll('compliance_checks'), getCheckByCodeName: getCheckByCodeName(knex), - getAllProjects: getAllProjects(knex), + getAllProjects: async () => getAll('projects'), deleteTasksByComplianceCheckId: deleteTasksByComplianceCheckId(knex), deleteAlertsByComplianceCheckId: deleteAlertsByComplianceCheckId(knex), addAlert: addAlert(knex), addTask: addTask(knex), upsertComplianceCheckResult: upsertComplianceCheckResult(knex), - getAllSSoftwareDesignTrainings: getAllSSoftwareDesignTrainings(knex), - getAllGithubRepositories: getAllGithubRepositories(knex), + getAllSSoftwareDesignTrainings: async () => getAll('software_design_training'), + getAllGithubRepositories: async () => getAll('github_repositories'), upsertOSSFScorecard: upsertOSSFScorecard(knex) } } From 966e780da0bd9df0c725b7afba8d72c52f44836e Mon Sep 17 00:00:00 2001 From: telekosmos Date: Thu, 19 Dec 2024 12:26:14 +0100 Subject: [PATCH 2/4] chore: refactor simple add -insert- functions --- src/store/index.js | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/store/index.js b/src/store/index.js index 885a9ed..54da14d 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -1,10 +1,15 @@ const debug = require('debug')('store') -const getAllFn = (knex) => async (table) => { +const getAllFn = knex => async (table) => { debug(`Fetching all records from ${table}...`) return knex(table).select('*') } +const addFn = knex => async (table, record) => { + debug(`Inserting ${record} in ${table}`) + return knex(table).insert(record).returning('*') +} + const upsertGithubRepository = knex => async (repository, orgId) => { debug(`Upserting repository (${repository.full_name})...`) @@ -61,16 +66,6 @@ const deleteTasksByComplianceCheckId = knex => async (complianceCheckId) => { return knex('compliance_checks_tasks').where({ compliance_check_id: complianceCheckId }).delete() } -const addAlert = knex => async (alert) => { - debug('Inserting alert...') - return knex('compliance_checks_alerts').insert(alert).returning('*') -} - -const addTask = knex => async (task) => { - debug('Inserting task...') - return knex('compliance_checks_tasks').insert(task).returning('*') -} - const upsertComplianceCheckResult = knex => async (result) => { const existingComplianceCheck = await knex('compliance_checks_results').where({ compliance_check_id: result.compliance_check_id }).first() if (existingComplianceCheck) { @@ -94,6 +89,7 @@ const upsertOSSFScorecard = knex => async (scorecard) => { const initializeStore = (knex) => { debug('Initializing store...') const getAll = getAllFn(knex) + const addTo = addFn(knex) return { addProject: addProject(knex), addGithubOrganization: addGithubOrganization(knex), @@ -105,8 +101,8 @@ const initializeStore = (knex) => { getAllProjects: async () => getAll('projects'), deleteTasksByComplianceCheckId: deleteTasksByComplianceCheckId(knex), deleteAlertsByComplianceCheckId: deleteAlertsByComplianceCheckId(knex), - addAlert: addAlert(knex), - addTask: addTask(knex), + addAlert: async (alert) => addTo('compliance_checks_alerts', alert), + addTask: async (task) => addTo('compliance_checks_tasks', task), upsertComplianceCheckResult: upsertComplianceCheckResult(knex), getAllSSoftwareDesignTrainings: async () => getAll('software_design_training'), getAllGithubRepositories: async () => getAll('github_repositories'), From 1fe2e590360d15a9c0c4ca71507d98563d146aae Mon Sep 17 00:00:00 2001 From: telekosmos Date: Thu, 19 Dec 2024 12:57:46 +0100 Subject: [PATCH 3/4] chore: refactor upsert functions --- src/store/index.js | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/store/index.js b/src/store/index.js index 54da14d..7995a22 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -10,14 +10,14 @@ const addFn = knex => async (table, record) => { return knex(table).insert(record).returning('*') } -const upsertGithubRepository = knex => async (repository, orgId) => { - debug(`Upserting repository (${repository.full_name})...`) - - const existingRepository = await knex('github_repositories').where({ full_name: repository.full_name }).first() - if (existingRepository) { - return knex('github_repositories').where({ full_name: repository.full_name, github_organization_id: orgId }).update(repository).returning('*') +const upsertRecord = async (knex, table, uniqueCriteria, data) => { + const existingRecord = await knex(table).where(uniqueCriteria).first() + if (existingRecord) { + return knex(table).where(uniqueCriteria).update(data).returning('*') } else { - return knex('github_repositories').insert({ ...repository, github_organization_id: orgId }).returning('*') + return knex(table) + .insert({ ...uniqueCriteria, ...data }) + .returning('*') } } @@ -66,24 +66,24 @@ const deleteTasksByComplianceCheckId = knex => async (complianceCheckId) => { return knex('compliance_checks_tasks').where({ compliance_check_id: complianceCheckId }).delete() } -const upsertComplianceCheckResult = knex => async (result) => { - const existingComplianceCheck = await knex('compliance_checks_results').where({ compliance_check_id: result.compliance_check_id }).first() - if (existingComplianceCheck) { - return knex('compliance_checks_results').where({ compliance_check_id: result.compliance_check_id }).update(result).returning('*') - } else { - return knex('compliance_checks_results').insert(result).returning('*') +const upsertComplianceCheckResult = (knex) => async (result) => + upsertRecord(knex, 'compliance_checks_results', { compliance_check_id: result.compliance_check_id }, result) + +const upsertOSSFScorecard = (knex) => async (scorecard) => { + const uniqueCriteria = { + github_repository_id: scorecard.github_repository_id, + scorecard_commit: scorecard.scorecard_commit } + return upsertRecord(knex, 'ossf_scorecard_results', uniqueCriteria, scorecard) } -const upsertOSSFScorecard = knex => async (scorecard) => { - // IMPORTANT: Check for repo_id and commit hash as multiple results can exist for the same repo - const query = { github_repository_id: scorecard.github_repository_id, scorecard_commit: scorecard.scorecard_commit } - const existingScorecard = await knex('ossf_scorecard_results').where(query).first() - if (existingScorecard) { - return knex('ossf_scorecard_results').where(query).update(scorecard).returning('*') - } else { - return knex('ossf_scorecard_results').insert(scorecard).returning('*') +const upsertGithubRepository = (knex) => async (repository, orgId) => { + const uniqueCriteria = { + full_name: repository.full_name, + github_organization_id: orgId } + const data = { ...repository, github_organization_id: orgId } + return upsertRecord(knex, 'github_repositories', uniqueCriteria, data) } const initializeStore = (knex) => { From 31dd780e152091764b68875ac6c45d871a18ed04 Mon Sep 17 00:00:00 2001 From: Ulises Gascon Date: Thu, 19 Dec 2024 20:33:26 +0100 Subject: [PATCH 4/4] chore: refactor to reduce async usage and support 4 arguments in store --- src/store/index.js | 67 +++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/src/store/index.js b/src/store/index.js index 7995a22..9f56cc5 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -1,16 +1,16 @@ const debug = require('debug')('store') -const getAllFn = knex => async (table) => { +const getAllFn = knex => (table) => { debug(`Fetching all records from ${table}...`) return knex(table).select('*') } -const addFn = knex => async (table, record) => { +const addFn = knex => (table, record) => { debug(`Inserting ${record} in ${table}`) return knex(table).insert(record).returning('*') } -const upsertRecord = async (knex, table, uniqueCriteria, data) => { +const upsertRecord = async ({ knex, table, uniqueCriteria, data }) => { const existingRecord = await knex(table).where(uniqueCriteria).first() if (existingRecord) { return knex(table).where(uniqueCriteria).update(data).returning('*') @@ -21,7 +21,7 @@ const upsertRecord = async (knex, table, uniqueCriteria, data) => { } } -const updateGithubOrganization = knex => async (organization) => { +const updateGithubOrganization = knex => (organization) => { const { login } = organization debug(`Updating organization (${login})...`) return knex('github_organizations').where({ login }).update(organization).returning('*') @@ -51,40 +51,47 @@ const addProject = knex => async (project) => { }).returning('*') } -const getCheckByCodeName = knex => async (codeName) => { +const getCheckByCodeName = knex => (codeName) => { debug(`Getting check by code name (${codeName})...`) return knex('compliance_checks').where({ code_name: codeName }).first() } -const deleteAlertsByComplianceCheckId = knex => async (complianceCheckId) => { +const deleteAlertsByComplianceCheckId = knex => (complianceCheckId) => { debug(`Deleting alerts by compliance_check_id (${complianceCheckId})...`) return knex('compliance_checks_alerts').where({ compliance_check_id: complianceCheckId }).delete() } -const deleteTasksByComplianceCheckId = knex => async (complianceCheckId) => { +const deleteTasksByComplianceCheckId = knex => (complianceCheckId) => { debug(`Deleting tasks by compliance_check_id (${complianceCheckId})...`) return knex('compliance_checks_tasks').where({ compliance_check_id: complianceCheckId }).delete() } +const upsertComplianceCheckResult = (knex) => (data) => + upsertRecord({ + knex, + table: 'compliance_checks_results', + uniqueCriteria: { compliance_check_id: data.compliance_check_id }, + data + }) -const upsertComplianceCheckResult = (knex) => async (result) => - upsertRecord(knex, 'compliance_checks_results', { compliance_check_id: result.compliance_check_id }, result) +const upsertOSSFScorecard = (knex) => (data) => upsertRecord({ + table: 'ossf_scorecard_results', + knex, + uniqueCriteria: { + github_repository_id: data.github_repository_id, + scorecard_commit: data.scorecard_commit + }, + data +}) -const upsertOSSFScorecard = (knex) => async (scorecard) => { - const uniqueCriteria = { - github_repository_id: scorecard.github_repository_id, - scorecard_commit: scorecard.scorecard_commit - } - return upsertRecord(knex, 'ossf_scorecard_results', uniqueCriteria, scorecard) -} - -const upsertGithubRepository = (knex) => async (repository, orgId) => { - const uniqueCriteria = { +const upsertGithubRepository = (knex) => (repository, orgId) => upsertRecord({ + table: 'github_repositories', + knex, + uniqueCriteria: { full_name: repository.full_name, github_organization_id: orgId - } - const data = { ...repository, github_organization_id: orgId } - return upsertRecord(knex, 'github_repositories', uniqueCriteria, data) -} + }, + data: { ...repository, github_organization_id: orgId } +}) const initializeStore = (knex) => { debug('Initializing store...') @@ -93,19 +100,19 @@ const initializeStore = (knex) => { return { addProject: addProject(knex), addGithubOrganization: addGithubOrganization(knex), - getAllGithubOrganizations: async () => getAll('github_organizations'), + getAllGithubOrganizations: () => getAll('github_organizations'), updateGithubOrganization: updateGithubOrganization(knex), upsertGithubRepository: upsertGithubRepository(knex), - getAllComplianceChecks: async () => getAll('compliance_checks'), + getAllComplianceChecks: () => getAll('compliance_checks'), getCheckByCodeName: getCheckByCodeName(knex), - getAllProjects: async () => getAll('projects'), + getAllProjects: () => getAll('projects'), deleteTasksByComplianceCheckId: deleteTasksByComplianceCheckId(knex), deleteAlertsByComplianceCheckId: deleteAlertsByComplianceCheckId(knex), - addAlert: async (alert) => addTo('compliance_checks_alerts', alert), - addTask: async (task) => addTo('compliance_checks_tasks', task), + addAlert: (alert) => addTo('compliance_checks_alerts', alert), + addTask: (task) => addTo('compliance_checks_tasks', task), upsertComplianceCheckResult: upsertComplianceCheckResult(knex), - getAllSSoftwareDesignTrainings: async () => getAll('software_design_training'), - getAllGithubRepositories: async () => getAll('github_repositories'), + getAllSSoftwareDesignTrainings: () => getAll('software_design_training'), + getAllGithubRepositories: () => getAll('github_repositories'), upsertOSSFScorecard: upsertOSSFScorecard(knex) } }