Exemple #1
0
    def __init__(self, data):

        # Internal GitHub id
        self.id = data.get('id', 0)

        # Title and Body
        self.title = data.get('title', '')
        self.body = data.get('body', '')

        # Public links
        self.html_url = data.get('html_url', '')
        self.diff_url = data.get('diff_url', '')
        self.issue_url = data.get('issue_url', '')
        self.commits_url = data.get('commits_url', '')
        self.review_comments_url = data.get('review_comments_url', '')

        # open | closed
        self.state = data.get('state', '')

        # Number in repository
        self.number = data.get('number', '')

        # Who opened
        self.user = None
        if 'user' in data:
            self.user = User(data['user'])

        # Who merged and sends
        self.sender = None
        if 'sender' in data:
            self.sender = User(data['sender'])

        # Whether this request closed or merged
        self.merged = data.get("merged", False)

        # Head branch
        self.head = None
        if 'head' in data:
            self.head = Branch(data['head'])

        # Base branch
        self.base = None
        if 'base' in data:
            self.base = Branch(data['base'])

        # Repository
        self.repository = None
        if 'repository' in data:
            self.repository = Repository(data['repository'])

        # Dates
        self.created_at = data.get('created_at', '')
        self.closed_at = data.get('closed_at', '')
        self.updated_at = data.get('updated_at', '')

        # Requested reviewers
        self.requested_reviewers = []
        if 'requested_reviewers' in data:
            for reviewer in data['requested_reviewers']:
                self.requested_reviewers.append(User(reviewer))
Exemple #2
0
    def __init__(self, data):

        # Internal GitHub id
        self.id = data.get('id', 0)
        self.node_id = data.get('node_id', 0)

        # Commented issue
        self.issue_url = data.get('issue_url', '')

        # Body
        self.body = data.get('body', '')

        # Public link
        self.url = data.get('url', '')
        self.html_url = data.get('html_url', '')

        # Who opened
        self.user = None
        if 'user' in data:
            self.user = User(data['user'])

        # Dates
        self.created_at = data.get('created_at', '')
        self.closed_at = data.get('closed_at', '')
        self.updated_at = data.get('updated_at', '')
Exemple #3
0
    async def process(self, payload, chat):
        """
        Processes Discussions event
        :param payload: JSON object with payload
        :param chat: current chat object
        :return:
        """

        self.sdk.log("Discussions event payload taken")

        try:
            self.discussion = Discussion(payload['discussion'])
            self.repository = Repository(payload['repository'])
            self.sender = User(payload['sender'])

        except Exception as e:
            self.sdk.log(
                'Cannot process DiscussionsEvent payload because of {}'.format(
                    e))

        action = payload['action']

        available_actions = {
            'created': self.created,
            'deleted': self.deleted,
        }

        if action not in available_actions:
            self.sdk.log('Unsupported Discussions action: {}'.format(action))
            return

        # call action handler
        await available_actions[action](chat['chat'], payload)
Exemple #4
0
    async def review_requested(self, chat_id, payload):
        """
        Pull request review requested action
        :param chat_id: Current user chat token
        :param payload: GitHub payload
        :return:
        """

        # get user.login or team.name
        requested_reviewer_name = ''

        if payload.get('requested_reviewer'):
            requested_reviewer = User(payload['requested_reviewer'])
            requested_reviewer_name = requested_reviewer.login

        if payload.get('requested_team'):
            requested_reviewer = Team(payload['requested_team'])
            requested_reviewer_name = requested_reviewer.name

        message = "🙀 {name} requested <code>{requested_reviewer}</code>'s review " \
                  "for pull request «<a href=\"{request_url}\">{request_title}</a>» " \
                  "[<a href=\"{repository_url}\">{repository_name}</a>]".format(
                    name=self.sender.login,
                    requested_reviewer=requested_reviewer_name,
                    request_url=self.pull_request.html_url,
                    request_title=html.escape(self.pull_request.title),
                    repository_url=self.repository.html_url,
                    repository_name=self.repository.full_name
        )

        await self.send(chat_id, message, 'HTML')
