Skip to content
This repository has been archived by the owner on Dec 3, 2024. It is now read-only.

Commit

Permalink
Merge branch 'add-sonarcloud' of https://github.com/DFE-Digital/ident…
Browse files Browse the repository at this point in the history
…ifiers-api into add-sonarcloud
  • Loading branch information
cshnimble committed Apr 8, 2024
2 parents 8d75580 + 322be4b commit c873643
Show file tree
Hide file tree
Showing 63 changed files with 3,896 additions and 123 deletions.
125 changes: 23 additions & 102 deletions .github/workflows/build-and-push-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ name: Deploy to environment

on:
push:
branches:
- main
branches: [ main ]
workflow_dispatch:
inputs:
environment:
Expand All @@ -14,68 +13,33 @@ on:
concurrency:
group: ${{ github.workflow }}-${{ github.event.inputs.environment }}

env:
DOCKER_IMAGE: identifiersapi-app
NODE_VERSION: 18

jobs:
set-env:
name: Determine environment
runs-on: ubuntu-22.04
outputs:
environment: ${{ steps.var.outputs.environment }}
branch: ${{ steps.var.outputs.branch }}
release: ${{ steps.var.outputs.release }}
checked-out-sha: ${{ steps.var.outputs.checked-out-sha }}
release: ${{steps.var.outputs.release}}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.ref }}
- name: Get branch name for push/dispatch event
run: |
GIT_REF=${{ github.ref_name }}
echo "branch_ref=${GIT_REF}" >> $GITHUB_ENV
- id: var
run: |
GIT_REF=${{ github.ref }}
GIT_REF=${{ env.branch_ref }}
GIT_BRANCH=${GIT_REF##*/}
INPUT=${{ github.event.inputs.environment }}
ENVIRONMENT=${INPUT:-"development"}
RELEASE=${ENVIRONMENT,,}-`date +%Y-%m-%d`.${{ github.run_number }}
CHECKED_OUT_SHA="$(git log -1 '--format=format:%H')"
echo "environment=${ENVIRONMENT,,}" >> $GITHUB_OUTPUT
echo "branch=$GIT_BRANCH" >> $GITHUB_OUTPUT
echo "release=${RELEASE}" >> $GITHUB_OUTPUT
echo "checked-out-sha=${CHECKED_OUT_SHA}" >> $GITHUB_OUTPUT
build-and-push-image:
name: Build and push to ACR
needs: set-env
runs-on: ubuntu-22.04
environment: ${{ needs.set-env.outputs.environment }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.ref }}

- name: Azure Container Registry login
uses: docker/login-action@v3
with:
username: ${{ secrets.AZURE_ACR_CLIENTID }}
password: ${{ secrets.AZURE_ACR_SECRET }}
registry: ${{ secrets.AZURE_ACR_URL }}

- name: Build and push docker image
uses: docker/build-push-action@v5
with:
context: .
file: Dockerfile
build-args: COMMIT_SHA=${{ needs.set-env.outputs.checked-out-sha }}
tags: |
${{ secrets.AZURE_ACR_URL }}/${{ env.DOCKER_IMAGE }}:${{ needs.set-env.outputs.branch }}
${{ secrets.AZURE_ACR_URL }}/${{ env.DOCKER_IMAGE }}:${{ needs.set-env.outputs.release }}
${{ secrets.AZURE_ACR_URL }}/${{ env.DOCKER_IMAGE }}:sha-${{ needs.set-env.outputs.checked-out-sha }}
${{ secrets.AZURE_ACR_URL }}/${{ env.DOCKER_IMAGE }}:latest
push: true
echo "release=${RELEASE,,}" >> $GITHUB_OUTPUT
create-tag:
if: needs.set-env.outputs.environment == 'production'
name: Tag and release
needs: set-env
runs-on: ubuntu-22.04
Expand All @@ -96,11 +60,9 @@ jobs:
script: |
try {
await github.rest.repos.createRelease({
draft: ${{ needs.set-env.outputs.environment == 'test' }},
generate_release_notes: true,
name: "${{ needs.set-env.outputs.release }}",
owner: context.repo.owner,
prerelease: ${{ needs.set-env.outputs.environment == 'test' }},
repo: context.repo.repo,
tag_name: "${{ needs.set-env.outputs.release }}",
});
Expand All @@ -109,58 +71,17 @@ jobs:
}
deploy-image:
name: Deploy to ${{ needs.set-env.outputs.environment }}
needs: [ build-and-push-image, set-env ]
runs-on: ubuntu-22.04
environment: ${{ needs.set-env.outputs.environment }}
steps:
- name: Azure login with ACA credentials
uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_ACA_CREDENTIALS }}

