Skip to content

Commit

Permalink
Merge pull request #12 from DEFRA/269770-create-blank-repository
Browse files Browse the repository at this point in the history
269770 create blank repository
  • Loading branch information
feedmypixel authored Dec 5, 2023
2 parents 544c200 + a1be85e commit e50a5a8
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 126 deletions.
183 changes: 148 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,69 +1,182 @@
# CDP Portal Stubs

A set of stubs for mocking out calls to github & aws for the cdp-portal.
A set of stubs for mocking calls to Github, Aws and Microsoft OIDC for the cdp-portal suite of applications.

- [CDP Portal Stubs](#cdp-portal-stubs)
- [What it currently provides](#what-it-currently-provides)
- [Setup](#setup)
- [MongoDB](#mongodb)
- [Start MongoDB](#start-mongodb)
- [Redis](#redis)
- [Localstack](#localstack)
- [Create queues](#create-queues)
- [start cdp-portal-backend](#start-cdp-portal-backend)
- [start cdp-self-service-ops](#start-cdp-self-service-ops)
- [Start cdp-user-services](#start-cdp-user-service-backend)
- [Start cdp-portal-frontend](#start-cdp-portal-frontend)
- [Start cdp-portal-stubs](#start-cdp-portal-stubs)
- [Test Data](#test-data)

## What it currently provides

Mock calls for github apis:

- Get/Set repo content
- Raise a PR
- Get Commit history
- Get repo data
- Trigger workflow
- Various graphql queries for enabling automerge and listing team data

Raising a PR will also result in a github webhook message being posted to the githubEvents queue, and in some cases a
workflow complete message.
Creating a new repository results in a workflow run complete message.

Mocks calls to an ECR repository

- Lists available repositories
- Gets manifest
- Gets minimal layers

An admin endpoint is provided to simulate a new image being pushed `POST /_admin/trigger-ecr-push/{repo}/{tag}`

## Setup

### start defra-mock-oidc-server
### MongoDB

Install [MongoDB](https://www.mongodb.com/docs/manual/tutorial/#installation) on your local machine

#### Start MongoDB

```bash
sudo mongod --dbpath ~/mongodb-cdp
```

### start mongo, redis, localstack
### Redis

### create queues
[https://redis.io/docs/getting-started/installation](https://redis.io/docs/getting-started/installation)

### start cdp-portal-backend
### Localstack

### start cdp-self-service-ops
```bash
docker run -d -p 4566:4566 -p 4510-4559:4510-4559 localstack/localstack:latest
```

Override the following config items:
#### Create queues

```bash
awslocal sqs create-queue --queue-name ecr-push-deployments --region eu-west-2
awslocal sqs create-queue --queue-name ecs-deployments --region eu-west-2
awslocal sqs create-queue --queue-name ecr-push-events --region eu-west-2
awslocal sqs create-queue --queue-name github-events --region eu-west-2
```

### Start cdp-portal-backend

[DEFRA/cdp-portal-backend](https://github.com/DEFRA/cdp-portal-backend)

You can add the following to `launchSettings.json` in your checkout of the `cdp-portal-backend`

> Note please obtain the test `Github__AppKey` from another dev
```json5
{
// other launch settings
Stubbed: {
commandName: 'Project',
dotnetRunMessages: true,
launchBrowser: false,
applicationUrl: 'http://localhost:5094',
environmentVariables: {
ASPNETCORE_ENVIRONMENT: 'Development',
Mongo__DatabaseUri: 'mongodb://127.0.0.1:27017',
AzureAd__Instance: 'http://localhost:3939/',
AzureAd__RequireHttpsMetadata: 'false',
Github__AppKey: '<OBTAIN_FROM_ANOTHER_DEV>',
Github__ApiUrl: 'http://localhost:3939'
}
}
}
```

Run

```bash
dotnet run --project Defra.Cdp.Backend.Api --launch-profile Stubbed
```

### Start cdp-self-service-ops

[DEFRA/cdp-self-service-ops](https://github.com/DEFRA/cdp-self-service-ops)

Override the following config items/environment variables:

```bash
export GITHUB_BASE_URL=http://localhost:3939
export OIDC_ISSUER_BASE_URL=http://127.0.0.1:5557/oidc
export OIDC_KEYS_URL=http://127.0.0.1:5557/oidc/.well-known/jwks.json
export OIDC_WELL_KNOWN_CONFIGURATION_URL=http://localhost:3939/63983fc2-cfff-45bb-8ec2-959e21062b9a/v2.0/.well-known/openid-configuration
```

Override using `npm` scripts

```bash
GITHUB_BASE_URL=http://localhost:3939 OIDC_WELL_KNOWN_CONFIGURATION_URL=http://localhost:3939/63983fc2-cfff-45bb-8ec2-959e21062b9a/v2.0/.well-known/openid-configuration npm run dev
```

### cdp-user-services
### Start cdp-user-service-backend

[DEFRA/cdp-user-service-backend](https://github.com/DEFRA/cdp-user-service-backend)

Override the following config items:
Override the following config items/environment variables:

```bash
export AZURE_CLIENT_SECRET=test_value
export OIDC_ISSUER_BASE_URL=http://127.0.0.1:5557/oidc
export OIDC_KEYS_URL=http://127.0.0.1:5557/oidc/.well-known/jwks.json
export GITHUB_BASE_URL=http://localhost:3939
export OIDC_WELL_KNOWN_CONFIGURATION_URL=http://localhost:3939/63983fc2-cfff-45bb-8ec2-959e21062b9a/v2.0/.well-known/openid-configuration
export OIDC_AUDIENCE=63983fc2-cfff-45bb-8ec2-959e21062b9a
```

### start cdp-portal-frontend
Override using `npm` scripts

Override the following config items:
```bash
GITHUB_BASE_URL=http://localhost:3939 OIDC_WELL_KNOWN_CONFIGURATION_URL=http://localhost:3939/63983fc2-cfff-45bb-8ec2-959e21062b9a/v2.0/.well-known/openid-configuration OIDC_AUDIENCE=63983fc2-cfff-45bb-8ec2-959e21062b9a npm run dev
```

### Start cdp-portal-frontend

[DEFRA/cdp-portal-frontend](https://github.com/DEFRA/cdp-portal-frontend)

Override the following config items/environment variables:

```bash
export AZURE_CLIENT_SECRET=test_value
export OAUTH_AUTH_URL=http://127.0.0.1:5557/oidc/authorize
export OAUTH_TOKEN_URL=http://127.0.0.1:5557/oidc/token
export AZURE_CLIENT_SECRET=test_value
export GITHUB_BASE_URL=http://localhost:3939
export OIDC_WELL_KNOWN_CONFIGURATION_URL=http://localhost:3939/63983fc2-cfff-45bb-8ec2-959e21062b9a/v2.0/.well-known/openid-configuration
export AZURE_TENANT_ID=63983fc2-cfff-45bb-8ec2-959e21062b9a
export APP_BASE_URL=http://localhost:3000
```

## What it currently provides
Override using `npm` scripts

Mock calls for github apis:
```bash
AZURE_CLIENT_SECRET=test_value GITHUB_BASE_URL=http://localhost:3939 OIDC_WELL_KNOWN_CONFIGURATION_URL=http://localhost:3939/63983fc2-cfff-45bb-8ec2-959e21062b9a/v2.0/.well-known/openid-configuration AZURE_TENANT_ID=63983fc2-cfff-45bb-8ec2-959e21062b9a APP_BASE_URL=http://localhost:3000 npm run dev
```

- Get/Set repo content
- Raise a PR
- Get Commit history
- Get repo data
- Trigger workflow
- Various graphql queries for enabling automerge and listing team data
### Start cdp-portal-stubs

Raising a PR will also result in a github webhook message being posted to the githubEvents queue, and in some cases a workflow complete message.
Creating a new repository results in a workflow run complete message.
Run in watch (dev) mode:

Mocks calls to an ECR repository
> Note with watch mode your refresh tokens will not work. You will have to sign out and in again to get a valid token
- Lists available repositories
- Gets manifest
- Gets minimal layers
```bash
npm run dev
```

An admin endpoint is provided to simulate a new image being pushed `POST /_admin/trigger-ecr-push/{repo}/{tag}`
Run:

```bash
npm start
```

## Test Data

The base set of services is held in /config/services.js. Adding or removing services to this list will result in them being returned in the mock API calls.
The base set of services is held in /config/services.js. Adding or removing services to this list will result in them
being returned in the mock API calls.
30 changes: 17 additions & 13 deletions src/api/github/controllers/trigger-workflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,28 @@ const triggerWorkflow = {
const org = request.params.org
const repo = request.params.repo
const workflowFile = request.params.workflow

const repoToCreate = request.payload.inputs.repositoryName
const serviceType = request.payload.inputs.serviceType
const owningTeam = request.payload.inputs.owningTeam
const inputs = request.payload.inputs
const repositoryName = inputs.repositoryName

request.logger.info(
`Stubbing triggering of workflow ${org}/${repo} ${workflowFile} to create repo ${repoToCreate} for ${owningTeam} of type ${serviceType}`
`Stubbing triggering of workflow ${org}/${repo} ${workflowFile} with inputs ${JSON.stringify(
inputs
)}`
)

await triggerCreateRepoWorkflow(request.sqs, repoToCreate)
await triggerCreateRepoWorkflow(request.sqs, repositoryName)

if (workflowFile === 'create_microservice.yml') {
const serviceTypeTemplate = inputs.serviceTypeTemplate

// update the list of services
if (serviceType.includes('frontend')) {
request.logger.info(`Adding ${repoToCreate} to public services`)
publicServices.push(repoToCreate)
} else {
protectedServices.push(repoToCreate)
request.logger.info(`Adding ${repoToCreate} to protected services`)
// Update the list of services
if (serviceTypeTemplate?.includes('frontend')) {
request.logger.info(`Adding ${repositoryName} to public services`)
publicServices.push(repositoryName)
} else {
protectedServices.push(repositoryName)
request.logger.info(`Adding ${repositoryName} to protected services`)
}
}

return h.response({}).code(200)
Expand Down
2 changes: 1 addition & 1 deletion src/api/github/events/trigger-create-repo-workflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const triggerCreateRepoWorkflow = async (sqs, repoName) => {
conclusion: 'success'
},
repository: {
name: 'cdp-boilerplate'
name: 'cdp-create-workflows'
}
}
const pendingMessage = {
Expand Down
3 changes: 0 additions & 3 deletions src/api/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { router } from '~/src/api/router'
import { requestLogger } from '~/src/helpers/logging/request-logger'
import { mongoPlugin } from '~/src/helpers/mongodb'
import { failAction } from '~/src/helpers/fail-action'
import { populateDb } from '~/src/helpers/db/populate-db'
import { sqsPlugin } from '~/src/helpers/sqs'

async function createServer() {
Expand Down Expand Up @@ -39,8 +38,6 @@ async function createServer() {

await server.register(sqsPlugin)

await server.register(populateDb)

return server
}

Expand Down
20 changes: 0 additions & 20 deletions src/helpers/db/fetch-entities.js

This file was deleted.

28 changes: 0 additions & 28 deletions src/helpers/db/populate-api.js

This file was deleted.

21 changes: 0 additions & 21 deletions src/helpers/db/populate-db.js

This file was deleted.

5 changes: 0 additions & 5 deletions src/helpers/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const mongoPlugin = {

const client = await MongoClient.connect(mongoUrl.toString(), mongoOptions)
const db = client.db(databaseName)
await createIndexes(db)

server.logger.info(`mongodb connected to ${databaseName}`)

Expand All @@ -28,8 +27,4 @@ const mongoPlugin = {
}
}

async function createIndexes(db) {
await db.collection('entities').createIndex({ id: 1 })
}

export { mongoPlugin }

0 comments on commit e50a5a8

Please sign in to comment.