Skip to content

Commit

Permalink
Use GITHUB_TOKEN on webhook
Browse files Browse the repository at this point in the history
Providing the `repo_name` parameter on webhook makes it possible to also
use `GITHUB_TOKEN` for the `repo_token` parameter.
  • Loading branch information
AndreMiras committed Apr 12, 2020
1 parent df415f1 commit 0213afd
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 19 deletions.
1 change: 0 additions & 1 deletion .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,5 @@ jobs:
- name: Coveralls Finished
uses: ./
with:
github-token: ${{ secrets.COVERALLS_REPO_TOKEN }}
parallel-finished: true
debug: true
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## [Unrelease]

- Made `github-token` parameter optional


## [v20200412]

- Leverages `with` keyword to configure the action
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ jobs:
uses: AndreMiras/coveralls-python-action@develop
with:
parallel-finished: true
github-token: ${{ secrets.COVERALLS_REPO_TOKEN }}
```
## Configuration
Expand All @@ -54,7 +53,6 @@ jobs:
# Default: false
parallel: ''
# Set to `true` for the last action when using `parallel: true`.
# Note this phase requires `github-token: ${{ secrets.COVERALLS_REPO_TOKEN }}`.
# Default: false
parallel-finished: ''
# Set to true to increase logger verbosity.
Expand Down
21 changes: 15 additions & 6 deletions src/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ def get_github_ref():
return os.environ.get("GITHUB_REF")


def get_github_repository():
"""e.g. octocat/Hello-World"""
return os.environ.get("GITHUB_REPOSITORY")


def get_pull_request_number(github_ref):
"""
>>> get_pull_request_number("refs/pull/<pull_request_number>/merge")
Expand All @@ -101,16 +106,20 @@ def get_build_number(github_sha, github_ref):

def post_webhook(repo_token):
""""
Note for this call, the repo token is always `COVERALLS_REPO_TOKEN`.
It cannot be the `GITHUB_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)
# note this (undocumented) parameter is optional, but needed for using
# `GITHUB_TOKEN` rather than `COVERALLS_REPO_TOKEN`
repo_name = get_github_repository()
json = {
"repo_token": repo_token,
"repo_name": repo_name,
"payload": {"build_num": build_num, "status": "done"},
}
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()
Expand Down
30 changes: 20 additions & 10 deletions tests/test_entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,11 @@ def test_post_webhook(self):
assert m_post.call_args_list == [
mock.call(
"https://coveralls.io/webhook",
params={"repo_token": "TOKEN"},
json={"payload": {"build_num": None, "status": "done"}},
json={
"repo_token": "TOKEN",
"repo_name": None,
"payload": {"build_num": None, "status": "done"},
},
)
]
# 2) only `GITHUB_SHA` is set
Expand All @@ -171,12 +174,13 @@ def test_post_webhook(self):
assert m_post.call_args_list == [
mock.call(
"https://coveralls.io/webhook",
params={"repo_token": "TOKEN"},
json={
"repo_token": "TOKEN",
"repo_name": None,
"payload": {
"build_num": "ffac537e6cbbf934b08745a378932722df287a53",
"status": "done",
}
},
},
)
]
Expand All @@ -190,31 +194,34 @@ def test_post_webhook(self):
assert m_post.call_args_list == [
mock.call(
"https://coveralls.io/webhook",
params={"repo_token": "TOKEN"},
json={
"repo_token": "TOKEN",
"repo_name": None,
"payload": {
"build_num": "ffac537e6cbbf934b08745a378932722df287a53",
"status": "done",
}
},
},
)
]
# 4) `GITHUB_REF` is a pull request
environ = {
"GITHUB_SHA": "ffac537e6cbbf934b08745a378932722df287a53",
"GITHUB_REF": "refs/pull/123/merge",
"GITHUB_REPOSITORY": "octocat/Hello-World",
}
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",
params={"repo_token": "TOKEN"},
json={
"repo_token": "TOKEN",
"repo_name": "octocat/Hello-World",
"payload": {
"build_num": "ffac537e6cbbf934b08745a378932722df287a53-PR-123",
"status": "done",
}
},
},
)
]
Expand All @@ -232,8 +239,11 @@ def test_post_webhook_error(self):
assert m_post.call_args_list == [
mock.call(
"https://coveralls.io/webhook",
params={"repo_token": "TOKEN"},
json={"payload": {"build_num": None, "status": "done"}},
json={
"repo_token": "TOKEN",
"repo_name": None,
"payload": {"build_num": None, "status": "done"},
},
)
]
assert ex_info.value.args == (json_response,)
Expand Down

0 comments on commit 0213afd

Please sign in to comment.