Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run SonarCloud only on pull requests to masterSecurity scan #4

Open
wants to merge 38 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
53226a6
Add unit tests
tpetchel Apr 4, 2019
0dc38dc
Support build configurations
tpetchel Apr 5, 2019
c6bf3a2
Add SDK version variable
tpetchel Apr 10, 2019
239f769
Add unit tests
tpetchel Apr 4, 2019
1ae708a
Support build configurations
tpetchel Apr 5, 2019
b7bee9f
Add SDK version variable
tpetchel Apr 10, 2019
5cdd89e
Add ProjectGuid
tpetchel Apr 12, 2019
82cb9dc
Merge branch 'unit-tests' of https://github.com/MicrosoftDocs/mslearn…
tpetchel Apr 12, 2019
e78924e
Change from mode to region
tpetchel Apr 12, 2019
8d5ff35
Simplify configuration
tpetchel Apr 23, 2019
7c01f3c
Run and publish unit tests
tpetchel Apr 23, 2019
eb6ee7a
Run and publish unit tests
tpetchel Apr 23, 2019
3097a58
Run and publish unit tests
tpetchel Apr 23, 2019
79fd0d4
Add code coverage
tpetchel Apr 23, 2019
1660663
Add code coverage
tpetchel Apr 23, 2019
81c6931
Add coverlet.msbuild
tpetchel Apr 24, 2019
41e4387
Add output directory
tpetchel Apr 24, 2019
20fd01a
Remove reportDirectory
tpetchel Apr 24, 2019
0e714ac
opencover
tpetchel Apr 24, 2019
4689da9
opencover,cobertura
tpetchel Apr 24, 2019
83e992d
quotes
tpetchel Apr 24, 2019
2016d43
cobertura only
tpetchel Apr 24, 2019
7bed41c
cobertura2
tpetchel Apr 25, 2019
63b0406
Add code coverage
tpetchel Apr 25, 2019
0ef3292
add failing test
tpetchel Apr 25, 2019
a93261b
Remove test code
tpetchel May 2, 2019
244bd17
Ignore .sonarqube/
tpetchel May 2, 2019
7f993bc
Update README.md
tpetchel May 22, 2019
70205cb
Merge branch 'master' into security-scan
tpetchel May 22, 2019
483b792
Merge branch 'master' into security-scan
tpetchel Aug 6, 2019
f2f834c
Merge branch 'master' into security-scan
tpetchel Sep 25, 2019
2383305
Merge branch 'master' into security-scan
tpetchel Oct 14, 2019
fde9b5b
Add trigger
tpetchel Nov 5, 2019
97f07a2
update to .NET Core 3.1
jamcneil Jan 15, 2020
1ed6a2e
Merge pull request #1211 from jamcneil/jm/security-scan-update
tpetchel Jan 21, 2020
f1ae2fd
Analyze code using SonarCloud
salbeck-sit Apr 3, 2020
a697f5a
Scan only on pulle requests to master
salbeck-sit Apr 20, 2020
8c2ea7b
Merge branch 'master' into security-scan
salbeck-sit Apr 20, 2020
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,6 @@ ASALocalRun/

# MFractors (Xamarin productivity tool) working folder
.mfractor/

# SonarQube
.sonarqube/
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq.Expressions;
using System.Threading.Tasks;
using NUnit.Framework;
using TailSpin.SpaceGame.Web;
using TailSpin.SpaceGame.Web.Models;

