def test_bot_unknown_command(self):
        github_token = self.oauth_token
        repo = self.g.get_repo("lmazuel/TestingRepo")
        issue = repo.get_issue(19)

        class BotCommand:
            pass

        bot = BotHandler(BotCommand(), "AutorestCI", github_token)

        fake_webhook = {
            'action': 'opened',  # What is the comment state?
            'repository': {
                'full_name':
                issue.repository.full_name  # On what repo is this command?
            },
            'issue': {
                'number': issue.number,  # On what issue is this comment?
                'body': "@AutorestCI command1 myparameter"  # Message?
            },
            'sender': {
                'login': "******"  # Who wrote the command?
            },
        }

        response = bot.issues(fake_webhook)
        assert "I didn't understand your command" in response['message']
        assert "command1 myparameter" in response['message']

        help_comment = list(issue.get_comments())[-1]
        assert "I didn't understand your command" in help_comment.body
        assert "command1 myparameter" in help_comment.body

        # Clean
        help_comment.delete()
    def test_bot_unknown_command(self):
        github_token = self.oauth_token
        repo = self.g.get_repo("lmazuel/TestingRepo")
        issue = repo.get_issue(19)

        class BotCommand:
            pass

        bot = BotHandler(BotCommand(), "AutorestCI", github_token)

        fake_webhook = {
            "action": "opened",  # What is the comment state?
            "repository": {
                "full_name": issue.repository.full_name
            },  # On what repo is this command?
            "issue": {
                "number": issue.number,  # On what issue is this comment?
                "body": "@AutorestCI command1 myparameter",  # Message?
            },
            "sender": {
                "login": "******"
            },  # Who wrote the command?
        }

        response = bot.issues(fake_webhook)
        assert "I didn't understand your command" in response["message"]
        assert "command1 myparameter" in response["message"]

        help_comment = list(issue.get_comments())[-1]
        assert "I didn't understand your command" in help_comment.body
        assert "command1 myparameter" in help_comment.body

        # Clean
        help_comment.delete()
    def test_bot_basic_command(self):
        github_token = self.oauth_token
        repo = self.g.get_repo("lmazuel/TestingRepo")
        issue = repo.get_issue(17)

        class BotCommand:
            @order
            def command1(self, issue, param1):
                assert issue.number == 17
                return "I did something with " + param1

        bot = BotHandler(BotCommand(), "AutorestCI", github_token)

        fake_webhook = {
            "action": "opened",  # What is the comment state?
            "repository": {
                "full_name": issue.repository.full_name
            },  # On what repo is this command?
            "issue": {
                "number": issue.number,  # On what issue is this comment?
                "body": "@AutorestCI command1 myparameter",  # Message?
            },
            "sender": {
                "login": "******"
            },  # Who wrote the command?
        }

        response = bot.issues(fake_webhook)
        assert response["message"] == "I did something with myparameter"

        help_comment = list(issue.get_comments())[-1]
        assert "I did something with myparameter" in help_comment.body

        # Clean
        help_comment.delete()
    def test_bot_help(self):
        github_token = self.oauth_token
        repo = self.g.get_repo("lmazuel/TestingRepo")
        issue = repo.get_issue(16)

        class BotHelp:
            @order
            def command1(self, issue):
                pass

            @order
            def command2(self, issue):
                pass

            def notacommand(self):
                pass

        bot = BotHandler(BotHelp(), "AutorestCI", github_token)

        fake_webhook = {
            'action': 'opened',  # What is the comment state?
            'repository': {
                'full_name':
                issue.repository.full_name  # On what repo is this command?
            },
            'issue': {
                'number': issue.number,  # On what issue is this comment?
                'body': "@AutorestCI help"  # Message?
            },
            'sender': {
                'login': "******"  # Who wrote the command?
            },
        }

        response = bot.issues(fake_webhook)
        assert "this help message" in response["message"]
        assert "command1" in response["message"]
        assert "notacommand" not in response["message"]

        help_comment = list(issue.get_comments())[-1]
        assert "this help message" in help_comment.body
        assert "command1" in help_comment.body
        assert "notacommand" not in help_comment.body

        # Clean
        help_comment.delete()
    def test_bot_help(self):
        github_token = self.oauth_token
        repo = self.g.get_repo("lmazuel/TestingRepo")
        issue = repo.get_issue(16)

        class BotHelp:
            @order
            def command1(self, issue):
                pass

            @order
            def command2(self, issue):
                pass

            def notacommand(self):
                pass

        bot = BotHandler(BotHelp(), "AutorestCI", github_token)

        fake_webhook = {
            "action": "opened",  # What is the comment state?
            "repository": {
                "full_name": issue.repository.full_name
            },  # On what repo is this command?
            "issue": {
                "number": issue.number,
                "body": "@AutorestCI help"
            },  # On what issue is this comment?  # Message?
            "sender": {
                "login": "******"
            },  # Who wrote the command?
        }

        response = bot.issues(fake_webhook)
        assert "this help message" in response["message"]
        assert "command1" in response["message"]
        assert "notacommand" not in response["message"]

        help_comment = list(issue.get_comments())[-1]
        assert "this help message" in help_comment.body
        assert "command1" in help_comment.body
        assert "notacommand" not in help_comment.body

        # Clean
        help_comment.delete()
