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.
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} {color_white}{repo_branch}{color_reset} {short_commit} - {repo_commit_author}: {repo_commit_message}','Build #{build_version} {color_green}passed{color_reset}. Details: {color_blue}{build_url}{color_reset} | Commit: {color_blue}{commit_url}{color_reset}'
57 See also https://github.com/gridsync/gridsync/blob/master/appveyor.yml for examples
61 - "python scripts/test/appveyor-irc-notify.py irc.oftc.net:6697 tor-ci success
63 - "python scripts/test/appveyor-irc-notify.py irc.oftc.net:6697 tor-ci failure
66 from __future__
import print_function
67 from __future__
import absolute_import
79 Return a dict of key value carfted from appveyor environment variables.
84 v
.replace('APPVEYOR_', '').lower(),
85 os
.getenv(v
, '').decode('utf-8')
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',
105 BUILD_FMT
= u
'{url}/project/{account_name}/{project_name}/build/{build_version}'
106 COMMIT_FMT
= u
'https://{repo_provider}.com/{repo_name}/commit/{repo_commit}'
111 build_url
=BUILD_FMT
.format(**vars),
112 commit_url
=COMMIT_FMT
.format(**vars),
113 short_commit
=vars["repo_commit"][:7],
125 Send IRC notification
127 apvy_vars
= appveyor_vars()
129 server
, port
= sys
.argv
[1].rsplit(":", 1)
130 channel
= sys
.argv
[2]
131 success
= sys
.argv
[3] == "success"
132 failure
= sys
.argv
[3] == "failure"
134 if success
or failure
:
136 messages
.append(u
"{repo_name} {color_white}{repo_branch}{color_reset} {short_commit} - {repo_commit_author}: {repo_commit_message}")
139 messages
.append(u
"Build #{build_version} {color_green}passed{color_reset}. Details: {color_blue}{build_url}{color_reset} Commit: {color_blue}{commit_url}{color_reset}")
141 messages
.append(u
"Build #{build_version} {color_red}failed{color_reset}. Details: {color_blue}{build_url}{color_reset} Commit: {color_blue}{commit_url}{color_reset}")
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()
161 line
= irc_file
.readline()
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':
175 print(u
'PRIVMSG #{} :{}'.format(channel
, msg
).encode("utf-8"))
176 irc_sock
.send(u
'PRIVMSG #{} :{}\r\n'.format(channel
, msg
).encode("utf-8"))
181 if __name__
== '__main__':
186 print('ERROR: Failed to send notification: \n' + traceback
.format_exc())