⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
54 changes: 0 additions & 54 deletions codecov-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ CodecovCLI is a new way for users to interact with Codecov directly from the use
- [create-commit](#create-commit)
- [create-report](#create-report)
- [do-upload](#do-upload)
- [create-report-results](#create-report-results)
- [get-report-results](#get-report-results)
- [pr-base-picking](#pr-base-picking)
- [send-notifications](#send-notifications)
- [empty-upload](#empty-upload)
- [How to Use Local Upload](#how-to-use-local-upload)
- [Work in Progress Features](#work-in-progress-features)
- [Plugin System](#plugin-system)
- [Static Analysis](#static-analysis)
Expand Down Expand Up @@ -123,8 +120,6 @@ Codecov-cli supports user input. These inputs, along with their descriptions and
| `create-commit` | Saves the commit's metadata in codecov, it's only necessary to run this once per commit
| `create-report` | Creates an empty report in codecov with initial data e.g. report name, report's commit
| `do-upload` | Searches for and uploads coverage data to codecov
| `create-report-results` | Used for local upload. It tells codecov that you finished local uploading and want it to calculate the results for you to get them locally.
| `get-report-results` | Used for local upload. It asks codecov to provide you the report results you calculated with the previous command.
| `pr-base-picking` | Tells codecov that you want to explicitly define a base for your PR
| `upload-process` | A wrapper for 3 commands. Create-commit, create-report and do-upload. You can use this command to upload to codecov instead of using the previously mentioned commands.
| `send-notifications` | A command that tells Codecov that you finished uploading and you want to be sent notifications. To disable automatically sent notifications please consider adding manual_trigger to your codecov.yml, so it will look like codecov: notify: manual_trigger: true.
Expand Down Expand Up @@ -156,7 +151,6 @@ Codecov-cli supports user input. These inputs, along with their descriptions and
|-r, --slug | owner/repo slug used instead of the private repo token in Self-hosted | Required
|-t, --token | Codecov upload token | Required
|--git-service | Git Provider. Options: github, gitlab, bitbucket, github_enterprise, gitlab_enterprise, bitbucket_server | Required
|--code| The code of the report. This is used in local uploading to isolate local reports from regular or cloud reports uploaded to codecov so they don't get merged. It's basically a name you give to your report e.g. local-report. | Optional
|-h, --help | Shows usage, and command options

## do-upload
Expand All @@ -165,7 +159,6 @@ Codecov-cli supports user input. These inputs, along with their descriptions and
| Option | Description | Usage
| :---: | :---: | :---: |
|-C, --sha, --commit-sha| Commit SHA (with 40 chars) | Required
|--report-code | The code of the report defined when creating the report. If unsure, leave default | Optional
|--network-root-folder | Root folder from which to consider paths on the network section default: (Current working directory) | Optional
|-s, --dir, --coverage-files-search-root-folder | Folder where to search for coverage files default: (Current Working Directory) | Optional
|--exclude, --coverage-files-search-exclude-folder | Folders to exclude from search | Optional
Expand All @@ -189,30 +182,6 @@ Codecov-cli supports user input. These inputs, along with their descriptions and
|--git-service | Git Provider. Options: github, gitlab, bitbucket, github_enterprise, gitlab_enterprise, bitbucket_server | Required
|-h, --help | Shows usage, and command options

## create-report-results
`codecovcli create-report-results [OPTIONS]`

| Option | Description | Usage
| :---: | :---: | :---: |
|--commit-sha | Commit SHA (with 40 chars) | Required
|--code | The code of the report. If unsure, leave default | Required
|--slug | owner/repo slug | Required
|--git-service | Git provider. Options: github, gitlab, bitbucket, github_enterprise, gitlab_enterprise, bitbucket_server | Optional
|-t, --token | Codecov upload token | Required
|-h, --help | Shows usage, and command options

## get-report-results
`codecovcli get-report-results [OPTIONS]`

| Option | Description | Usage
| :---: | :---: | :---: |
|--commit-sha | Commit SHA (with 40 chars) | Required
|--code | The code of the report. If unsure, leave default | Required
|--slug | owner/repo slug | Required
|--git-service | Git provider. Options: github, gitlab, bitbucket, github_enterprise, gitlab_enterprise, bitbucket_server | Optional
|-t, --token | Codecov upload token | Required
|-h, --help | Shows usage, and command options

## pr-base-picking
`codecovcli pr-base-picking [OPTIONS]`

Expand Down Expand Up @@ -254,25 +223,6 @@ are ignored by codecov (including README and configuration files)
| --git-service | Options: github, gitlab, bitbucket, github_enterprise, gitlab_enterprise, bitbucket_server | Optional |
| -h, --help | Show this message and exit. | Optional |

# How to Use Local Upload

The CLI also supports "dry run" local uploading. This is useful if you prefer to see Codecov status checks and coverage reporting locally, in your terminal, as opposed to opening a PR and waiting for your full CI to run. Local uploads do not interfere with regular uploads made from your CI for any given commit / Pull Request.

Local Upload is accomplished as follows:

```
pip install codecov-cli
codecovcli create-commit
codecovcli create-report --code <CODE>
codecovcli do-upload --report-code <CODE>
codecovcli create-report-results --code <CODE>
codecovcli get-report-results --code <CODE>
```

Codecov will calculate the coverage results, and return them in your terminal, telling you whether your PR will fail or pass the coverage check.

Note: In order for Local Upload to work, it must be used against a commit on the origin repository. Local Upload does not work for arbitrary diffs or uncommitted changes on your local machine.

# Work in Progress Features

The following features are somewhat implemented in code, but are not yet meant for use. These features will be documented once they are fully implemented in the CLI.
Expand All @@ -281,10 +231,6 @@ The following features are somewhat implemented in code, but are not yet meant f

To provide extensibility to some of its commands, the CLI makes use of a plugin system. For most cases, the default commands are sufficient. But in some cases, having some custom logic specific to your use case can be beneficial. Note that full documentation of the plugin system is pending, as the feature is still heavily a work in progress.

## Static Analysis

The CLI can perform basic static analysis on Python code today. This static analysis is meant to power more future looking Codecov features and, as such, is not required or in active use today. As more functionality dependent on static analysis becomes available for use, we will document static analysis in detail here.

# Contributions

This repository, like all of Codecov's repositories, strives to follow our general [Contributing guidelines](https://github.com/codecov/contributing). If you're considering making a contribution to this repository, we encourage review of our Contributing guidelines first.
Expand Down
2 changes: 1 addition & 1 deletion codecov-cli/codecov_cli/commands/create_report_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
logger = logging.getLogger("codecovcli")


@click.command()
@click.command(hidden=True, deprecated=True)
@click.option(
"--code", help="The code of the report. If unsure, leave default", default="default"
)
Expand Down
4 changes: 1 addition & 3 deletions codecov-cli/codecov_cli/commands/get_report_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import click
import sentry_sdk

from codecov_cli.fallbacks import CodecovOption, FallbackFieldEnum
from codecov_cli.helpers.args import get_cli_args
from codecov_cli.helpers.encoder import encode_slug
from codecov_cli.helpers.git import GitService
from codecov_cli.helpers.options import global_options
from codecov_cli.services.report import send_reports_result_get_request
from codecov_cli.types import CommandContext
Expand All @@ -15,7 +13,7 @@
logger = logging.getLogger("codecovcli")


@click.command()
@click.command(hidden=True, deprecated=True)
@click.option(
"--code", help="The code of the report. If unsure, leave default", default="default"
)
Expand Down
213 changes: 8 additions & 205 deletions codecov-cli/codecov_cli/commands/labelanalysis.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import json
import logging
import pathlib
import time
from typing import Dict, List, Optional

import click
import requests
import sentry_sdk

from codecov_cli.fallbacks import CodecovOption, FallbackFieldEnum
from codecov_cli.helpers import request
from codecov_cli.helpers.args import get_cli_args
from codecov_cli.helpers.config import CODECOV_API_URL
from codecov_cli.helpers.validators import validate_commit_sha
from codecov_cli.runners import get_runner
from codecov_cli.runners.types import (
Expand Down Expand Up @@ -92,7 +88,6 @@ def label_analysis(
):
with sentry_sdk.start_transaction(op="task", name="Label Analysis"):
with sentry_sdk.start_span(name="labelanalysis"):
enterprise_url = ctx.obj.get("enterprise_url")
args = get_cli_args(ctx)
logger.debug(
"Starting label analysis",
Expand Down Expand Up @@ -124,18 +119,6 @@ def label_analysis(
extra=dict(extra_log_attributes=dict(config=runner.params)),
)

upload_url = enterprise_url or CODECOV_API_URL
url = f"{upload_url}/labels/labels-analysis"
token_header = f"Repotoken {token}"
payload = {
"base_commit": base_commit_sha,
"head_commit": head_commit_sha,
"requested_labels": None,
}
# Send the initial label analysis request without labels
# Because labels might take a long time to collect
eid = _send_labelanalysis_request(payload, url, token_header)

logger.info("Collecting labels...")
requested_labels = runner.collect_tests()
logger.info(f"Collected {len(requested_labels)} test labels")
Expand All @@ -145,98 +128,15 @@ def label_analysis(
extra_log_attributes=dict(labels_collected=requested_labels)
),
)
payload["requested_labels"] = requested_labels

if eid:
# Initial request with no labels was successful
# Now we PATCH the labels in
patch_url = f"{upload_url}/labels/labels-analysis/{eid}"
_patch_labels(payload, patch_url, token_header)
else:
# Initial request with no labels failed
# Retry it
eid = _send_labelanalysis_request(payload, url, token_header)
if eid is None:
_fallback_to_collected_labels(
requested_labels,
runner,
dry_run=dry_run,
dry_run_format=dry_run_format,
fallback_reason="codecov_unavailable",
)
return

has_result = False
logger.info("Waiting for list of tests to run...")
start_wait = time.monotonic()
time.sleep(1)
while not has_result:
resp_data = request.get(
f"{upload_url}/labels/labels-analysis/{eid}",
headers={"Authorization": token_header},
)
resp_json = resp_data.json()
if resp_json["state"] == "finished":
logger.info(
"Received list of tests from Codecov",
extra=dict(
extra_log_attributes=dict(
processing_errors=resp_json.get("errors", [])
)
),
)
request_result = _potentially_calculate_absent_labels(
resp_json["result"], requested_labels
)
if not dry_run:
runner.process_labelanalysis_result(request_result)
else:
_dry_run_output(
LabelAnalysisRequestResult(request_result),
runner,
dry_run_format,
# It's possible that the task had processing errors and fallback to all tests
# Even though it's marked as FINISHED (not ERROR) it's not a true success
fallback_reason=(
"test_list_processing_errors"
if resp_json.get("errors", None)
else None
),
)
return
if resp_json["state"] == "error":
logger.error(
"Request had problems calculating",
extra=dict(
extra_log_attributes=dict(
base_commit=resp_json["base_commit"],
head_commit=resp_json["head_commit"],
external_id=resp_json["external_id"],
)
),
)
_fallback_to_collected_labels(
collected_labels=requested_labels,
runner=runner,
dry_run=dry_run,
dry_run_format=dry_run_format,
fallback_reason="test_list_processing_failed",
)
return
if max_wait_time and (time.monotonic() - start_wait) > max_wait_time:
logger.error(
f"Exceeded max waiting time of {max_wait_time} seconds. Running all tests.",
)
_fallback_to_collected_labels(
collected_labels=requested_labels,
runner=runner,
dry_run=dry_run,
dry_run_format=dry_run_format,
fallback_reason="max_wait_time_exceeded",
)
return
logger.info("Waiting more time for result...")
time.sleep(5)
_fallback_to_collected_labels(
requested_labels,
runner,
dry_run=dry_run,
dry_run_format=dry_run_format,
fallback_reason="codecov_unavailable",
)
return


def _parse_runner_params(runner_params: List[str]) -> Dict[str, str]:
Expand Down Expand Up @@ -271,103 +171,6 @@ def _parse_runner_params(runner_params: List[str]) -> Dict[str, str]:
return final_params


def _potentially_calculate_absent_labels(
request_result, requested_labels
) -> LabelAnalysisRequestResult:
if request_result["absent_labels"]:
# This means that Codecov already calculated everything for us
final_result = LabelAnalysisRequestResult(request_result)
else:
# Here we have to calculate the absent labels
# And also remove labels that maybe don't exist anymore from the set of labels to test
# Because codecov didn't have this info previously
requested_labels_set = set(requested_labels)
present_diff_labels_set = set(request_result.get("present_diff_labels", []))
present_report_labels_set = set(request_result.get("present_report_labels", []))
global_level_labels_set = set(request_result.get("global_level_labels", []))
final_result = LabelAnalysisRequestResult(
{
"present_report_labels": sorted(
present_report_labels_set & requested_labels_set
),
"present_diff_labels": sorted(
present_diff_labels_set & requested_labels_set
),
"absent_labels": sorted(
requested_labels_set - present_report_labels_set
),
"global_level_labels": sorted(
global_level_labels_set & requested_labels_set
),
}
)
logger.info(
"Received information about tests to run",
extra=dict(
extra_log_attributes=dict(
absent_labels=len(final_result.absent_labels),
present_diff_labels=len(final_result.present_diff_labels),
global_level_labels=len(final_result.global_level_labels),
present_report_labels=len(final_result.present_report_labels),
)
),
)
return final_result


def _patch_labels(payload, url, token_header):
logger.info("Sending collected labels to Codecov...")
try:
response = request.patch(
url, json=payload, headers={"Authorization": token_header}
)
if response.status_code < 300:
logger.info("Labels successfully sent to Codecov")
except requests.RequestException:
raise click.ClickException(click.style("Unable to reach Codecov", fg="red"))


def _send_labelanalysis_request(payload, url, token_header):
logger.info(
"Requesting set of labels to run...",
extra=dict(
extra_log_attributes=dict(
with_labels=(payload["requested_labels"] is not None)
)
),
)
try:
response = request.post(
url, data=payload, headers={"Authorization": token_header}
)
if response.status_code >= 500:
logger.warning(
"Sorry. Codecov is having problems",
extra=dict(extra_log_attributes=dict(status_code=response.status_code)),
)
return None
if response.status_code >= 400:
logger.warning(
"Got a 4XX status code back from Codecov",
extra=dict(
extra_log_attributes=dict(
status_code=response.status_code, response_json=response.json()
)
),
)
raise click.ClickException(
"There is some problem with the submitted information"
)
except requests.RequestException:
raise click.ClickException(click.style("Unable to reach Codecov", fg="red"))
eid = response.json()["external_id"]
logger.info(
"Label Analysis request successful",
extra=dict(extra_log_attributes=dict(request_id=eid)),
)
return eid


def _dry_run_json_output(
labels_to_run: set,
labels_to_skip: set,
Expand Down
2 changes: 1 addition & 1 deletion codecov-cli/codecov_cli/commands/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

@click.command()
@click.option(
"--code", help="The code of the report. If unsure, leave default", default="default"
"--code", help="The code of the report. If unsure, leave default", default="default", hidden=True
)
@click.option(
"-P",
Expand Down
Loading
Loading