Beispiel #1
0
    def test_send_and_join(self):
        with mock.patch('socket.socket') as mocked_socket:
            with mock.patch('time.sleep') as time:
                msocket = mock.MagicMock()
                mocked_socket.return_value = msocket

                client = irc.IrcClient('#rust-bots')
                msocket.connect.assert_called_once_with(
                    ("irc.mozilla.org", 6667))
                msocket.reset_mock()

                client.send_then_quit("test")
                msocket.send.assert_has_calls([
                    mock.call("PRIVMSG #rust-bots :test\r\n"),
                    mock.call("QUIT :bot out\r\n")
                ])
Beispiel #2
0
    def set_assignee(self, assignee, owner, repo, issue, user, author,
                     to_mention):
        try:
            self.api_req("PATCH", issue_url % (owner, repo, issue),
                         {"assignee": assignee})['body']
        except urllib.error.HTTPError as e:
            if e.code == 201:
                pass
            else:
                raise e

        if assignee:
            irc_name_of_reviewer = self.get_irc_nick(assignee)
            if irc_name_of_reviewer:
                client = irc.IrcClient(target="#rust-bots")
                client.send_then_quit(
                    "{}: ping to review issue https://www.github.com/{}/{}/pull/{} by {}."
                    .format(irc_name_of_reviewer, owner, repo, issue, author))
        self.run_commands(to_mention, owner, repo, issue, user)
Beispiel #3
0
def set_assignee(assignee, owner, repo, issue, user, token, author,
                 to_mention):
    global issue_url
    try:
        result = api_req("PATCH", issue_url % (owner, repo, issue),
                         {"assignee": assignee}, user, token)['body']
    except urllib2.HTTPError, e:
        if e.code == 201:
            pass
        else:
            raise e

    if assignee:
        irc_name_of_reviewer = get_irc_nick(assignee)
        if irc_name_of_reviewer:
            client = irc.IrcClient(target="#rust-bots")
            client.send_then_quit(
                "{}: ping to review issue https://www.github.com/{}/{}/pull/{} by {}."
                .format(irc_name_of_reviewer, owner, repo, issue, author))

    if to_mention and len(to_mention) > 0:
        message = ''
        for mention in to_mention:
            if len(message) > 0:
                message += '\n\n'
            message += "%s\n\ncc %s" % (mention['message'], ','.join(
                [x for x in mention['reviewers'] if x != user]))
        post_comment(message, owner, repo, issue, user, token)


def get_collaborators(owner, repo, user, token):
Beispiel #4
0
class HighfiveHandler(object):
    def __init__(self, payload, config, config_dir=None):
        self.payload = payload

        self.integration_user = config.github_username
        self.integration_token = config.github_token

        self.config_dir = config_dir
        self.repo_config = self.load_repo_config()

    def load_repo_config(self):
        '''Load the repository configuration.'''
        (org, repo) = self.payload['repository', 'full_name'].split('/')
        try:
            return self._load_json_file(os.path.join(org, repo) + '.json')
        except IOError:
            raise UnsupportedRepoError

    def run(self, event):
        if event == "ping":
            return "Ping received! The webhook is configured correctly!\n"
        elif event == "pull_request" and self.payload["action"] == "opened":
            self.new_pr()
            return 'OK\n'
        elif event == "issue_comment" and self.payload["action"] == "created":
            self.new_comment()
            return 'OK\n'
        else:
            return 'Unsupported webhook event.\n'

    def _load_json_file(self, name):
        config_dir = self.config_dir
        if not self.config_dir:
            config_dir = os.path.join(os.path.dirname(__file__), 'configs')

        with open(os.path.join(config_dir, name)) as config:
            return json.load(config)

    def modifies_submodule(self, diff):
        return submodule_re.match(diff)

    def api_req(self, method, url, data=None, media_type=None):
        data = None if not data else json.dumps(data)
        headers = {} if not data else {'Content-Type': 'application/json'}
        req = urllib2.Request(url, data, headers)
        req.get_method = lambda: method
        if self.integration_token:
            req.add_header("Authorization", "token %s" % self.integration_token)

        if media_type:
            req.add_header("Accept", media_type)
        f = urllib2.urlopen(req)
        header = f.info()
        if header.get('Content-Encoding') == 'gzip':
            buf = StringIO(f.read())
            f = gzip.GzipFile(fileobj=buf)
        body = f.read()
        return { "header": header, "body": body }

    def set_assignee(self, assignee, owner, repo, issue, user, author, to_mention):
        try:
            self.api_req(
                "PATCH", issue_url % (owner, repo, issue),
                {"assignee": assignee}
            )['body']
        except urllib2.HTTPError, e:
            if e.code == 201:
                pass
            else:
                raise e

        if assignee:
            irc_name_of_reviewer = self.get_irc_nick(assignee)
            if irc_name_of_reviewer:
                client = irc.IrcClient(target="#rust-bots")
                client.send_then_quit("{}: ping to review issue https://www.github.com/{}/{}/pull/{} by {}."
                    .format(irc_name_of_reviewer, owner, repo, issue, author))
        self.run_commands(to_mention, owner, repo, issue, user)
