示例#1
0
    def test_parse_project_name(self):
        project = parse_project_name_from_repo_url(
            "https://github.com/flalanne/gitbot/issues/1", "issues")
        assert project.get('namespace') == 'flalanne'
        assert project.get('name') == 'gitbot'
        assert project.get('url') == 'https://github.com/flalanne/gitbot'

        project = parse_project_name_from_repo_url(
            "https://github.com/flalanne/gitbot", "issues")
        assert project.get('namespace') == 'flalanne'
        assert project.get('name') == 'gitbot'
        assert project.get('url') == 'https://github.com/flalanne/gitbot'

        project = parse_project_name_from_repo_url(
            "https://github.com/flalanne/gitbot")
        assert project.get('namespace') == 'flalanne'
        assert project.get('name') == 'gitbot'
        assert project.get('url') == 'https://github.com/flalanne/gitbot'

        project = parse_project_name_from_repo_url(
            "https://mine.gitlab.com/repo")
        assert project.get('name') == 'repo'
        assert project.get('url') == 'https://mine.gitlab.com/repo'

        project = parse_project_name_from_repo_url(
            "https://mine.gitlab.com/myrepo/issues/1", "issues")
        assert project.get('name') == 'myrepo'
        assert project.get('url') == 'https://mine.gitlab.com/myrepo'
示例#2
0
文件: views.py 项目: pipex/gitbot
    def issue(self, data):
        if not self.check_object_kind(data, 'issue'):
            # This should not happen
            return default_response()

        # Get the issue object
        issue = data.get('object_attributes')

        # Get the project data from the url, since there is no 'repository' provided
        project = parse_project_name_from_repo_url(issue.get('url'),
                                                   resource='issues')

        # If the project has a namespace, check that the namespace exists in slack
        # Otherwise try to find the channel matching the project name
        # Finally check SLACK_DEVELOPERS_CHANNEL or #general
        names = [
            i for i in [
                project.get('namespace'),
                project.get('name'),
                app.config.get('SLACK_DEVELOPERS_CHANNEL'), '#general'
            ] if i is not None
        ]

        channel = None
        namespace_user = User.findBy(
            'gitlab_name',
            project.get('namespace')) if project.get('namespace') else None
        if namespace_user:
            # If the namespace is a slack user, send data directly to the user channel
            channel = '@' + namespace_user.name

        for name in names:
            if channel:
                break
            channel = name if Channel.exists(name) else None

        # Get the user info
        gitlab_user = data.get('user')

        # Check if the username matches slack username
        user = {}
        user['full_name'] = gitlab_user.get('name')
        slack_user = User.findBy('gitlab_name', gitlab_user.get('username'))
        if slack_user:
            user = slack_user

        # Generate the response text
        message = render_template('issue.txt',
                                  user=user,
                                  project=project,
                                  issue=issue)

        if not app.config.get('TESTING', False):
            # Send message to slack
            slack.chat.post_message(channel, message)
        else:
            # Return message to check in testing
            return message

        return default_response()
示例#3
0
文件: views.py 项目: pipex/gitbot
    def tag_push(self, data):
        # Publish news of the new version of the repo in general
        if not self.check_object_kind(data, 'tag_push'):
            # This should not happen
            return default_response()

        # Get the project data
        repository = data.get('repository')
        project = parse_project_name_from_repo_url(repository.get('homepage'))

        # For now all tag messages go to #general to notify the whole team of the
        # new version
        channel = '#general'
        if project.get('namespace') and User.findBy('gitlab_name',
                                                    project.get('namespace')):
            # If the namespace is a slack user, we probably don't need to notify of a new
            # tag push
            return default_response()

        # Get the tag reference
        reference = data.get('ref')

        # Usually the reference is given by refs/tags/<tagname>
        refs, name, tag = reference.split('/')
        message = data.get('message')

        user = {}
        user['full_name'] = data.get('user_name')
        if data.get('user_email'):
            # Get the user by email. Assume that the same email is used for
            # Gitlab and slack
            slack_user = User.findBy('email', data.get('user_email'))
            if slack_user:
                user = slack_user

        team = project.get('namespace', project.get('name'))

        # Generate the response text
        response = render_template('tag.txt',
                                   user=user,
                                   project=project,
                                   message=message,
                                   team=team,
                                   tag=tag)

        if not app.config.get('TESTING', False):
            # Send message to slack
            slack.chat.post_message(channel, response)
        else:
            # slack.chat.post_message('#slack-test', response)
            # Return message to check in testing
            return response

        return default_response()