namespace Tests
{
public class DocumentDBRepository_GetItemsAsyncShould
{
private IDocumentDBRepository<Score> _scoreRepository;

[SetUp]
public void Setup()
{
using (Stream scoresData = typeof(IDocumentDBRepository<Score>)
.Assembly
.GetManifestResourceStream("Tailspin.SpaceGame.Web.SampleData.scores.json"))
{
_scoreRepository = new LocalDocumentDBRepository<Score>(scoresData);
}
}

[TestCase("Milky Way")]
[TestCase("Andromeda")]
[TestCase("Pinwheel")]
[TestCase("NGC 1300")]
[TestCase("Messier 82")]
public void FetchOnlyRequestedGameRegion(string gameRegion)
{
const int PAGE = 0; // take the first page of results
const int MAX_RESULTS = 10; // sample up to 10 results

// Form the query predicate.
// This expression selects all scores for the provided game region.
Expression<Func<Score, bool>> queryPredicate = score => (score.GameRegion == gameRegion);

// Fetch the scores.
Task<IEnumerable<Score>> scoresTask = _scoreRepository.GetItemsAsync(
queryPredicate, // the predicate defined above
score => 1, // we don't care about the order
PAGE,
MAX_RESULTS
);
IEnumerable<Score> scores = scoresTask.Result;

// Verify that each score's game region matches the provided game region.
Assert.That(scores, Is.All.Matches<Score>(score => score.GameRegion == gameRegion));
}
}
}
23 changes: 23 additions & 0 deletions Tailspin.SpaceGame.Web.Tests/Tailspin.SpaceGame.Web.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
<ProjectGuid>{773BA444-0D67-4F37-8762-17E108CCD5F5}</ProjectGuid>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.msbuild" Version="2.6.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="nunit" Version="3.10.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Tailspin.SpaceGame.Web\Tailspin.SpaceGame.Web.csproj" />
</ItemGroup>

