From bd4a08a8191a8cf4692852e50bc786dea3c649a5 Mon Sep 17 00:00:00 2001 From: Eduardo Amaral Date: Tue, 6 May 2025 22:56:42 -0300 Subject: [PATCH 1/2] =?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 From 30b781ee20d08d3e25490b38ddc34dcf1d0e73aa Mon Sep 17 00:00:00 2001 From: Eduardo Amaral Date: Tue, 6 May 2025 23:28:11 -0300 Subject: [PATCH 2/2] =?UTF-8?q?cicd-valida=C3=A7=C3=A3o-branch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/card-validation.yaml | 6 +++--- scripts/validate_pr.py | 18 +++++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/.github/workflows/card-validation.yaml b/.github/workflows/card-validation.yaml index 071f57c..93c04d1 100644 --- a/.github/workflows/card-validation.yaml +++ b/.github/workflows/card-validation.yaml @@ -41,9 +41,9 @@ jobs: 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" + + "The branch name and PR title must follow the `ATOM-PJT-` pattern.\n" + + "- Ensure the branch name starts with `ATOM-PJT-`.\n" + + "- Ensure the PR title contains `ATOM-PJT-`.\n\n" + "Please adjust and try again. Thank you!" }); console.log(`Comment added: ${comment.html_url}`); diff --git a/scripts/validate_pr.py b/scripts/validate_pr.py index 823da4b..5e99282 100644 --- a/scripts/validate_pr.py +++ b/scripts/validate_pr.py @@ -2,26 +2,26 @@ def validate_branch_and_pr_title(branch, pr_title): """ - Validates if the branch name and PR title follow the 'TADocs-' pattern. + Validates if the branch name and PR title follow the 'ATOM-PJT-' 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. + bool: True if both the branch and the pull request follow the 'ATOM-PJT-' 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-'.") + # Verify if the branch starts with 'ATOM-PJT-' + if not branch.startswith("ATOM-PJT-"): + print(f"The branch '{branch}' does not start with the pattern 'ATOM-PJT-'.") 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-'.") + # Verify if the branch contains with 'ATOM-PJT-' + if "ATOM-PJT-" not in pr_title: + print(f"The PR title '{pr_title}' does not contain the pattern 'ATOM-PJT-'.") return False - print(f"Validation successful: The branch '{branch}' and the PR title '{pr_title}' follow the 'TADocs-' pattern.") + print(f"Validation successful: The branch '{branch}' and the PR title '{pr_title}' follow the 'ATOM-PJT-' pattern.") return True