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

fix for parallel mode, flag_name & base_path #8

Merged
merged 11 commits into from
Nov 29, 2020
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
uses: AndreMiras/coveralls-python-action@develop
with:
parallel: true
flag-name: Unit Test

coveralls_finish:
needs: test
Expand All @@ -56,6 +57,12 @@ jobs:
# Set to `true` for the last action when using `parallel: true`.
# Default: false
parallel-finished: ''
# A name to identify the current job. This is useful in combination with `parallel: true`.
# Default: false
flag-name: ''
# A sub-directory in which coverage was executed.
# Default: false
base-path: ''
# Set to true to increase logger verbosity.
# Default: false
debug: ''
Expand Down
10 changes: 10 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ inputs:
parallel-finished:
description: 'Set to true for the last action when using `parallel: true`.'
default: false
base-path:
description: 'The name of a sub-directory in which the coverage files are to be found.'
default: false
flag-name:
description: 'A description of the current job used in connection with parallel.'
default: false
debug:
description: 'Set to `true` to increase logger verbosity.'
default: false
Expand All @@ -23,6 +29,10 @@ runs:
args:
- --github-token
- ${{ inputs.github-token }}
- --base-path
- ${{ inputs.base-path }}
- --flag-name
- ${{ inputs.flag-name }}
- --parallel
- ${{ inputs.parallel }}
- --parallel-finished
Expand Down
31 changes: 18 additions & 13 deletions src/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,21 @@ def set_failed(message):
sys.exit(ExitCode.FAILURE)


def patch_os_environ(repo_token, parallel):
def patch_os_environ(repo_token, parallel, flag_name):
"""
Temporarily updates the environment variable to satisfy coveralls Python API.
That is because the coveralls package API consumes mostly environment variables.
"""
# https://github.com/coveralls-clients/coveralls-python/blob/2.0.0/coveralls/api.py#L146
parallel = "true" if parallel else ""
environ = {"COVERALLS_REPO_TOKEN": repo_token, "COVERALLS_PARALLEL": parallel}
if flag_name:
environ["COVERALLS_FLAG_NAME"] = flag_name
log.debug(f"Patching os.environ with: {environ}")
return mock.patch.dict("os.environ", environ)


def run_coveralls(repo_token, parallel=False):
def run_coveralls(repo_token, parallel=False, flag_name=False, base_path=False):
"""Submits job to coveralls."""
# note that coveralls.io "service_name" can either be:
# - "github-actions" (local development?)
Expand All @@ -44,9 +46,11 @@ def run_coveralls(repo_token, parallel=False):
# (depending on where it's ran from?)
service_names = ("github", "github-actions")
result = None
if base_path and os.path.exists(base_path):
os.chdir(base_path)
for service_name in service_names:
log.info(f"Trying submitting coverage with service_name: {service_name}...")
with patch_os_environ(repo_token, parallel):
with patch_os_environ(repo_token, parallel, flag_name):
coveralls = Coveralls(service_name=service_name)
try:
result = coveralls.wear()
Expand Down Expand Up @@ -84,6 +88,11 @@ def get_github_repository():
return os.environ.get("GITHUB_REPOSITORY")


def get_github_run_id():
"""e.g. 88748489334"""
return os.environ.get("GITHUB_RUN_ID")


def get_pull_request_number(github_ref):
"""
>>> get_pull_request_number("refs/pull/<pull_request_number>/merge")
Expand All @@ -96,18 +105,10 @@ def is_pull_request(github_ref):
return github_ref and github_ref.startswith("refs/pull/")


def get_build_number(github_sha, github_ref):
build_number = github_sha
if is_pull_request(github_ref):
pull_request_number = get_pull_request_number(github_ref)
build_number = f"{github_sha}-PR-{pull_request_number}"
return build_number


def post_webhook(repo_token):
"""https://docs.coveralls.io/parallel-build-webhook"""
url = "https://coveralls.io/webhook"
build_num = get_build_number(get_github_sha(), get_github_ref())
build_num = get_github_run_id()
# note this (undocumented) parameter is optional, but needed for using
# `GITHUB_TOKEN` rather than `COVERALLS_REPO_TOKEN`
repo_name = get_github_repository()
Expand Down Expand Up @@ -137,6 +138,8 @@ def str_to_bool(value):
def parse_args():
parser = argparse.ArgumentParser(description="Greetings")
parser.add_argument("--github-token", nargs=1, required=True)
parser.add_argument("--flag-name", required=False, default=False)
parser.add_argument("--base-path", required=False, default=False)
parser.add_argument(
"--parallel", type=str_to_bool, nargs="?", const=True, default=False
)
Expand All @@ -160,13 +163,15 @@ def main():
debug = args.debug
repo_token = args.github_token[0]
parallel = args.parallel
flag_name = args.flag_name
base_path = args.base_path
parallel_finished = args.parallel_finished
set_log_level(debug)
log.debug(f"args: {args}")
if parallel_finished:
post_webhook(repo_token)
else:
run_coveralls(repo_token, parallel)
run_coveralls(repo_token, parallel, flag_name, base_path)


