Example #1
0
    async def setUp(self):
        global root
        git.get_repo_dirpath.cache_clear()
        settings['repository_path'] = repopath.name
        print('repository_path:', repopath.name)
        if root is not None:
            return
        await git.init_git_repo('abcd', 'efgh')
        root = git.get_repo_dirpath('abcd', 'efgh')
        self.assertTrue(os.path.exists(root))
        print('testing repo', root)
        git._check_exists(root)
        with tempfile.TemporaryDirectory() as tmpdir:
            clone(self, tmpdir, root)
            commit_file(self, tmpdir, 'test1.txt', 'testing file1',
                        'init message')
        with tempfile.TemporaryDirectory() as tmpdir:
            clone(self, tmpdir, root)
            commit_file(self, tmpdir, 'test2.txt', 'testing file2',
                        'test commit')

        async def _mock(*args):
            return {'dest_branch': 'master'}

        pullrequest.get_pull_request_info = _mock
Example #2
0
async def pull_request_preview(dstowner: str, dstrepo: str, dstbranch: str,
                               srcowner: str, srcrepo: str,
                               srcbranch: str) -> Dict[str, Any]:
    if dstowner != srcowner or dstrepo != srcrepo:
        dstroot = git.get_repo_dirpath(dstowner, dstrepo)
        srcroot = git.get_repo_dirpath(srcowner, srcrepo)
        await git.git_fetch(dstroot,
                            'FETCH_HEAD',
                            srcroot,
                            srcbranch,
                            fetch_head=True)
        srcbranch = 'FETCH_HEAD'
    result = {}
    result['log'] = await git.git_commit_logs(dstowner,
                                              dstrepo,
                                              'FETCH_HEAD',
                                              pretty=git.MEDIUM,
                                              base=dstbranch)
    result['diff'] = await git.git_diff(dstroot, dstbranch, srcbranch)
    return result
Example #3
0
async def create_pull_request_git(dstowner: str,
                                  dstrepo: str,
                                  dstbranch: str,
                                  pullid: int,
                                  srcowner: str,
                                  srcrepo: str,
                                  srcbranch: str,
                                  authorname: str,
                                  authoremail: str,
                                  content: str = None,
                                  *,
                                  is_update=False):
    dstroot = git.get_repo_dirpath(dstowner, dstrepo)
    srcroot = git.get_repo_dirpath(srcowner, srcrepo)
    pr_head_branch = 'refs/pull/{}/head'.format(pullid)
    pr_merge_branch = 'refs/pull/{}/merge'.format(pullid)
    if is_update:
        await git.git_rm_branch(dstroot, pr_head_branch, force=True)
        await git.git_rm_branch(dstroot, pr_merge_branch, force=True)
    await git.git_fetch(dstroot, pr_head_branch, srcroot, srcbranch)
    workingdir = await get_working_dir(dstowner, dstrepo, pullid)
    await git.git_fetch(workingdir, pr_head_branch, dstroot, pr_head_branch)
    flag_auto_merged = True
    output = ''
    try:
        await git.git_config(workingdir, 'user.name', authorname)
        await git.git_config(workingdir, 'user.email', authoremail)
        await git.git_merge(workingdir, dstbranch, pr_head_branch)
        msg = get_merge_message(pullid, srcowner, srcbranch, content)
        await git.git_commit(workingdir, msg)
    except git.CanNotAutoMerge as e:
        await git.git_merge_action(workingdir, 'abort')
        flag_auto_merged = False
        output = e.args[0]
    except RuntimeError as e:
        raise e
    await git.git_fetch(dstroot, pr_merge_branch, workingdir, dstbranch)
    return flag_auto_merged, output
Example #4
0
 async def _before_setUp(self):
     global root
     if root is not None:
         return
     git.get_repo_dirpath.cache_clear()
     settings['repository_path'] = repopath.name
     print('repository_path:', repopath.name)
     await git.init_git_repo('abcd', 'efgh')
     root = git.get_repo_dirpath('abcd', 'efgh')
     self.assertTrue(os.path.exists(root))
     git._check_exists(root)
     # print('s test', root)
     with tempfile.TemporaryDirectory() as tmpdir:
         clone(self, tmpdir, root)
         commit_file(self, tmpdir, 'test1.txt', 'testing file1',
                     'init message')
     with tempfile.TemporaryDirectory() as tmpdir:
         clone(self, tmpdir, root)
         commit_file(self, tmpdir, 'test2.txt', 'testing file2',
                     'test commit')
Example #5
0
    async def handle_git_request(self, method, owner, reponame, path_info):
        for method, pattern in GIT_URL_PATTERNS:
            if re.match(pattern + '$', path_info):
                break
        else:
            self.write("Request not supported: {}/{}".format(owner, reponame))
            raise HTTPError(404)

        try:
            root = git.get_repo_dirpath(owner, reponame)
        except ValueError as e:
            raise HTTPError(404) from e

        if not os.path.exists(root):
            raise HTTPError(404)

        query_string = self.request.query
        if method == 'POST':
            content_type = self.request.headers.get('Accept', '').replace(
                'result', 'request')
        else:
            content_type = None
        env = get_git_http_backend_env(method, query_string, root, path_info,
                                       content_type)
        returncode, out, err = await run(['git', 'http-backend'],
                                         cwd=root,
                                         env=env,
                                         input=self.request.body,
                                         stdout=PIPE,
                                         stderr=PIPE,
                                         timeout=10)
        if len(err) > 0 or returncode != 0:
            logging.error(err.decode())
            raise HTTPError(500)
        header, body = parse_cgi_stdout(out)
        for k, v in header.items():
            self.set_header(k, v)
        self.write(body)
Example #6
0
async def merge_pull_request_git(dstowner: str, dstrepo: str, pullid: int):
    prinfo = await get_pull_request_info(dstowner, dstrepo, pullid)
    if prinfo is None:
        raise ValueError('PullRequest Not Found')
    dstbranch = prinfo['dest_branch']
    if not has_working_dir(dstowner, dstrepo, pullid):
        await update_pull_request(dstowner, dstrepo, pullid)
    dstroot = git.get_repo_dirpath(dstowner, dstrepo)
    pr_merge_branch = 'refs/pull/{}/merge'.format(pullid)
    workingdir = await get_working_dir(dstowner, dstrepo, pullid)
    print(workingdir, pr_merge_branch, dstbranch)

    latest_commit_merge, latest_commit_dst = await asyncio.gather(
        git.git_log(dstroot, pr_merge_branch, maxsize=1),
        git.git_log(dstroot, dstbranch, maxsize=1))
    if (not is_merge_message(latest_commit_merge[0][1], pullid)
            or latest_commit_dst[0][0] == latest_commit_merge[0][0]):
        raise RuntimeError('merge was not actually performed.')

    await git.git_fetch(workingdir, pr_merge_branch, dstroot, pr_merge_branch)
    await git.git_checkout(workingdir, pr_merge_branch)
    await git.git_push(workingdir, dstbranch, 'origin')
    finish_working_dir(dstowner, dstrepo, pullid)
    return True
Example #7
0
 async def test_diff(self):
     root = git.get_repo_dirpath('abcd', 'efgh')
     result = await git.git_diff(root, 'master', 'HEAD^1')
     self.assertEqual(result[0][0], 'a/test2.txt b/test2.txt')