示例#4
0
文件: views.py 项目: pipex/gitbot
    def issue(self, data):
        if not self.check_object_kind(data, 'issue'):
            # This should not happen
            return default_response()

        # Get the issue object
        issue = data.get('object_attributes')

        # Get the project data from the url, since there is no 'repository' provided
        project = parse_project_name_from_repo_url(issue.get('url'), resource='issues')

        # If the project has a namespace, check that the namespace exists in slack
        # Otherwise try to find the channel matching the project name
        # Finally check SLACK_DEVELOPERS_CHANNEL or #general
        names = [i for i in [project.get('namespace'),
                             project.get('name'),
                             app.config.get('SLACK_DEVELOPERS_CHANNEL'),
                             '#general'] if i is not None]

        channel = None
        namespace_user = User.findBy('gitlab_name', project.get('namespace')) if project.get('namespace') else None
        if namespace_user:
            # If the namespace is a slack user, send data directly to the user channel
            channel = '@' + namespace_user.name

        for name in names:
            if channel:
                break
            channel = name if Channel.exists(name) else None

        # Get the user info
        gitlab_user = data.get('user')

        # Check if the username matches slack username
        user = {}
        user['full_name'] = gitlab_user.get('name')
        slack_user = User.findBy('gitlab_name', gitlab_user.get('username'))
        if slack_user:
            user = slack_user

        # Generate the response text
        message = render_template('issue.txt', user=user, project=project, issue=issue)

        if not app.config.get('TESTING', False):
            # Send message to slack
            slack.chat.post_message(channel, message)
        else:
            # Return message to check in testing
            return message

        return default_response()
示例#5
0
文件: util.py 项目: pipex/gitbot
    def test_parse_project_name(self):
        project = parse_project_name_from_repo_url("https://github.com/flalanne/gitbot/issues/1", "issues")
        assert project.get('namespace') == 'flalanne'
        assert project.get('name') == 'gitbot'
        assert project.get('url') == 'https://github.com/flalanne/gitbot'

        project = parse_project_name_from_repo_url("https://github.com/flalanne/gitbot", "issues")
        assert project.get('namespace') == 'flalanne'
        assert project.get('name') == 'gitbot'
        assert project.get('url') == 'https://github.com/flalanne/gitbot'

        project = parse_project_name_from_repo_url("https://github.com/flalanne/gitbot")
        assert project.get('namespace') == 'flalanne'
        assert project.get('name') == 'gitbot'
        assert project.get('url') == 'https://github.com/flalanne/gitbot'

        project = parse_project_name_from_repo_url("https://mine.gitlab.com/repo")
        assert project.get('name') == 'repo'
        assert project.get('url') == 'https://mine.gitlab.com/repo'

        project = parse_project_name_from_repo_url("https://mine.gitlab.com/myrepo/issues/1", "issues")
        assert project.get('name') == 'myrepo'
        assert project.get('url') == 'https://mine.gitlab.com/myrepo'
示例#6
0
文件: views.py 项目: pipex/gitbot
    def tag_push(self, data):
        # Publish news of the new version of the repo in general
        if not self.check_object_kind(data, 'tag_push'):
            # This should not happen
            return default_response()

        # Get the project data
        repository = data.get('repository')
        project = parse_project_name_from_repo_url(repository.get('homepage'))

        # For now all tag messages go to #general to notify the whole team of the
        # new version
        channel = '#general'
        if project.get('namespace') and User.findBy('gitlab_name', project.get('namespace')):
            # If the namespace is a slack user, we probably don't need to notify of a new
            # tag push
            return default_response()

        # Get the tag reference
        reference = data.get('ref')

        # Usually the reference is given by refs/tags/<tagname>
        refs, name, tag = reference.split('/')
        message = data.get('message')

        user = {}
        user['full_name'] = data.get('user_name')
        if data.get('user_email'):
            # Get the user by email. Assume that the same email is used for
            # Gitlab and slack
            slack_user = User.findBy('email', data.get('user_email'))
            if slack_user:
                user = slack_user

        team = project.get('namespace', project.get('name'))

        # Generate the response text
        response = render_template('tag.txt', user=user, project=project, message=message, team=team, tag=tag)

        if not app.config.get('TESTING', False):
            # Send message to slack
            slack.chat.post_message(channel, response)
        else:
            # slack.chat.post_message('#slack-test', response)
            # Return message to check in testing
            return response

        return default_response()