Skip to content

Commit 00b98cc

Browse files
committed
Add option to specify PR as raw payload JSON
In addition to using the four separate `pr_*` arguments, PR information can now be specified as a raw JSON representation of a PR webhook payload. This makes it easier to specify the correct arguments and is more flexible when the payload is received from an API call.
1 parent cf57ec0 commit 00b98cc

3 files changed

Lines changed: 44 additions & 1 deletion

File tree

action.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ inputs:
5959
description: 'source of the Pull Request'
6060
required: false
6161

62+
pull_request_payload:
63+
description: 'Pull Request in jSON payload form'
64+
required: false
65+
6266
runs:
6367
using: 'docker'
6468
image: 'Dockerfile'

github_status_embed/__main__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@
7676
# Extract Action arguments by creating dataclasses
7777
workflow = Workflow.from_arguments(arguments)
7878
webhook = Webhook.from_arguments(arguments)
79-
pull_request = PullRequest.from_arguments(arguments)
79+
if arguments.get("pull_request_payload", False):
80+
pull_request = PullRequest.from_payload(arguments)
81+
else:
82+
pull_request = PullRequest.from_arguments(arguments)
8083

8184
# Send webhook
8285
success = send_webhook(workflow, webhook, pull_request, dry_run)

github_status_embed/types.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import dataclasses
44
import enum
5+
import json
6+
import logging
57
import typing
68

9+
log = logging.getLogger(__name__)
710

811
MIN_EMBED_FIELD_LENGTH = 20
912

@@ -199,6 +202,39 @@ class PullRequest(TypedDataclass, optional=True):
199202
pr_title: str
200203
pr_source: str
201204

205+
@classmethod
206+
def from_payload(cls, arguments: typing.Dict[str, str]) -> typing.Optional[PullRequest]:
207+
"""Create a Pull Request instance from Pull Request Payload JSON."""
208+
# Safe load the JSON Payload provided as a command line argument.
209+
raw_payload = arguments.pop('pull_request_payload')
210+
log.debug(f"Attempting to parse PR Payload JSON: {raw_payload!r}.")
211+
try:
212+
payload = json.loads(raw_payload)
213+
except json.JSONDecodeError:
214+
log.debug("Failed to parse JSON, dropping down to empty payload")
215+
payload = {}
216+
else:
217+
log.debug("Successfully parsed parsed payload")
218+
219+
# If the payload contains multiple PRs in a list, use the first one.
220+
if isinstance(payload, list):
221+
log.debug("The payload contained a list, extracting first PR.")
222+
payload = payload[0] if payload else {}
223+
224+
if not payload:
225+
log.warning("PR payload could not be parsed, attempting regular pr arguments.")
226+
return cls.from_arguments(arguments)
227+
228+
# Get the target arguments from the payload, yielding similar results
229+
# when keys are missing as to when their corresponding arguments are
230+
# missing.
231+
arguments["pr_author_login"] = payload.get('user', {}).get('login', '')
232+
arguments["pr_number"] = payload.get('number', '')
233+
arguments["pr_title"] = payload.get('title', '')
234+
arguments["pr_source"] = payload.get('head', {}).get('label', '')
235+
236+
return cls.from_arguments(arguments)
237+
202238
@property
203239
def author(self) -> str:
204240
"""Return the `pr_author_login` field."""

0 commit comments

Comments
 (0)