tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

appveyor-irc-notify.py (8344B)


      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
      5 #
      6 # From: https://raw.githubusercontent.com/gridsync/gridsync/def54f8166089b733d166665fdabcad4cdc526d8/misc/irc-notify.py
      7 # and: https://github.com/gridsync/gridsync
      8 #
      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.
     13 #
     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.
     20 #
     21 # Modified by Marcin Cieślak in 2018:
     22 #  - Accept UTF-8
     23 #  - only guess github URLs
     24 #  - stop using ANSI colors
     25 #
     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
     31 
     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.
     35 #
     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.
     39 #
     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.
     43 
     44 """Simple AppVeyor IRC notification script.
     45 
     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.
     51 
     52 
     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}
     68 
     69 See also https://github.com/gridsync/gridsync/blob/master/appveyor.yml for examples
     70 in Appveyor's YAML:
     71 
     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 """
     77 
     78 # Future imports for Python 2.7, mandatory in 3.0
     79 from __future__ import division
     80 from __future__ import print_function
     81 from __future__ import unicode_literals
     82 
     83 import os
     84 import random
     85 import socket
     86 import ssl
     87 import sys
     88 import time
     89 
     90 
     91 def appveyor_vars():
     92    """
     93    Return a dict of key value crafted from appveyor environment variables.
     94    """
     95 
     96    vars = dict([
     97            (
     98                v.replace('APPVEYOR_', '').lower(),
     99                os.getenv(v, '').decode('utf-8')
    100            ) for v in [
    101                'APPVEYOR_ACCOUNT_NAME',
    102                'APPVEYOR_BUILD_VERSION',
    103                'APPVEYOR_PROJECT_NAME',
    104                'APPVEYOR_PULL_REQUEST_HEAD_COMMIT',
    105                'APPVEYOR_PULL_REQUEST_HEAD_REPO_BRANCH',
    106                'APPVEYOR_PULL_REQUEST_HEAD_REPO_NAME',
    107                'APPVEYOR_PULL_REQUEST_NUMBER',
    108                'APPVEYOR_PULL_REQUEST_TITLE',
    109                'APPVEYOR_REPO_BRANCH',
    110                'APPVEYOR_REPO_COMMIT',
    111                'APPVEYOR_REPO_COMMIT_AUTHOR',
    112                'APPVEYOR_REPO_COMMIT_AUTHOR_EMAIL',
    113                'APPVEYOR_REPO_COMMIT_MESSAGE',
    114                'APPVEYOR_REPO_COMMIT_MESSAGE_EXTENDED',
    115                'APPVEYOR_REPO_COMMIT_TIMESTAMP',
    116                'APPVEYOR_REPO_NAME',
    117                'APPVEYOR_REPO_PROVIDER',
    118                'APPVEYOR_REPO_TAG_NAME',
    119                'APPVEYOR_URL',
    120            ]
    121    ])
    122 
    123    BUILD_FMT = u'{url}/project/{account_name}/{project_name}/build/{build_version}'
    124 
    125    if vars["repo_tag_name"]:
    126        BRANCH_FMT = u'{repo_name} {repo_tag_name} {short_commit}'
    127    else:
    128        BRANCH_FMT = u'{repo_name} {repo_branch} {short_commit}'
    129 
    130    vars.update(head_commit=vars["repo_commit"])
    131 
    132    if vars["repo_provider"].lower().startswith('github'):
    133        COMMIT_FMT = u'https://github.com/{repo_name}/commit/{repo_commit}'
    134        if vars["pull_request_number"]:
    135            vars.update(head_commit=vars["pull_request_head_commit"])
    136            BRANCH_FMT = u'{repo_name} {repo_branch} pull {pull_request_head_repo_name} {pull_request_head_repo_branch} {short_commit}'
    137            COMMIT_FMT = u'https://github.com/{pull_request_head_repo_name}/commit/{pull_request_head_commit}'
    138            PULL_FMT = u'https://github.com/{repo_name}/pull/{pull_request_number}'
    139            vars.update(pull_url=PULL_FMT.format(**vars))
    140        vars.update(commit_url=COMMIT_FMT.format(**vars))
    141 
    142    vars.update(short_commit=vars["head_commit"][:10])
    143 
    144    vars.update(
    145        build_url=BUILD_FMT.format(**vars),
    146        branch_detail=BRANCH_FMT.format(**vars),
    147    )
    148    return vars
    149 
    150 
    151 def notify():
    152    """
    153    Send IRC notification
    154    """
    155    apvy_vars = appveyor_vars()
    156 
    157    server, port = sys.argv[1].rsplit(":", 1)
    158    channel = sys.argv[2]
    159    success = sys.argv[3] == "success"
    160    failure = sys.argv[3] == "failure"
    161 
    162    if success or failure:
    163        messages = []
    164        messages.append(u"{branch_detail} - {repo_commit_author}: {repo_commit_message}")
    165 
    166        if success:
    167            messages.append(u"Build #{build_version} passed. Details: {build_url}")
    168        if failure:
    169            messages.append(u"Build #{build_version} failed. Details: {build_url}")
    170 
    171        if "commit_url" in apvy_vars:
    172            messages.append(u"Commit: {commit_url}")
    173 
    174        if "pull_url" in apvy_vars:
    175            messages.append(u"Pull: {pull_url}")
    176 
    177    else:
    178        messages = sys.argv[3:]
    179        messages = ' '.join(messages)
    180        messages = messages.decode("utf-8").split(',')
    181 
    182    print(repr(apvy_vars))
    183    messages = [msg.format(**apvy_vars).strip() for msg in messages]
    184 
    185    irc_username = 'appveyor-ci'
    186    irc_nick = irc_username
    187 
    188    # establish connection
    189    irc_sock = ssl.wrap_socket(socket.socket(socket.AF_INET, socket.SOCK_STREAM))
    190    irc_sock.connect((socket.gethostbyname(server), int(port)))
    191    irc_sock.send('NICK {0}\r\nUSER {0} * 0 :{0}\r\n'.format(irc_username).encode())
    192    irc_sock.send('JOIN #{0}\r\n'.format(channel).encode())
    193    irc_file = irc_sock.makefile()
    194 
    195    while irc_file:
    196        line = irc_file.readline()
    197        print(line.rstrip())
    198        response = line.split()
    199 
    200        if response[0] == 'PING':
    201            irc_file.send('PONG {}\r\n'.format(response[1]).encode())
    202 
    203        elif response[1] == '433':
    204            irc_sock.send('NICK {}\r\n'.format(irc_nick).encode())
    205 
    206        elif response[1] == '001':
    207            time.sleep(5)
    208            # send notification
    209            for msg in messages:
    210                print(u'PRIVMSG #{} :{}'.format(channel, msg).encode("utf-8"))
    211                irc_sock.send(u'PRIVMSG #{} :{}\r\n'.format(channel, msg).encode("utf-8"))
    212            time.sleep(5)
    213            return
    214 
    215 
    216 if __name__ == '__main__':
    217    try:
    218        notify()
    219    except:
    220        import traceback
    221        print('ERROR: Failed to send notification: \n' + traceback.format_exc())