Skip to content

Commit 0213afd

Browse files
committed
Use GITHUB_TOKEN on webhook
Providing the `repo_name` parameter on webhook makes it possible to also use `GITHUB_TOKEN` for the `repo_token` parameter.
1 parent df415f1 commit 0213afd

5 files changed

Lines changed: 40 additions & 19 deletions

File tree

.github/workflows/push.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,5 @@ jobs:
4242
- name: Coveralls Finished
4343
uses: ./
4444
with:
45-
github-token: ${{ secrets.COVERALLS_REPO_TOKEN }}
4645
parallel-finished: true
4746
debug: true

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Change Log
22

3+
## [Unrelease]
4+
5+
- Made `github-token` parameter optional
6+
7+
38
## [v20200412]
49

510
- Leverages `with` keyword to configure the action

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ jobs:
4040
uses: AndreMiras/coveralls-python-action@develop
4141
with:
4242
parallel-finished: true
43-
github-token: ${{ secrets.COVERALLS_REPO_TOKEN }}
4443
```
4544
4645
## Configuration
@@ -54,7 +53,6 @@ jobs:
5453
# Default: false
5554
parallel: ''
5655
# Set to `true` for the last action when using `parallel: true`.
57-
# Note this phase requires `github-token: ${{ secrets.COVERALLS_REPO_TOKEN }}`.
5856
# Default: false
5957
parallel-finished: ''
6058
# Set to true to increase logger verbosity.

src/entrypoint.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ def get_github_ref():
7979
return os.environ.get("GITHUB_REF")
8080

8181

82+
def get_github_repository():
83+
"""e.g. octocat/Hello-World"""
84+
return os.environ.get("GITHUB_REPOSITORY")
85+
86+
8287
def get_pull_request_number(github_ref):
8388
"""
8489
>>> get_pull_request_number("refs/pull/<pull_request_number>/merge")
@@ -101,16 +106,20 @@ def get_build_number(github_sha, github_ref):
101106

102107
def post_webhook(repo_token):
103108
""""
104-
Note for this call, the repo token is always `COVERALLS_REPO_TOKEN`.
105-
It cannot be the `GITHUB_TOKEN`.
106109
https://docs.coveralls.io/parallel-build-webhook
107110
"""
108111
url = "https://coveralls.io/webhook"
109112
build_num = get_build_number(get_github_sha(), get_github_ref())
110-
params = {"repo_token": repo_token}
111-
json = {"payload": {"build_num": build_num, "status": "done"}}
112-
log.debug(f'requests.post("{url}", params={params}, json={json})')
113-
response = requests.post(url, params=params, json=json)
113+
# note this (undocumented) parameter is optional, but needed for using
114+
# `GITHUB_TOKEN` rather than `COVERALLS_REPO_TOKEN`
115+
repo_name = get_github_repository()
116+
json = {
117+
"repo_token": repo_token,
118+
"repo_name": repo_name,
119+
"payload": {"build_num": build_num, "status": "done"},
120+
}
121+
log.debug(f'requests.post("{url}", json={json})')
122+
response = requests.post(url, json=json)
114123
response.raise_for_status()
115124
log.debug(f"response.json(): {response.json()}")
116125
assert response.json() == {"done": True}, response.json()

tests/test_entrypoint.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,11 @@ def test_post_webhook(self):
158158
assert m_post.call_args_list == [
159159
mock.call(
160160
"https://coveralls.io/webhook",
161-
params={"repo_token": "TOKEN"},
162-
json={"payload": {"build_num": None, "status": "done"}},
161+
json={
162+
"repo_token": "TOKEN",
163+
"repo_name": None,
164+
"payload": {"build_num": None, "status": "done"},
165+
},
163166
)
164167
]
165168
# 2) only `GITHUB_SHA` is set
@@ -171,12 +174,13 @@ def test_post_webhook(self):
171174
assert m_post.call_args_list == [
172175
mock.call(
173176
"https://coveralls.io/webhook",
174-
params={"repo_token": "TOKEN"},
175177
json={
178+
"repo_token": "TOKEN",
179+
"repo_name": None,
176180
"payload": {
177181
"build_num": "ffac537e6cbbf934b08745a378932722df287a53",
178182
"status": "done",
179-
}
183+
},
180184
},
181185
)
182186
]
@@ -190,31 +194,34 @@ def test_post_webhook(self):
190194
assert m_post.call_args_list == [
191195
mock.call(
192196
"https://coveralls.io/webhook",
193-
params={"repo_token": "TOKEN"},
194197
json={
198+
"repo_token": "TOKEN",
199+
"repo_name": None,
195200
"payload": {
196201
"build_num": "ffac537e6cbbf934b08745a378932722df287a53",
197202
"status": "done",
198-
}
203+
},
199204
},
200205
)
201206
]
202207
# 4) `GITHUB_REF` is a pull request
203208
environ = {
204209
"GITHUB_SHA": "ffac537e6cbbf934b08745a378932722df287a53",
205210
"GITHUB_REF": "refs/pull/123/merge",
211+
"GITHUB_REPOSITORY": "octocat/Hello-World",
206212
}
207213
with patch_requests_post(json_response) as m_post, patch_os_envirion(environ):
208214
entrypoint.post_webhook(repo_token)
209215
assert m_post.call_args_list == [
210216
mock.call(
211217
"https://coveralls.io/webhook",
212-
params={"repo_token": "TOKEN"},
213218
json={
219+
"repo_token": "TOKEN",
220+
"repo_name": "octocat/Hello-World",
214221
"payload": {
215222
"build_num": "ffac537e6cbbf934b08745a378932722df287a53-PR-123",
216223
"status": "done",
217-
}
224+
},
218225
},
219226
)
220227
]
@@ -232,8 +239,11 @@ def test_post_webhook_error(self):
232239
assert m_post.call_args_list == [
233240
mock.call(
234241
"https://coveralls.io/webhook",
235-
params={"repo_token": "TOKEN"},
236-
json={"payload": {"build_num": None, "status": "done"}},
242+
json={
243+
"repo_token": "TOKEN",
244+
"repo_name": None,
245+
"payload": {"build_num": None, "status": "done"},
246+
},
237247
)
238248
]
239249
assert ex_info.value.args == (json_response,)

0 commit comments

Comments
 (0)