Exemple #5
0
    async def process(self, payload, chat):
        """
        Processes Watch event
        :param payload: JSON object with payload
        :param chat: current chat object
        :return:
        """

        self.sdk.log("Watch event payload taken {}".format(payload))

        try:

            self.repository = Repository(payload['repository'])
            self.sender = User(payload['sender'])

        except Exception as e:
            self.sdk.log(
                'Cannot process WatchEvent payload because of {}'.format(e))

        await self.send(
            chat['chat'],
            '⭐️ <a href=\"{}\">{}</a> starred <a href=\"{}\">{}</a> ★ {}'.
            format(self.sender.html_url, self.sender.login,
                   self.repository.html_url, self.repository.full_name,
                   self.repository.stargazers_count), 'HTML')
Exemple #6
0
    def __init__(self, data):

        # Internal GitHub id
        self.id = data.get('id', 0)

        # Who create
        self.user = None
        if 'user' in data:
            self.user = User(data['user'])

        # Body
        self.body = data.get('body', '')

        # Dates
        self.submitted_at = data.get('submitted_at', '')

        self.html_url = data.get('html_url', '')

        # Review result
        self.state = data.get('state', '')

        # Linked pull request
        self.pull_request_url = ''
        if 'pull_request' in data:
            self.pull_request_url = data['pull_request'].get('html_url', '')
Exemple #7
0
    async def process(self, payload, chat):
        """
        Processes IssueComment event
        :param payload: JSON object with payload
        :param chat: current chat object
        :return:
        """

        self.sdk.log("IssueComment event payload taken")

        try:
            self.issue = Issue(payload['issue'])
            self.comment = IssueComment(payload['comment'])
            self.repository = Repository(payload['repository'])
            self.sender = User(payload['sender'])

        except Exception as e:
            self.sdk.log('Cannot process IssueCommentEvent payload because of {}'.format(e))

        action = payload['action']

        available_actions = {
            'created': self.created
        }

        if action not in available_actions:
            self.sdk.log('Unsupported IssueComment action: {}'.format(action))
            return

        # call action handler
        await available_actions[action](chat['chat'], payload)
Exemple #8
0
    async def process(self, payload, chat):
        """
        Processes Ping event
        :param payload: JSON object with payload
            zen - Random string of GitHub zen
            hook_id - The ID of the webhook that triggered the ping
            hook - The webhook configuration
        :param chat: current chat object
        :return:
        """

        self.sdk.log("Ping event payload taken {}".format(payload))

        try:

            self.repository = Repository(payload['repository'])
            self.sender = User(payload['sender'])
            self.hook = Hook(payload['hook'])

        except Exception as e:
            self.sdk.log(
                'Cannot process PingEvent payload because of {}'.format(e))

        await self.send(
            chat['chat'], '👏 Repository {} successfully linked. Boom.'.format(
                self.repository.full_name), 'HTML')
Exemple #9
0
    def __init__(self, data):

        self.url = data.get('url', '')

        self.author = None
        if 'author' in data:
            self.author = User(data['author'])

        self.committer = None
        if 'committer' in data:
            self.committer = User(data['committer'])

        self.message = data.get('message', '')
        self.tree = data.get('tree', None)
        self.comment_count = data.get('comment_count', 0)

        self.added = data.get('added', [])
        self.removed = data.get('removed', [])
        self.modified = data.get('modified', [])
Exemple #10
0
    def __init__(self, data):

        # Internal GitHub id
        self.id = data.get('id', 0)

        # Title and Body
        self.title = data.get('title', '')
        self.body = data.get('body', '')

        # Public link
        self.html_url = data.get('html_url', '')

        # Number in repository
        self.number = data.get('number', '')

        # Who opened
        self.user = None
        if 'user' in data:
            self.user = User(data['user'])

        # Who closed
        self.closed_by = None
        if 'closed_by' in data:
            self.closed_by = User(data['closed_by'])

        # Labels
        self.labels = data.get('labels', '')

        # Dates
        self.created_at = data.get('created_at', '')
        self.closed_at = data.get('closed_at', '')
        self.updated_at = data.get('updated_at', '')

        self.state = data.get('state', '')

        # Linked pull request
        self.pull_request_url = ''
        if 'pull_request' in data:
            self.pull_request_url = data['pull_request'].get('html_url', '')
