-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from secure-dashboards/feat/add-wf-update-gith…
…ub-orgs
- Loading branch information
Showing
17 changed files
with
1,080 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,4 +49,4 @@ jobs: | |
run: npm run db:seed | ||
|
||
- name: Run tests | ||
run: npm test | ||
run: npm run test:ci |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`list - Non-Interactive Mode Should provide a list of available workflows 1`] = ` | ||
[ | ||
{ | ||
"description": "Check the organizations stored and update the information with the GitHub API.", | ||
"name": "update-github-orgs", | ||
}, | ||
] | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
const inquirer = require('inquirer').default | ||
const knexInit = require('knex') | ||
const { getConfig } = require('../../src/config') | ||
const { runWorkflowCommand, listWorkflowCommand } = require('../../src/cli') | ||
const { resetDatabase, getAllProjects, getAllGithubOrgs, addGithubOrg, addProject } = require('../utils') | ||
const { github } = require('../../src/providers') | ||
const { sampleGithubOrg } = require('../fixtures') | ||
|
||
const { dbSettings } = getConfig('test') | ||
|
||
let knex | ||
|
||
beforeAll(() => { | ||
knex = knexInit(dbSettings) | ||
}) | ||
beforeEach(async () => { | ||
await resetDatabase(knex) | ||
jest.clearAllMocks() | ||
}) | ||
afterEach(jest.clearAllMocks) | ||
afterAll(async () => { | ||
await resetDatabase(knex) | ||
await knex.destroy() | ||
}) | ||
|
||
describe('list - Non-Interactive Mode', () => { | ||
jest.spyOn(inquirer, 'prompt').mockImplementation(async () => ({})) | ||
|
||
test('Should provide a list of available workflows', async () => { | ||
// @TODO: This test can be improved, currently is used to ensure that all the commands are listed | ||
await expect(listWorkflowCommand()).toMatchSnapshot() | ||
}) | ||
}) | ||
|
||
describe('run update-github-orgs - Interactive Mode', () => { | ||
// Mock inquirer for testing | ||
jest.spyOn(inquirer, 'prompt').mockImplementation(async (questions) => { | ||
const questionMap = { | ||
'What is the name of the workflow?': 'update-github-orgs' | ||
} | ||
return questions.reduce((acc, question) => { | ||
acc[question.name] = questionMap[question.message] | ||
return acc | ||
}, {}) | ||
}) | ||
|
||
test('Should throw an error when no Github orgs are stored in the database', async () => { | ||
const projects = await getAllProjects(knex) | ||
expect(projects.length).toBe(0) | ||
const githubOrgs = await getAllGithubOrgs(knex) | ||
expect(githubOrgs.length).toBe(0) | ||
await expect(runWorkflowCommand(knex, {})) | ||
.rejects | ||
.toThrow('No organizations found. Please add organizations/projects before running this workflow.') | ||
}) | ||
|
||
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 projects = await getAllProjects(knex) | ||
expect(projects.length).toBe(1) | ||
let githubOrgs = await getAllGithubOrgs(knex) | ||
expect(githubOrgs.length).toBe(1) | ||
expect(githubOrgs[0].description).toBe(null) | ||
// Mock the fetchOrgByLogin method | ||
jest.spyOn(github, 'fetchOrgByLogin').mockResolvedValue(sampleGithubOrg) | ||
await runWorkflowCommand(knex, {}) | ||
// Check the database changes | ||
githubOrgs = await getAllGithubOrgs(knex) | ||
expect(githubOrgs.length).toBe(1) | ||
expect(githubOrgs[0].description).toBe(sampleGithubOrg.description) | ||
}) | ||
|
||
test.todo('Should throw an error when the Github API is not available') | ||
}) | ||
|
||
describe('run update-github-orgs - Non-Interactive Mode', () => { | ||
// Mock inquirer for testing | ||
jest.spyOn(inquirer, 'prompt').mockImplementation(async (questions) => { | ||
const questionMap = { | ||
'What is the name of the workflow?': 'update-github-orgs' | ||
} | ||
return questions.reduce((acc, question) => { | ||
acc[question.name] = questionMap[question.message] | ||
return acc | ||
}, {}) | ||
}) | ||
|
||
test('Should throw an error when invalid name is provided', async () => { | ||
await expect(runWorkflowCommand(knex, { name: 'invented' })) | ||
.rejects | ||
.toThrow('Invalid workflow name. Please enter a valid workflow name.') | ||
}) | ||
|
||
test('Should throw an error when no name is provided', async () => { | ||
await expect(runWorkflowCommand(knex, { name: undefined })) | ||
.rejects | ||
.toThrow('Invalid workflow name. Please enter a valid workflow name.') | ||
}) | ||
|
||
test('Should throw an error when no Github orgs are stored in the database', async () => { | ||
const projects = await getAllProjects(knex) | ||
expect(projects.length).toBe(0) | ||
const githubOrgs = await getAllGithubOrgs(knex) | ||
expect(githubOrgs.length).toBe(0) | ||
await expect(runWorkflowCommand(knex, { name: 'update-github-orgs' })) | ||
.rejects | ||
.toThrow('No organizations found. Please add organizations/projects before running this workflow.') | ||
}) | ||
|
||
test.todo('Should update the project with new information available') | ||
test.todo('Should throw an error when the Github API is not available') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// https://docs.github.com/en/rest/orgs/orgs?apiVersion=2022-11-28#get-an-organization | ||
const sampleGithubOrg = { | ||
login: 'github', | ||
id: 1, | ||
node_id: 'MDEyOk9yZ2FuaXphdGlvbjE=', | ||
url: 'https://api.github.com/orgs/github', | ||
repos_url: 'https://api.github.com/orgs/github/repos', | ||
events_url: 'https://api.github.com/orgs/github/events', | ||
hooks_url: 'https://api.github.com/orgs/github/hooks', | ||
issues_url: 'https://api.github.com/orgs/github/issues', | ||
members_url: 'https://api.github.com/orgs/github/members{/member}', | ||
public_members_url: 'https://api.github.com/orgs/github/public_members{/member}', | ||
avatar_url: 'https://github.com/images/error/octocat_happy.gif', | ||
description: 'A great organization', | ||
name: 'github', | ||
company: 'GitHub', | ||
blog: 'https://github.com/blog', | ||
location: 'San Francisco', | ||
email: '[email protected]', | ||
twitter_username: 'github', | ||
is_verified: true, | ||
has_organization_projects: true, | ||
has_repository_projects: true, | ||
public_repos: 2, | ||
public_gists: 1, | ||
followers: 20, | ||
following: 0, | ||
html_url: 'https://github.com/octocat', | ||
created_at: '2008-01-14T04:33:35Z', | ||
type: 'Organization', | ||
total_private_repos: 100, | ||
owned_private_repos: 100, | ||
private_gists: 81, | ||
disk_usage: 10000, | ||
collaborators: 8, | ||
billing_email: '[email protected]', | ||
plan: { | ||
name: 'Medium', | ||
space: 400, | ||
private_repos: 20, | ||
filled_seats: 4, | ||
seats: 5 | ||
}, | ||
default_repository_permission: 'read', | ||
members_can_create_repositories: true, | ||
two_factor_requirement_enabled: true, | ||
members_allowed_repository_creation_type: 'all', | ||
members_can_create_public_repositories: false, | ||
members_can_create_private_repositories: false, | ||
members_can_create_internal_repositories: false, | ||
members_can_create_pages: true, | ||
members_can_create_public_pages: true, | ||
members_can_create_private_pages: true, | ||
members_can_fork_private_repositories: false, | ||
web_commit_signoff_required: false, | ||
updated_at: '2014-03-03T18:58:10Z', | ||
deploy_keys_enabled_for_repositories: false, | ||
dependency_graph_enabled_for_new_repositories: false, | ||
dependabot_alerts_enabled_for_new_repositories: false, | ||
dependabot_security_updates_enabled_for_new_repositories: false, | ||
advanced_security_enabled_for_new_repositories: false, | ||
secret_scanning_enabled_for_new_repositories: false, | ||
secret_scanning_push_protection_enabled_for_new_repositories: false, | ||
secret_scanning_push_protection_custom_link: 'https://github.com/octo-org/octo-repo/blob/main/im-blocked.md', | ||
secret_scanning_push_protection_custom_link_enabled: false | ||
} | ||
|
||
module.exports = { | ||
sampleGithubOrg | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,6 @@ | ||
const { sampleGithubOrg } = require('./fixtures') | ||
const { validateGithubOrg } = require('../src/schemas') | ||
|
||
// https://docs.github.com/en/rest/orgs/orgs?apiVersion=2022-11-28#get-an-organization | ||
const sampleGithubOrg = { | ||
login: 'github', | ||
id: 1, | ||
node_id: 'MDEyOk9yZ2FuaXphdGlvbjE=', | ||
url: 'https://api.github.com/orgs/github', | ||
repos_url: 'https://api.github.com/orgs/github/repos', | ||
events_url: 'https://api.github.com/orgs/github/events', | ||
hooks_url: 'https://api.github.com/orgs/github/hooks', | ||
issues_url: 'https://api.github.com/orgs/github/issues', | ||
members_url: 'https://api.github.com/orgs/github/members{/member}', | ||
public_members_url: 'https://api.github.com/orgs/github/public_members{/member}', | ||
avatar_url: 'https://github.com/images/error/octocat_happy.gif', | ||
description: 'A great organization', | ||
name: 'github', | ||
company: 'GitHub', | ||
blog: 'https://github.com/blog', | ||
location: 'San Francisco', | ||
email: '[email protected]', | ||
twitter_username: 'github', | ||
is_verified: true, | ||
has_organization_projects: true, | ||
has_repository_projects: true, | ||
public_repos: 2, | ||
public_gists: 1, | ||
followers: 20, | ||
following: 0, | ||
html_url: 'https://github.com/octocat', | ||
created_at: '2008-01-14T04:33:35Z', | ||
type: 'Organization', | ||
total_private_repos: 100, | ||
owned_private_repos: 100, | ||
private_gists: 81, | ||
disk_usage: 10000, | ||
collaborators: 8, | ||
billing_email: '[email protected]', | ||
plan: { | ||
name: 'Medium', | ||
space: 400, | ||
private_repos: 20, | ||
filled_seats: 4, | ||
seats: 5 | ||
}, | ||
default_repository_permission: 'read', | ||
members_can_create_repositories: true, | ||
two_factor_requirement_enabled: true, | ||
members_allowed_repository_creation_type: 'all', | ||
members_can_create_public_repositories: false, | ||
members_can_create_private_repositories: false, | ||
members_can_create_internal_repositories: false, | ||
members_can_create_pages: true, | ||
members_can_create_public_pages: true, | ||
members_can_create_private_pages: true, | ||
members_can_fork_private_repositories: false, | ||
web_commit_signoff_required: false, | ||
updated_at: '2014-03-03T18:58:10Z', | ||
deploy_keys_enabled_for_repositories: false, | ||
dependency_graph_enabled_for_new_repositories: false, | ||
dependabot_alerts_enabled_for_new_repositories: false, | ||
dependabot_security_updates_enabled_for_new_repositories: false, | ||
advanced_security_enabled_for_new_repositories: false, | ||
secret_scanning_enabled_for_new_repositories: false, | ||
secret_scanning_push_protection_enabled_for_new_repositories: false, | ||
secret_scanning_push_protection_custom_link: 'https://github.com/octo-org/octo-repo/blob/main/im-blocked.md', | ||
secret_scanning_push_protection_custom_link_enabled: false | ||
} | ||
|
||
describe('schemas', () => { | ||
describe('validateGithubOrg', () => { | ||
test('Should not throw an error with valid data', () => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,25 @@ | ||
const resetDatabase = async (knex) => { | ||
await knex('github_organizations').del() | ||
await knex('projects').del() | ||
await knex.raw('TRUNCATE TABLE github_organizations RESTART IDENTITY CASCADE') | ||
await knex.raw('TRUNCATE TABLE projects RESTART IDENTITY CASCADE') | ||
} | ||
|
||
const getAllProjects = (knex) => knex('projects').select('*') | ||
const getAllGithubOrgs = (knex) => knex('github_organizations').select('*') | ||
|
||
const addProject = async (knex, { name, category }) => { | ||
const [project] = await knex('projects').insert({ name, category }).returning('*') | ||
return project | ||
} | ||
|
||
const addGithubOrg = async (knex, data) => { | ||
const [githubOrg] = await knex('github_organizations').insert(data).returning('*') | ||
return githubOrg | ||
} | ||
|
||
module.exports = { | ||
resetDatabase, | ||
getAllProjects, | ||
getAllGithubOrgs | ||
getAllGithubOrgs, | ||
addProject, | ||
addGithubOrg | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.