Skip to content

Commit

Permalink
Merge pull request #34 from DEFRA/squid-workflow
Browse files Browse the repository at this point in the history
adds stub for cdp-squid-proxy workflow
  • Loading branch information
christopherjturner authored Jul 9, 2024
2 parents aec663f + 013cdf9 commit d937edb
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 64 deletions.
134 changes: 99 additions & 35 deletions src/api/github/controllers/trigger-workflow.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,124 @@
import { triggerCreateRepoWorkflow } from '~/src/api/github/events/trigger-create-repo-workflow'
import {
ecrRepos,
githubRepos,
topicsPerfTestSuite,
topicsService,
topicsTestSuite
} from '~/src/config/mock-data'
import { triggerWorkflowStatus } from '~/src/api/github/events/trigger-workflow-status'

const triggerWorkflow = {
handler: async (request, h) => {
const org = request.params.org
const repo = request.params.repo
const workflowFile = request.params.workflow
const inputs = request.payload.inputs
const repositoryName = inputs.repositoryName
const workflowRepo = request.params.repo
switch (workflowRepo) {
case 'cdp-create-workflows':
await handleCdpCreateWorkflows(request)
break
case 'cdp-squid-proxy':
await handleSquidWorkflows(request)
break
default:
return h
.response({ message: `unknown workflow ${workflowRepo}` })
.code(400)
}

request.logger.info(
`Stubbing triggering of workflow ${org}/${repo} ${workflowFile} with inputs ${JSON.stringify(
inputs
)}`
)
return h.response({}).code(200)
}
}

await triggerCreateRepoWorkflow(request.sqs, repositoryName)
const handleCdpCreateWorkflows = async (request) => {
const org = request.params.org
const workflowFile = request.params.workflow
const inputs = request.payload.inputs
const repositoryName = inputs.repositoryName

if (workflowFile === 'create_microservice.yml') {
request.logger.info(`Adding service ${repositoryName} to github repos`)
githubRepos.push({ name: repositoryName, topics: topicsService })
request.logger.info(
`Stubbing triggering of workflow ${org}/cdp-create-workflows/${workflowFile} with inputs ${JSON.stringify(
inputs
)}`
)

// technically this should happen on the TF-svc stage but its easier to
// detect the service type+name here
if (ecrRepos[repositoryName] === undefined) {
ecrRepos[repositoryName] = { tags: [], runMode: 'service' }
}
await triggerWorkflowStatus(
request.sqs,
'cdp-create-workflows',
repositoryName,
'completed',
'success',
1
)
if (workflowFile === 'create_microservice.yml') {
request.logger.info(`Adding service ${repositoryName} to github repos`)
githubRepos.push({ name: repositoryName, topics: topicsService })

// technically this should happen on the TF-svc stage but its easier to
// detect the service type+name here
if (ecrRepos[repositoryName] === undefined) {
ecrRepos[repositoryName] = { tags: [], runMode: 'service' }
}
}

if (workflowFile === 'create_env_test_suite.yml') {
request.logger.info(`Adding test suite ${repositoryName} to github repos`)
githubRepos.push({ name: repositoryName, topics: topicsTestSuite })
if (workflowFile === 'create_env_test_suite.yml') {
request.logger.info(`Adding test suite ${repositoryName} to github repos`)
githubRepos.push({ name: repositoryName, topics: topicsTestSuite })

if (ecrRepos[repositoryName] === undefined) {
ecrRepos[repositoryName] = { tags: [], runMode: 'job' }
}
if (ecrRepos[repositoryName] === undefined) {
ecrRepos[repositoryName] = { tags: [], runMode: 'job' }
}
}

if (workflowFile === 'create_perf_test_suite.yml') {
request.logger.info(
`Adding perf test suite ${repositoryName} to github repos`
)
githubRepos.push({ name: repositoryName, topics: topicsPerfTestSuite })
if (workflowFile === 'create_perf_test_suite.yml') {
request.logger.info(
`Adding perf test suite ${repositoryName} to github repos`
)
githubRepos.push({ name: repositoryName, topics: topicsPerfTestSuite })

if (ecrRepos[repositoryName] === undefined) {
ecrRepos[repositoryName] = { tags: [], runMode: 'job' }
}
if (ecrRepos[repositoryName] === undefined) {
ecrRepos[repositoryName] = { tags: [], runMode: 'job' }
}
}
}

return h.response({}).code(200)
const handleSquidWorkflows = async (request) => {
const org = request.params.org
const workflowFile = request.params.workflow
const inputs = request.payload.inputs
const service = inputs.service

request.logger.info(
`Stubbing triggering of workflow ${org}/cdp-squid-proxy/${workflowFile} with inputs ${JSON.stringify(
inputs
)}`
)

if (workflowFile === 'create_service.yml') {
request.logger.info(`Adding service ${service} to squid config`)
await triggerWorkflowStatus(
request.sqs,
'cdp-squid-proxy',
service,
'requested',
null,
0
)

await triggerWorkflowStatus(
request.sqs,
'cdp-squid-proxy',
service,
'in_progress',
null,
1
)

await triggerWorkflowStatus(
request.sqs,
'cdp-squid-proxy',
service,
'completed',
'success',
2
)
}
}

Expand Down
29 changes: 0 additions & 29 deletions src/api/github/events/trigger-create-repo-workflow.js

This file was deleted.

49 changes: 49 additions & 0 deletions src/api/github/events/trigger-workflow-status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { SendMessageCommand } from '@aws-sdk/client-sqs'
import { config } from '~/src/config'

/**
*
* @param sqs
* @param {string} workflowRepo
* @param {string} runId
* @param {string} action
* @param {string|null} conclusion
* @param {number} delay
* @returns {Promise<void>}
*/
const triggerWorkflowStatus = async (
sqs,
workflowRepo,
runId,
action,
conclusion,
delay = 1
) => {
const payload = {
github_event: 'workflow_run',
action,
workflow_run: {
head_sha: 'f1d2d2f924e986ac86fdf7b36c94bcdf32beec15',
head_branch: 'main',
name: runId,
conclusion,
html_url: 'http://localhost:3939/#local-stub',
created_at: new Date().toISOString(),
updated_at: new Date().toISOString()
},
repository: {
name: workflowRepo
}
}
const command = {
QueueUrl: config.get('sqsGithubQueue'),
MessageBody: JSON.stringify(payload),
DelaySeconds: delay,
MessageAttributes: {},
MessageSystemAttributes: {}
}

await sqs.send(new SendMessageCommand(command))
}

export { triggerWorkflowStatus }

0 comments on commit d937edb

Please sign in to comment.