Exemple #11
0
    async def process_repository_event(self, payload, chat):
        try:
            self.repository = Repository(payload['repository'])
            self.sender = User(payload['sender'])
            self.hook = Hook(payload['hook'])

        except Exception as e:
            self.sdk.log('Cannot process PingEvent payload because of {}'.format(e))

        await self.send(
            chat['chat'],
            '👏 Repository {} successfully linked. Boom.'.format(self.repository.full_name),
            'HTML'
        )
Exemple #12
0
    def __init__(self, data):

        self.ref = data.get('ref', '')
        self.sha = data.get('sha', '')

        # Who opened
        self.user = None
        if 'user' in data:
            self.user = User(data['user'])

        # Who merged and sends
        self.repo = None
        if 'repo' in data:
            self.repo = Repository(data['repo'])
Exemple #13
0
    async def process(self, payload, chat):
        """
        Processes Pull Request event
        :param payload: JSON object with payload
            action  string      - Can be one of "assigned", "unassigned", "review_requested", "review_request_removed",
                                "labeled", "unlabeled", "opened", "edited", "closed", or "reopened".
                                If the action is "closed" and the merged key is false, the pull request was closed
                                with unmerged commits. If the action is "closed" and the merged key is true,
                                the pull request was merged. While webhooks are also triggered
                                when a pull request is synchronized, Events API timelines don't include
                                pull request events with the "synchronize" action.
            number  integer     - The pull request number.

            changes object          - The changes to the comment if the action was "edited".
            changes[title][from]    - The previous version of the title if the action was "edited".
            changes[body][from]     - The previous version of the body if the action was "edited".

            pull_request        - Pull request object

        :param chat: current chat object
        :return:
        """

        self.sdk.log("PullRequest event payload taken {}".format(payload))

        try:
            self.pull_request = PullRequest(payload['pull_request'])
            self.repository = Repository(payload['repository'])
            self.sender = User(payload['sender'])

        except Exception as e:
            self.sdk.log(
                'Cannot process PullRequest payload because of {}'.format(e))

        action = payload['action']

        available_actions = {
            'opened': self.opened,
            'reopened': self.reopened,
            'closed': self.closed,
            'review_requested': self.review_requested
        }

        if action not in available_actions:
            self.sdk.log('Unsupported PullRequest action: {}'.format(action))
            return

        # call action handler
        await available_actions[action](chat['chat'], payload)
    async def process(self, payload, chat):
        """
        Processes Pull Request Review event
        :param payload: JSON object with payload
            action          string  - The action that was performed. Can be "submitted", "edited", or "dismissed".
            pull_request    object  - Pull request object
            review          object  - The review that was affected.
            changes[body][from]     - The previous version of the body if the action was "edited".

        :param chat: current chat object
        :return:
        """

        self.sdk.log(
            "PullRequestReview event payload taken {}".format(payload))

        try:
            self.pull_request = PullRequest(payload['pull_request'])
            self.repository = Repository(payload['repository'])
            self.sender = User(payload['sender'])
            self.review = PullRequestReview(payload['review'])
            self.action = payload['action']

        except Exception as e:
            self.sdk.log(
                'Cannot process PullRequestReview payload because of {}'.
                format(e))

        state = self.review.state

        available_states = {
            'approved': self.approved,
            'commented': self.commented,
            'changes_requested': self.changes_requested
        }

        if state not in available_states:
            self.sdk.log(
                'Unsupported PullRequestReview state: {}'.format(state))
            return

        if self.action != "submitted":
            self.sdk.log(
                'PullRequestReview action is not equal "submitted": {}'.format(
                    state))
            return

        # call action handler
        await available_states[state](chat['chat'], payload)
Exemple #15
0
    def __init__(self, data):

        # Internal GitHub id
        self.id = data.get('id', 0)

        self.name = data.get('name', '')
        self.full_name = data.get('full_name', '')
        self.description = data.get('description', '')

        # Public link
        self.html_url = data.get('html_url', '')

        # Owner represented as User
        self.owner = User(data['owner'])

        self.private = data.get('private', 'false')
        self.git_url = data.get('git_url', '')
        self.clone_url = data.get('clone_url', '')

        self.stargazers_count = data.get('stargazers_count', 0)
