Remove check-tor script
[tor.git] / scripts / test / appveyor-irc-notify.py
blobcfe0afe7ae233ab71bab24045b9b0ee47679be4a
1 # coding=utf8
2 # Copyright (C) 2015-2016 Christopher R. Wood
3 # Copyright (c) 2018 The Tor Project
4 # Copyright (c) 2018 isis agora lovecruft
6 # From: https://raw.githubusercontent.com/gridsync/gridsync/def54f8166089b733d166665fdabcad4cdc526d8/misc/irc-notify.py
7 # and: https://github.com/gridsync/gridsync
9 # Modified by nexB on October 2016:
10 # - rework the handling of environment variables.
11 # - made the script use functions
12 # - support only Appveyor loading its environment variable to craft IRC notices.
14 # Modified by isis agora lovecruft <isis@torproject.org> in 2018:
15 # - Make IRC server configurable.
16 # - Make bot IRC nick deterministic.
17 # - Make bot join the channel rather than sending NOTICE messages externally.
18 # - Fix a bug which always caused sys.exit() to be logged as a traceback.
19 # - Actually reset the IRC colour codes after printing.
21 # Modified by Marcin Cieślak in 2018:
22 # - Accept UTF-8
23 # - only guess github URLs
24 # - stop using ANSI colors
26 # Modified by teor in 2018:
27 # - fix github provider detection ('gitHub' or 'gitHubEnterprise', apparently)
28 # - make short commits 10 hexdigits long (that's what git does for tor)
29 # - generate correct branches and URLs for pull requests and tags
30 # - switch to one URL per line
32 # This program is free software; you can redistribute it and/or modify it under the
33 # terms of the GNU General Public License as published by the Free Software Foundation;
34 # either version 2 of the License, or (at your option) any later version.
36 # This program is distributed in the hope that it will be useful, but WITHOUT ANY
37 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
38 # PARTICULAR PURPOSE. See the GNU General Public License for more details.
40 # You should have received a copy of the GNU General Public License along with this
41 # program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
42 # Fifth Floor, Boston, MA 02110-1301 USA.
44 """Simple AppVeyor IRC notification script.
46 The first argument is an IRC server and port; the second is the channel. Other
47 arguments passed to the script will be sent as notice messages content and any
48 {var}-formatted environment variables will be expanded automatically, replaced
49 with a corresponding Appveyor environment variable value. Use commas to
50 delineate multiple messages.
53 Example:
54 export APPVEYOR_ACCOUNT_NAME=isislovecruft
55 export APPVEYOR_BUILD_VERSION=1
56 export APPVEYOR_PROJECT_NAME=tor
57 export APPVEYOR_PULL_REQUEST_NUMBER=pull_request_number
58 export APPVEYOR_PULL_REQUEST_TITLE=pull_request_title
59 export APPVEYOR_REPO_BRANCH=repo_branch
60 export APPVEYOR_REPO_COMMIT=22c95b72e29248dc4de9b85e590ee18f6f587de8
61 export APPVEYOR_REPO_COMMIT_AUTHOR=isislovecruft
62 export APPVEYOR_REPO_COMMIT_MESSAGE="some IRC test"
63 export APPVEYOR_REPO_COMMIT_TIMESTAMP=2018-04-23
64 export APPVEYOR_REPO_NAME=isislovecruft/tor
65 export APPVEYOR_REPO_PROVIDER=github
66 export APPVEYOR_URL=https://ci.appveyor.com
67 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}
69 See also https://github.com/gridsync/gridsync/blob/master/appveyor.yml for examples
70 in Appveyor's YAML:
72 on_success:
73 - "python scripts/test/appveyor-irc-notify.py irc.oftc.net:6697 tor-ci success
74 on_failure:
75 - "python scripts/test/appveyor-irc-notify.py irc.oftc.net:6697 tor-ci failure
76 """
78 from __future__ import print_function
79 from __future__ import absolute_import
81 import os
82 import random
83 import socket
84 import ssl
85 import sys
86 import time
89 def appveyor_vars():
90 """
91 Return a dict of key value crafted from appveyor environment variables.
92 """
94 vars = dict([
96 v.replace('APPVEYOR_', '').lower(),
97 os.getenv(v, '').decode('utf-8')
98 ) for v in [
99 'APPVEYOR_ACCOUNT_NAME',
100 'APPVEYOR_BUILD_VERSION',
101 'APPVEYOR_PROJECT_NAME',
102 'APPVEYOR_PULL_REQUEST_HEAD_COMMIT',
103 'APPVEYOR_PULL_REQUEST_HEAD_REPO_BRANCH',
104 'APPVEYOR_PULL_REQUEST_HEAD_REPO_NAME',
105 'APPVEYOR_PULL_REQUEST_NUMBER',
106 'APPVEYOR_PULL_REQUEST_TITLE',
107 'APPVEYOR_REPO_BRANCH',
108 'APPVEYOR_REPO_COMMIT',
109 'APPVEYOR_REPO_COMMIT_AUTHOR',
110 'APPVEYOR_REPO_COMMIT_AUTHOR_EMAIL',
111 'APPVEYOR_REPO_COMMIT_MESSAGE',
112 'APPVEYOR_REPO_COMMIT_MESSAGE_EXTENDED',
113 'APPVEYOR_REPO_COMMIT_TIMESTAMP',
114 'APPVEYOR_REPO_NAME',
115 'APPVEYOR_REPO_PROVIDER',
116 'APPVEYOR_REPO_TAG_NAME',
117 'APPVEYOR_URL',
121 BUILD_FMT = u'{url}/project/{account_name}/{project_name}/build/{build_version}'
123 if vars["repo_tag_name"]:
124 BRANCH_FMT = u'{repo_name} {repo_tag_name} {short_commit}'
125 else:
126 BRANCH_FMT = u'{repo_name} {repo_branch} {short_commit}'
128 vars.update(head_commit=vars["repo_commit"])
130 if vars["repo_provider"].lower().startswith('github'):
131 COMMIT_FMT = u'https://github.com/{repo_name}/commit/{repo_commit}'
132 if vars["pull_request_number"]:
133 vars.update(head_commit=vars["pull_request_head_commit"])
134 BRANCH_FMT = u'{repo_name} {repo_branch} pull {pull_request_head_repo_name} {pull_request_head_repo_branch} {short_commit}'
135 COMMIT_FMT = u'https://github.com/{pull_request_head_repo_name}/commit/{pull_request_head_commit}'
136 PULL_FMT = u'https://github.com/{repo_name}/pull/{pull_request_number}'
137 vars.update(pull_url=PULL_FMT.format(**vars))
138 vars.update(commit_url=COMMIT_FMT.format(**vars))
140 vars.update(short_commit=vars["head_commit"][:10])
142 vars.update(
143 build_url=BUILD_FMT.format(**vars),
144 branch_detail=BRANCH_FMT.format(**vars),
146 return vars
149 def notify():
151 Send IRC notification
153 apvy_vars = appveyor_vars()
155 server, port = sys.argv[1].rsplit(":", 1)
156 channel = sys.argv[2]
157 success = sys.argv[3] == "success"
158 failure = sys.argv[3] == "failure"
160 if success or failure:
161 messages = []
162 messages.append(u"{branch_detail} - {repo_commit_author}: {repo_commit_message}")
164 if success:
165 messages.append(u"Build #{build_version} passed. Details: {build_url}")
166 if failure:
167 messages.append(u"Build #{build_version} failed. Details: {build_url}")
169 if "commit_url" in apvy_vars:
170 messages.append(u"Commit: {commit_url}")
172 if "pull_url" in apvy_vars:
173 messages.append(u"Pull: {pull_url}")
175 else:
176 messages = sys.argv[3:]
177 messages = ' '.join(messages)
178 messages = messages.decode("utf-8").split(',')
180 print(repr(apvy_vars))
181 messages = [msg.format(**apvy_vars).strip() for msg in messages]
183 irc_username = 'appveyor-ci'
184 irc_nick = irc_username
186 # establish connection
187 irc_sock = ssl.wrap_socket(socket.socket(socket.AF_INET, socket.SOCK_STREAM))
188 irc_sock.connect((socket.gethostbyname(server), int(port)))
189 irc_sock.send('NICK {0}\r\nUSER {0} * 0 :{0}\r\n'.format(irc_username).encode())
190 irc_sock.send('JOIN #{0}\r\n'.format(channel).encode())
191 irc_file = irc_sock.makefile()
193 while irc_file:
194 line = irc_file.readline()
195 print(line.rstrip())
196 response = line.split()
198 if response[0] == 'PING':
199 irc_file.send('PONG {}\r\n'.format(response[1]).encode())
201 elif response[1] == '433':
202 irc_sock.send('NICK {}\r\n'.format(irc_nick).encode())
204 elif response[1] == '001':
205 time.sleep(5)
206 # send notification
207 for msg in messages:
208 print(u'PRIVMSG #{} :{}'.format(channel, msg).encode("utf-8"))
209 irc_sock.send(u'PRIVMSG #{} :{}\r\n'.format(channel, msg).encode("utf-8"))
210 time.sleep(5)
211 return
214 if __name__ == '__main__':
215 try:
216 notify()
217 except:
218 import traceback
219 print('ERROR: Failed to send notification: \n' + traceback.format_exc())