Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor - store functions #157

Merged
merged 4 commits into from
Dec 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 56 additions & 72 deletions src/store/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
const debug = require('debug')('store')

const upsertGithubRepository = knex => async (repository, orgId) => {
debug(`Upserting repository (${repository.full_name})...`)
const getAllFn = knex => (table) => {
debug(`Fetching all records from ${table}...`)
return knex(table).select('*')
}

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('*')
} else {
return knex('github_repositories').insert({ ...repository, github_organization_id: orgId }).returning('*')
}
const addFn = knex => (table, record) => {
debug(`Inserting ${record} in ${table}`)
return knex(table).insert(record).returning('*')
}

const getAllGithubOrganizations = knex => async () => {
debug('Getting all GitHub organizations...')
return knex('github_organizations').select()
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(table)
.insert({ ...uniqueCriteria, ...data })
.returning('*')
}
}

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('*')
Expand Down Expand Up @@ -46,89 +51,68 @@ 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) => {
const getCheckByCodeName = knex => (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) => {
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 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) {
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 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 }
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 upsertComplianceCheckResult = (knex) => (data) =>
upsertRecord({
knex,
table: 'compliance_checks_results',
uniqueCriteria: { compliance_check_id: data.compliance_check_id },
data
})

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 upsertGithubRepository = (knex) => (repository, orgId) => upsertRecord({
table: 'github_repositories',
knex,
uniqueCriteria: {
full_name: repository.full_name,
github_organization_id: orgId
},
data: { ...repository, github_organization_id: orgId }
})

const initializeStore = (knex) => {
debug('Initializing store...')
const getAll = getAllFn(knex)
const addTo = addFn(knex)
return {
addProject: addProject(knex),
addGithubOrganization: addGithubOrganization(knex),
getAllGithubOrganizations: getAllGithubOrganizations(knex),
getAllGithubOrganizations: () => getAll('github_organizations'),
updateGithubOrganization: updateGithubOrganization(knex),
upsertGithubRepository: upsertGithubRepository(knex),
getAllComplianceChecks: getAllComplianceChecks(knex),
getAllComplianceChecks: () => getAll('compliance_checks'),
getCheckByCodeName: getCheckByCodeName(knex),
getAllProjects: getAllProjects(knex),
getAllProjects: () => getAll('projects'),
deleteTasksByComplianceCheckId: deleteTasksByComplianceCheckId(knex),
deleteAlertsByComplianceCheckId: deleteAlertsByComplianceCheckId(knex),
addAlert: addAlert(knex),
addTask: addTask(knex),
addAlert: (alert) => addTo('compliance_checks_alerts', alert),
addTask: (task) => addTo('compliance_checks_tasks', task),
upsertComplianceCheckResult: upsertComplianceCheckResult(knex),
getAllSSoftwareDesignTrainings: getAllSSoftwareDesignTrainings(knex),
getAllGithubRepositories: getAllGithubRepositories(knex),
getAllSSoftwareDesignTrainings: () => getAll('software_design_training'),
getAllGithubRepositories: () => getAll('github_repositories'),
upsertOSSFScorecard: upsertOSSFScorecard(knex)
}
}
Expand Down
Loading