2 From: https://raw.githubusercontent.com/wesnoth/wesnoth/b28d8d469af047cbbb20b18757c07b1bfc6afa31/utils/appveyor/irc-notify.py
3 which is from: https://raw.githubusercontent.com/nexB/scancode-toolkit/48aeaf76ce9f53d02223c41c1b2ad1d1ad73b851/etc/scripts/irc-notify.py
5 Copyright (C) 2015-2016 Christopher R. Wood
7 This program is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software Foundation;
9 either version 2 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along with this
16 program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
17 Fifth Floor, Boston, MA 02110-1301 USA.
19 Simple AppVeyor IRC notification script.
21 Modified by nexB on October 2016:
22 - rework the handling of environment variables.
23 - made the script use functions
24 - support only Appveyor loading its environment variable to craft IRC notices.
26 Modified by Jyrki Vesterinen on November 2016:
28 - join the channel instead of sending an external message.
30 The first argument is a Freenode channel. Other arguments passed to the script will be
31 sent as notice messages content and any {var}-formatted environment variables will be
32 expanded automatically, replaced with a corresponding Appveyor environment variable
33 value. se commas to delineate multiple messages.
35 Modified by Martin Quinson on June 2018:
37 - Use OFTC instead of Freenode
40 export APPVEYOR_URL=https://ci.appveyor.com
41 export APPVEYOR_PROJECT_NAME=attributecode
42 export APPVEYOR_REPO_COMMIT_AUTHOR=pombredanne
43 export APPVEYOR_REPO_COMMIT_TIMESTAMP=2016-10-31
44 export APPVEYOR_REPO_PROVIDER=gihub
45 export APPVEYOR_REPO_BRANCH=repo_branch
46 export APPVEYOR_PULL_REQUEST_TITLE=pull_request_title
47 export APPVEYOR_BUILD_VERSION=1
48 export APPVEYOR_REPO_COMMIT=22c95b72e29248dc4de9b85e590ee18f6f587de8
49 export APPVEYOR_REPO_COMMIT_MESSAGE="some IRC test"
50 export APPVEYOR_ACCOUNT_NAME=nexB
51 export APPVEYOR_PULL_REQUEST_NUMBER=pull_request_number
52 export APPVEYOR_REPO_NAME=nexB/attributecode
53 python etc/scripts/irc-notify.py aboutcode '[{project_name}:{branch}] {short_commit}: "{message}" ({author}) {color_red}Succeeded','Details: {build_url} | Commit: {commit_url}'
55 See also https://github.com/gridsync/gridsync/blob/master/appveyor.yml for examples
59 - "python etc/scripts/irc-notify.py channel [{project_name}:{branch}] {short_commit}: \"{message}\" ({author}) {color_green}Succeeded,Details: {build_url},Commit: {commit_url}"
61 - "python etc/scripts/irc-notify.py channel [{project_name}:{branch}] {short_commit}: \"{message}\" ({author}) {color_red}Failed,Details: {build_url},Commit: {commit_url}"
74 Return a dict of key value crafted from appveyor environment variables.
76 from os import environ
78 appveyor_url = environ.get('APPVEYOR_URL')
79 message_extended = environ.get('APPVEYOR_REPO_COMMIT_MESSAGE_EXTENDED')
80 configuration_name = environ.get('CONFIGURATION')
81 branch = environ.get('APPVEYOR_REPO_BRANCH')
82 author = environ.get('APPVEYOR_REPO_COMMIT_AUTHOR')
83 author_email = environ.get('APPVEYOR_REPO_COMMIT_AUTHOR_EMAIL')
84 timestamp = environ.get('APPVEYOR_REPO_COMMIT_TIMESTAMP')
85 repo_provider = environ.get('APPVEYOR_REPO_PROVIDER')
86 project_name = environ.get('APPVEYOR_PROJECT_NAME')
87 project_slug = environ.get('APPVEYOR_PROJECT_SLUG')
88 pull_request_title = environ.get('APPVEYOR_PULL_REQUEST_TITLE')
89 build_version = environ.get('APPVEYOR_BUILD_VERSION')
90 commit = environ.get('APPVEYOR_REPO_COMMIT')
91 message = environ.get('APPVEYOR_REPO_COMMIT_MESSAGE')
92 account_name = environ.get('APPVEYOR_ACCOUNT_NAME')
93 pull_request_number = environ.get('APPVEYOR_PULL_REQUEST_NUMBER')
94 repo_name = environ.get('APPVEYOR_REPO_NAME')
96 short_commit = commit[:7]
97 build_url = '{appveyor_url}/project/{account_name}/{project_slug}/build/{build_version}'.format(**locals())
98 commit_url = 'https://{repo_provider}.com/{repo_name}/commit/{commit}'.format(**locals())
101 appveyor_url=appveyor_url,
102 account_name=account_name,
103 project_name=project_name,
104 project_slug=project_slug,
105 build_version=build_version,
109 repo_provider=repo_provider,
112 configuration_name=configuration_name,
114 author_email=author_email,
117 short_commit=short_commit,
119 message_extended=message_extended,
121 pull_request_title=pull_request_title,
122 pull_request_number=pull_request_number,
124 commit_url=commit_url,
135 if __name__ == '__main__':
136 apvy_vars = appveyor_vars()
138 channel = sys.argv[1]
139 messages = sys.argv[2:]
140 messages = ' '.join(messages)
141 messages = messages.split(',')
142 messages = [msg.format(**apvy_vars).strip() for msg in messages]
144 irc_username = 'Appveyor'
145 irc_nick = irc_username + str(random.randint(1, 9999))
148 # establish connection
149 irc_sock = ssl.wrap_socket(socket.socket(socket.AF_INET, socket.SOCK_STREAM))
150 irc_sock.connect((socket.gethostbyname('irc.oftc.net'), 6697))
151 irc_sock.send('NICK {0}\r\nUSER {0} * 0 :{0}\r\n'.format(irc_username).encode('utf_8'))
152 irc_file = irc_sock.makefile()
155 line = irc_file.readline()
157 response = line.split()
159 if response[0] == 'PING':
160 irc_file.send('PONG {}\r\n'.format(reponse[1]).encode('utf_8'))
162 elif response[1] == '433':
163 irc_sock.send('NICK {}\r\n'.format(irc_nick).encode('utf_8'))
165 elif response[1] == '001':
168 irc_sock.send('JOIN #{}\r\n'.format(channel).encode('utf_8'))
171 print('PRIVMSG #{} :{}'.format(channel, msg))
172 irc_sock.send('PRIVMSG #{} :{}\r\n'.format(channel, msg).encode('utf_8'))
175 irc_sock.send('PART #{}\r\n'.format(channel).encode('utf_8'))
177 except BaseException:
178 e = sys.exc_info()[0]