Beispiel #5
0
class HighfiveHandler(object):
    def __init__(self, payload):
        self.payload = payload

        self.config = ConfigParser.RawConfigParser()
        self.config.read('./config')
        self.integration_user = self.config.get('github', 'user')
        self.integration_token = self.config.get('github', 'token')

        self.repo_config = self.load_repo_config()

    def load_repo_config(self):
        '''Load the repository configuration.'''
        (org, repo) = self.payload['repository', 'full_name'].split('/')
        try:
            return self._load_json_file(os.path.join(org, repo) + '.json')
        except IOError:
            raise UnsupportedRepoError

    def run(self):
        if self.payload["action"] == "opened":
            self.new_pr()
        elif self.payload["action"] == "created":
            self.new_comment()
        else:
            print self.payload["action"]
            sys.exit(0)

    def _load_json_file(self, name):
        configs_dir = os.path.join(os.path.dirname(__file__), 'configs')

        with open(os.path.join(configs_dir, name)) as config:
            return json.load(config)

    def modifies_submodule(self, diff):
        return submodule_re.match(diff)

    def api_req(self, method, url, data=None, media_type=None):
        data = None if not data else json.dumps(data)
        headers = {} if not data else {'Content-Type': 'application/json'}
        req = urllib2.Request(url, data, headers)
        req.get_method = lambda: method
        if self.integration_token:
            req.add_header("Authorization",
                           "token %s" % self.integration_token)

        if media_type:
            req.add_header("Accept", media_type)
        f = urllib2.urlopen(req)
        header = f.info()
        if header.get('Content-Encoding') == 'gzip':
            buf = StringIO(f.read())
            f = gzip.GzipFile(fileobj=buf)
        body = f.read()
        return {"header": header, "body": body}

    def set_assignee(self, assignee, owner, repo, issue, user, author,
                     to_mention):
        try:
            self.api_req("PATCH", issue_url % (owner, repo, issue),
                         {"assignee": assignee})['body']
        except urllib2.HTTPError, e:
            if e.code == 201:
                pass
            else:
                raise e

        if assignee:
            irc_name_of_reviewer = self.get_irc_nick(assignee)
            if irc_name_of_reviewer:
                client = irc.IrcClient(target="#rust-bots")
                client.send_then_quit(
                    "{}: ping to review issue https://www.github.com/{}/{}/pull/{} by {}."
                    .format(irc_name_of_reviewer, owner, repo, issue, author))

        if to_mention and len(to_mention) > 0:
            message = ''
            for mention in to_mention:
                if len(message) > 0:
                    message += '\n\n'
                message += "%s\n\ncc %s" % (mention['message'], ','.join(
                    [x for x in mention['reviewers'] if x != user]))
            self.post_comment(message, owner, repo, issue)