This repository was archived by the owner on Apr 15, 2025. It is now read-only.
forked from carlos-jenkins/python-github-webhooks
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample
More file actions
61 lines (53 loc) · 2.07 KB
/
example
File metadata and controls
61 lines (53 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
# Rename this file to "repository" in order to
# have it process repo deletions and create issues.
# You will also need to change the content of lines
# 16, 17, 20, 21 and the <user> mention on line 56
#
# Original Source: Sample Projects related to the GitHub Platform
# https://github.com/github/platform-samples/tree/master/hooks/python/flask-github-webhooks
# License: Creative Commons Zero v1.0 Universal
import sys
import json
import requests
# Authentication for the user who is filing the issue. Username/API_KEY
USERNAME = ''
API_KEY = ''
# The repository to add this issue to
REPO_OWNER = 'my-github-org'
REPO_NAME = 'github-test-admin'
def create_github_issue(title, body=None, labels=None):
"""
Create an issue on github.com using the given parameters.
:param title: This is the title of the GitHub Issue
:param body: Optional - This is the body of the issue, or the main text
:param labels: Optional - What type of issue are we creating
:return:
"""
# Our url to create issues via POST
url = f'https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/issues'
# Create an authenticated session to create the issue
session = requests.Session()
session.auth = (USERNAME, API_KEY)
# Create the issue
issue = {'title': title,
'body': body,
'labels': labels}
# Add the issue to our repository
r = session.post(url, json.dumps(issue))
if r.status_code == 201:
print('Successfully created Issue "%s"' % title)
else:
print('Failed to create Issue "%s"' % title)
print('Response:', r.content)
if __name__ == '__main__':
with open(sys.argv[1]) as jsp:
payload = json.loads(jsp.read())
action = payload['action']
repo = payload['repository']['full_name']
if action == 'deleted':
create_github_issue('%s was deleted' % repo, 'Seems we\'ve got ourselves a bit of an issue here.\n\n@<user>',
['deleted'])
outfile = f'/tmp/webhook-{repo}.log'
with open(outfile, 'w') as f:
f.write(json.dumps(payload))