From 97503bb6a1cd827592d2838ac495285ac51acf69 Mon Sep 17 00:00:00 2001 From: Ulises Gascon Date: Wed, 4 Dec 2024 15:39:30 +0100 Subject: [PATCH] feat: add a project will create associated records for the GitHub orgs --- __tests__/cli.test.js | 20 +++++++++++++++----- src/cli/project.js | 13 ++++++------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/__tests__/cli.test.js b/__tests__/cli.test.js index 1f230d3..9519e32 100644 --- a/__tests__/cli.test.js +++ b/__tests__/cli.test.js @@ -2,7 +2,7 @@ const inquirer = require('inquirer').default const knexInit = require('knex') const { getConfig } = require('../src/config/') const { runAddProjectCommand } = require('../src/cli') -const { resetDatabase, getAllProjects } = require('./utils') +const { resetDatabase, getAllProjects, getAllGithubOrgs } = require('./utils') const { dbSettings } = getConfig('test') @@ -35,12 +35,17 @@ describe('Interactive Mode', () => { test('Add a project with name, GitHub URLs, and category', async () => { let projects = await getAllProjects(knex) expect(projects.length).toBe(0) + let githubOrgs = await getAllGithubOrgs(knex) + expect(githubOrgs.length).toBe(0) await runAddProjectCommand(knex, {}) projects = await getAllProjects(knex) expect(projects.length).toBe(1) expect(projects[0].name).toBe('eslint') expect(projects[0].category).toBe('impact') - // @TODO: Add test for githubUrls when it is implemented + githubOrgs = await getAllGithubOrgs(knex) + expect(githubOrgs.length).toBe(1) + expect(githubOrgs[0].login).toBe('eslint') + expect(githubOrgs[0].html_url).toBe('https://github.com/eslint') }) test('Prevent to add a project that already exists', async () => { @@ -51,7 +56,7 @@ describe('Interactive Mode', () => { expect(projects.length).toBe(1) await expect(runAddProjectCommand(knex, {})) .rejects - .toThrow('Project with name eslint already exists') + .toThrow('Project with name (eslint) already exists') projects = await getAllProjects(knex) expect(projects.length).toBe(1) }) @@ -61,12 +66,17 @@ describe('Non-Interactive Mode', () => { test('Add a project with name, GitHub URLs, and category', async () => { let projects = await getAllProjects(knex) expect(projects.length).toBe(0) + let githubOrgs = await getAllGithubOrgs(knex) + expect(githubOrgs.length).toBe(0) await runAddProjectCommand(knex, { name: 'eslint', githubUrls: ['https://github.com/eslint'], category: 'impact' }) projects = await getAllProjects(knex) expect(projects.length).toBe(1) expect(projects[0].name).toBe('eslint') expect(projects[0].category).toBe('impact') - // @TODO: Add test for githubUrls when it is implemented + githubOrgs = await getAllGithubOrgs(knex) + expect(githubOrgs.length).toBe(1) + expect(githubOrgs[0].login).toBe('eslint') + expect(githubOrgs[0].html_url).toBe('https://github.com/eslint') }) test('Prevent to add a project that already exists', async () => { @@ -77,7 +87,7 @@ describe('Non-Interactive Mode', () => { expect(projects.length).toBe(1) await expect(runAddProjectCommand(knex, { name: 'eslint', githubUrls: ['https://github.com/eslint'], category: 'impact' })) .rejects - .toThrow('Project with name eslint already exists') + .toThrow('Project with name (eslint) already exists') projects = await getAllProjects(knex) expect(projects.length).toBe(1) }) diff --git a/src/cli/project.js b/src/cli/project.js index c8e7d38..3b15a02 100644 --- a/src/cli/project.js +++ b/src/cli/project.js @@ -9,7 +9,7 @@ const { initializeStore } = require('../store') const { projectCategories } = getConfig() async function runAddProjectCommand (knex, options = {}) { - const { addProject } = initializeStore(knex) + const { addProject, addGithubOrganization } = initializeStore(knex) if (Object.keys(options).length > 0) { if (!options.name) { @@ -90,16 +90,15 @@ async function runAddProjectCommand (knex, options = {}) { await addProject({ name: answers.name.toLowerCase(), - category: answers.category, - githubOrgs: answers.githubUrls.map((url) => ({ - url, - name: url.split('https://github.com/')[1] - })) + category: answers.category }) debug(`Project (${answers.name}) added successfully!`) - // @TODO: Add Organizations to the database + await Promise.all(answers.githubUrls.map((url) => addGithubOrganization({ + html_url: url, + login: url.split('https://github.com/')[1] + }))) return answers }