Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing KeyError for X-Request-Id header, use X-Github-Delivery for event ID instead #2

Merged
merged 3 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyghee/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def get_event_info(request):
"""
event_info = {
'action': request.json.get('action', UNKNOWN),
'id': request.headers['X-Request-Id'],
'id': request.headers['X-Github-Delivery'],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit puzzled by this, and X-Request-Id suddenly isn't there anymore...
Maybe that's my mistake, and I'm relying on something that's not always there in the header.

Where did you get the X-Github-Delivery from, is there a reference for that, that this uniquely identifies an event?

I'm wondering whether we should use this instead:

 'id': request.headers.get('X-Request-Id') or request.headers.get('X-Github-Delivery'),

Maybe we also need to make sure that we get proper error reporting if neither of these keys are available?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on https://apievangelist.com/2017/09/13/webhook-delivery-headers-from-github-api/, X-GitHub-Delivery indeed seems to be the correct field to use to get a unique ID for the event

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I logged the HTTP headers and compared their values with the "Event ID" shown on the smee.io/channel page. I don't know if there is a reference.

Apparently, both headers may be valid, see octokit/webhooks.js#579 Don't know if they can be present both in a single request. Your suggestion to use one of the headers might be the right way to go forward.

Copy link
Author

@trz42 trz42 May 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly, the X-Request-Id was not present in my limited tests only triggering installation events.

'signature-sha1': request.headers['X-Hub-Signature'],
'timestamp_raw': request.headers['Timestamp'],
'type': request.headers['X-GitHub-Event'],
Expand Down
4 changes: 2 additions & 2 deletions tests/event_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
'Timestamp': TIMESTAMP_001,
'X-GitHub-Event': EVENT_TYPE_CREATE,
'X-Hub-Signature': 'sha1=0123456789abcedf0123456789abcedf01234567', # fake signature!
'X-Request-Id': REQUEST_ID_001,
'X-Github-Delivery': REQUEST_ID_001,
}, {
# request.json
'action': ACTION_CREATED,
Expand All @@ -41,7 +41,7 @@
'Timestamp': TIMESTAMP_001,
'X-GitHub-Event': EVENT_TYPE_ISSUE_COMMENT,
'X-Hub-Signature': 'sha1=0123456789abcedf0123456789abcedf01234567', # fake signature!
'X-Request-Id': REQUEST_ID_001,
'X-Github-Delivery': REQUEST_ID_001,
}, {
# request.json
'action': ACTION_CREATED,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_process_event(tmpdir):
expected_header = {
'Timestamp': TIMESTAMP_001,
'X-GitHub-Event': event_type,
'X-Request-Id': REQUEST_ID_001,
'X-Github-Delivery': REQUEST_ID_001,
}
with open(os.path.join(event_data_dir, header_fp), 'r') as fp:
header_data = json.load(fp)
Expand Down