Exemple #16
0
    def __init__(self, data):

        # Internal GitHub id
        self.id = data.get('id', 0)

        # Title
        self.title = data.get('title', '')

        # Public link
        self.html_url = data.get('html_url', '')

        # Number in repository
        self.number = data.get('number', '')

        # Category
        self.category = Category(data['category'])

        # Who created
        self.user = None
        if 'user' in data:
            self.user = User(data['user'])
Exemple #17
0
    async def assigned(self, chat_id, payload):
        """
        Issue assigned action
        :param chat_id: Current user chat token
        :param payload: GitHub payload
        :return:
        """

        assignee = User(payload['assignee'])

        message = "📌 {assignee} has been assigned to the issue «<code>{issue_title}</code>» " \
                  "by {author} [{repository_name}]".format(
                    assignee=assignee.login,
                    author=self.sender.login,
                    issue_title=html.escape(self.issue.title),
                    repository_name=self.repository.name
        ) + "\n\n"

        message += self.issue.html_url

        await self.send(chat_id, message, 'HTML')
Exemple #18
0
    async def process(self, payload, chat):
        """
        Processes Push event
        :param payload: JSON object with payload
            ref             string      The full Git ref that was pushed. Example: "refs/heads/master".
            head            string      The SHA of the most recent commit on ref after the push.
            before          string      The SHA of the most recent commit on ref before the push.
            size            integer     The number of commits in the push.
            distinct_size   integer     The number of distinct commits in the push.
            commits         array       An array of commit objects describing the pushed commits.
                                        (The array includes a maximum of 20 commits.
                                        If necessary, you can use the Commits API to fetch additional commits.
                                        This limit is applied to timeline events only
                                        and isn't applied to webhook deliveries.)

            compare Compare URL in repository

            pusher  Data of pusher user. Contains 'name' and 'email' fields
            sender  Who sends event
        :param chat: current chat object
        :return:
        """

        self.sdk.log("Push event payload taken {}".format(payload))

        try:

            self.repository = Repository(payload['repository'])
            self.sender = User(payload['sender'])

            for commit in payload['commits']:
                self.commits.append(Commit(commit))

        except Exception as e:
            self.sdk.log('Cannot process PingEvent payload because of {}'.format(e))

        if bool(payload['deleted']):
            message = "Branch {} has been deleted".format(payload['ref'])
            self.sdk.log(message)
            return

        if bool(payload['created']):
            message = "Branch {} has been created".format(payload['ref'])
            self.sdk.log(message)

        try:
            branch_name = payload['ref'].split('/')[-1]
            if 'branch' in chat and chat['branch'] != '*' and chat['branch'] != branch_name:
                return
        except Exception as e:
            self.sdk.log("Exception: {}".format(e))

        # Start building message

        message = '👊 {} pushed {} {} to {} \n\n'.format(
            self.sender.login,
            len(self.commits),
            "commits" if len(self.commits) > 1 else "commit",
            payload['ref']
        )

        # Compose lists of added|removed|modified filenames

        added = []
        removed = []
        modified = []

        for commit in self.commits:

            # Append commits messages
            message += html.escape('* {}\n'.format(commit.message))

            if len(commit.added):
                for added_file in commit.added:
                    if added_file not in added:
                        added.append(added_file)
            if len(commit.removed):
                for removed_file in commit.removed:
                    if removed_file not in removed:
                        removed.append(removed_file)
            if len(commit.modified):
                for modified_file in commit.modified:
                    if modified_file not in modified:
                        modified.append(modified_file)

        if len(added):
            message += '\nNew files: \n'
            for file_name in added:
                message += html.escape(file_name) + '\n'

        if len(removed):
            message += '\nRemoved files: \n'
            for file_name in removed:
                message += html.escape(file_name) + '\n'

        if len(modified):
            message += '\nModified files: \n'
            for file_name in modified:
                message += html.escape(file_name) + '\n'

        message += '\n ' + payload['compare']

        await self.send(
            chat['chat'],
            message,
            'HTML'
        )