Skip to content

Commit

Permalink
Merge pull request #25 from secure-dashboards/fix/add-relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
UlisesGascon authored Dec 5, 2024
2 parents 147776a + dbd4e7f commit 191615f
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 17 deletions.
4 changes: 2 additions & 2 deletions __tests__/cli/workflows.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ describe('run update-github-orgs - Interactive Mode', () => {

test('Should update the project with new information available', async () => {
// Prepare the database
await addProject(knex, { name: sampleGithubOrg.login, category: 'impact' })
await addGithubOrg(knex, { login: sampleGithubOrg.login, html_url: sampleGithubOrg.html_url })
const project = await addProject(knex, { name: sampleGithubOrg.login, category: 'impact' })
await addGithubOrg(knex, { login: sampleGithubOrg.login, html_url: sampleGithubOrg.html_url, project_id: project.id })
const projects = await getAllProjects(knex)
expect(projects.length).toBe(1)
let githubOrgs = await getAllGithubOrgs(knex)
Expand Down
7 changes: 5 additions & 2 deletions src/cli/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async function runAddProjectCommand (knex, options = {}) {

answers.githubUrls = Array.isArray(answers.githubUrls) ? answers.githubUrls : stringToArray(answers.githubUrls)

await addProject({
const [project] = await addProject({
name: answers.name.toLowerCase(),
category: answers.category
})
Expand All @@ -97,9 +97,12 @@ async function runAddProjectCommand (knex, options = {}) {

await Promise.all(answers.githubUrls.map((url) => addGithubOrganization({
html_url: url,
login: url.split('https://github.com/')[1]
login: url.split('https://github.com/')[1],
project_id: project.id
})))

debug(`All orgs were stored and linked to (${answers.name}) added successfully!`)

return answers
}

Expand Down
5 changes: 1 addition & 4 deletions src/database/migrations/1733318617773_add_github_orgs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ exports.up = async (knex) => {
table.integer('github_org_id').unique()
table.string('node_id')
table.string('url')
table.string('repos_url')
table.string('avatar_url')
table.text('description')
table.string('name')
table.string('company')
table.string('blog')
table.string('location')
table.string('email')
table.string('twitter_username')
table.boolean('is_verified')
table.boolean('has_organization_projects')
Expand Down Expand Up @@ -57,6 +55,7 @@ exports.up = async (knex) => {

// Foreign key to 'projects' table
table.integer('project_id')
.notNullable()
.unsigned()
.references('id')
.inTable('projects')
Expand All @@ -78,6 +77,4 @@ exports.down = async (knex) => {
await knex.raw('DROP TRIGGER IF EXISTS set_updated_at_github_organizations ON github_organizations;')
// Drop table
await knex.schema.dropTableIfExists('github_organizations')
// Drop the reusable function
await knex.raw('DROP FUNCTION IF EXISTS update_updated_at_column;')
}
4 changes: 1 addition & 3 deletions src/database/schema/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,12 @@ CREATE TABLE public.github_organizations (
github_org_id integer,
node_id character varying(255),
url character varying(255),
repos_url character varying(255),
avatar_url character varying(255),
description text,
name character varying(255),
company character varying(255),
blog character varying(255),
location character varying(255),
email character varying(255),
twitter_username character varying(255),
is_verified boolean,
has_organization_projects boolean,
Expand Down Expand Up @@ -93,7 +91,7 @@ CREATE TABLE public.github_organizations (
github_archived_at timestamp with time zone,
created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
project_id integer
project_id integer NOT NULL
);


Expand Down
9 changes: 3 additions & 6 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const getAllGithubOrganizations = knex => async () => {
const updateGithubOrganization = knex => async (organization) => {
const { login } = organization
debug(`Updating organization (${login})...`)
return knex('github_organizations').where({ login }).update(organization)
return knex('github_organizations').where({ login }).update(organization).returning('*')
}

const addGithubOrganization = knex => async (organization) => {
Expand All @@ -18,10 +18,7 @@ const addGithubOrganization = knex => async (organization) => {
throw new Error(`Organization with login (${organization.login}) already exists`)
}
debug(`Inserting organization (${organization.login})...`)
return knex('github_organizations').insert({
html_url: organization.html_url,
login: organization.login
})
return knex('github_organizations').insert(organization).returning('*')
}

const addProject = knex => async (project) => {
Expand All @@ -35,7 +32,7 @@ const addProject = knex => async (project) => {
return knex('projects').insert({
name,
category
})
}).returning('*')
}

const initializeStore = (knex) => {
Expand Down

0 comments on commit 191615f

Please sign in to comment.