Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 86 additions & 1 deletion __tests__/httpServer/apiV1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ let serverStop
let app
let knex
let getAllProjects
let addProject
let getAllGithubOrganizationsByProjectsId

beforeAll(async () => {
// Initialize server asynchronously
Expand All @@ -43,7 +45,9 @@ beforeAll(async () => {
app = request(server)
knex = knexInit(dbSettings);
({
getAllProjects
getAllProjects,
addProject,
getAllGithubOrganizationsByProjectsId
} = initializeStore(knex))
})

Expand Down Expand Up @@ -222,4 +226,85 @@ describe('HTTP Server API V1', () => {

test.todo('should return 500 when workflow execution times out')
})

describe('POST /api/v1/project/:projectId/gh-org', () => {
let projectId

beforeEach(async () => {
// Create a test project for each test
const project = await addProject({ name: 'test-project' })
projectId = project.id
})

test('should return 201 and add a new GitHub organization', async () => {
const githubOrgUrl = 'https://github.com/expressjs'

const response = await app
.post(`/api/v1/project/${projectId}/gh-org`)
.send({ githubOrgUrl })

expect(response.status).toBe(201)
expect(response.body).toHaveProperty('id')
expect(response.body).toHaveProperty('login', 'expressjs')
expect(response.body).toHaveProperty('html_url', githubOrgUrl)
expect(response.body).toHaveProperty('project_id', projectId)

// Verify organization was added to the database
const orgs = await getAllGithubOrganizationsByProjectsId([projectId])
expect(orgs.length).toBe(1)
expect(orgs[0].html_url).toBe(githubOrgUrl)
})

test('should return 400 for invalid project ID', async () => {
const response = await app
.post('/api/v1/project/invalid/gh-org')
.send({ githubOrgUrl: 'https://github.com/expressjs' })

expect(response.status).toBe(400)
expect(response.body).toHaveProperty('errors')
expect(response.body.errors[0]).toHaveProperty('message', 'Invalid project ID')
})

test('should return 400 for invalid GitHub organization URL', async () => {
const response = await app
.post(`/api/v1/project/${projectId}/gh-org`)
.send({ githubOrgUrl: 'https://invalid-url.com/org' })

expect(response.status).toBe(400)
expect(response.body).toHaveProperty('errors')
expect(response.body.errors[0]).toHaveProperty('message', 'must match pattern "https://github.com/[^/]+"')
})

test('should return 404 for project not found', async () => {
const nonExistentProjectId = 9999999

const response = await app
.post(`/api/v1/project/${nonExistentProjectId}/gh-org`)
.send({ githubOrgUrl: 'https://github.com/expressjs' })

expect(response.status).toBe(404)
expect(response.body).toHaveProperty('errors')
expect(response.body.errors[0]).toHaveProperty('message', 'Project not found')
})

test('should return 409 for duplicate GitHub organization', async () => {
const githubOrgUrl = 'https://github.com/expressjs'

// First add the organization
await app
.post(`/api/v1/project/${projectId}/gh-org`)
.send({ githubOrgUrl })

// Try to add it again
const response = await app
.post(`/api/v1/project/${projectId}/gh-org`)
.send({ githubOrgUrl })

expect(response.status).toBe(409)
expect(response.body).toHaveProperty('errors')
expect(response.body.errors[0]).toHaveProperty('message', 'GitHub organization already exists for this project')
})

test.todo('should return 500 for internal server error')
})
})
39 changes: 37 additions & 2 deletions src/httpServer/routers/apiV1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const pkg = require('../../../package.json')
const { logger } = require('../../utils')
const { logger, validateGithubUrl } = require('../../utils')
const { initializeStore } = require('../../store')
const _ = require('lodash')
const { isSlug } = require('validator')
Expand Down Expand Up @@ -27,14 +27,49 @@ const runWorkflow = ({ workflowName, knex, data } = {}) => new Promise((resolve,
})

function createApiRouter (knex, express) {
const { addProject, getProjectByName } = initializeStore(knex)
const { addProject, getProjectByName, addGithubOrganization, getProjectById, getAllGithubOrganizationsByProjectsId } = initializeStore(knex)

const router = express.Router()

router.get('/__health', (req, res) => {
res.json({ status: 'ok', timestamp: new Date().toISOString(), version: pkg.version, name: pkg.name })
})

router.post('/project/:projectId/gh-org', async (req, res) => {
const projectId = parseInt(req.params.projectId)
const { githubOrgUrl } = req.body

if (!projectId || !Number.isInteger(projectId)) {
return res.status(400).json({ errors: [{ message: 'Invalid project ID' }] })
}
if (!githubOrgUrl || !validateGithubUrl(githubOrgUrl)) {
return res.status(400).json({ errors: [{ message: 'Invalid GitHub organization name' }] })
}

const project = await getProjectById(projectId)
if (!project) {
return res.status(404).json({ errors: [{ message: 'Project not found' }] })
}

const existingGhOrg = await getAllGithubOrganizationsByProjectsId([projectId])
if (existingGhOrg.some(org => org.html_url === githubOrgUrl)) {
return res.status(409).json({ errors: [{ message: 'GitHub organization already exists for this project' }] })
}

try {
const org = await addGithubOrganization({
html_url: githubOrgUrl,
login: githubOrgUrl.split('https://github.com/')[1],
project_id: project.id
})

return res.status(201).json(org)
} catch (err) {
logger.error(err)
return res.status(500).json({ errors: [{ message: 'Internal server error' }] })
}
})

router.post('/project', async (req, res) => {
try {
// Validate request body
Expand Down
Loading
Loading