def make_pull_request( settings: Settings, branch_name: str, ) -> bitbucket_server_api.Response: headers = { 'Authorization': f'Basic {settings.auth}', 'Content-Type': 'application/json', } remote = git.remote('.') remote_url = remote[:-len('.git')] if remote.endswith('.git') else remote *prefix, project, repo_slug = remote_url.split('/') head = branch_name autofix_lib.run('git', 'push', 'origin', f'HEAD:{branch_name}', '--quiet') title = subprocess.check_output(('git', 'log', '-1', '--format=%s')) body = subprocess.check_output(('git', 'log', '-1', '--format=%b')) data = json.dumps({ 'title': title.decode().strip(), 'description': body.decode().strip(), 'state': 'OPEN', 'open': True, 'closed': False, 'fromRef': { 'id': head, 'repository': { 'slug': repo_slug, 'project': { 'key': project, }, }, }, 'toRef': { 'id': autofix_lib.target_branch(), 'repository': { 'slug': repo_slug, 'project': { 'key': project, }, }, }, 'locked': False, 'reviewers': [], }).encode() end_point = f'projects/{project}/repos/{repo_slug}/pull-requests' return bitbucket_server_api.req( f'https://{settings.base_url}/rest/api/1.0/{end_point}', data=data, headers=headers, method='POST', )
def repo_context(repo: str, *, use_color: bool) -> Generator[None, None, None]: print(color.fmt(f'***{repo}', color.TURQUOISE_H, use_color=use_color)) try: remote = git.remote(repo) with tempfile.TemporaryDirectory() as tmpdir: run('git', 'clone', '--quiet', repo, tmpdir) with cwd(tmpdir): run('git', 'remote', 'set-url', 'origin', remote) run('git', 'fetch', '--prune', '--quiet') yield except Exception: print(color.fmt(f'***Errored', color.RED_H, use_color=use_color)) traceback.print_exc()
def _fetch_reset(path: str) -> None: def _git(*cmd: str) -> None: subprocess.check_call(('git', '-C', path, *cmd)) try: branch = _default_branch(git.remote(path)) _git('remote', 'set-branches', 'origin', branch) _git('fetch', 'origin', branch) _git('checkout', branch) _git('reset', '--hard', f'origin/{branch}') except subprocess.CalledProcessError: # TODO: color / tty print(f'Error fetching {path}')
def test_git_remote(tmpdir): r1 = tmpdir.join('1') r2 = tmpdir.join('2') subprocess.check_call(('git', 'init', r1)) subprocess.check_call(( 'git', '-C', r1, 'commit', '--allow-empty', '-m', 'foo', )) subprocess.check_call(('git', 'clone', r1, r2)) assert git.remote(r2) == r1
def _get_current_state_helper( path: str, ) -> Generator[Tuple[str, str], None, None]: if not os.path.exists(path): return pths = [] seen_git = False for direntry in os.scandir(path): if direntry.name == '.git': seen_git = True elif direntry.is_dir(): # pragma: no branch (defensive) pths.append(direntry) if seen_git: yield path, git.remote(path) else: for pth in pths: yield from _get_current_state_helper(os.fspath(pth))
def make_pull_request( settings: Settings, branch_name: str, ) -> github_api.Response: headers = {'Authorization': f'token {settings.api_key}'} remote_url = git.remote('.') _, _, repo_slug = remote_url.rpartition(':') if settings.fork: resp = github_api.req( f'{settings.base_url}/repos/{repo_slug}/forks', headers=headers, method='POST', ) new_slug = resp.json['full_name'] new_remote = remote_url.replace(repo_slug, new_slug) autofix_lib.run('git', 'remote', 'add', 'fork', new_remote) remote = 'fork' head = f'{settings.username}:{branch_name}' else: remote = 'origin' head = branch_name autofix_lib.run('git', 'push', remote, f'HEAD:{branch_name}', '--quiet') title = subprocess.check_output(('git', 'log', '-1', '--format=%s')) body = subprocess.check_output(('git', 'log', '-1', '--format=%b')) data = json.dumps({ 'title': title.decode().strip(), 'body': body.decode().strip(), 'base': autofix_lib.target_branch(), 'head': head, }).encode() return github_api.req( f'{settings.base_url}/repos/{repo_slug}/pulls', data=data, headers=headers, method='POST', )
def _fetch_reset(path: str, *, all_branches: bool) -> None: def _git(*cmd: str) -> None: subprocess.check_call(('git', '-C', path, *cmd)) try: branch = _default_branch(git.remote(path)) if all_branches: _git( 'config', 'remote.origin.fetch', '+refs/heads/*:refs/remotes/origin/*', ) else: _git('remote', 'set-branches', 'origin', branch) _git('fetch', 'origin') _git('checkout', branch) _git('reset', '--hard', f'origin/{branch}') except subprocess.CalledProcessError: # TODO: color / tty print(f'Error fetching {path}')
def push(settings: Settings, branch_name: str) -> None: auth = requests.auth.HTTPBasicAuth(settings.username, settings.api_key) remote_url = git.remote('.') _, _, repo_slug = remote_url.rpartition(':') if settings.fork: resp = requests.post( f'https://api.github.com/repos/{repo_slug}/forks', auth=auth, ) new_slug = resp.json()['full_name'] new_remote = remote_url.replace(repo_slug, new_slug) autofix_lib.run('git', 'remote', 'add', 'fork', new_remote) remote = 'fork' head = f'{settings.username}:{branch_name}' else: remote = 'origin' head = branch_name autofix_lib.run('git', 'push', remote, f'HEAD:{branch_name}', '--quiet') title = subprocess.check_output(('git', 'log', '-1', '--format=%s')) body = subprocess.check_output(('git', 'log', '-1', '--format=%b')) data = json.dumps({ 'title': title.decode().strip(), 'body': body.decode().strip(), 'base': 'master', 'head': head, }) resp = requests.post( f'https://api.github.com/repos/{repo_slug}/pulls', data=data, auth=auth, ) url = resp.json()['html_url'] print(f'Pull request created at {url}')
def push(settings: Settings, branch_name: str) -> None: autofix_lib.run('git', 'push', 'origin', f'HEAD:{branch_name}', '--quiet') title = subprocess.check_output(('git', 'log', '-1', '--format=%s')) body = subprocess.check_output(('git', 'log', '-1', '--format=%b')) data = json.dumps({ 'title': title.decode().strip(), 'body': body.decode().strip(), 'base': 'master', 'head': branch_name, }) repo_slug = git.remote('.').split(':')[-1] resp = requests.post( f'https://api.github.com/repos/{repo_slug}/pulls', data=data, auth=requests.auth.HTTPBasicAuth(settings.username, settings.api_key), ) url = resp.json()['html_url'] print(f'Pull request created at {url}')
def push(settings: Settings, branch_name: str) -> None: headers = { 'Private-Token': settings.api_key, 'Content-Type': 'application/json', } remote_url = git.remote('.') _, _, repo_slug = remote_url.rpartition(':') repo_slug = _strip_trailing_dot_git(repo_slug) repo_slug = urllib.parse.quote(repo_slug, safe='') if settings.fork: raise NotImplementedError('fork support not yet implemented') else: remote = 'origin' head = branch_name autofix_lib.run('git', 'push', remote, f'HEAD:{head}', '--quiet') title = subprocess.check_output(('git', 'log', '-1', '--format=%s')) body = subprocess.check_output(('git', 'log', '-1', '--format=%b')) data = json.dumps({ 'source_branch': head, 'target_branch': autofix_lib.target_branch(), 'title': title.decode().strip(), 'description': body.decode().strip(), }).encode() resp = gitlab_api.req( f'{settings.base_url}/projects/{repo_slug}/merge_requests', data=data, headers=headers, method='POST', ) url = resp.json['web_url'] print(f'Pull request created at {url}')