-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_creator.py
120 lines (107 loc) · 6.57 KB
/
message_creator.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import html
def escape(text):
return html.escape(text)
class MessageCreator:
def gitlab(self, webhook_request):
kind = webhook_request.json["object_kind"]
if kind == "push":
project = escape(webhook_request.json["project"]["name"])
ref = escape(webhook_request.json["ref"].split('/')[-1])
user_name = escape(webhook_request.json["user_name"])
message = "📢 New <b>{}</b> on <i>{} ({})</i> by {} 📢 \n".format(kind, project, ref, user_name)
commits = webhook_request.json["commits"]
for c in commits:
message += "〰〰〰〰〰\n🔔 "+ escape(c["message"]) + " <i>({})</i>\n".format(escape(c["author"]["name"]))
elif kind == "tag_push":
project = escape(webhook_request.json["project"]["name"])
ref = escape(webhook_request.json["ref"].split('/')[-1])
user_name = escape(webhook_request.json["user_name"])
message = "📢 New <b>{}</b> on <i>{}</i>({}) by {} 📢 \n".format(kind, project, ref, user_name)
elif kind == "issue":
project = escape(webhook_request.json["project"]["name"])
user_name = escape(webhook_request.json["user"]["user_name"])
message = "📢 New <b>{}</b> on <i>{}</i> by {} 📢 \n".format(kind, project, user_name)
message += escape(webhook_request.json["object_attributes"]["title"])
elif kind == "pipeline":
project = escape(webhook_request.json["project"]["name"])
user_name = escape(webhook_request.json["commit"]["author"]["name"])
url = escape(webhook_request.json["commit"]["url"])
ref = escape(webhook_request.json["object_attributes"]["ref"].split('/')[-1])
message = '📢 New <b>pipeline</b> event for <a href="{}">push</a> on <i>{} ({})</i> by {} 📢 \n'.format(url, project, ref, user_name)
builds = webhook_request.json["builds"]
for b in builds:
status = b['status']
detailed_status = status
if status == "success":
detailed_status = "success 🎉🥳"
elif status == "created":
detailed_status = "created 🏗"
elif status == "skipped":
detailed_status = "skipped 🖐"
elif status == "failed":
detailed_status = "failed 🧟👊🏼"
elif status == "pending":
detailed_status = "pending 🕓"
return False, "" # send no message that contains "pending" state
elif status == "running":
detailed_status = "running 🛵"
return False, "" # send no message that contains "running" state
elif status == "canceled":
detailed_status = "canceled 🚫"
else:
detailed_status = escape(detailed_status)
message += "➖ <b>{}</b> ({}): {}\n".format(b["name"], b["stage"], detailed_status)
else:
return False, ""
return True, message
def github(self, webhook_request):
kind = webhook_request.headers['X-GitHub-Event']
project = webhook_request.json["repository"]["name"]
if kind == "push":
ref = webhook_request.json["ref"].split('/')[-1]
user_name = webhook_request.json["pusher"]['name']
message = "📢 New <b>{}</b> on <i>{} ({})</i> by {} 📢 \n".format("Push", project, ref, user_name)
commits = webhook_request.json["commits"]
for c in commits:
message += "〰〰〰〰〰\n🔔 "+ c["message"] + " <i>({})</i>\n".format(c["author"]["name"])
elif kind == "create":
ref = webhook_request.json["ref"]
user_name = webhook_request.json["sender"]['login']
message = "📢 New <b>{}</b> on <i>{}</i>({}) by {} 📢 \n".format(webhook_request.json['ref_type'], project, ref, user_name)
else:
return False, ""
return True, message
def new_gitlab(self, url, secret):
return "Set this url in your gitlab webhook setting:\n" +\
"URL: <code>{}</code>\nSecret Token: <code>{}</code>\n".format(url, secret) +\
'Send /help_gitlab for more info.'
def new_github(self, url, secret):
return "Set this url in your github webhook setting:\n" +\
"URL: <code>{}</code>\nSecret Token: <code>{}</code>\n".format(url, secret) +\
'Send /help_github for more info.'
def migrate_from_heroku_notification(self, github_url, gitlab_url, secret):
return \
"⚠️ <b>Action Required</b>\n\n" +\
"To continue using GittyBot, you need to <b>update both the Webhook URL and the Secret Token</b> in your GitHub/GitLab project settings.\n\n" +\
"Click on /help_github or /help_gitlab, if you need help finding the webhook settings.\n\n" +\
"New GitHub Webhook URL: <code>{}</code>\n\n".format(github_url) +\
"New GitLab Webhook URL: <code>{}</code>\n\n".format(gitlab_url) +\
"New Secret Token: <code>{}</code>\n\n".format(secret) +\
"〰〰〰〰〰\n" +\
"More info:\n" +\
"GittyBot is up for more than 6 years, using the free plan of the Heroku cloud platform. But unfortunately, starting November 28th, 2022, free Heroku services will no longer be available. So, to continue the bot's service, I moved it to another cloud. Therefore, the old URLs need to be changed.\n" +\
"By the way, I recently made this bot open-source. The code is available at https://github.com/mohsenasm/gittybot. Any star ⭐️ is appreciated!"
def help_gitlab(self):
return "1. Go to *your project* on the GitLab website\n" +\
"2. Click on *setting*⚙ icon\n" +\
"3. Click on *integrations*\n" +\
"4. Enter *URL*, *Secret Token* and, check *Enable SSL verification*\n" +\
"5. Modify *Trigger Check List* and click on *Add Webhook*"
def help_github(self):
return "1. Go to *your project* on the GitHub website\n" +\
"2. Click on *⚙ Settings*\n" +\
"3. Choose *Webhooks* from left menu\n" +\
"4. Click on *Add Webhook* button\n" +\
"5. Enter *URL*, *Secret Token* and, choose *application/json* for the Content type field\n" +\
"6. Choose *Send me everything.*\n" +\
"7. Check *Active* and click on *Add Webhook* button"