def try_main():
Expand Down
96 changes: 29 additions & 67 deletions tests/test_entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,29 @@ def test_main(self):
"entrypoint.run_coveralls"
) as m_run_coveralls:
entrypoint.main()
assert m_run_coveralls.call_args_list == [mock.call("TOKEN", False)]
assert m_run_coveralls.call_args_list == [
mock.call("TOKEN", False, False, False)
]

def test_main_flag_name(self):
argv = ["src/entrypoint.py", "--github-token", "TOKEN", "--flag-name", "FLAG"]
with patch_sys_argv(argv), mock.patch(
"entrypoint.run_coveralls"
) as m_run_coveralls:
entrypoint.main()
assert m_run_coveralls.call_args_list == [
mock.call("TOKEN", False, "FLAG", False)
]

def test_main_base_path(self):
argv = ["src/entrypoint.py", "--github-token", "TOKEN", "--base-path", "SRC"]
with patch_sys_argv(argv), mock.patch(
"entrypoint.run_coveralls"
) as m_run_coveralls:
entrypoint.main()
assert m_run_coveralls.call_args_list == [
mock.call("TOKEN", False, False, "SRC")
]

def test_main_parallel_finished(self):
argv = ["src/entrypoint.py", "--github-token", "TOKEN", "--parallel-finished"]
Expand Down Expand Up @@ -123,31 +145,11 @@ def test_run_coveralls_wear_error_twice(self):
entrypoint.run_coveralls(repo_token="TOKEN")
assert ex_info.value.args == (entrypoint.ExitCode.FAILURE,)

def test_get_build_number(self):
github_sha = "ffac537e6cbbf934b08745a378932722df287a53"
github_ref = "refs/pull/123/merge"
assert (
entrypoint.get_build_number(github_sha, github_ref)
== "ffac537e6cbbf934b08745a378932722df287a53-PR-123"
)
github_ref = "refs/heads/feature-branch-1"
assert (
entrypoint.get_build_number(github_sha, github_ref)
== "ffac537e6cbbf934b08745a378932722df287a53"
)
github_ref = None
assert (
entrypoint.get_build_number(github_sha, github_ref)
== "ffac537e6cbbf934b08745a378932722df287a53"
)

def test_post_webhook(self):
"""
Tests different uses cases:
1) default, no environment variable
2) only `GITHUB_SHA` is set
3) `GITHUB_REF` is a branch
4) `GITHUB_REF` is a pull request
2) `GITHUB_RUN_ID` is set
"""
repo_token = "TOKEN"
json_response = {"done": True}
Expand All @@ -165,50 +167,10 @@ def test_post_webhook(self):
},
)
]
# 2) only `GITHUB_SHA` is set
environ = {
"GITHUB_SHA": "ffac537e6cbbf934b08745a378932722df287a53",
}
with patch_requests_post(json_response) as m_post, patch_os_envirion(environ):
entrypoint.post_webhook(repo_token)
assert m_post.call_args_list == [
mock.call(
"https://coveralls.io/webhook",
json={
"repo_token": "TOKEN",
"repo_name": None,
"payload": {
"build_num": "ffac537e6cbbf934b08745a378932722df287a53",
"status": "done",
},
},
)
]
# 3) `GITHUB_REF` is a branch
environ = {
"GITHUB_SHA": "ffac537e6cbbf934b08745a378932722df287a53",
"GITHUB_REF": "refs/heads/feature-branch-1",
}
with patch_requests_post(json_response) as m_post, patch_os_envirion(environ):
entrypoint.post_webhook(repo_token)
assert m_post.call_args_list == [
mock.call(
"https://coveralls.io/webhook",
json={
"repo_token": "TOKEN",
"repo_name": None,
"payload": {
"build_num": "ffac537e6cbbf934b08745a378932722df287a53",
"status": "done",
},
},
)
]
# 4) `GITHUB_REF` is a pull request
# 2) `GITHUB_RUN_ID` and `GITHUB_REPOSITORY` are set
environ = {
"GITHUB_SHA": "ffac537e6cbbf934b08745a378932722df287a53",
"GITHUB_REF": "refs/pull/123/merge",
"GITHUB_REPOSITORY": "octocat/Hello-World",
"GITHUB_RUN_ID": "845347868344",
"GITHUB_REPOSITORY": "AndreMiras/coveralls-python-action",
}
with patch_requests_post(json_response) as m_post, patch_os_envirion(environ):
entrypoint.post_webhook(repo_token)
Expand All @@ -217,9 +179,9 @@ def test_post_webhook(self):
"https://coveralls.io/webhook",
json={
"repo_token": "TOKEN",
"repo_name": "octocat/Hello-World",
"repo_name": "AndreMiras/coveralls-python-action",
"payload": {
"build_num": "ffac537e6cbbf934b08745a378932722df287a53-PR-123",
"build_num": "845347868344",
"status": "done",
},
},
Expand Down