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

Add code coverage upload command #1520

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

nikita-tkachenko-datadog
Copy link

@nikita-tkachenko-datadog nikita-tkachenko-datadog commented Jan 9, 2025

What and why?

Adds a new coverage upload command that sends code coverage reports to Datadog.
See EVP usage in Code Coverage product prototype RFC for details
SDTEST-1183

Review checklist

  • Feature or bugfix MUST have appropriate tests (unit, integration)

@nikita-tkachenko-datadog nikita-tkachenko-datadog added the ci-visibility Related to [junit, gate, metric, tag, trace, dora, deployment] label Jan 9, 2025
@datadog-datadog-prod-us1
Copy link

datadog-datadog-prod-us1 bot commented Jan 9, 2025

Datadog Report

Branch report: nikita-tkachenko/code-coverage-upload
Commit report: f79d7cf
Test service: datadog-ci-tests

✅ 0 Failed, 244 Passed, 0 Skipped, 1m 37.24s Total duration (2m 17.58s time saved)

@nikita-tkachenko-datadog nikita-tkachenko-datadog changed the title Nikita tkachenko/code coverage upload Add code coverage upload command Jan 9, 2025
@nikita-tkachenko-datadog nikita-tkachenko-datadog marked this pull request as ready for review January 9, 2025 14:04
Copy link

@michaelcretzman michaelcretzman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved.

I see [//]: <> (TODO: Add link to the documentation page) in the doc. Please update this before publishing.

Copy link
Contributor

@juan-fernandez juan-fernandez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks mostly good! Left minor comments 😄

src/commands/coverage/README.md Outdated Show resolved Hide resolved
src/commands/coverage/README.md Outdated Show resolved Hide resolved
```

- `--path` is the directory or file path in which the code coverage reports are located. If you pass a folder, the CLI will look for all `.xml` files in it. This argument can be provided multiple times with different values
- `--flush` (default: `false`): you may pass `--flush=1` or `--flush=true` to signal that you have uploaded all the coverage reports for the current commit/PR. This will trigger the coverage reports to be processed and the results to displayed in the UI.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious, what happens if the user forgets to pass this?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reports will be uploaded to Datadog, but the coverage metric will not be produced. This is needed for the cases when there are multiple independent CI jobs producing their own coverage reports - in such scenario the flush should only happen once all of the reports from all of the jobs have been uploaded

- The resulting dictionary will be merged with whatever is in the `DD_MEASURES` environment variable. If a `key` appears both in `--measures` and `DD_MEASURES`, whatever value is in `DD_MEASURES` will take precedence.
- `--dry-run` (default: `false`): it will run the command without the final upload step. All other checks are performed.
- `--skip-git-metadata-upload` (default: `true`): if you want to upload git metadata, you may pass `--skip-git-metadata-upload=0` or `--skip-git-metadata-upload=false`.
- `--git-repository-url` is a string with the repository URL to retrieve git metadata from. If this is missing, the URL is retrieved from the local git repository.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this has confused users in the past. Do we need it? I think this should've only been in the git metadata upload command

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is indeed optional. I don't know how commonly this is used. Removed it for now

src/commands/coverage/README.md Outdated Show resolved Hide resolved
src/commands/coverage/utils.ts Outdated Show resolved Hide resolved
src/commands/coverage/upload.ts Outdated Show resolved Hide resolved
Comment on lines 278 to 295
const pathsByFormat: {format: string; paths: string[]}[] = []

for (const codeCoverageReportPath of uniqueFiles) {
const format: string | undefined = (await detectFormat(codeCoverageReportPath)) || this.format
const validationErrorMessage = await validateCoverageReport(codeCoverageReportPath, format, this.format)
if (validationErrorMessage) {
this.context.stdout.write(renderInvalidFile(codeCoverageReportPath, validationErrorMessage))
} else {
let formatEntry = pathsByFormat.find((entry) => entry.format === format)
if (!formatEntry) {
formatEntry = {format: format as string, paths: []}
pathsByFormat.push(formatEntry)
}
formatEntry.paths.push(codeCoverageReportPath)
}
}

return pathsByFormat
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think pathsByFormat could actually be a dictionary more simply 😄 .

❓ Also: what's this.format ?

    const pathsByFormat: {[key:string]: string[]} = {}

    for (const codeCoverageReportPath of uniqueFiles) {
      const format = detectFormat(codeCoverageReportPath)
      const validationErrorMessage = validateCoverageReport(codeCoverageReportPath, format)
      if (validationErrorMessage) {
        this.context.stdout.write(renderInvalidFile(codeCoverageReportPath, validationErrorMessage))
      } else {
        const paths = pathsByFormat[format]
        if (!paths) {
          pathsByFormat[format] = [codeCoverageReportPath]
        } else {
          pathsByFormat[format].push(codeCoverageReportPath)
        }
      }
    }

    return pathsByFormat

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it to be a dictionary.
this.format allows the users to specify format manually in case our logic cannot detect it automatically

src/commands/coverage/upload.ts Outdated Show resolved Hide resolved
src/commands/coverage/upload.ts Show resolved Hide resolved
src/commands/coverage/renderer.ts Show resolved Hide resolved
src/commands/coverage/upload.ts Show resolved Hide resolved
src/commands/coverage/upload.ts Outdated Show resolved Hide resolved
src/commands/coverage/utils.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@Drarig29 Drarig29 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good on my end, thanks for handling the comments!

Copy link
Contributor

@juan-fernandez juan-fernandez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏 Great job! Left some minor comments

src/commands/coverage/__tests__/upload.test.ts Outdated Show resolved Hide resolved
src/commands/coverage/__tests__/upload.test.ts Outdated Show resolved Hide resolved
src/commands/coverage/__tests__/upload.test.ts Outdated Show resolved Hide resolved
src/commands/coverage/__tests__/upload.test.ts Outdated Show resolved Hide resolved
src/commands/coverage/__tests__/upload.test.ts Outdated Show resolved Hide resolved
src/commands/coverage/renderer.ts Outdated Show resolved Hide resolved
src/commands/coverage/renderer.ts Outdated Show resolved Hide resolved
src/commands/coverage/upload.ts Show resolved Hide resolved
@datadog-datadog-prod-us1
Copy link

Datadog Report

Branch report: nikita-tkachenko/code-coverage-upload
Commit report: 377b3c3
Test service: datadog-ci-tests

✅ 0 Failed, 9116 Passed, 0 Skipped, 4m 30.8s Total Time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci-visibility Related to [junit, gate, metric, tag, trace, dora, deployment]
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants