Skip to content

Commit

Permalink
Merge branch 'release/v20201129'
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreMiras committed Nov 30, 2020
2 parents 4df6e2e + 3329aa9 commit f5fd5c3
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 129 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v1
- uses: actions/setup-python@v2

- name: Unit tests
run: make test
run: make test PYTHON_WITH_VERSION=python3

- name: Default arguments
uses: ./
Expand Down
10 changes: 6 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
# Change Log

## [v20200413]
## [v20201129]
- Improves exception logging
- Fixes KeyError with `coveralls==2.2.0`
- Fixes entrypoint assertion error ([dfm](https://github.com/dfm)), refs #5
- Fixes parallel mode ([johanneswilm](https://github.com/johanneswilm)), refs #7 and #8

## [v20200413]
- Made `github-token` parameter optional


## [v20200412]

- Leverages `with` keyword to configure the action
- Adds parallel support
- Adds webhook support
- Increases test coverage to 100%


## [v20200411]

- Initial release
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pytest: virtualenv
test: pytest lint

lint/isort: virtualenv
$(ISORT) --check-only --recursive --diff $(SOURCES)
$(ISORT) --check-only --diff $(SOURCES)

lint/flake8: virtualenv
$(FLAKE8) $(SOURCES)
Expand All @@ -46,7 +46,7 @@ lint/black: virtualenv
lint: lint/isort lint/flake8 lint/black

format/isort: virtualenv
$(ISORT) --recursive $(SOURCES)
$(ISORT) $(SOURCES)

format/black: virtualenv
$(BLACK) $(SOURCES)
Expand Down
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@ jobs:
uses: AndreMiras/coveralls-python-action@develop
with:
parallel: true
flag-name: Unit Test

coveralls_finish:
needs: test
runs-on: ubuntu-latest
steps:
- name: Coveralls Finished
uses: AndreMiras/coveralls-python-action@develop
with:
parallel-finished: true
coveralls_finish:
needs: test
runs-on: ubuntu-latest
steps:
- name: Coveralls Finished
uses: AndreMiras/coveralls-python-action@develop
with:
parallel-finished: true
```
## Configuration
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
54 changes: 32 additions & 22 deletions src/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,53 @@ class ExitCode(Enum):


def set_failed(message):
log.error(message)
exc_info = message if isinstance(message, Exception) else None
log.error(message, exc_info=exc_info)
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?)
# - "github" (from GitHub jobs?)
# for some reasons the "service_name" can be one or the other
# (depending on where it's ran from?)
service_names = ("github", "github-actions")
# sets `service_job_id` key so it exists, refs:
# https://github.com/coveralls-clients/coveralls-python/pull/241/files#r532248047
service_job_id = None
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):
coveralls = Coveralls(service_name=service_name)
with patch_os_environ(repo_token, parallel, flag_name):
coveralls = Coveralls(
service_name=service_name, service_job_id=service_job_id
)
try:
result = coveralls.wear()
break
except CoverallsException as e:
log.warning(
f"Failed submitting coverage with service_name: {service_name}"
f"Failed submitting coverage with service_name: {service_name}",
exc_info=e,
)
log.warning(e)
if result is None:
set_failed("Failed to submit coverage")
log.debug(result)
Expand Down Expand Up @@ -84,6 +94,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,20 +111,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
"""
"""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 All @@ -121,8 +126,9 @@ def post_webhook(repo_token):
log.debug(f'requests.post("{url}", json={json})')
response = requests.post(url, json=json)
response.raise_for_status()
log.debug(f"response.json(): {response.json()}")
assert response.json() == {"done": True}, response.json()
result = response.json()
log.debug(f"response.json(): {result}")
assert result.get("done", False), result


def str_to_bool(value):
Expand All @@ -138,6 +144,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 @@ -161,13 +169,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
Loading

0 comments on commit f5fd5c3

Please sign in to comment.