def status(): cfg = workdir.read_config(WORKDIR) db = workdir.read_database(WORKDIR) gh = github.create_github_client(cfg.credentials.api_key) click.secho("Collecting data...") if len(db.repositories) == 0: error("No repositories in database.") return pr_missing = [] pr_merged = [] pr_open = [] pr_closed = [] # group repositories by PR state for repository in db.repositories: if repository.existing_pr is None: pr_missing.append(repository) continue pull_request = github.get_pull_request(gh, repository) if pull_request.merged: pr_merged.append(repository) elif pull_request.state == github.PullRequestState.OPEN.value: pr_open.append(repository) elif pull_request.state == github.PullRequestState.CLOSED.value: pr_closed.append(repository) total = len(db.repositories) _print_repository_list("Merged PRs", pr_merged, total) _print_repository_list("Closed PRs", pr_closed, total) _print_repository_list("Open PRs", pr_open, total) _print_repository_list("Missing PRs", pr_missing, total)
def run(pull_repos: bool, push_delay: Optional[float]): """Run update logic and create pull requests if changes made""" cfg = workdir.read_config(WORKDIR) db = workdir.read_database(WORKDIR) _ensure_set_up(cfg, db) gh = github.create_github_client(cfg.credentials.api_key) repositories = db.repositories_to_process() change_pushed = False for i, repository in enumerate(repositories, start=1): if change_pushed and push_delay is not None: click.secho(f"Sleeping for {push_delay} seconds...") time.sleep(push_delay) click.secho(f"[{i}/{len(repositories)}] Updating '{repository.name}'", bold=True) try: repo.reset_and_run_script(repository, db, cfg, WORKDIR, pull_repos) change_pushed = repo.push_changes(repository, db, cfg, gh, WORKDIR) except CliException as e: error(f"Error: {e}") click.secho(f"Done!", bold=True)
def pull(fetch_repo_list: bool, update_repos: bool, process_count: int): """Pull down repositories based on configuration""" cfg = workdir.read_config(WORKDIR) gh = github.create_github_client(cfg.credentials.api_key) user = github.get_user(gh) click.secho(f"Running under user '{user.name}' with email '{user.email}'") # get repositories db_old = workdir.read_database(WORKDIR) if fetch_repo_list: click.secho("Gathering repository list...") repositories = github.gather_repository_list(gh, cfg.repositories) # merge existing database with new one click.secho("Updating database") db_new = database.Database(user=user, repositories=repositories) db_old.merge_into(db_new) workdir.write_database(WORKDIR, db_old) else: click.secho("Not gathering repository list") repositories = db_old.repositories_to_process() # pull all repositories click.secho("Pulling repositories...") repo.pull_repositories_parallel( user, Path(cfg.credentials.ssh_key_file), WORKDIR.repos_dir, repositories, update_repos, process_count, )
def _set_all_pull_requests_state(state: github.PullRequestState): cfg = workdir.read_config(WORKDIR) db = workdir.read_database(WORKDIR) gh = github.create_github_client(cfg.credentials.api_key) for repository in db.repositories: if repository.existing_pr is not None: try: github.set_pull_request_state(gh, repository, state) click.secho( f"Updated {repository.name} pull request state to {state.value}" ) except ValueError as e: click.secho(f"{e}")
def test(pull_repos: bool): """Check what expected diff will be for command execution""" cfg = workdir.read_config(WORKDIR) db = workdir.read_database(WORKDIR) _ensure_set_up(cfg, db) for repository in db.repositories_to_process(): try: repo.reset_and_run_script(repository, db, cfg, WORKDIR, pull_repos) diff = repo.get_diff(WORKDIR.repos_dir, repository) click.secho(f"Diff for repository '{repository.name}':\n{diff}") except CliException as e: error(f"Error: {e}") if not click.confirm("Continue?"): return click.secho("\n")
def test_no_override_files(tmp_path): wd = workdir.WorkDir(Path(tmp_path)) test_key = Path(tmp_path) / "test_key" test_key.touch() result = run_cli( wd, ["init", "--api-key", "not-written", "--ssh-key-file", f"{test_key}"], cfg=simple_test_config(), db=simple_test_database(), ) cfg = workdir.read_config(wd) db = workdir.read_database(wd) assert cfg.credentials.api_key == "test" assert db.repositories[0].name == "test" assert "config file exists" in result.output assert "database file exists" in result.output
def reset(): """Mark all mapped repositories as not done""" db = workdir.read_database(WORKDIR) db.reset() workdir.write_database(WORKDIR, db) click.secho("Repositories marked as not done")