Example #6
0
def notify():
    """Github main endpoint."""
    github_bot = GithubHandler()
    bot = BotHandler(github_bot)
    github_index = {
        'ping': ping,
        'issue_comment': bot.issue_comment,
        'issues': bot.issues
    }
    return handle_github_webhook(github_index,
                                 request.headers['X-GitHub-Event'],
                                 request.get_json())
    def test_bot_basic_failure(self, format_exc):
        format_exc.return_value = 'something to do with an exception'

        github_token = self.oauth_token
        repo = self.g.get_repo("lmazuel/TestingRepo")
        issue = repo.get_issue(18)

        class BotCommand:
            @order
            def command1(self, issue, param1):
                assert issue.number == 18
                raise ValueError("Not happy")

        bot = BotHandler(BotCommand(), "AutorestCI", github_token)

        fake_webhook = {
            'action': 'opened',  # What is the comment state?
            'repository': {
                'full_name':
                issue.repository.full_name  # On what repo is this command?
            },
            'issue': {
                'number': issue.number,  # On what issue is this comment?
                'body': "@AutorestCI command1 myparameter"  # Message?
            },
            'sender': {
                'login': "******"  # Who wrote the command?
            },
        }

        response = bot.issues(fake_webhook)
        assert response['message'] == 'Nothing for me or exception'

        help_comment = list(issue.get_comments())[-1]
        assert "something to do with an exception" in help_comment.body
        assert "```python" in help_comment.body

        # Clean
        help_comment.delete()
    def test_bot_basic_failure(self, format_exc):
        format_exc.return_value = "something to do with an exception"

        github_token = self.oauth_token
        repo = self.g.get_repo("lmazuel/TestingRepo")
        issue = repo.get_issue(18)

        class BotCommand:
            @order
            def command1(self, issue, param1):
                assert issue.number == 18
                raise ValueError("Not happy")

        bot = BotHandler(BotCommand(), "AutorestCI", github_token)

        fake_webhook = {
            "action": "opened",  # What is the comment state?
            "repository": {
                "full_name": issue.repository.full_name
            },  # On what repo is this command?
            "issue": {
                "number": issue.number,  # On what issue is this comment?
                "body": "@AutorestCI command1 myparameter",  # Message?
            },
            "sender": {
                "login": "******"
            },  # Who wrote the command?
        }

        response = bot.issues(fake_webhook)
        assert response["message"] == "Nothing for me or exception"

        help_comment = list(issue.get_comments())[-1]
        assert "something to do with an exception" in help_comment.body
        assert "```python" in help_comment.body

        # Clean
        help_comment.delete()
Example #9
0
def rest_notify():
    """Github rest endpoint."""
    sdkid = request.args.get("sdkid")
    sdkbase = request.args.get("sdkbase", "master")
    sdk_tag = request.args.get("repotag", sdkid.split("/")[-1].lower())

    if not sdkid:
        return {'message': 'sdkid is a required query parameter'}

    rest_bot = RestAPIRepoHandler(sdkid, sdk_tag, sdkbase)
    bot = BotHandler(rest_bot)
    github_index = {
        'ping': ping,
        'push': push,
        'pull_request': rest_pull_request,
        'issue_comment': bot.issue_comment,
        'issues': bot.issues
    }
    if not _WORKER_THREAD.is_alive():
        _WORKER_THREAD.start()

    return handle_github_webhook(github_index,
                                 request.headers['X-GitHub-Event'],
                                 request.get_json())