- name: Update Azure Container Apps Revision
uses: azure/CLI@v2
id: azure
with:
azcliversion: 2.45.0
inlineScript: |
az config set extension.use_dynamic_install=yes_without_prompt
az containerapp update \
--name ${{ secrets.AZURE_ACA_NAME }} \
--resource-group ${{ secrets.AZURE_ACA_RESOURCE_GROUP }} \
--image ${{ secrets.AZURE_ACR_URL }}/${{ env.DOCKER_IMAGE }}:${{ needs.set-env.outputs.release }} \
--output none
cypress-tests:
name: Run Cypress Tests
if: needs.set-env.outputs.environment == 'test' || needs.set-env.outputs.environment == 'development'
needs: [ deploy-image, set-env ]
runs-on: ubuntu-22.04
environment: ${{ needs.set-env.outputs.environment }}
defaults:
run:
working-directory: CypressTests
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.ref }}

- name: Setup node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}

- name: Npm install
run: npm install

- name: Run cypress
run: npm run cy:run -- --env apiKey="${{ secrets.IDENTIFIERS_API_KEY }}",url="${{ secrets.IDENTIFIERS_API_BASE_URL }}"

- name: Upload screenshots
if: ${{ failure() }}
uses: actions/upload-artifact@v4
with:
name: screenshots-${{ needs.set-env.outputs.environment }}
path: screenshots
name: Deploy to environment
needs: [ set-env ]
uses: DFE-Digital/deploy-azure-container-apps-action/.github/workflows/build-push-deploy.yml@main
with:
docker-image-name: 'identapi-app'
docker-build-file-name: 'docker/Dockerfile'
environment: ${{ needs.set-env.outputs.environment }}
secrets:
azure-acr-client-id: ${{ secrets.AZURE_ACR_CLIENTID }}
azure-acr-secret: ${{ secrets.AZURE_ACR_SECRET }}
azure-acr-url: ${{ secrets.AZURE_ACR_URL }}
azure-aca-credentials: ${{ secrets.AZURE_ACA_CREDENTIALS }}
azure-aca-name: ${{ secrets.AZURE_ACA_NAME }}
azure-aca-resource-group: ${{ secrets.AZURE_ACA_RESOURCE_GROUP }}
8 changes: 7 additions & 1 deletion .github/workflows/continuous-integration-dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,14 @@ jobs:
run: |
dotnet-sonarscanner begin /k:"DFE-Digital_identifiers-api" /o:"dfe-digital" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.coverageReportPaths=CoverageReport/SonarQube.xml
dotnet build --no-restore
<<<<<<< HEAD
dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}"
dotnet test --no-build --verbosity normal --collect:"XPlat Code Coverage"

