You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While creating webhook for question Which events would you like to trigger this webhook? select Let me select individual events. then check Pull request
Below is a sample python web hook server for github events
#!/usr/bin/env python"""Github webhook server for mint automation."""fromBaseHTTPServerimportBaseHTTPRequestHandler, HTTPServerimportjsonfrompprintimportpprintdef_handle_pr_event(event_dict):
action=event_dict.get('action')
ifnotaction:
returnpprint(action)
ifactionnotin ['opened', 'closed', 'reopened', 'synchronize']:
returnifaction=='closed':
# [FIXME] cancel running mint verification job if anyreturnifaction=='synchronize':
# [FIXME] cancel running mint verification job if anypasspull_request_url=event_dict.get(
'pull_request', {}).get('_links', {}).get('html', {}).get('href')
# [FIXME] start mint verification jobpprint(pull_request_url)
classGHWebhookHandler(BaseHTTPRequestHandler):
def_send_error_headers(self):
self.send_response(400)
self.end_headers()
deflog_message(self, format, *args):
passdefdo_GET(self):
self._send_error_headers()
defdo_HEAD(self):
self._send_error_headers()
defdo_PUT(self):
self._send_error_headers()
defdo_POST(self):
content_length=int(self.headers['Content-Length'])
ifcontent_length<=0:
self.send_response(411)
self.end_headers()
returnpost_data=self.rfile.read(content_length)
event=json.loads(post_data)
self.send_response(202)
self.end_headers()
_handle_pr_event(event)
defrunHTTPServer(port):
httpd=HTTPServer(('', port), GHWebhookHandler)
try:
httpd.serve_forever()
exceptKeyboardInterrupt:
passhttpd.server_close()
if__name__=="__main__":
fromsysimportargvport=12345iflen(argv) ==2:
port=int(argv[1])
runHTTPServer(port)
Use ngrok to expose this webhook server by ./ngrok http 12345. Add Forwarding HTTP URL shown in ngrok console to Payload URL when creating webhook in github. Make sure Active to checked.
Below python code can be used to add a comment in the pull request.
fromgithubimportGithubgh=Github('<GENERATED-GITHUB-TOKEN>')
gh.get_repo("USER/REPO").get_pull(<PULL_REQUEST_ID>).create_issue_comment("<YOUR COMMENT TO ADD>")
Editing previously added comment in a PR using external program
Below python code can be used to add a comment in the pull request.
fromgithubimportGithubgh=Github('<GENERATED-GITHUB-TOKEN>')
comments=gh.get_repo("USER/REPO").get_pull(<PULL_REQUEST_ID>).get_issue_comments()
# Identify which comment we added previously. Assumed here comments[1]comments[1].edit("<UPDATED COMMENT>")