Skip to content

Commit 96c0578

Browse files
add X-Tokenless header when uploading from fork (#335)
Public forks will accept tokenless uploads. Currently we were just sending an empty header (no Authorization). These changes add a header `X-Tokenless: fork_slug` so we know easily that the request is from a fork, and which fork it's from. I also have a tendency to compulsively add typehints to complex types.
1 parent b304bf0 commit 96c0578

7 files changed

Lines changed: 32 additions & 13 deletions

File tree

codecov_cli/helpers/git.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import logging
22
import re
33
from enum import Enum
4+
from typing import Optional
45
from urllib.parse import urlparse
56

67
from codecov_cli.helpers.encoder import decode_slug
8+
from codecov_cli.helpers.git_services import PullDict
79
from codecov_cli.helpers.git_services.github import Github
810

911
slug_regex = re.compile(r"[^/\s]+\/[^/\s]+$")
@@ -92,15 +94,15 @@ def parse_git_service(remote_repo_url: str):
9294
return None
9395

9496

95-
def is_fork_pr(pull_dict):
97+
def is_fork_pr(pull_dict: PullDict) -> bool:
9698
"""
9799
takes in dict: pull_dict
98100
returns true if PR is made in a fork context, false if not.
99101
"""
100102
return pull_dict and pull_dict["head"]["slug"] != pull_dict["base"]["slug"]
101103

102104

103-
def get_pull(service, slug, pr_num):
105+
def get_pull(service, slug, pr_num) -> Optional[PullDict]:
104106
"""
105107
takes in str git service e.g. github, gitlab etc., slug in the owner/repo format, and the pull request number
106108
returns the pull request info gotten from the git service provider if successful, None if not
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from typing import TypedDict
2+
3+
4+
class CommitInfo(TypedDict):
5+
sha: str
6+
label: str
7+
ref: str
8+
slug: str
9+
10+
11+
class PullDict(TypedDict):
12+
url: str
13+
head: CommitInfo
14+
base: CommitInfo

codecov_cli/helpers/git_services/github.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
import requests
44

5+
from codecov_cli.helpers.git_services import PullDict
6+
57

68
class Github:
79
api_url = "https://api.github.com"
810
api_version = "2022-11-28"
911

10-
def get_pull_request(self, slug, pr_number):
12+
def get_pull_request(self, slug, pr_number) -> PullDict:
1113
pull_url = f"/repos/{slug}/pulls/{pr_number}"
1214
url = self.api_url + pull_url
1315
headers = {"X-GitHub-Api-Version": self.api_version}

codecov_cli/services/commit/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def send_commit_data(
4646
decoded_slug = decode_slug(slug)
4747
pull_dict = get_pull(service, decoded_slug, pr) if not token else None
4848
if is_fork_pr(pull_dict):
49-
headers = {}
49+
headers = {"X-Tokenless": pull_dict["head"]["slug"]}
5050
branch = pull_dict["head"]["slug"] + ":" + branch
5151
logger.info("The PR is happening in a forked repo. Using tokenless upload.")
5252
else:

codecov_cli/services/report/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ def send_create_report_request(
5151
pull_dict = (
5252
get_pull(service, decoded_slug, pull_request_number) if not token else None
5353
)
54-
headers = (
55-
{} if not token and is_fork_pr(pull_dict) else get_token_header_or_fail(token)
56-
)
54+
if is_fork_pr(pull_dict):
55+
headers = {"X-Tokenless": pull_dict["head"]["slug"]}
56+
else:
57+
headers = get_token_header_or_fail(token)
5758
upload_url = enterprise_url or CODECOV_API_URL
5859
url = f"{upload_url}/upload/{service}/{encoded_slug}/commits/{commit_sha}/reports"
5960
return send_post_request(url=url, headers=headers, data=data)

codecov_cli/services/upload/upload_sender.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ def send_upload_data(
5656
pull_dict = (
5757
get_pull(git_service, slug, pull_request_number) if not token else None
5858
)
59-
headers = (
60-
{}
61-
if not token and is_fork_pr(pull_dict)
62-
else get_token_header_or_fail(token)
63-
)
59+
60+
if is_fork_pr(pull_dict):
61+
headers = {"X-Tokenless": pull_dict["head"]["slug"]}
62+
else:
63+
headers = get_token_header_or_fail(token)
6464
encoded_slug = encode_slug(slug)
6565
upload_url = enterprise_url or CODECOV_API_URL
6666
url = f"{upload_url}/upload/{git_service}/{encoded_slug}/commits/{commit_sha}/reports/{report_code}/uploads"

tests/services/commit/test_commit_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,5 +195,5 @@ def mock_request(*args, headers={}, **kwargs):
195195
"pullid": "1",
196196
"branch": "user_forked_repo/codecov-cli:branch",
197197
},
198-
headers={},
198+
headers={"X-Tokenless": "user_forked_repo/codecov-cli"},
199199
)

0 commit comments

Comments
 (0)