</Project>
8 changes: 7 additions & 1 deletion Tailspin.SpaceGame.Web.sln
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@


Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tailspin.SpaceGame.Web", "Tailspin.SpaceGame.Web\Tailspin.SpaceGame.Web.csproj", "{A0C4E31E-AC75-4F39-9F59-0AA19D9B8F46}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tailspin.SpaceGame.Web.Tests", "Tailspin.SpaceGame.Web.Tests\Tailspin.SpaceGame.Web.Tests.csproj", "{773BA444-0D67-4F37-8762-17E108CCD5F5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -13,5 +15,9 @@ Global
{A0C4E31E-AC75-4F39-9F59-0AA19D9B8F46}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A0C4E31E-AC75-4F39-9F59-0AA19D9B8F46}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A0C4E31E-AC75-4F39-9F59-0AA19D9B8F46}.Release|Any CPU.Build.0 = Release|Any CPU
{773BA444-0D67-4F37-8762-17E108CCD5F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{773BA444-0D67-4F37-8762-17E108CCD5F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{773BA444-0D67-4F37-8762-17E108CCD5F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{773BA444-0D67-4F37-8762-17E108CCD5F5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions Tailspin.SpaceGame.Web/LocalDocumentDBRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public LocalDocumentDBRepository(string fileName)
_items = JsonConvert.DeserializeObject<List<T>>(File.ReadAllText(fileName));
}

public LocalDocumentDBRepository(Stream stream)
{
// Serialize the items from the provided JSON document.
_items = JsonConvert.DeserializeObject<List<T>>(new StreamReader(stream).ReadToEnd());
}

/// <summary>
/// Retrieves the item from the store with the given identifier.
/// </summary>
Expand Down
56 changes: 39 additions & 17 deletions Tailspin.SpaceGame.Web/Tailspin.SpaceGame.Web.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<ProjectGuid>{A0C4E31E-AC75-4F39-9F59-0AA19D9B8F46}</ProjectGuid>
</PropertyGroup>

<ItemGroup>
<!-- <PackageReference Include="Microsoft.AspNetCore.App" /> -->
<!-- <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" /> -->
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
</ItemGroup>

<ItemGroup>
<Folder Include="wwwroot\images\avatars\" />
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<ProjectGuid>{A0C4E31E-AC75-4F39-9F59-0AA19D9B8F46}</ProjectGuid>
</PropertyGroup>

<ItemGroup>
<!-- <PackageReference Include="Microsoft.AspNetCore.App" /> -->
<!-- <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" /> -->
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
</ItemGroup>

<ItemGroup>
<Folder Include="wwwroot\images\avatars\" />
</ItemGroup>
<ItemGroup>
<Content Remove="SampleData\profiles.json" />
<Content Remove="SampleData\scores.json" />
<Content Remove="SampleData\profiles.json" />
<Content Remove="SampleData\scores.json" />
</ItemGroup>
<ItemGroup>
<Content Update="SampleData\profiles.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="SampleData\scores.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="SampleData\profiles.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</EmbeddedResource>
<EmbeddedResource Include="SampleData\scores.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</EmbeddedResource>
</ItemGroup>
</Project>
70 changes: 70 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ steps:
inputs:
version: '$(dotnetSdkVersion)'

- task: UseDotNet@2
displayName: 'Use .NET Core SDK 2.1.505 for SonarCloud'
inputs:
version: '2.1.505'

- task: Npm@1
displayName: 'Run npm install'
inputs:
Expand All @@ -38,13 +43,78 @@ steps:
command: 'restore'
projects: '**/*.csproj'

- task: SonarCloudPrepare@1
displayName: 'Prepare SonarCloud analysis'
inputs:
SonarCloud: 'SonarCloud connection 1'
organization: '$(SonarOrganization)'
scannerMode: 'MSBuild'
projectKey: '$(SonarProjectKey)'
projectName: '$(SonarProjectName)'
projectVersion: '$(Build.BuildNumber)'
extraProperties: |
sonar.cs.opencover.reportsPaths=$(Build.SourcesDirectory)/TestResults/Coverage/coverage.opencover.xml
sonar.exclusions=**/wwwroot/lib/**/*
condition: |
and
(
succeeded(),
eq(variables['Build.Reason'], 'PullRequest'),
eq(variables['System.PullRequest.TargetBranch'], 'master')
)

- task: DotNetCoreCLI@2
displayName: 'Build the project - $(buildConfiguration)'
inputs:
command: 'build'
arguments: '--no-restore --configuration $(buildConfiguration)'
projects: '**/*.csproj'

- task: DotNetCoreCLI@2
displayName: 'Install ReportGenerator'
inputs:
command: custom
custom: tool
arguments: 'install --global dotnet-reportgenerator-globaltool'

- task: DotNetCoreCLI@2
displayName: 'Run unit tests - $(buildConfiguration)'
inputs:
command: 'test'
arguments: '--no-build --configuration $(buildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat="cobertura%2copencover" /p:CoverletOutput=$(Build.SourcesDirectory)/TestResults/Coverage/'
publishTestResults: true
projects: '**/*.Tests.csproj'

- script: |
reportgenerator -reports:$(Build.SourcesDirectory)/**/coverage.cobertura.xml -targetdir:$(Build.SourcesDirectory)/CodeCoverage -reporttypes:HtmlInline_AzurePipelines
displayName: 'Create code coverage report'

- task: SonarCloudAnalyze@1
displayName: 'Run SonarCloud code analysis'
condition: |
and
(
succeeded(),
eq(variables['Build.Reason'], 'PullRequest'),
eq(variables['System.PullRequest.TargetBranch'], 'master')
)

- task: SonarCloudPublish@1
displayName: 'Publish SonarCloud quality gate results'
condition: |
and
(
succeeded(),
eq(variables['Build.Reason'], 'PullRequest'),
eq(variables['System.PullRequest.TargetBranch'], 'master')
)

- task: PublishCodeCoverageResults@1
displayName: 'Publish code coverage report'
inputs:
codeCoverageTool: 'cobertura'
summaryFileLocation: '$(Build.SourcesDirectory)/**/coverage.cobertura.xml'

- task: DotNetCoreCLI@2
displayName: 'Publish the project - $(buildConfiguration)'
inputs:
Expand Down