Exemplo n.º 1
0
    def post(self):
        try:
            self.response.headers.add_header("Access-Control-Allow-Origin", "*")

            username = self.request.get("username")
            session_token = self.request.get("session_token")
            user_entry = UserEntry.lookup(username, session_token)
            assert user_entry is not None

            # Parse the repo name, owner and PR issue number from the referrer URL.
            pull_request_url = self.request.get("referrer")
            assert pull_request_url is not None
            split_url = pull_request_url.split("/")
            owner_name = split_url[3]
            repo_name = split_url[4]
            issue_number = int(split_url[6])
            logging.info("owner=%s, repo=%s, issue=%s" % (owner_name, repo_name, issue_number))

            repo = GitHubRepo(user_entry.oauth_token, owner_name, repo_name)

            pull = repo.repo.get_pull(issue_number)
            head = pull.head.ref
            base = pull.base.ref

            # Merge base into head
            merge_commit = repo.repo.merge(head, base)

            if merge_commit:
                # TODO: Wait for CI build to pass (use git hook)
                tree = merge_commit.commit.tree
            else:
                head_commit = repo.repo.get_commit(pull.head.sha)
                tree = head_commit.commit.tree

            squash_commit_message = (
                "%s\n" % self.request.get("commit_message") + "\n"
                "---\n"
                "Squash commit from %s created by Whaler." % pull_request_url
            )
            base_branch = repo.repo.get_git_ref("heads/%s" % base)
            parents = [repo.repo.get_git_commit(base_branch.object.sha)]
            author = self.get_author(pull)
            committer = self.get_committer(repo)
            new_commit = repo.repo.create_git_commit(squash_commit_message, tree, parents, author, committer)

            base_branch.edit(new_commit.sha)

            pull.create_issue_comment(self.MERGE_MESSAGE % new_commit.sha)
            pull.edit(state="closed")

            # Delete the merged branch.
            head_branch = repo.repo.get_git_ref("heads/%s" % head)
            head_branch.delete()
        except:
            arguments = {}
            for arg_key in self.request.arguments():
                arguments[arg_key] = self.request.get(arg_key)
            logging.debug(repr(arguments))
            logging.debug(repr(self.request.headers))
            raise
Exemplo n.º 2
0
    def get(self, username):
        try:
            code = self.request.get('code')
            state = str(self.request.get('state')).split(' ')
            referrer = state[0]
            session_token = state[1]

            token_url = "https://github.com/login/oauth/access_token?" + \
                        "client_id=%s&" % GITHUB_APP_CLIENT_ID + \
                        "client_secret=%s&" % GITHUB_APP_CLIENT_SECRET + \
                        "code=%s&" % code

            result = urlfetch.fetch(token_url, method=urlfetch.POST)
            result_dict = parse_form_encoded_body(result.content)

            UserEntry.update(username=username, oauth_token=result_dict['access_token'],
                                            session_token=session_token)

            self.redirect(referrer)
        except:
            logging.info(repr(self.request))
            raise
Exemplo n.º 3
0
    def get(self):
        self.response.headers.add_header('Access-Control-Allow-Origin', '*')

        username = self.request.get('username')
        assert username is not None
        assert '/' not in username

        redirect = self.request.get('redirect')
        assert redirect is not None

        session_token = self.request.get('session_token')
        assert session_token is not None

        user_entry = UserEntry.lookup(username, session_token)

        if user_entry is None:
            logging.info('Requesting new oauth token.')
            self.response.text = 'https://github.com/login/oauth/authorize?' + \
                                 'client_id=%s&' % GITHUB_APP_CLIENT_ID + \
                                 'redirect_uri=%s/oauth_callback/%s' % (BASE_URL, username) + \
                                 '&scope=public_repo,repo,write:repo_hook' + \
                                 '&state=%s+%s' % (redirect, session_token)
Exemplo n.º 4
0
    def get(self):
        self.response.headers.add_header('Access-Control-Allow-Origin', '*')

        username = self.request.get('username')
        assert username is not None
        assert '/' not in username

        redirect = self.request.get('redirect')
        assert redirect is not None
        
        session_token = self.request.get('session_token')
        assert session_token is not None

        user_entry = UserEntry.lookup(username, session_token)

        if user_entry is None:
            logging.info('Requesting new oauth token.')
            self.response.text = 'https://github.com/login/oauth/authorize?' + \
                                 'client_id=%s&' % GITHUB_APP_CLIENT_ID + \
                                 'redirect_uri=%s/oauth_callback/%s' % (BASE_URL, username) + \
                                 '&scope=public_repo,repo,write:repo_hook' + \
                                 '&state=%s+%s' % (redirect, session_token)
Exemplo n.º 5
0
    def post(self):
        try:
            self.response.headers.add_header('Access-Control-Allow-Origin',
                                             '*')

            username = self.request.get('username')
            session_token = self.request.get('session_token')
            user_entry = UserEntry.lookup(username, session_token)
            assert user_entry is not None

            # Parse the repo name, owner and PR issue number from the referrer URL.
            pull_request_url = self.request.get('referrer')
            assert pull_request_url is not None
            split_url = pull_request_url.split('/')
            owner_name = split_url[3]
            repo_name = split_url[4]
            issue_number = int(split_url[6])
            logging.info("owner=%s, repo=%s, issue=%s" %
                         (owner_name, repo_name, issue_number))

            repo = GitHubRepo(user_entry.oauth_token, owner_name, repo_name)

            pull = repo.repo.get_pull(issue_number)
            head = pull.head.ref
            base = pull.base.ref

            # Merge base into head
            merge_commit = repo.repo.merge(head, base)

            if merge_commit:
                # TODO: Wait for CI build to pass (use git hook)
                tree = merge_commit.commit.tree
            else:
                head_commit = repo.repo.get_commit(pull.head.sha)
                tree = head_commit.commit.tree

            squash_commit_message = "%s\n" % self.request.get('commit_message') + \
                                    "\n" \
                                    "---\n" \
                                    "Squash commit from %s created by Whaler." % pull_request_url
            base_branch = repo.repo.get_git_ref("heads/%s" % base)
            parents = [repo.repo.get_git_commit(base_branch.object.sha)]
            author = self.get_author(pull)
            committer = self.get_committer(repo)
            new_commit = repo.repo.create_git_commit(squash_commit_message,
                                                     tree, parents, author,
                                                     committer)

            base_branch.edit(new_commit.sha)

            pull.create_issue_comment(self.MERGE_MESSAGE % new_commit.sha)
            pull.edit(state='closed')

            # Delete the merged branch.
            head_branch = repo.repo.get_git_ref("heads/%s" % head)
            head_branch.delete()
        except:
            arguments = {}
            for arg_key in self.request.arguments():
                arguments[arg_key] = self.request.get(arg_key)
            logging.debug(repr(arguments))
            logging.debug(repr(self.request.headers))
            raise