user1176409
user1176409

Reputation: 1

I want to get data from azure pipeline including code coverage and lines of code

I have just started working with Azure Pipelines and have just about gotten ok at it.

I have been creating pipeline builds and then using GET requests to send the data over to a website to display the results.

I have now been asked if I can get line of code and code coverage added to the website. Meaning I need to update my GET requests to include this.

I have been able to log lines of code in the pipeline log file by using some updated yaml code. This creates a text file in the log.

Annoyingly these results are not added to the GET request. I'm using the end points https://dev.azure.com/{organization}/_apis/public/build/builds/{buildId}/ticketedlogs?projectId={projectId}&api-version=4.1-preview.1

and https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs/{runId}/logs/{logId}?api-version=7.1-preview.1

I can get a list of all the builds back but nothing else. I can't seem to target an exact build or project.

I cannot make anything for code coverage work. I have tried coverlet with no luck. I keep getting error messages saying the request is not compatible with net5...but I'm on net7 and nothing says net5.

I NEED to get the data sent somehow to my web site.

I am considering things such as

  1. adding a post request to the yaml pipeline
  2. getting the log zip file and then processing the results to find line number
  3. send everything to Google Drive and then use the API to get folders and data from drive

This is my failed code coverage:

- stage: 'Test'
  displayName: 'Test stage'
  jobs:
  - job: 'TestWithCodeCoverage'
    displayName: 'Test with Code Coverage'

    steps:
    - task: DotNetCoreCLI@2
      inputs:
        command: 'test'
        projects: '**/*.csproj'
        arguments: '--configuration $(buildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura'
        publishTestResults: true
        testRunTitle: 'Code Coverage'

    - script: |
        dotnet tool install -g coverlet.console
        coverlet "$(Build.SourcesDirectory)/PipelineRepo.Tests/bin/$(buildConfiguration)/netcoreappX.Y/PipelineRepo.Tests.dll" --target "dotnet" --targetargs "test PipelineRepo.Tests/PipelineRepo.Tests.csproj --configuration $(buildConfiguration)" --format cobertura
      displayName: 'Calculate Code Coverage'

    - task: PublishCodeCoverageResults@1
      displayName: 'Publish Code Coverage Results'
      inputs:
        codeCoverageTool: 'Cobertura'
        summaryFileLocation: '$(Agent.TempDirectory)/**/*.cobertura.xml'
        reportDirectory: '$(Build.SourcesDirectory)/CodeCoverageReports'

//How I get lines of code saved to log

    - powershell: |
    $loc = Get-ChildItem -Recurse -File -Path $(System.DefaultWorkingDirectory) -Include *.cs,*.vb,*.js,*.ts,*.html,*.css,*.scss,*.json | Get-Content | Measure-Object -Line
    Write-Host "Total lines of code: $($loc.Lines)"
  displayName: 'Count Lines of Code xxx'
  condition: succeeded()

I'm pretty stuck and wondering if anyone know how to do this with APIs or just has a cunning way to send the data to myself.

Upvotes: 0

Views: 988

Answers (1)

Juanma Feliu
Juanma Feliu

Reputation: 1346

Not usre if it will help you but you can publish aditional files as artifacts with additionalCodeCoverageFiles option using a pattern to upload more info to report:

task: PublishCodeCoverageResults@1
  inputs:
    codeCoverageTool: 'JaCoCo' # 'Cobertura' | 'JaCoCo'. Required. Code coverage tool. Default: JaCoCo.
    summaryFileLocation: # string. Required. Summary file. 
    #pathToSources: # string. Path to Source files. 
    #reportDirectory: # string. Report directory. 
    additionalCodeCoverageFiles: # string. Additional files. 
    #failIfCoverageEmpty: false # boolean. Fail when code coverage results are missing. Default: false.

https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/reference/publish-code-coverage-results-v1?view=azure-pipelines

Upvotes: 0

Related Questions