# Todo add this back to previous step once tests in place
# reportgenerator -reports:./**/coverage.cobertura.xml -targetdir:./CoverageReport -reporttypes:SonarQube
# reportgenerator -reports:./**/coverage.cobertura.xml -targetdir:./CoverageReport -reporttypes:SonarQube
=======
dotnet test --no-build --verbosity normal --collect:"XPlat Code Coverage"
reportgenerator -reports:./**/coverage.cobertura.xml -targetdir:./CoverageReport -reporttypes:SonarQube
dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}"
>>>>>>> 322be4b39eb8a99247ce69050f07091f2867ef62
2 changes: 1 addition & 1 deletion .github/workflows/continuous-integration-terraform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
fi
- name: Validate Terraform docs
uses: terraform-docs/gh-actions@v1.0.0
uses: terraform-docs/gh-actions@v1.1.0
with:
working-dir: terraform
config-file: .terraform-docs.yml
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
!/.idea/.idea.*/.idea
/.idea/.idea.*/.idea/*
!/.idea/.idea.*/.idea/runConfigurations
.idea
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
.vscode
Expand Down
37 changes: 37 additions & 0 deletions Dfe.Identifiers.Api.Test/ApiTestFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Net.Mime;
using Dfe.Identifiers.Infrastructure.Context;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

namespace Dfe.Identifiers.Api.Test
{
public class ApiTestFixture : IDisposable
{
private const string connectionStringKey = "ConnectionStrings:Default";
private DbContextOptions<MstrContext> _dbContextOptions { get; init; }

public ApiTestFixture()
{
var builder = new ConfigurationBuilder()
.AddUserSecrets<ApiTestFixture>();

Configuration = builder.Build();
_dbContextOptions = new DbContextOptionsBuilder<MstrContext>()
.UseSqlServer(Configuration[connectionStringKey])
.Options;
using var context = GetMstrContext();
context.Database.EnsureDeleted();
context.Database.Migrate();
}

private IConfigurationRoot Configuration { get; init; }

public MstrContext GetMstrContext() => new(_dbContextOptions);

public void Dispose()
{
}
}
}
39 changes: 39 additions & 0 deletions Dfe.Identifiers.Api.Test/DatabaseModelBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using AutoFixture;
using Dfe.Identifiers.Domain.Models;

namespace Dfe.Identifiers.Api.Test
{
public static class DatabaseModelBuilder
{
private static readonly Fixture _fixture = new Fixture();

public static Trust BuildTrust()
{
var result = _fixture.Create<Trust>();
result.SK = null;
result.TrustStatus = "Open";
result.TrustTypeId = 30;
result.TrustType = null;
result.TrustStatusId = null;
result.RegionId = null;
result.TrustBandingId = null;
result.RID = result.RID.Substring(0, 10);

return result;
}

public static Establishment BuildEstablishment()
{
var result = _fixture.Create<Establishment>();
result.SK = null;
result.LocalAuthority = null;
result.EstablishmentType = null;
result.PK_GIAS_URN = _fixture.Create<int>().ToString();
// Only 224 or 228 are valid in this subset of test data used (see mstr context)
result.EstablishmentTypeId = 224;
result.LocalAuthorityId = 1;

return result;
}
}
}
42 changes: 42 additions & 0 deletions Dfe.Identifiers.Api.Test/Dfe.Identifiers.Api.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<UserSecretsId>29f2c83e-d293-4cdb-b0bf-7ec5ed71bee5</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoFixture" Version="4.18.1" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/>
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="xunit" Version="2.4.2"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Dfe.Identifiers.Api\Dfe.Identifiers.Api.csproj" />
</ItemGroup>

<ItemGroup>
<Content Update="appsettings.test.json">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>

</Project>
16 changes: 16 additions & 0 deletions Dfe.Identifiers.Api.Test/Extensions/ActionResultExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.AspNetCore.Mvc;

namespace Dfe.Identifiers.Api.Test.Extensions;

public static class ActionResultExtension
{
public static Domain.Identifiers.IdentifiersCollection? GetIdentifiers(this ActionResult<Domain.Identifiers.IdentifiersCollection> actionResult)
{
return (Domain.Identifiers.IdentifiersCollection?)((OkObjectResult?)actionResult.Result)?.Value;
}

public static int? GetStatusCode<T>(this ActionResult<T> actionResult)
{
return ((ObjectResult?)actionResult.Result)?.StatusCode;
}
}
1 change: 1 addition & 0 deletions Dfe.Identifiers.Api.Test/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
Loading

0 comments on commit c873643

Please sign in to comment.