diff --git a/.github/workflows/check-locks-on-owners-submission.yml b/.github/workflows/check-locks-on-owners-submission.yml index d6382800..a36c6699 100644 --- a/.github/workflows/check-locks-on-owners-submission.yml +++ b/.github/workflows/check-locks-on-owners-submission.yml @@ -38,39 +38,15 @@ jobs: with: python-version: ${{ steps.setup-python.outputs.python-version }} - - name: get files changed - id: get_files_changed - env: - BOT_TOKEN: ${{ secrets.BOT_TOKEN }} - PR_API_URL: ${{ github.event.pull_request._links.self.href }} - run: | - # get files in PR - ./ve1/bin/pr-artifact --api-url="${PR_API_URL}" \ - --get-files - - name: check if only an OWNERS file is pushed id: check_for_owners env: - PR_FILES: ${{ steps.get_files_changed.outputs.pr_files }} + BOT_TOKEN: ${{ secrets.BOT_TOKEN }} + PR_API_URL: ${{ github.event.pull_request._links.self.href }} run: | - # check if PR contains just one redhat/community OWNERS file - first_file=$(yq '.0' <<< "${PR_FILES}") - total_file_count=$(yq 'length' <<< ${PR_FILES}) - - if [ "${total_file_count}" == 1 ]; then - if [[ "${first_file}" == "charts/redhat/"*/*"/OWNERS" ]] || [[ "${first_file}" == "charts/community/"*/*"/OWNERS" ]] ; then - echo "An OWNERS file has been modified or added" - echo "merge_pr=true" | tee -a $GITHUB_OUTPUT - else - echo "The file in the PR is not a Red Hat or Community OWNERS file" - echo "merge_pr=false" | tee -a $GITHUB_OUTPUT - echo "msg=ERROR: PR does not include a redhat/community OWNERS file." >> $GITHUB_OUTPUT - fi - else - echo "The PR contains multiple files." - echo "msg=ERROR: PR contains multiple files." >> $GITHUB_OUTPUT - echo "merge_pr=false" | tee -a $GITHUB_OUTPUT - fi + ./ve1/bin/check-for-owners --api-url="${PR_API_URL}" \ + --allowed-category redhat \ + --allowed-category community # We know that only one file was modified at this point, and it seems # mergeable. Determine if that file was created or modified here. @@ -117,7 +93,7 @@ jobs: working-directory: "pr-branch" id: fact-check env: - PR_FILES: ${{ steps.get_files_changed.outputs.pr_files }} + PR_FILES: ${{ steps.check_for_owners.outputs.pr_files }} CHART_NAME: ${{ steps.gather-metadata.outputs.chart-name }} ORGANIZATION: ${{ steps.gather-metadata.outputs.organization }} run: | diff --git a/.github/workflows/mercury_bot.yml b/.github/workflows/mercury_bot.yml index 66a434db..8f1c54f0 100644 --- a/.github/workflows/mercury_bot.yml +++ b/.github/workflows/mercury_bot.yml @@ -33,39 +33,14 @@ jobs: with: python-version: ${{ steps.setup-python.outputs.python-version }} - - name: get files changed - id: get_files_changed - env: - BOT_TOKEN: ${{ secrets.BOT_TOKEN }} - PR_API_URL: ${{ github.event.pull_request._links.self.href }} - run: | - # get files in PR - ./ve1/bin/pr-artifact --api-url="${PR_API_URL}" \ - --get-files - - name: check if only an OWNERS file is pushed id: check_for_owners env: - PR_FILES: ${{ steps.get_files_changed.outputs.pr_files }} + BOT_TOKEN: ${{ secrets.BOT_TOKEN }} + PR_API_URL: ${{ github.event.pull_request._links.self.href }} run: | - # check if PR contains just one partner OWNERS file - first_file=$(yq '.0' <<< "${PR_FILES}") - total_file_count=$(yq 'length' <<< ${PR_FILES}) - - if [ "${total_file_count}" == 1 ]; then - if [[ "${first_file}" == "charts/partners/"*/*"/OWNERS" ]] ; then - echo "An OWNERS file has been modified or added" - echo "merge_pr=true" | tee -a $GITHUB_OUTPUT - else - echo "The file in the PR is not a partner OWNERS file" - echo "merge_pr=false" | tee -a $GITHUB_OUTPUT - echo "msg=ERROR: PR does not include a partner OWNERS file." >> $GITHUB_OUTPUT - fi - else - echo "The PR contains multiple files." - echo "msg=ERROR: PR contains multiple files." >> $GITHUB_OUTPUT - echo "merge_pr=false" | tee -a $GITHUB_OUTPUT - fi + ./ve1/bin/check-for-owners --api-url="${PR_API_URL}" \ + --allowed-category partners # We know that only one file was modified at this point, and it seems # mergeable. Determine if that file was created or modified here. @@ -115,7 +90,7 @@ jobs: working-directory: "pr-branch" id: fact-check env: - PR_FILES: ${{ steps.get_files_changed.outputs.pr_files }} + PR_FILES: ${{ steps.check_for_owners.outputs.pr_files }} CHART_NAME: ${{ steps.gather-metadata.outputs.chart-name }} ORGANIZATION: ${{ steps.gather-metadata.outputs.organization }} run: | diff --git a/scripts/setup.cfg b/scripts/setup.cfg index 22be6cf0..d18100a1 100644 --- a/scripts/setup.cfg +++ b/scripts/setup.cfg @@ -50,3 +50,4 @@ console_scripts = generate-chart-locks=packagemapping.generatelocks:main extract-metadata-from-pr=pullrequest.metadata:main assert-redhat-owners-file-meta=owners.redhat_metadata:main + check-for-owners=pullrequest.check_for_owners:main diff --git a/scripts/src/pullrequest/check_for_owners.py b/scripts/src/pullrequest/check_for_owners.py new file mode 100644 index 00000000..bd1de272 --- /dev/null +++ b/scripts/src/pullrequest/check_for_owners.py @@ -0,0 +1,70 @@ +import argparse +import sys + +from submission import submission +from tools import gitutils + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument( + "-u", + "--api-url", + dest="api_url", + type=str, + required=True, + help="API URL for the pull request", + ) + parser.add_argument( + "-c", + "--allowed-category", + dest="categories", + required=True, + help="Allowed category for this chart (community, partners, or redhat). Can be specified multiple times", + action="append", + choices=["community", "partners", "redhat"], + ) + args = parser.parse_args() + + # Initiate submission and pull modified files + s = submission.Submission(args.api_url) + + gitutils.add_output("pr_files", s.modified_files) + + # Fail early if there isn't exactly one modified file + if not s.modified_files: + print("The PR doesn't contain any files") + gitutils.add_output("merge_pr", "false") + gitutils.add_output("msg", "ERROR: PR doesn't contain any files") + sys.exit(10) + + if len(s.modified_files) > 1: + print("The PR contains multiple files.") + gitutils.add_output("merge_pr", "false") + gitutils.add_output("msg", "ERROR: PR contains multiple files.") + sys.exit(20) + + # Parse the modified file to classify the file + try: + s.parse_modified_files() + except submission.SubmissionError: + # Errors that may occur while parsing the files are irrelevant in this case + pass + + if len(s.modified_owners) == 1 and s.chart.category in args.categories: + print("An OWNERS file has been modified or added") + gitutils.add_output("merge_pr", "true") + else: + print( + f"The file in the PR is not a {'/'.join(x for x in args.categories)} OWNERS file" + ) + gitutils.add_output("merge_pr", "false") + gitutils.add_output( + "msg", + f"ERROR: PR does not include a {'/'.join(x for x in args.categories)} OWNERS file.", + ) + sys.exit(30) + + +if __name__ == "__main__": + main()