Skip to content

Commit

Permalink
feat: remove --category flag from add project command
Browse files Browse the repository at this point in the history
  • Loading branch information
UlisesGascon committed Dec 30, 2024
1 parent 5cf580a commit c378af7
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 80 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ node --env-file=.env index.js workflow run --name populate-repos-list
Add a new project:

```bash
node index.js project add [--name <name>] [--github-urls <urls...>] [--category <category>]
node index.js project add [--name <name>] [--github-urls <urls...>]
```

Example:

```bash
node index.js project add --name express --github-urls https://github.com/expressjs https://github.com/pillarjs https://github.com/jshttp --category impact
node index.js project add --name express --github-urls https://github.com/expressjs https://github.com/pillarjs https://github.com/jshttp
```

### Workflows
Expand Down
2 changes: 1 addition & 1 deletion __tests__/checks/githubOrgMFA.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ beforeAll(async () => {

beforeEach(async () => {
await resetDatabase(knex)
project = await addProject({ name: sampleGithubOrg.login, category: 'impact' })
project = await addProject({ name: sampleGithubOrg.login })
})

afterAll(async () => {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/checks/softwareDesignTraining.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ beforeAll(async () => {

beforeEach(async () => {
await resetDatabase(knex)
project = await addProject({ name: 'project', category: 'impact' })
project = await addProject({ name: 'project' })
})

afterAll(async () => {
Expand Down
12 changes: 4 additions & 8 deletions __tests__/checks/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ describe('githubOrgMFA', () => {

projects = [
{
id: 1,
category: 'impact'
id: 1
},
{
id: 2,
category: 'at-large'
id: 2
}
]
})
Expand Down Expand Up @@ -154,12 +152,10 @@ describe('softwareDesignTraining', () => {

projects = [
{
id: 1,
category: 'impact'
id: 1
},
{
id: 2,
category: 'at-large'
id: 2
}
]
})
Expand Down
41 changes: 9 additions & 32 deletions __tests__/cli/project.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ const { dbSettings } = getConfig('test')
jest.spyOn(inquirer, 'prompt').mockImplementation(async (questions) => {
const questionMap = {
'What is the name of the project?': 'eslint',
'Enter the GitHub URLs (comma-separated):': 'https://github.com/eslint',
'Select a category:': 'impact'
'Enter the GitHub URLs (comma-separated):': 'https://github.com/eslint'
}
return questions.reduce((acc, question) => {
acc[question.name] = questionMap[question.message]
Expand Down Expand Up @@ -39,7 +38,7 @@ afterAll(async () => {
})

describe('Interactive Mode', () => {
test('Add a project with name, GitHub URLs, and category', async () => {
test('Add a project with name and GitHub URLs', async () => {
let projects = await getAllProjects()
expect(projects.length).toBe(0)
let githubOrgs = await getAllGithubOrgs()
Expand All @@ -48,7 +47,6 @@ describe('Interactive Mode', () => {
projects = await getAllProjects()
expect(projects.length).toBe(1)
expect(projects[0].name).toBe('eslint')
expect(projects[0].category).toBe('impact')
githubOrgs = await getAllGithubOrgs()
expect(githubOrgs.length).toBe(1)
expect(githubOrgs[0].login).toBe('eslint')
Expand All @@ -70,16 +68,15 @@ describe('Interactive Mode', () => {
})

describe('Non-Interactive Mode', () => {
test('Add a project with name, GitHub URLs, and category', async () => {
test('Add a project with name and GitHub URLs', async () => {
let projects = await getAllProjects()
expect(projects.length).toBe(0)
let githubOrgs = await getAllGithubOrgs()
expect(githubOrgs.length).toBe(0)
await runAddProjectCommand(knex, { name: 'eslint', githubUrls: ['https://github.com/eslint'], category: 'impact' })
await runAddProjectCommand(knex, { name: 'eslint', githubUrls: ['https://github.com/eslint'] })
projects = await getAllProjects()
expect(projects.length).toBe(1)
expect(projects[0].name).toBe('eslint')
expect(projects[0].category).toBe('impact')
githubOrgs = await getAllGithubOrgs()
expect(githubOrgs.length).toBe(1)
expect(githubOrgs[0].login).toBe('eslint')
Expand All @@ -89,10 +86,10 @@ describe('Non-Interactive Mode', () => {
test('Prevent to add a project that already exists', async () => {
let projects = await getAllProjects()
expect(projects.length).toBe(0)
await runAddProjectCommand(knex, { name: 'eslint', githubUrls: ['https://github.com/eslint'], category: 'impact' })
await runAddProjectCommand(knex, { name: 'eslint', githubUrls: ['https://github.com/eslint'] })
projects = await getAllProjects(knex)
expect(projects.length).toBe(1)
await expect(runAddProjectCommand(knex, { name: 'eslint', githubUrls: ['https://github.com/eslint'], category: 'impact' }))
await expect(runAddProjectCommand(knex, { name: 'eslint', githubUrls: ['https://github.com/eslint'] }))
.rejects
.toThrow('Project with name (eslint) already exists')
projects = await getAllProjects(knex)
Expand All @@ -102,7 +99,7 @@ describe('Non-Interactive Mode', () => {
test('Error when no name is provided', async () => {
let projects = await getAllProjects(knex)
expect(projects.length).toBe(0)
await expect(runAddProjectCommand(knex, { githubUrls: ['https://github.com/eslint'], category: 'impact' }))
await expect(runAddProjectCommand(knex, { githubUrls: ['https://github.com/eslint'] }))
.rejects
.toThrow('Project name is required')
projects = await getAllProjects(knex)
Expand All @@ -112,37 +109,17 @@ describe('Non-Interactive Mode', () => {
test('Error when no GitHub URLs are provided', async () => {
let projects = await getAllProjects(knex)
expect(projects.length).toBe(0)
await expect(runAddProjectCommand(knex, { name: 'eslint', category: 'impact' }))
await expect(runAddProjectCommand(knex, { name: 'eslint' }))
.rejects
.toThrow('GitHub URLs are required')
projects = await getAllProjects(knex)
expect(projects.length).toBe(0)
})

test('Error when no category is provided', async () => {
let projects = await getAllProjects(knex)
expect(projects.length).toBe(0)
await expect(runAddProjectCommand(knex, { name: 'eslint', githubUrls: ['https://github.com/eslint'] }))
.rejects
.toThrow('Category is required')
projects = await getAllProjects(knex)
expect(projects.length).toBe(0)
})

test('Error when invalid category is provided', async () => {
let projects = await getAllProjects(knex)
expect(projects.length).toBe(0)
await expect(runAddProjectCommand(knex, { name: 'eslint', githubUrls: ['https://github.com/eslint'], category: 'invalid' }))
.rejects
.toThrow('Invalid category, use one of the following')
projects = await getAllProjects(knex)
expect(projects.length).toBe(0)
})

test('Error when invalid GitHub URLs are provided', async () => {
let projects = await getAllProjects(knex)
expect(projects.length).toBe(0)
await expect(runAddProjectCommand(knex, { name: 'eslint', githubUrls: ['invalid-url'], category: 'impact' }))
await expect(runAddProjectCommand(knex, { name: 'eslint', githubUrls: ['invalid-url'] }))
.rejects
.toThrow('Invalid URL: invalid-url. Please enter valid GitHub URLs.')
projects = await getAllProjects(knex)
Expand Down
10 changes: 3 additions & 7 deletions __tests__/cli/workflows.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('run update-github-orgs', () => {

test('Should update the project with new information available', async () => {
// Prepare the database
const project = await addProject({ name: sampleGithubOrg.login, category: 'impact' })
const project = await addProject({ name: sampleGithubOrg.login })
await addGithubOrg({ login: sampleGithubOrg.login, html_url: sampleGithubOrg.html_url, project_id: project.id })
const projects = await getAllProjects()
expect(projects.length).toBe(1)
Expand Down Expand Up @@ -105,7 +105,7 @@ describe('run upsert-github-repositories', () => {
})
test('Should add the repositories related to the organization', async () => {
// Prepare the database
const project = await addProject({ name: sampleGithubOrg.login, category: 'impact' })
const project = await addProject({ name: sampleGithubOrg.login })
await addGithubOrg({ login: sampleGithubOrg.login, html_url: sampleGithubOrg.html_url, project_id: project.id })
const projects = await getAllProjects()
expect(projects.length).toBe(1)
Expand All @@ -123,7 +123,7 @@ describe('run upsert-github-repositories', () => {
expect(githubRepos[0].description).toBe(sampleGithubRepository.description)
})
test('Should update the repositories related to the organization', async () => {
const project = await addProject({ name: sampleGithubOrg.login, category: 'impact' })
const project = await addProject({ name: sampleGithubOrg.login })
const org = await addGithubOrg({ login: sampleGithubOrg.login, html_url: sampleGithubOrg.html_url, project_id: project.id })
const githubRepoData = simplifyObject(sampleGithubRepository, {
include: ['node_id', 'name', 'full_name', 'html_url', 'url', 'git_url', 'ssh_url', 'clone_url', 'visibility', 'default_branch']
Expand Down Expand Up @@ -157,7 +157,3 @@ describe('run run-all-checks', () => {
describe('run upsert-ossf-scorecard', () => {
test.todo('Should upsert the OSSF Scorecard scoring by running and checking every repository in the database')
})

describe('run show-reports', () => {
test.todo('Should start a http server that shows all the files and folders in the output directory')
})
3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { Command } = require('commander')
const { getConfig } = require('./src/config')
const { projectCategories, dbSettings } = getConfig()
const { dbSettings } = getConfig()
const { logger } = require('./src/utils')
const { runAddProjectCommand, runWorkflowCommand, listWorkflowCommand, listCheckCommand, runCheckCommand, runChecklist, listChecklist } = require('./src/cli')
const knex = require('knex')(dbSettings)
Expand All @@ -17,7 +17,6 @@ project
.description('Add a new project')
.option('--name <name>', 'Name of the project')
.option('--github-urls <urls...>', 'GitHub URLs of the project')
.option('--category <category>', `Category of the project. Choose from: ${projectCategories.join(', ')}`)
.action(async (options) => {
try {
await runAddProjectCommand(knex, options)
Expand Down
23 changes: 2 additions & 21 deletions src/cli/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ const inquirer = require('inquirer').default
const { stringToArray } = require('@ulisesgascon/string-to-array')
const isSlug = require('validator/lib/isSlug.js')
const debug = require('debug')('cli')
const { getConfig } = require('../config')
const { validateGithubUrl, logger } = require('../utils')
const { initializeStore } = require('../store')

const { projectCategories } = getConfig()

async function runAddProjectCommand (knex, options = {}) {
const { addProject, addGithubOrganization } = initializeStore(knex)

Expand All @@ -20,14 +17,6 @@ async function runAddProjectCommand (knex, options = {}) {
throw new Error('GitHub URLs are required')
}

if (!options.category) {
throw new Error('Category is required')
}

if (!projectCategories.includes(options.category)) {
throw new Error(`Invalid category, use one of the following: ${projectCategories.join(', ')}`)
}

if (options.githubUrls) {
const urls = options.githubUrls
if (urls.length === 0) {
Expand All @@ -41,7 +30,7 @@ async function runAddProjectCommand (knex, options = {}) {
}
}

const answers = options.name && options.githubUrls && options.category
const answers = options.name && options.githubUrls
? options
: await inquirer.prompt([
{
Expand Down Expand Up @@ -76,21 +65,13 @@ async function runAddProjectCommand (knex, options = {}) {
return true
},
when: () => !options.githubUrls
},
{
type: 'list',
name: 'category',
message: 'Select a category:',
choices: projectCategories,
when: () => !options.category
}
])

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

const project = await addProject({
name: answers.name.toLowerCase(),
category: answers.category
name: answers.name.toLowerCase()
})

debug(`Project (${answers.name}) added successfully!`)
Expand Down
3 changes: 0 additions & 3 deletions src/config/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { normalizeBoolean } = require('@ulisesgascon/normalize-boolean')
const projectCategories = ['impact', 'at-large', 'incubation', 'emeritus']

const dbSettings = {
client: 'pg',
Expand Down Expand Up @@ -32,13 +31,11 @@ const staticServer = {
}

const defaultValues = {
projectCategories,
ossfScorecardSettings,
dbSettings,
staticServer
}
const testEnvironment = {
projectCategories,
ossfScorecardSettings,
dbSettings,
staticServer
Expand Down
5 changes: 2 additions & 3 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,15 @@ const addGithubOrganization = knex => async (organization) => {
}

const addProject = knex => async (project) => {
const { name, category } = project
const { name } = project
const projectExists = await knex('projects').where({ name }).first()
debug(`Checking if project (${name}) exists...`)
if (projectExists) {
throw new Error(`Project with name (${name}) already exists`)
}
debug(`Inserting project (${name})...`)
return knex('projects').insert({
name,
category
name
}).returning('*').then(results => results[0])
}

Expand Down

0 comments on commit c378af7

Please sign in to comment.