From 013cdf9900d25cbbbb27c81d3d6a4b89d966612b Mon Sep 17 00:00:00 2001 From: Chris Turner <23338096+christopherjturner@users.noreply.github.com> Date: Tue, 9 Jul 2024 13:32:29 +0100 Subject: [PATCH] adds stub for cdp-squid-proxy workflow --- .../github/controllers/trigger-workflow.js | 134 +++++++++++++----- .../events/trigger-create-repo-workflow.js | 29 ---- .../github/events/trigger-workflow-status.js | 49 +++++++ 3 files changed, 148 insertions(+), 64 deletions(-) delete mode 100644 src/api/github/events/trigger-create-repo-workflow.js create mode 100644 src/api/github/events/trigger-workflow-status.js diff --git a/src/api/github/controllers/trigger-workflow.js b/src/api/github/controllers/trigger-workflow.js index 4f84915..52c50b0 100644 --- a/src/api/github/controllers/trigger-workflow.js +++ b/src/api/github/controllers/trigger-workflow.js @@ -1,4 +1,3 @@ -import { triggerCreateRepoWorkflow } from '~/src/api/github/events/trigger-create-repo-workflow' import { ecrRepos, githubRepos, @@ -6,55 +5,120 @@ import { 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 + ) } } diff --git a/src/api/github/events/trigger-create-repo-workflow.js b/src/api/github/events/trigger-create-repo-workflow.js deleted file mode 100644 index 8c2cb86..0000000 --- a/src/api/github/events/trigger-create-repo-workflow.js +++ /dev/null @@ -1,29 +0,0 @@ -import { SendMessageCommand } from '@aws-sdk/client-sqs' -import { config } from '~/src/config' - -const triggerCreateRepoWorkflow = async (sqs, repoName) => { - const payload = { - github_event: 'workflow_run', - action: 'completed', - workflow_run: { - head_sha: 'f1d2d2f924e986ac86fdf7b36c94bcdf32beec15', - headBranch: 'main', - name: repoName, - conclusion: 'success' - }, - repository: { - name: 'cdp-create-workflows' - } - } - const pendingMessage = { - QueueUrl: config.get('sqsGithubQueue'), - MessageBody: JSON.stringify(payload), - DelaySeconds: 1, - MessageAttributes: {}, - MessageSystemAttributes: {} - } - - await sqs.send(new SendMessageCommand(pendingMessage)) -} - -export { triggerCreateRepoWorkflow } diff --git a/src/api/github/events/trigger-workflow-status.js b/src/api/github/events/trigger-workflow-status.js new file mode 100644 index 0000000..a2edc15 --- /dev/null +++ b/src/api/github/events/trigger-workflow-status.js @@ -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} + */ +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 }