<commit_before>from http.server import HTTPServer, BaseHTTPRequestHandler
from threading import Thread
import hmac
import json
import urllib.parse
from .main import PullReqState, parse_commands
from . import utils
from socketserver import ThreadingMixIn
import github3
import jinja2
import requests
import pkg_resources

class RequestHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == '/':
            resp_status = 200
            resp_text = self.server.tpls['index'].render(repos=sorted(self.server.repos))

        elif self.path.startswith('/queue/'):
            repo_name = self.path.split('/', 2)[2]

            repo = self.server.repos[repo_name]
            pull_states = sorted(self.server.states[repo_name].values())

            rows = []
            for state in pull_states:
                rows.append({
                    'status': 'approved' if state.status == '' and state.approved_by else state.status,
                    'priority': 'rollup' if state.rollup else state.priority,
                    'url': 'https://github.com/{}/{}/pull/{}'.format(repo.owner, repo.name, state.num),
                    'num': state.num,
                    'approved_by': state.approved_by,
                    'title': state.title,
                    'head_ref': state.head_ref,
                    'mergeable': 'yes' if state.mergeable is True else 'no' if state.mergeable is False else '',
                    'assignee': state.assignee,
                })

            resp_status = 200
            resp_text = self.server.tpls['queue'].render(
                repo_name = repo.name,
                states = rows,
                oauth_client_id = self.server.cfg['main']['oauth_client_id'],
                total = len(pull_states),
                approved = len([x for x in pull_states if x.approved_by]),
                rolled_up = len([x for x in pull_states if x.rollup]),
                failed = len([x for x in pull_states if x.status == 'failure' or x.status == 'error']),
            )

        elif self.path.startswith('/rollup'):
            args = urllib.parse.parse_qs(self.path[self.path.index('?')+1:])
            code = args['code'][0]
            state = json.loads(args['state'][0])

            res = requests.post('https://github.com/login/oauth/access_token', data={
                'client_id': self.server.cfg['main']['oauth_client_id'],
                'client_secret': self.server.cfg['main']['oauth_client_secret'],
                'code': code,
            })
            args = urllib.parse.parse_qs(res.text)
            token = args['access_token'][0]

            repo = self.server.repos[state['repo']]
            repo_cfg = self.server.repo_cfgs[repo.name]

            user_gh = github3.login(token=token)
            user_repo = user_gh.repository(user_gh.user().login, repo.name)
            base_repo = user_gh.repository(repo.owner.login, repo.name)

            rollup_states = [x for x in self.server.states[repo.name].values() if x.rollup and x.approved_by]

            if not rollup_states:
                resp_status = 200
                resp_text = 'No pull requests are marked as rollup'
            else:
                master_sha = repo.ref('heads/' + repo_cfg['master_branch']).object.sha
                try:
                    utils.github_set_ref(
                        user_repo,
                        'heads/' + repo_cfg['rollup_branch'],
                        master_sha,
                        force=True,
                    )
                except github3.models.GitHubError:
                    user_repo.create_ref(
                        'refs/heads/' + repo_cfg['rollup_branch'],
                        master_sha,
                    )

                successes = []
                failures = []
<commit_msg>Roll up pull requests in specific order<commit_after>from http.server import HTTPServer, BaseHTTPRequestHandler
from threading import Thread
import hmac
import json
import urllib.parse
from .main import PullReqState, parse_commands
from . import utils
from socketserver import ThreadingMixIn
import github3
import jinja2
import requests
import pkg_resources

class RequestHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == '/':
            resp_status = 200
            resp_text = self.server.tpls['index'].render(repos=sorted(self.server.repos))

        elif self.path.startswith('/queue/'):
            repo_name = self.path.split('/', 2)[2]

            repo = self.server.repos[repo_name]
            pull_states = sorted(self.server.states[repo_name].values())

            rows = []
            for state in pull_states:
                rows.append({
                    'status': 'approved' if state.status == '' and state.approved_by else state.status,
                    'priority': 'rollup' if state.rollup else state.priority,
                    'url': 'https://github.com/{}/{}/pull/{}'.format(repo.owner, repo.name, state.num),
                    'num': state.num,
                    'approved_by': state.approved_by,
                    'title': state.title,
                    'head_ref': state.head_ref,
                    'mergeable': 'yes' if state.mergeable is True else 'no' if state.mergeable is False else '',
                    'assignee': state.assignee,
                })

            resp_status = 200
            resp_text = self.server.tpls['queue'].render(
                repo_name = repo.name,
                states = rows,
                oauth_client_id = self.server.cfg['main']['oauth_client_id'],
                total = len(pull_states),
                approved = len([x for x in pull_states if x.approved_by]),
                rolled_up = len([x for x in pull_states if x.rollup]),
                failed = len([x for x in pull_states if x.status == 'failure' or x.status == 'error']),
            )

        elif self.path.startswith('/rollup'):
            args = urllib.parse.parse_qs(self.path[self.path.index('?')+1:])
            code = args['code'][0]
            state = json.loads(args['state'][0])

            res = requests.post('https://github.com/login/oauth/access_token', data={
                'client_id': self.server.cfg['main']['oauth_client_id'],
                'client_secret': self.server.cfg['main']['oauth_client_secret'],
                'code': code,
            })
            args = urllib.parse.parse_qs(res.text)
            token = args['access_token'][0]

            repo = self.server.repos[state['repo']]
            repo_cfg = self.server.repo_cfgs[repo.name]

            user_gh = github3.login(token=token)
            user_repo = user_gh.repository(user_gh.user().login, repo.name)
            base_repo = user_gh.repository(repo.owner.login, repo.name)

            rollup_states = [x for x in self.server.states[repo.name].values() if x.rollup and x.approved_by]
            rollup_states.sort(key=lambda x: x.num)

            if not rollup_states:
                resp_status = 200
                resp_text = 'No pull requests are marked as rollup'
            else:
                master_sha = repo.ref('heads/' + repo_cfg['master_branch']).object.sha
                try:
                    utils.github_set_ref(
                        user_repo,
                        'heads/' + repo_cfg['rollup_branch'],
                        master_sha,
                        force=True,
                    )
                except github3.models.GitHubError:
                    user_repo.create_ref(
                        'refs/heads/' + repo_cfg['rollup_branch'],
                        master_sha,
                    )

                successes = []
                failures = []

{'fraction_non_alphanumeric': 0.11564274651522974, 'fraction_numerical': 0.00490449148167269, 'mean_word_length': 1.694367176634214, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '862499', 'n_tokens_mistral': 2227, 'n_tokens_neox': 2090, 'n_words': 441}