Skip to content

Commit

Permalink
ci: add a workflow to check if the branch's base commit is on right p…
Browse files Browse the repository at this point in the history
…lace
  • Loading branch information
matheuswhite committed May 10, 2024
1 parent 5bd62e1 commit 030e3ca
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
76 changes: 76 additions & 0 deletions .github/commit_checks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""
:file: commit_checks.py
:author: Paulo Santos ([email protected])
:brief: Realiza a verificação do histórico de commits.
:version: 0.1
:date: 29-04-2024
:copyright: Copyright (c) 2024
"""

import argparse
import subprocess
import sys


def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("-s", "--srcb", metavar="sbranch", help="Branch de origem da comparação.")
parser.add_argument("-t", "--tgtb", metavar="tbranch", help="Branch de destino da comparação.",
default="origin/main")

source_branch = str(parser.parse_args().srcb)
target_branch = str(parser.parse_args().tgtb)

if source_branch == "None":
print("\nBranch a ser verificada não fornecida.")
return -1

invocation = f"git rev-list --count --left-right {target_branch}...{source_branch}"

try:
out = subprocess.check_output(invocation, shell=True)
behind, ahead = [int(dist) for dist in out.decode("utf-8").rsplit("\t", maxsplit=1)]
except Exception as e:
print(f"\nErro ao verificar o histórico: {e}")
return -1

if behind > 0:
print(f"\n{behind} commits que precisam ser incorporados no PR por rebase à main.\n")

return 1

if ahead > COMMIT_LIMIT:
print(f"\n{ahead} commits a serem adicionados, divida o PR para que contenha menos que {COMMIT_LIMIT + 1}"
" commits.")

return 1

print(f"\nHistórico está atualizado. ({ahead} commits a frente)")

invocation = f"git describe --all --first-parent {source_branch}~1 --abbrev=0"

try:
out = subprocess.check_output(invocation, shell=True)
parent_head = out.decode("utf-8").split("/")[-1].strip()
except Exception as e:
print(f"\nErro ao verificar o histórico: {e}")

return -1

if parent_head != "main":
print(f"\nA branch tem origem em `{parent_head}`, resolva a `{parent_head}` primeiro. (A branch deve ter origem "
"na main)")

return 1

print("\nA branch tem origem na main.")

return 0


if __name__ == "__main__":
COMMIT_LIMIT = 15
check_status = main()

sys.exit(check_status)
20 changes: 20 additions & 0 deletions .github/workflows/commit-checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: commit-checks

on:
pull_request:
branches: [ "main" ]

jobs:
commit-check:
name: Check the branch base commit
runs-on: ubuntu-latest
steps:
- name: Checkout para a branch a ser testada.
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0

- name: Verifica os commits temporal e posicionalmente.
run: |
python ./.github/commit_checks.py -s origin/"${{github.head_ref}}"

0 comments on commit 030e3ca

Please sign in to comment.