Skip to content

Commit

Permalink
feat: add a project will create associated records for the GitHub orgs
Browse files Browse the repository at this point in the history
  • Loading branch information
UlisesGascon committed Dec 4, 2024
1 parent 24ea99f commit 97503bb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
20 changes: 15 additions & 5 deletions __tests__/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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 () => {
Expand All @@ -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)
})
Expand All @@ -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 () => {
Expand All @@ -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)
})
Expand Down
13 changes: 6 additions & 7 deletions src/cli/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 97503bb

Please sign in to comment.