From bd4a08a8191a8cf4692852e50bc786dea3c649a5 Mon Sep 17 00:00:00 2001 From: Eduardo Amaral Date: Tue, 6 May 2025 22:56:42 -0300 Subject: [PATCH] =?UTF-8?q?verifica=C3=A7=C3=A3o/cicd?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/scripts/validate_pr.py | 41 +++++++++++++++++++++ .github/workflows/card-validation.yaml | 51 ++++++++++++++++++++++++++ scripts/validate_pr.py | 41 +++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 .github/scripts/validate_pr.py create mode 100644 .github/workflows/card-validation.yaml create mode 100644 scripts/validate_pr.py diff --git a/.github/scripts/validate_pr.py b/.github/scripts/validate_pr.py new file mode 100644 index 0000000..823da4b --- /dev/null +++ b/.github/scripts/validate_pr.py @@ -0,0 +1,41 @@ +import os + +def validate_branch_and_pr_title(branch, pr_title): + """ + Validates if the branch name and PR title follow the 'TADocs-' pattern. + Args: + branch (str): The name of the branch to validate. + pr_title (str): The title of the pull request (PR) to validate. + Returns: + bool: True if both the branch and the pull request follow the 'TADocs-' pattern, False otherwise. + Prints: + str: A message indicating whether the branch and PR title are valid or specifying the validation error. + """ + + # Verify if the branch starts with 'TADocs-' + if not branch.startswith("TADocs-"): + print(f"The branch '{branch}' does not start with the pattern 'TADocs-'.") + return False + # Verify if the branch contains with 'TADocs-' + if "TADocs-" not in pr_title: + print(f"The PR title '{pr_title}' does not contain the pattern 'TADocs-'.") + return False + + print(f"Validation successful: The branch '{branch}' and the PR title '{pr_title}' follow the 'TADocs-' pattern.") + + return True + +if __name__ == "__main__": + PR_BRANCH = os.getenv("GITHUB_HEAD_REF") + PR_TITLE = os.getenv("PR_TITLE") + + if not PR_BRANCH: + raise ValueError("The environment variable GITHUB_HEAD_REF is not defined.") + if not PR_TITLE: + raise ValueError("The environment variable PR_TITLE is not defined.") + + print(f"Captured PR branch: {PR_BRANCH}") + print(f"Captured PR title: {PR_TITLE}") + + if not validate_branch_and_pr_title(PR_BRANCH, PR_TITLE): + exit(1) \ No newline at end of file diff --git a/.github/workflows/card-validation.yaml b/.github/workflows/card-validation.yaml new file mode 100644 index 0000000..071f57c --- /dev/null +++ b/.github/workflows/card-validation.yaml @@ -0,0 +1,51 @@ +name: Validate PR with Ticket +on: + pull_request: + types: [opened, reopened, synchronize, edited] +jobs: + validate-pr: + runs-on: ubuntu-latest + + permissions: + pull-requests: write + issues: write + contents: read + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.x' + + - name: Validate PR ticket link + id: validate_pr + env: + GITHUB_HEAD_REF: ${{ github.head_ref }} # Branch name + PR_TITLE: ${{ github.event.pull_request.title }} # PR Title + run: | + if ! python scripts/validate_pr.py; then + echo "validation_failed=true" >> $GITHUB_OUTPUT + fi + + - name: Notify user and fail job if validation fails + if: steps.validate_pr.outputs.validation_failed == 'true' + uses: actions/github-script@v6 + with: + script: | + // Add a comment to the PR + const { data: comment } = await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number, + body: "**Validation Failed!** \n\n" + + "The branch name and PR title must follow the `TADocs-` pattern.\n" + + "- Ensure the branch name starts with `TADocs-`.\n" + + "- Ensure the PR title contains `TADocs-`.\n\n" + + "Please adjust and try again. Thank you!" + }); + console.log(`Comment added: ${comment.html_url}`); + // Fail the job manually + throw new Error("Validation failed. Check the comment on the PR for more details."); \ No newline at end of file diff --git a/scripts/validate_pr.py b/scripts/validate_pr.py new file mode 100644 index 0000000..823da4b --- /dev/null +++ b/scripts/validate_pr.py @@ -0,0 +1,41 @@ +import os + +def validate_branch_and_pr_title(branch, pr_title): + """ + Validates if the branch name and PR title follow the 'TADocs-' pattern. + Args: + branch (str): The name of the branch to validate. + pr_title (str): The title of the pull request (PR) to validate. + Returns: + bool: True if both the branch and the pull request follow the 'TADocs-' pattern, False otherwise. + Prints: + str: A message indicating whether the branch and PR title are valid or specifying the validation error. + """ + + # Verify if the branch starts with 'TADocs-' + if not branch.startswith("TADocs-"): + print(f"The branch '{branch}' does not start with the pattern 'TADocs-'.") + return False + # Verify if the branch contains with 'TADocs-' + if "TADocs-" not in pr_title: + print(f"The PR title '{pr_title}' does not contain the pattern 'TADocs-'.") + return False + + print(f"Validation successful: The branch '{branch}' and the PR title '{pr_title}' follow the 'TADocs-' pattern.") + + return True + +if __name__ == "__main__": + PR_BRANCH = os.getenv("GITHUB_HEAD_REF") + PR_TITLE = os.getenv("PR_TITLE") + + if not PR_BRANCH: + raise ValueError("The environment variable GITHUB_HEAD_REF is not defined.") + if not PR_TITLE: + raise ValueError("The environment variable PR_TITLE is not defined.") + + print(f"Captured PR branch: {PR_BRANCH}") + print(f"Captured PR title: {PR_TITLE}") + + if not validate_branch_and_pr_title(PR_BRANCH, PR_TITLE): + exit(1) \ No newline at end of file