From 9f9bf19e6103c609c9d90e8dc3bfbc3026451ed3 Mon Sep 17 00:00:00 2001 From: gabriel-farache Date: Wed, 9 Oct 2024 16:34:56 +0200 Subject: [PATCH] Update CI to merge automated PR is one already exists for the workflow Signed-off-by: gabriel-farache --- .github/workflows/m2k-func.yaml | 37 +++++++------ .github/workflows/main.yml | 32 ++++------- scripts/create_automated_m2k_kfunc_pr.sh | 34 ++++++++++++ scripts/create_automated_pr.sh | 30 +++++++++++ scripts/create_or_update_automated_pr.sh | 67 ++++++++++++++++++++++++ 5 files changed, 160 insertions(+), 40 deletions(-) create mode 100644 scripts/create_automated_m2k_kfunc_pr.sh create mode 100644 scripts/create_automated_pr.sh create mode 100644 scripts/create_or_update_automated_pr.sh diff --git a/.github/workflows/m2k-func.yaml b/.github/workflows/m2k-func.yaml index a21d794f..8946784c 100644 --- a/.github/workflows/m2k-func.yaml +++ b/.github/workflows/m2k-func.yaml @@ -85,6 +85,13 @@ jobs: - name: Send PRs to config repo if: ${{ ! inputs.it_mode }} run: | + # determine pr or commit url + if [ ${{ fromJSON(steps.get_pr_data.outputs.result)}} != "" ]; then + PR_OR_COMMIT_URL="${{ fromJSON(steps.get_pr_data.outputs.result).html_url }}" + else + PR_OR_COMMIT_URL="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" + fi + git config --global user.email "${{ github.event.head_commit.author.email || 'parodos@redhat.com' }}" git config --global user.name ${{ github.event.head_commit.author.name || github.triggering_actor }} gh repo clone $WF_CONFIG_REPO config-repo @@ -94,23 +101,15 @@ jobs: ./hack/bump_chart_version.sh move2kube --bump-tag-version yq --inplace '.kfunction.image="quay.io/orchestrator/serverless-workflow-m2k-kfunc:${{ github.sha }}"' charts/move2kube/values.yaml + cd .. - git add -A - - # determine pr or commit url - if [ ${{ fromJSON(steps.get_pr_data.outputs.result)}} != "" ]; then - PR_OR_COMMIT_URL="${{ fromJSON(steps.get_pr_data.outputs.result).html_url }}" - else - PR_OR_COMMIT_URL="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" - fi - - git commit -m "(m2k-kfunc) Automated PR for m2k-kfunc" - echo "Automated PR from $PR_OR_COMMIT_URL" | git commit --amend --file=- - git remote set-url origin https://${{ env.GH_TOKEN }}@github.com/$WF_CONFIG_REPO - git push origin HEAD - gh pr create -f --title "m2k-kfunc: Automatic m2k-kfunc image tag bump" \ - --body " - Updating generated image tag for m2k-kfunc application - - This PR was created automatically as a result of merging $PR_OR_COMMIT_URL - " + sh scripts/create_or_update_automated_pr.sh $WF_CONFIG_REPO \ + ${{ inputs.workflow_id }} \ + ${PR_OR_COMMIT_URL} \ + config-repo/charts/move2kube/values.yaml \ + scripts/create_automated_m2k_kfunc_pr.sh \ + ${{ github.event.head_commit.author.email || 'parodos@redhat.com' }} \ + ${{ github.event.head_commit.author.name || github.triggering_actor }} \ + $WORKDIR \ + ${{ env.GH_TOKEN }} \ + $WF_CONFIG_REPO diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 16426f59..96d0b05a 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -112,17 +112,6 @@ jobs: if: ${{ ! inputs.it_mode }} run: | WORKDIR=${{ runner.temp }}/serverless-workflows - git config --global user.email "${{ github.event.head_commit.author.email || 'parodos@redhat.com' }}" - git config --global user.name ${{ github.event.head_commit.author.name || github.triggering_actor }} - gh repo clone $WF_CONFIG_REPO config-repo - cd config-repo - git switch -c ${{ inputs.workflow_id }}-autopr-$RANDOM - - ./hack/bump_chart_version.sh ${{ inputs.workflow_id }} --bump-tag-version - - mkdir -p charts/${{ inputs.workflow_id }}/templates - cp ${WORKDIR}/${{ inputs.workflow_id }}/manifests/* charts/${{ inputs.workflow_id }}/templates - git add -A # determine pr or commit url if [ ${{ fromJSON(steps.get_pr_data.outputs.result)}} != "" ]; then @@ -131,13 +120,14 @@ jobs: PR_OR_COMMIT_URL="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" fi - git commit -m "(${{ inputs.workflow_id }}) Automated PR" - echo "Automated PR from $PR_OR_COMMIT_URL" | git commit --amend --file=- - git remote set-url origin https://${{ env.GH_TOKEN }}@github.com/$WF_CONFIG_REPO - git push origin HEAD - gh pr create -f --title "${{ inputs.workflow_id }}: Automatic manifests generation" \ - --body " - Updating generated manifests for ${{ inputs.workflow_id }} workflow - - This PR was created automatically as a result of merging $PR_OR_COMMIT_URL - " + sh scripts/create_or_update_automated_pr.sh $WF_CONFIG_REPO \ + ${{ inputs.workflow_id }} \ + ${PR_OR_COMMIT_URL} \ + "" \ + scripts/create_automated_pr.sh \ + ${{ github.event.head_commit.author.email || 'parodos@redhat.com' }} \ + ${{ github.event.head_commit.author.name || github.triggering_actor }} \ + $WORKDIR \ + ${{ env.GH_TOKEN }} \ + $WF_CONFIG_REPO + diff --git a/scripts/create_automated_m2k_kfunc_pr.sh b/scripts/create_automated_m2k_kfunc_pr.sh new file mode 100644 index 00000000..3f742e64 --- /dev/null +++ b/scripts/create_automated_m2k_kfunc_pr.sh @@ -0,0 +1,34 @@ +#!/bin/bash +USER_EMAIL=$1 +USER_NAME=$2 +WORKDIR=$3 +WORKFLOW_ID=$4 +PR_OR_COMMIT_URL=$5 +GH_TOKEN=$6 +WF_CONFIG_REPO=$7 +INPUT_VALUES_FILEPATH=$8 + +git config --global user.email "${USER_EMAIL}" +git config --global user.name "${USER_NAME}" +gh repo clone "${WF_CONFIG_REPO}" config-repo +cd config-repo || exit +git switch -c "${WORKFLOW_ID}"-autopr-"${RANDOM}" + +./../hack/bump_chart_version.sh "${WORKFLOW_ID}" --bump-tag-version + +cp "${INPUT_VALUES_FILEPATH}" charts/move2kube/values.yaml + +mkdir -p charts/"${WORKFLOW_ID}"/templates +cp "${WORKDIR}"/"${WORKFLOW_ID}"/manifests/* charts/"${WORKFLOW_ID}"/templates +git add -A + +git commit -m "(m2k-kfunc) Automated PR" +echo "Automated PR from ${PR_OR_COMMIT_URL}" | git commit --amend --file=- +git remote set-url origin https://"${GH_TOKEN}"@github.com/"${WF_CONFIG_REPO}" +git push origin HEAD +gh pr create -f --title "m2k-kfunc: Automatic manifests generation" \ +--body " +Updating generated manifests for m2k-kfunc application + +This PR was created automatically as a result of merging ${PR_OR_COMMIT_URL} +" \ No newline at end of file diff --git a/scripts/create_automated_pr.sh b/scripts/create_automated_pr.sh new file mode 100644 index 00000000..00811746 --- /dev/null +++ b/scripts/create_automated_pr.sh @@ -0,0 +1,30 @@ +#!/bin/bash +USER_EMAIL=$1 +USER_NAME=$2 +WORKDIR=$3 +WORKFLOW_ID=$4 +PR_OR_COMMIT_URL=$5 +GH_TOKEN=$6 +WF_CONFIG_REPO=$7 + +git config --global user.email "${USER_EMAIL}" +git config --global user.name "${USER_NAME}" +gh repo clone "${WF_CONFIG_REPO}" config-repo +cd config-repo || exit +git switch -c "${WORKFLOW_ID}"-autopr-"${RANDOM}" + +./../hack/bump_chart_version.sh "${WORKFLOW_ID}" --bump-tag-version +mkdir -p charts/"${WORKFLOW_ID}"/templates +cp "${WORKDIR}"/"${WORKFLOW_ID}"/manifests/* charts/"${WORKFLOW_ID}"/templates +git add -A + +git commit -m "(${WORKFLOW_ID}) Automated PR" +echo "Automated PR from $PR_OR_COMMIT_URL" | git commit --amend --file=- +git remote set-url origin https://"${GH_TOKEN}"@github.com/"${WF_CONFIG_REPO}" +git push origin HEAD +gh pr create -f --title "${WORKFLOW_ID}: Automatic manifests generation" \ +--body " +Updating generated manifests for ${WORKFLOW_ID} workflow + +This PR was created automatically as a result of merging ${PR_OR_COMMIT_URL} +" \ No newline at end of file diff --git a/scripts/create_or_update_automated_pr.sh b/scripts/create_or_update_automated_pr.sh new file mode 100644 index 00000000..1904f02d --- /dev/null +++ b/scripts/create_or_update_automated_pr.sh @@ -0,0 +1,67 @@ +#!/bin/bash + +# Set repository (update with your repo) +REPO=$1 +WORKFLOW_ID=$2 +PR_OR_COMMIT_URL=$3 +INPUT_VALUES_FILEPATH=$4 + +CREATE_PR_SCRIPT=$5 +USER_EMAIL=$6 +USER_NAME=$7 +WORKDIR=$8 +GH_TOKEN=$9 +WF_CONFIG_REPO=${10} + +# Get all open PRs for the repository +prs=$(gh pr list --repo "${REPO}" --json number --jq '.[].number') + +# Loop through each PR and check for the version in Chart.yaml +for pr in $prs; do + echo "Checking PR #$pr..." + + # Fetch the list of changed files for the PR + files=$(gh pr diff "$pr" --name-only --repo "${REPO}") + + # Check if Chart.yaml is in the list of changed files + if echo "$files" | grep -q "charts/${WORKFLOW_ID}/Chart.yaml"; then + echo "Chart.yaml found in PR #${pr}" + + # Get the contents of the Chart.yaml file from the PR + chart_content=$(gh pr diff "$pr" --repo "${REPO}" -- "charts/${WORKFLOW_ID}/Chart.yaml") + + # Check if the content has a version entry + if echo "$chart_content" | grep -q "^version: .*"; then + # Get the PR details (branch) + pr_branch=$(gh pr view "$pr" --repo "$REPO" --json headRefName --jq '.headRefName') + + # Checkout the PR branch + git fetch origin "$pr_branch" + git checkout "$pr_branch" + + # Create the new file + cp "${WORKDIR}/${WORKFLOW_ID}/manifests/*" "charts/${WORKFLOW_ID}/templates" + if [ "${INPUT_VALUES_FILEPATH}" != "" ]; then + cp "${INPUT_VALUES_FILEPATH}" "charts/${WORKFLOW_ID}/values.yaml" + fi + git add -A + + git commit -m "(${WORKFLOW_ID}) Automated PR" + echo "Automated PR from $PR_OR_COMMIT_URL" | git commit --amend --file=- + + git push origin "$pr_branch" + + echo "Changes pushed to PR #$pr on branch $pr_branch" + + exit 0 + fi + fi +done + +sh "${CREATE_PR_SCRIPT}" "${USER_EMAIL}" \ + "${USER_NAME}" \ + "${WORKDIR}" \ + "${WORKFLOW_ID}" \ + "${PR_OR_COMMIT_URL}" \ + "${GH_TOKEN}" \ + "${WF_CONFIG_REPO}"