def check_fix(**kwargs: Any) -> None: autofix_lib.run( sys.executable, '-m', 'pre_commit', 'run', '--all-files', **kwargs, )
def apply_fix( *, ls_files_cmd: Sequence[str], sed_cmd: Sequence[str], ) -> None: filenames_b = zsplit(subprocess.check_output(ls_files_cmd)) filenames = [f.decode() for f in filenames_b] filenames = [f for f in filenames if tags_from_path(f) & {'file', 'text'}] autofix_lib.run(*sed_cmd, *filenames)
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 push(settings: Settings, branch_name: str) -> None: autofix_lib.run('git', 'checkout', 'master', '--quiet') autofix_lib.run('git', 'pull', '--quiet') autofix_lib.run( 'git', 'merge', '--no-edit', '--no-ff', branch_name, '--quiet', ) autofix_lib.run('git', 'push', 'origin', 'HEAD', '--quiet')
def push(settings: Settings, branch_name: str) -> None: autofix_lib.run('git', 'checkout', autofix_lib.target_branch(), '--quiet') autofix_lib.run('git', 'pull', '--quiet') ff_flag = '--ff-only' if settings.fast_forward else '--no-ff' autofix_lib.run( 'git', 'merge', '--no-edit', ff_flag, branch_name, '--quiet', ) autofix_lib.run('git', 'push', 'origin', 'HEAD', '--quiet')
def apply_fix() -> None: # Fix CI config path = Path("LICENSE") if not path.exists(): path_txt = Path("LICENSE.txt") if path_txt.exists(): licence = path_txt.read_text() path = path_txt else: licence = None else: licence = path.read_text() licence2 = new_licence(licence) if licence2 != licence: path.write_text(licence2) autofix_lib.run("git", "add", str(path))
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 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}')
def apply_fix() -> None: # Fix CI config path = Path("LICENSE.txt") if path.exists(): newpath = path.rename("LICENSE") autofix_lib.run("git", "rm", str(path)) autofix_lib.run("git", "add", str(newpath)) filenames_b = zsplit(subprocess.check_output(["git", "ls-files", "-z"])) filenames = [f.decode() for f in filenames_b] autofix_lib.run("sed", "-i", "s/LICENSE.txt/LICENSE/g", *filenames)
def apply_fix(*, ls_files_cmd, sed_cmd): filenames = zsplit(subprocess.check_output(ls_files_cmd)) filenames = [f.decode() for f in filenames] autofix_lib.run(*sed_cmd, *filenames)
def test_run(capfd): autofix_lib.run('echo', 'h"i') out, _ = capfd.readouterr() assert out == ('$ echo \'h"i\'\n' 'h"i\n')
def apply_fix() -> None: autofix_lib.run(sys.executable, '-m', 'pre_commit', 'autoupdate') # This may return nonzero for fixes, that's ok! check_fix(check=False)
def apply_fix() -> None: autofix_lib.run(sys.executable, '-m', 'setup_py_upgrade', '.') setup_cfg_fmt_cmd = (sys.executable, '-m', 'setup_cfg_fmt', 'setup.cfg') autofix_lib.run(*setup_cfg_fmt_cmd, check=False)
def apply_fix() -> None: autofix_lib.run(sys.executable, '-m', 'pre_commit', 'migrate-config')
def apply_fix() -> None: autofix_lib.run(sys.executable, '-m', 'setup_py_upgrade', '.') autofix_lib.run(sys.executable, '-m', 'setup_cfg_fmt', 'setup.cfg')
def _run_all_files(**kwargs): autofix_lib.run( sys.executable, '-m', 'pre_commit', 'run', '--all-files', **kwargs, )