-
Notifications
You must be signed in to change notification settings - Fork 7
/
send_message.py
executable file
·130 lines (106 loc) · 4.24 KB
/
send_message.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
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python
'''
This is the Python Code for a drone.io plugin to send messages using Cisco Spark
'''
import drone
import requests
import os
spark_urls = {
"messages": "https://api.ciscospark.com/v1/messages",
"rooms": "https://api.ciscospark.com/v1/rooms",
"people": "https://api.ciscospark.com/v1/people"
}
spark_headers = {}
spark_headers["Content-type"] = "application/json"
def get_roomId(payload):
'''
Determine the roomId to send the message to.
'''
# If an explict roomId was provided as a varg, verify it's a valid roomId
if ("roomId" in payload["vargs"].keys()):
if verify_roomId(payload["vargs"]["roomId"]):
return payload["vargs"]["roomId"]
# If a roomName is provided, send to room with that title
elif ("roomName" in payload["vargs"].keys()):
# Try to find room based on room name
response = requests.get(
spark_urls["rooms"],
headers = spark_headers
)
rooms = response.json()["items"]
#print("Number Rooms: " + str(len(rooms)))
for room in rooms:
#print("Room: " + room["title"])
if payload["vargs"]["roomName"] == room["title"]:
return room["id"]
# If no valid roomId could be found in the payload, raise error
raise(LookupError("roomId can't be determined"))
def verify_roomId(roomId):
'''
Check if the roomId provided is valid
'''
url = "%s/%s" % (spark_urls["rooms"], roomId)
response = requests.get(
url,
headers = spark_headers
)
if response.status_code == 200:
return True
else:
return False
def standard_message(payload):
'''
This will create a standard notification message.
'''
status = payload["build"]["status"]
if status == "success":
message = "##Build for %s is Successful \n" % (payload["repo"]["full_name"])
message = message + "**Build author:** [%s](%s) \n" % (payload["build"]["author"], payload["build"]["author_email"])
else:
message = "#Build for %s FAILED!!! \n" % (payload["repo"]["full_name"])
message = message + "**Drone blames build author:** [%s](%s) \n" % (payload["build"]["author"], payload["build"]["author_email"])
message = message + "###Build Details \n"
message = message + "* [Build Log](%s/%s/%s)\n" % (payload["system"]["link_url"], payload["repo"]["full_name"], payload["build"]["number"])
message = message + "* [Commit Log](%s)\n" % (payload["build"]["link_url"])
message = message + "* **Branch:** %s\n" % (payload["build"]["branch"])
message = message + "* **Event:** %s\n" % (payload["build"]["event"])
message = message + "* **Commit Message:** %s\n" % (payload["build"]["message"])
return message
def send_message(message_data, message_text):
message_data["markdown"] = message_text
response = requests.post(
spark_urls["messages"],
headers = spark_headers,
json = message_data
)
return response
def main():
payload = drone.plugin.get_input()
vargs = payload["vargs"]
# Prepare headers and message objects
spark_headers["Authorization"] = "Bearer %s" % (vargs["auth_token"])
spark_message = {}
# Determine destination for message
try:
# First look for a valid roomId or roomName
roomId = get_roomId(payload)
spark_message["roomId"] = roomId
except LookupError:
# See if a personEmail was provided
if "personEmail" in vargs.keys():
spark_message["toPersonEmail"] = vargs["personEmail"]
else:
raise(LookupError("Requires valid roomId, roomName, or personEmail to be provided. "))
# Send Standard message
standard_notify = send_message(spark_message, standard_message(payload))
if standard_notify.status_code != 200:
print(standard_notify.json()["message"])
raise(SystemExit("Something went wrong..."))
# If there was a message sent from .drone.yml
if "message" in vargs.keys():
custom_notify = send_message(spark_message, vargs["message"])
if custom_notify.status_code != 200:
print(custom_notify.json()["message"])
raise (SystemExit("Something went wrong..."))
if __name__ == "__main__":
main()