tests: this reminds my I hate ANSI colors in everything
[tor/appveyor.git] / scripts / test / appveyor-irc-notify.py
blob689823f2c7914ba264cdeeda6df5afc471e128bb
1 # Copyright (C) 2015-2016 Christopher R. Wood
2 # Copyright (c) 2018 The Tor Project
3 # Copyright (c) 2018 isis agora lovecruft
5 # From: https://raw.githubusercontent.com/gridsync/gridsync/def54f8166089b733d166665fdabcad4cdc526d8/misc/irc-notify.py
6 # and: https://github.com/gridsync/gridsync
8 # Modified by nexB on October 2016:
9 # - rework the handling of environment variables.
10 # - made the script use functions
11 # - support only Appveyor loading its environment variable to craft IRC notices.
13 # Modified by isis agora lovecruft <isis@torproject.org> in 2018:
14 # - Make IRC server configurable.
15 # - Make bot IRC nick deterministic.
16 # - Make bot join the channel rather than sending NOTICE messages externally.
17 # - Fix a bug which always caused sys.exit() to be logged as a traceback.
18 # - Actually reset the IRC colour codes after printing.
20 # This program is free software; you can redistribute it and/or modify it under the
21 # terms of the GNU General Public License as published by the Free Software Foundation;
22 # either version 2 of the License, or (at your option) any later version.
24 # This program is distributed in the hope that it will be useful, but WITHOUT ANY
25 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
26 # PARTICULAR PURPOSE. See the GNU General Public License for more details.
28 # You should have received a copy of the GNU General Public License along with this
29 # program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
30 # Fifth Floor, Boston, MA 02110-1301 USA.
32 """Simple AppVeyor IRC notification script.
34 The first argument is an IRC server and port; the second is the channel. Other
35 arguments passed to the script will be sent as notice messages content and any
36 {var}-formatted environment variables will be expanded automatically, replaced
37 with a corresponding Appveyor environment variable value. Use commas to
38 delineate multiple messages.
41 Example:
42 export APPVEYOR_URL=https://ci.appveyor.com
43 export APPVEYOR_PROJECT_NAME=tor
44 export APPVEYOR_REPO_COMMIT_AUTHOR=isislovecruft
45 export APPVEYOR_REPO_COMMIT_TIMESTAMP=2018-04-23
46 export APPVEYOR_REPO_PROVIDER=gihub
47 export APPVEYOR_REPO_BRANCH=repo_branch
48 export APPVEYOR_PULL_REQUEST_TITLE=pull_request_title
49 export APPVEYOR_BUILD_VERSION=1
50 export APPVEYOR_REPO_COMMIT=22c95b72e29248dc4de9b85e590ee18f6f587de8
51 export APPVEYOR_REPO_COMMIT_MESSAGE="some IRC test"
52 export APPVEYOR_ACCOUNT_NAME=isislovecruft
53 export APPVEYOR_PULL_REQUEST_NUMBER=pull_request_number
54 export APPVEYOR_REPO_NAME=isislovecruft/tor
55 python ./appveyor-irc-notify.py irc.oftc.net:6697 tor-ci '{repo_name} {repo_branch} {short_commit} - {repo_commit_author}: {repo_commit_message}','Build #{build_version} passed. Details: {build_url} | Commit: {commit_url}
57 See also https://github.com/gridsync/gridsync/blob/master/appveyor.yml for examples
58 in Appveyor's YAML:
60 on_success:
61 - "python scripts/test/appveyor-irc-notify.py irc.oftc.net:6697 tor-ci success
62 on_failure:
63 - "python scripts/test/appveyor-irc-notify.py irc.oftc.net:6697 tor-ci failure
64 """
66 from __future__ import print_function
67 from __future__ import absolute_import
69 import os
70 import random
71 import socket
72 import ssl
73 import sys
74 import time
77 def appveyor_vars():
78 """
79 Return a dict of key value carfted from appveyor environment variables.
80 """
82 vars = dict([
84 v.replace('APPVEYOR_', '').lower(),
85 os.getenv(v, '').decode('utf-8')
86 ) for v in [
87 'APPVEYOR_URL',
88 'APPVEYOR_REPO_COMMIT_MESSAGE_EXTENDED',
89 'APPVEYOR_REPO_BRANCH',
90 'APPVEYOR_REPO_COMMIT_AUTHOR',
91 'APPVEYOR_REPO_COMMIT_AUTHOR_EMAIL',
92 'APPVEYOR_REPO_COMMIT_TIMESTAMP',
93 'APPVEYOR_REPO_PROVIDER',
94 'APPVEYOR_PROJECT_NAME',
95 'APPVEYOR_PULL_REQUEST_TITLE',
96 'APPVEYOR_BUILD_VERSION',
97 'APPVEYOR_REPO_COMMIT',
98 'APPVEYOR_REPO_COMMIT_MESSAGE',
99 'APPVEYOR_ACCOUNT_NAME',
100 'APPVEYOR_PULL_REQUEST_NUMBER',
101 'APPVEYOR_REPO_NAME'
105 BUILD_FMT = u'{url}/project/{account_name}/{project_name}/build/{build_version}'
107 if vars["repo_provider"] == 'github':
108 COMMIT_FMT = u'https://{repo_provider}.com/{repo_name}/commit/{repo_commit}'
109 vars.update(commit_url=COMMIT_FMT.format(**vars))
111 vars.update(
112 build_url=BUILD_FMT.format(**vars),
113 short_commit=vars["repo_commit"][:7],
115 return vars
118 def notify():
120 Send IRC notification
122 apvy_vars = appveyor_vars()
124 server, port = sys.argv[1].rsplit(":", 1)
125 channel = sys.argv[2]
126 success = sys.argv[3] == "success"
127 failure = sys.argv[3] == "failure"
129 if success or failure:
130 messages = []
131 messages.append(u"{repo_name} {repo_branch} {short_commit} - {repo_commit_author}: {repo_commit_message}")
133 if success:
134 m = u"Build #{build_version} passed. Details: {build_url}"
135 if failure:
136 m = u"Build #{build_version} failed. Details: {build_url}"
138 if "commit_url" in apvy_vars:
139 m += " Commit: {commit_url}"
141 messages.append(m)
142 else:
143 messages = sys.argv[3:]
144 messages = ' '.join(messages)
145 messages = messages.decode("utf-8").split(',')
147 print(repr(apvy_vars))
148 messages = [msg.format(**apvy_vars).strip() for msg in messages]
150 irc_username = 'appveyor-ci'
151 irc_nick = irc_username
153 # establish connection
154 irc_sock = ssl.wrap_socket(socket.socket(socket.AF_INET, socket.SOCK_STREAM))
155 irc_sock.connect((socket.gethostbyname(server), int(port)))
156 irc_sock.send('NICK {0}\r\nUSER {0} * 0 :{0}\r\n'.format(irc_username).encode())
157 irc_sock.send('JOIN #{0}\r\n'.format(channel).encode())
158 irc_file = irc_sock.makefile()
160 while irc_file:
161 line = irc_file.readline()
162 print(line.rstrip())
163 response = line.split()
165 if response[0] == 'PING':
166 irc_file.send('PONG {}\r\n'.format(reponse[1]).encode())
168 elif response[1] == '433':
169 irc_sock.send('NICK {}\r\n'.format(irc_nick).encode())
171 elif response[1] == '001':
172 time.sleep(5)
173 # send notification
174 for msg in messages:
175 print(u'PRIVMSG #{} :{}'.format(channel, msg).encode("utf-8"))
176 irc_sock.send(u'PRIVMSG #{} :{}\r\n'.format(channel, msg).encode("utf-8"))
177 time.sleep(5)
178 return
181 if __name__ == '__main__':
182 try:
183 notify()
184 except:
185 import traceback
186 print('ERROR: Failed to send notification: \n' + traceback.format_exc())