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 X-Tokenless header when uploading from fork #334

Closed
wants to merge 8 commits into from
Closed
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ Codecov-cli supports user input. These inputs, along with their descriptions and
| :---: | :---: | :---: |
| -C, --sha, --commit-sha TEXT |Commit SHA (with 40 chars) | Required
| -r, --slug TEXT |owner/repo slug used instead of the private repo token in Self-hosted | Required
| -t, --token UUID |Codecov upload token | Required
| -t, --token TEXT |Codecov upload token | Required
| --git-service | Git provider. Options: github, gitlab, bitbucket, github_enterprise, gitlab_enterprise, bitbucket_server | Optional
| -h,--help |Show this message and exit.

Expand Down
4 changes: 1 addition & 3 deletions codecov_cli/commands/base_picking.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import typing
import uuid

import click

Expand Down Expand Up @@ -36,7 +35,6 @@
"-t",
"--token",
help="Codecov upload token",
type=click.UUID,
envvar="CODECOV_TOKEN",
)
@click.option(
Expand All @@ -51,7 +49,7 @@ def pr_base_picking(
base_sha: str,
pr: typing.Optional[int],
slug: typing.Optional[str],
token: typing.Optional[uuid.UUID],
token: typing.Optional[str],
service: typing.Optional[str],
):
enterprise_url = ctx.obj.get("enterprise_url")
Expand Down
3 changes: 1 addition & 2 deletions codecov_cli/commands/commit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import typing
import uuid

import click

Expand Down Expand Up @@ -42,7 +41,7 @@ def create_commit(
pull_request_number: typing.Optional[int],
branch: typing.Optional[str],
slug: typing.Optional[str],
token: typing.Optional[uuid.UUID],
token: typing.Optional[str],
git_service: typing.Optional[str],
fail_on_error: bool,
):
Expand Down
5 changes: 1 addition & 4 deletions codecov_cli/commands/create_report_result.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import logging
import uuid

import click

from codecov_cli.fallbacks import CodecovOption, FallbackFieldEnum
from codecov_cli.helpers.git import GitService
from codecov_cli.helpers.options import global_options
from codecov_cli.services.report import create_report_results_logic

Expand All @@ -23,7 +20,7 @@ def create_report_results(
code: str,
slug: str,
git_service: str,
token: uuid.UUID,
token: str,
fail_on_error: bool,
):
enterprise_url = ctx.obj.get("enterprise_url")
Expand Down
3 changes: 1 addition & 2 deletions codecov_cli/commands/empty_upload.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import typing
import uuid

import click

Expand All @@ -19,7 +18,7 @@ def empty_upload(
ctx,
commit_sha: str,
slug: typing.Optional[str],
token: typing.Optional[uuid.UUID],
token: typing.Optional[str],
git_service: typing.Optional[str],
fail_on_error: typing.Optional[bool],
):
Expand Down
3 changes: 1 addition & 2 deletions codecov_cli/commands/get_report_results.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
import uuid

import click

Expand All @@ -24,7 +23,7 @@ def get_report_results(
code: str,
slug: str,
git_service: str,
token: uuid.UUID,
token: str,
fail_on_error: bool,
):
enterprise_url = ctx.obj.get("enterprise_url")
Expand Down
4 changes: 1 addition & 3 deletions codecov_cli/commands/report.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import logging
import uuid

import click

from codecov_cli.fallbacks import CodecovOption, FallbackFieldEnum
from codecov_cli.helpers.git import GitService
from codecov_cli.helpers.options import global_options
from codecov_cli.services.report import create_report_logic

Expand Down Expand Up @@ -32,7 +30,7 @@ def create_report(
code: str,
slug: str,
git_service: str,
token: uuid.UUID,
token: str,
fail_on_error: bool,
pull_request_number: int,
):
Expand Down
3 changes: 1 addition & 2 deletions codecov_cli/commands/send_notifications.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import typing
import uuid

import click

Expand All @@ -19,7 +18,7 @@ def send_notifications(
ctx,
commit_sha: str,
slug: typing.Optional[str],
token: typing.Optional[uuid.UUID],
token: typing.Optional[str],
git_service: typing.Optional[str],
fail_on_error: bool,
):
Expand Down
3 changes: 1 addition & 2 deletions codecov_cli/commands/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os
import pathlib
import typing
import uuid

import click

Expand Down Expand Up @@ -185,7 +184,7 @@ def do_upload(
coverage_files_search_explicitly_listed_files: typing.List[pathlib.Path],
disable_search: bool,
disable_file_fixes: bool,
token: typing.Optional[uuid.UUID],
token: typing.Optional[str],
plugin_names: typing.List[str],
branch: typing.Optional[str],
slug: typing.Optional[str],
Expand Down
3 changes: 1 addition & 2 deletions codecov_cli/commands/upload_process.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging
import pathlib
import typing
import uuid

import click

Expand Down Expand Up @@ -38,7 +37,7 @@ def upload_process(
coverage_files_search_explicitly_listed_files: typing.List[pathlib.Path],
disable_search: bool,
disable_file_fixes: bool,
token: typing.Optional[uuid.UUID],
token: typing.Optional[str],
plugin_names: typing.List[str],
branch: typing.Optional[str],
slug: typing.Optional[str],
Expand Down
1 change: 0 additions & 1 deletion codecov_cli/helpers/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"-t",
"--token",
help="Codecov upload token",
type=click.UUID,
envvar="CODECOV_TOKEN",
),
click.option(
Expand Down
7 changes: 2 additions & 5 deletions codecov_cli/helpers/request.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
import uuid
from time import sleep

import click
Expand Down Expand Up @@ -75,14 +74,12 @@ def send_post_request(
return request_result(post(url=url, data=data, headers=headers, params=params))


def get_token_header_or_fail(token: uuid.UUID) -> dict:
def get_token_header_or_fail(token: str) -> dict:
if token is None:
raise click.ClickException(
"Codecov token not found. Please provide Codecov token with -t flag."
)
if not isinstance(token, uuid.UUID):
raise click.ClickException(f"Token must be UUID. Received {type(token)}")
return {"Authorization": f"token {token.hex}"}
return {"Authorization": f"token {token}"}


@retry_request
Expand Down
3 changes: 1 addition & 2 deletions codecov_cli/services/commit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import typing
import uuid

from codecov_cli.helpers.config import CODECOV_API_URL
from codecov_cli.helpers.encoder import decode_slug, encode_slug
Expand All @@ -20,7 +19,7 @@ def create_commit_logic(
pr: typing.Optional[str],
branch: typing.Optional[str],
slug: typing.Optional[str],
token: uuid.UUID,
token: str,
service: typing.Optional[str],
enterprise_url: typing.Optional[str] = None,
fail_on_error: bool = False,
Expand Down
5 changes: 2 additions & 3 deletions codecov_cli/services/report/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
import logging
import time
import uuid

import requests

Expand All @@ -25,7 +24,7 @@ def create_report_logic(
code: str,
slug: str,
service: str,
token: uuid.UUID,
token: str,
enterprise_url: str,
pull_request_number: int,
fail_on_error: bool = False,
Expand Down Expand Up @@ -65,7 +64,7 @@ def create_report_results_logic(
code: str,
slug: str,
service: str,
token: uuid.UUID,
token: str,
enterprise_url: str,
fail_on_error: bool = False,
):
Expand Down
3 changes: 1 addition & 2 deletions codecov_cli/services/upload/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import typing
import uuid
from pathlib import Path

import click
Expand Down Expand Up @@ -39,7 +38,7 @@ def do_upload_logic(
coverage_files_search_exclude_folders: typing.List[Path],
coverage_files_search_explicitly_listed_files: typing.List[Path],
plugin_names: typing.List[str],
token: uuid.UUID,
token: str,
branch: typing.Optional[str],
slug: typing.Optional[str],
pull_request_number: typing.Optional[str],
Expand Down
7 changes: 2 additions & 5 deletions codecov_cli/services/upload/legacy_upload_sender.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import logging
import typing
import uuid
from dataclasses import dataclass

import requests

from codecov_cli import __version__ as codecov_cli_version
from codecov_cli.helpers.config import LEGACY_CODECOV_API_URL
from codecov_cli.helpers.request import send_post_request, send_put_request
Expand Down Expand Up @@ -39,7 +36,7 @@ def send_upload_data(
self,
upload_data: UploadCollectionResult,
commit_sha: str,
token: uuid.UUID,
token: str,
env_vars: typing.Dict[str, str],
report_code: str = None,
name: typing.Optional[str] = None,
Expand Down Expand Up @@ -70,7 +67,7 @@ def send_upload_data(
}

if token:
headers = {"X-Upload-Token": token.hex}
headers = {"X-Upload-Token": token}
else:
logger.warning("Token is empty.")
headers = {"X-Upload-Token": ""}
Expand Down
3 changes: 1 addition & 2 deletions codecov_cli/services/upload/upload_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import json
import logging
import typing
import uuid
import zlib
from typing import Any, Dict

Expand All @@ -29,7 +28,7 @@ def send_upload_data(
self,
upload_data: UploadCollectionResult,
commit_sha: str,
token: uuid.UUID,
token: str,
env_vars: typing.Dict[str, str],
report_code: str,
name: typing.Optional[str] = None,
Expand Down
2 changes: 1 addition & 1 deletion tests/commands/test_invoke_upload_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_upload_process_options(mocker):
" -C, --sha, --commit-sha TEXT Commit SHA (with 40 chars) [required]",
" -Z, --fail-on-error Exit with non-zero code in case of error",
" --git-service [github|gitlab|bitbucket|github_enterprise|gitlab_enterprise|bitbucket_server]",
" -t, --token UUID Codecov upload token",
" -t, --token TEXT Codecov upload token",
" -r, --slug TEXT owner/repo slug used instead of the private",
" repo token in Self-hosted",
" --report-code TEXT The code of the report. If unsure, leave",
Expand Down
5 changes: 2 additions & 3 deletions tests/helpers/test_legacy_upload_sender.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import uuid
from urllib import parse

import pytest
Expand All @@ -11,7 +10,7 @@
from tests.data import reports_examples

upload_collection = UploadCollectionResult(["1", "apple.py", "3"], [], [])
random_token = uuid.UUID("f359afb9-8a2a-42ab-a448-c3d267ff495b")
random_token = "f359afb9-8a2a-42ab-a448-c3d267ff495b"
random_sha = "845548c6b95223f12e8317a1820705f64beaf69e"
named_upload_data = {
"name": "name",
Expand Down Expand Up @@ -68,7 +67,7 @@ class TestUploadSender(object):
def test_upload_sender_post_called_with_right_parameters(
self, mocked_responses, mocked_legacy_upload_endpoint, mocked_storage_server
):
headers = {"X-Upload-Token": random_token.hex}
headers = {"X-Upload-Token": random_token}
params = {
"package": f"codecov-cli/{codecov_cli_version}",
"commit": random_sha,
Expand Down
9 changes: 1 addition & 8 deletions tests/helpers/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_get_token_header_or_fail():
# Test with a valid UUID token
token = uuid.uuid4()
result = get_token_header_or_fail(token)
assert result == {"Authorization": f"token {token.hex}"}
assert result == {"Authorization": f"token {str(token)}"}

# Test with a None token
token = None
Expand All @@ -69,13 +69,6 @@ def test_get_token_header_or_fail():
== "Codecov token not found. Please provide Codecov token with -t flag."
)

# Test with an invalid token type
token = "invalid_token"
with pytest.raises(Exception) as e:
get_token_header_or_fail(token)

assert str(e.value) == f"Token must be UUID. Received {type(token)}"


def test_request_retry(mocker, valid_response):
expected_response = request_result(valid_response)
Expand Down
5 changes: 2 additions & 3 deletions tests/helpers/test_upload_sender.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
import uuid
from pathlib import Path

import pytest
Expand All @@ -13,7 +12,7 @@
from tests.data import reports_examples

upload_collection = UploadCollectionResult(["1", "apple.py", "3"], [], [])
random_token = uuid.UUID("f359afb9-8a2a-42ab-a448-c3d267ff495b")
random_token = "f359afb9-8a2a-42ab-a448-c3d267ff495b"
random_sha = "845548c6b95223f12e8317a1820705f64beaf69e"
named_upload_data = {
"report_code": "report_code",
Expand Down Expand Up @@ -133,7 +132,7 @@ class TestUploadSender(object):
def test_upload_sender_post_called_with_right_parameters(
self, mocked_responses, mocked_legacy_upload_endpoint, mocked_storage_server
):
headers = {"Authorization": f"token {random_token.hex}"}
headers = {"Authorization": f"token {random_token}"}

mocked_legacy_upload_endpoint.match = [
matchers.json_params_matcher(request_data),
Expand Down