From e0f650a46eac674dc7859191bd5a2c628dacc0fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Brunner?= Date: Tue, 1 Mar 2022 10:20:07 +0100 Subject: [PATCH] Fix main branch detection --- .github/workflows/audit.yaml | 2 +- .github/workflows/main.yaml | 2 +- c2cciutils/__init__.py | 95 ++++++++++++-------- c2cciutils/audit.py | 2 +- c2cciutils/publish.py | 7 +- c2cciutils/scripts/audit.py | 2 +- c2cciutils/scripts/checks.py | 3 +- example-project/.github/workflows/audit.yaml | 2 +- 8 files changed, 69 insertions(+), 46 deletions(-) diff --git a/.github/workflows/audit.yaml b/.github/workflows/audit.yaml index e6a849568..3a1b94e30 100644 --- a/.github/workflows/audit.yaml +++ b/.github/workflows/audit.yaml @@ -33,6 +33,6 @@ jobs: - run: python3 -m pip install --user --force-reinstall dist/*.whl - name: Audit - run: c2cciutils-audit --branch=${{ matrix.branch }} + run: c2cciutils-audit env: GITHUB_TOKEN: ${{ github.token }} diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 238414ef5..4cb3afe8e 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -118,6 +118,6 @@ jobs: - run: python3 -m pip install --user --force-reinstall dist/*.whl - name: Audit - run: c2cciutils-audit --branch=master + run: c2cciutils-audit env: GITHUB_TOKEN: ${{ github.token }} diff --git a/c2cciutils/__init__.py b/c2cciutils/__init__.py index 8ab9221da..d6309f605 100644 --- a/c2cciutils/__init__.py +++ b/c2cciutils/__init__.py @@ -76,7 +76,7 @@ def get_master_branch(repo: List[str]) -> Tuple[str, bool]: return master_branch, success -def get_config() -> c2cciutils.configuration.Configuration: +def get_config(branch: Optional[str] = None) -> c2cciutils.configuration.Configuration: """ Get the configuration, with project and autodetections. """ @@ -142,7 +142,7 @@ def get_config() -> c2cciutils.configuration.Configuration: config, ) - based_on_master = get_based_on_master(repo, master_branch, config) if credentials else False + based_on_master = get_based_on_master(repo, branch, master_branch, config) if credentials else False has_docker_files = bool( subprocess.run( ["git", "ls-files", "*/Dockerfile*", "Dockerfile*"], stdout=subprocess.PIPE, check=True @@ -302,7 +302,7 @@ def get_config() -> c2cciutils.configuration.Configuration: { "clean.yaml": {"steps": [{"run_re": "c2cciutils-clean$"}]}, "audit.yaml": { - "steps": [{"run_re": "c2cciutils-audit --branch=.*$", "env": ["GITHUB_TOKEN"]}], + "steps": [{"run_re": "c2cciutils-audit.*$", "env": ["GITHUB_TOKEN"]}], "strategy-fail-fast": False, }, }, @@ -651,8 +651,28 @@ def get_git_files_mime( return result +def get_branch(branch: Optional[str]) -> str: + """ + Get the branch name. + + Arguments: + branch: The forced to use branch name + + Return the branch name + """ + + return branch or ( + subprocess.run(["git", "rev-parse", "--abbrev-ref", "HEAD"], check=True, stdout=subprocess.PIPE) + .stdout.decode() + .strip() + ) + + def get_based_on_master( - repo: List[str], master_branch: str, config: c2cciutils.configuration.Configuration + repo: List[str], + override_current_branch: Optional[str], + master_branch: str, + config: c2cciutils.configuration.Configuration, ) -> bool: """ Check that we are not on a release branch (to avoid errors in versions check). @@ -662,45 +682,42 @@ def get_based_on_master( Arguments: repo: The repository [, ] + override_current_branch: The branch to use instead of the current one master_branch: The master branch name config: The full configuration """ if os.environ.get("GITHUB_REF", "").startswith("refs/tags/"): # The tags are never consider as based on master return False - if os.environ.get("GITHUB_REF", "").startswith("refs/heads/"): - current_branch = os.environ["GITHUB_REF"][len("refs/heads/") :] - if current_branch == master_branch: - return True - branches_re = compile_re(config["version"].get("branch_to_version_re", []), "refs/heads/") - if match(current_branch, branches_re): - return False - commits_json = graphql( - "commits.graphql", {"name": repo[1], "owner": repo[0], "branch": current_branch} - )["repository"]["ref"]["target"]["history"]["nodes"] - branches_json = [ - branch - for branch in ( - graphql("branches.graphql", {"name": repo[1], "owner": repo[0]})["repository"]["refs"][ - "nodes" - ] - ) - if branch["name"] != current_branch and match(branch["name"], branches_re) - ] - based_branch = master_branch - found = False - for commit in commits_json: - for branch in branches_json: - commits = [ - branch_commit - for branch_commit in branch["target"]["history"]["nodes"] - if commit["oid"] == branch_commit["oid"] - ] - if commits: - based_branch = branch["name"] - found = True - break - if found: + current_branch = get_branch(override_current_branch) + if current_branch == master_branch: + return True + branches_re = compile_re(config["version"].get("branch_to_version_re", [])) + if match(current_branch, branches_re): + return False + commits_json = graphql("commits.graphql", {"name": repo[1], "owner": repo[0], "branch": current_branch})[ + "repository" + ]["ref"]["target"]["history"]["nodes"] + branches_json = [ + branch + for branch in ( + graphql("branches.graphql", {"name": repo[1], "owner": repo[0]})["repository"]["refs"]["nodes"] + ) + if branch["name"] != current_branch and match(branch["name"], branches_re) + ] + based_branch = master_branch + found = False + for commit in commits_json: + for branch in branches_json: + commits = [ + branch_commit + for branch_commit in branch["target"]["history"]["nodes"] + if commit["oid"] == branch_commit["oid"] + ] + if commits: + based_branch = branch["name"] + found = True break - return based_branch == master_branch - return True + if found: + break + return based_branch == master_branch diff --git a/c2cciutils/audit.py b/c2cciutils/audit.py index 4422fb9cd..3fcefc76b 100644 --- a/c2cciutils/audit.py +++ b/c2cciutils/audit.py @@ -430,7 +430,7 @@ def outdated_versions( if "errors" in json_response: raise RuntimeError(json.dumps(json_response["errors"], indent=2)) - if json_response["repository"]["defaultBranchRef"]["name"] != args.branch: + if json_response["repository"]["defaultBranchRef"]["name"] != c2cciutils.get_branch(args.branch): return True success = True diff --git a/c2cciutils/publish.py b/c2cciutils/publish.py index 2a3a5bcc0..ca08c8330 100644 --- a/c2cciutils/publish.py +++ b/c2cciutils/publish.py @@ -329,7 +329,12 @@ def docker( ) if latest: subprocess.run( - ["docker", "tag", f"{image_config['name']}:{tag_src}", f"{config['server']}/{image_config['name']}:{tag_src}"], + [ + "docker", + "tag", + f"{image_config['name']}:{tag_src}", + f"{config['server']}/{image_config['name']}:{tag_src}", + ], check=True, ) subprocess.run( diff --git a/c2cciutils/scripts/audit.py b/c2cciutils/scripts/audit.py index 8c2a11c05..266fc3449 100644 --- a/c2cciutils/scripts/audit.py +++ b/c2cciutils/scripts/audit.py @@ -15,7 +15,7 @@ def main() -> None: Run the audit. """ parser = argparse.ArgumentParser(description="Run the audit of c2cciutils.") - parser.add_argument("--branch", required=True, help="The audited branch") + parser.add_argument("--branch", help="The branch to audit, not defined means autodetect") args = parser.parse_args() diff --git a/c2cciutils/scripts/checks.py b/c2cciutils/scripts/checks.py index 127f18f8e..17fc00361 100644 --- a/c2cciutils/scripts/checks.py +++ b/c2cciutils/scripts/checks.py @@ -18,10 +18,11 @@ def main() -> None: parser.add_argument("--fix", action="store_true", help="fix black and isort issues") parser.add_argument("--stop", action="store_true", help="stop on first error") parser.add_argument("--check", help="runs only the specified check") + parser.add_argument("--branch", help="The branch to check, not defined means autodetect") args = parser.parse_args() - full_config = c2cciutils.get_config() + full_config = c2cciutils.get_config(args.branch) config = full_config.get("checks", {}) success = True for key, conf in config.items(): diff --git a/example-project/.github/workflows/audit.yaml b/example-project/.github/workflows/audit.yaml index 82816cbb6..5c203ac6f 100644 --- a/example-project/.github/workflows/audit.yaml +++ b/example-project/.github/workflows/audit.yaml @@ -32,6 +32,6 @@ jobs: - run: python3 -m pip install --user --requirement=ci/requirements.txt - name: Audit - run: c2cciutils-audit --branch=${{ matrix.branch }} + run: c2cciutils-audit env: GITHUB_TOKEN: ${{ github.token }}