Skip to content

Commit

Permalink
Draft webhook support
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreMiras committed Apr 12, 2020
1 parent b565df3 commit 46adbba
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 31 deletions.
15 changes: 14 additions & 1 deletion .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ jobs:
with:
debug: true

# TODO
- name: With --parallel
uses: ./
with:
parallel: true

coveralls_finish:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Coveralls Finished
uses: ./
with:
github-token: ${{ secrets.COVERALLS_REPO_TOKEN }}
parallel-finished: true
debug: true
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,15 @@ jobs:

- name: Coveralls
uses: AndreMiras/coveralls-python-action@develop
with:
parallel: true

coveralls_finish:
needs: test
runs-on: ubuntu-latest
steps:
- name: Coveralls Finished
uses: AndreMiras/coveralls-python-action@develop
with:
parallel-finished: true
```
79 changes: 49 additions & 30 deletions src/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import argparse
import logging
import os
import sys
from enum import Enum
from unittest import mock
Expand Down Expand Up @@ -61,34 +62,53 @@ def run_coveralls(repo_token, parallel=False):
log.info(result["url"])


def post_webhook(repo_token, build_num):
""""
# https://docs.coveralls.io/parallel-build-webhook
coveralls_finish:
name: Coveralls finished webhook
needs: ["Tests"]
runs-on: ubuntu-latest
steps:
- name: webhook
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COVERALLS_SERVICE_NAME: github
TRAVIS_JOB_ID: ${{ github.ref }}:${{ github.sha }}
run: |
curl "https://coveralls.io/webhook?repo_token=$COVERALLS_REPO_TOKEN" \
--data "payload[job_id]=$TRAVIS_JOB_ID&payload[status]=done"
def get_github_sha():
"""e.g. ffac537e6cbbf934b08745a378932722df287a53"""
return os.environ.get("GITHUB_SHA")


def get_github_ref():
"""
The branch or tag ref that triggered the workflow.
For example, refs/heads/feature-branch-1.
If neither a branch or tag is available for the variable will not exist.
- for pull_request events: refs/pull/<pull_request_number>/merge
- for push event: refs/heads/<branch>
https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables
"""
return os.environ.get("GITHUB_REF")


def get_pull_request_number(github_ref):
"""
>>> get_pull_request_number("refs/pull/<pull_request_number>/merge")
"<pull_request_number>"
"""
url = f"https://coveralls.io/webhook?repo_token={repo_token}"
# TRAVIS_JOB_ID: ${{ github.ref }}:${{ github.sha }}
# data = "payload[job_id]=$TRAVIS_JOB_ID&payload[status]=done"
data = {
"payload": {
# TODO job_id?
"build_num": build_num,
"status": "done",
}
}
requests.post(url, data)
return github_ref.split("/")[2]


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())
params = {"repo_token": repo_token}
json = {"payload": {"build_num": build_num, "status": "done"}}
log.debug(f'requests.post("{url}", params={params}, json={json})')
response = requests.post(url, params=params, json=json)
response.raise_for_status()
log.debug(f"response.json(): {response.json()}")


def str_to_bool(value):
Expand Down Expand Up @@ -129,10 +149,9 @@ def main():
parallel = args.parallel
parallel_finished = args.parallel_finished
set_log_level(debug)
log.debug(f"args: {args}")
if parallel_finished:
# TODO
# post_webhook(repo_token, build_num)
pass
post_webhook(repo_token)
else:
run_coveralls(repo_token, parallel)

Expand Down
99 changes: 99 additions & 0 deletions tests/test_entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def patch_sys_argv(argv):
return mock.patch("sys.argv", argv)


def patch_requests_post():
return mock.patch("entrypoint.requests.post")


class TestEntryPoint:
def test_main_no_token(self):
"""Argument `--github-token` is required."""
Expand Down Expand Up @@ -57,3 +61,98 @@ def test_run_coveralls_github_token(self):
mock.call.debug(m_wear.return_value),
mock.call.info(url),
]

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
"""
repo_token = "TOKEN"
# 1) default, no environment variable
environ = {}
with patch_requests_post() as m_post, patch_os_envirion(environ):
entrypoint.post_webhook(repo_token)
assert m_post.call_args_list == [
mock.call(
"https://coveralls.io/webhook",
params={"repo_token": "TOKEN"},
json={"payload": {"build_num": None, "status": "done"}},
)
]
# 2) only `GITHUB_SHA` is set
environ = {
"GITHUB_SHA": "ffac537e6cbbf934b08745a378932722df287a53",
}
with patch_requests_post() as m_post, patch_os_envirion(environ):
entrypoint.post_webhook(repo_token)
assert m_post.call_args_list == [
mock.call(
"https://coveralls.io/webhook",
params={"repo_token": "TOKEN"},
json={
"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() as m_post, patch_os_envirion(environ):
entrypoint.post_webhook(repo_token)
assert m_post.call_args_list == [
mock.call(
"https://coveralls.io/webhook",
params={"repo_token": "TOKEN"},
json={
"payload": {
"build_num": "ffac537e6cbbf934b08745a378932722df287a53",
"status": "done",
}
},
)
]
# 4) `GITHUB_REF` is a pull request
environ = {
"GITHUB_SHA": "ffac537e6cbbf934b08745a378932722df287a53",
"GITHUB_REF": "refs/pull/123/merge",
}
with patch_requests_post() as m_post, patch_os_envirion(environ):
entrypoint.post_webhook(repo_token)
assert m_post.call_args_list == [
mock.call(
"https://coveralls.io/webhook",
params={"repo_token": "TOKEN"},
json={
"payload": {
"build_num": "ffac537e6cbbf934b08745a378932722df287a53-PR-123",
"status": "done",
}
},
)
]

0 comments on commit 46adbba

Please sign in to comment.