From 79324fc7a37321039c12a5c8047fae7a3ac96902 Mon Sep 17 00:00:00 2001 From: shilongliu Date: Wed, 21 Sep 2022 16:58:34 +0800 Subject: [PATCH] [actions] Add github actions to auto cherry-pick prs to release branches --- .github/workflows/pr_cherrypick_poststep.yml | 50 ++++++++ .github/workflows/pr_cherrypick_prestep.yml | 119 +++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 .github/workflows/pr_cherrypick_poststep.yml create mode 100644 .github/workflows/pr_cherrypick_prestep.yml diff --git a/.github/workflows/pr_cherrypick_poststep.yml b/.github/workflows/pr_cherrypick_poststep.yml new file mode 100644 index 000000000000..29f7cb102e63 --- /dev/null +++ b/.github/workflows/pr_cherrypick_poststep.yml @@ -0,0 +1,50 @@ +name: PostCherryPick +on: + pull_request_target: + types: + - closed + branches: + - 202205 + - 202111 + - 202106 + - 202012 + - 201911 + - 201811 + +jobs: + post_cherry_pick: + if: github.event.pull_request.merged == true && contains(github.event.pull_request.labels.*.name, 'automerge') + runs-on: ubuntu-latest + steps: + - name: Debug + env: + GITHUB_CONTEXT: ${{ toJson(github) }} + run: echo $GITHUB_CONTEXT | jq + - name: Checkout + uses: actions/checkout@v3 + with: + persist-credentials: false + - name: Main + env: + GITHUB_CONTEXT: ${{ toJson(github) }} + TOKEN: ${{ secrets.TOKEN }} + run: | + set -e + pr_url=$(echo $GITHUB_CONTEXT | jq ".event.pull_request._links.html.href" | sed 's/"//g') + base_ref=$(echo $GITHUB_CONTEXT | jq ".base_ref" | sed 's/"//g') + echo ${TOKEN} | gh auth login --with-token + comment=$(gh pr view $pr_url --json comments | jq -r '.comments | [.[] | select(.author.login=="mssonicbld")] | first | .body') + origin_pr_url=$(echo $comment | sed 's/Original PR: //') + echo ============================= + echo pr_url: $pr_url + echo base_ref: $base_ref + echo comment: $comment + echo origin_pr_url: $origin_pr_url + echo ============================= + # Add label + if [[ "$origin_pr_url" == "" ]];then + echo No comment from mssonicbld + exit 0 + fi + gh pr edit $origin_pr_url --add-label "Included in ${base_ref} Branch" + gh pr edit $origin_pr_url --remove-label "Created PR to ${base_ref} Branch" diff --git a/.github/workflows/pr_cherrypick_prestep.yml b/.github/workflows/pr_cherrypick_prestep.yml new file mode 100644 index 000000000000..a2d54881b2ff --- /dev/null +++ b/.github/workflows/pr_cherrypick_prestep.yml @@ -0,0 +1,119 @@ +name: PreCherryPick +on: + pull_request_target: + types: + - labeled + - closed + branches: + - master + +jobs: + pre_cherry_pick: + if: github.event.pull_request.merged == true && (github.event.action == "closed" || contains(github.event.label.name, "Approved for ${{ matrix.branch }} Branch") ) + runs-on: ubuntu-latest + strategy: + matrix: + branch: [202205,202111,202106,202012,201911,201811] + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + persist-credentials: false + - name: Debug + env: + GITHUB_CONTEXT: ${{ toJson(github) }} + run: echo $GITHUB_CONTEXT | jq + - name: Main + env: + GITHUB_CONTEXT: ${{ toJson(github) }} + TOKEN: ${{ secrets.TOKEN }} + run: | + set -e + + sha=$(echo $GITHUB_CONTEXT | jq -r ".event.pull_request.merge_commit_sha") + pr_id=$(echo $GITHUB_CONTEXT | jq -r ".event.number") + pr_url=$(echo $GITHUB_CONTEXT | jq -r ".event.pull_request._links.html.href") + repository=$(echo $GITHUB_CONTEXT | jq -r ".repository") + labels_count=$(echo $GITHUB_CONTEXT | jq ".event.pull_request.labels | length") + labels_json=$(echo $GITHUB_CONTEXT | jq ".event.pull_request.labels") + echo ============================= + echo SHA: $sha + echo PRID: $pr_id + echo pr_url: $pr_url + echo repository: $repository + echo labels_count: $labels_count + echo labels_json: $labels_json + echo ${TOKEN} | gh auth login --with-token + echo ============================= + if [[ "$(echo $GITHUB_CONTEXT | jq -r '.event.action')" == "labeled" ]] && \ + [[ "$(echo $GITHUB_CONTEXT | jq -r '.event.label.name')" != "Approved for ${{ matrix.branch }} Branch" ]];then + echo "Newly added label didn't match." + exit 0 + fi + create_pr='' + for (( i=0; i<$labels_count; i++ )) + do + label=$(echo $labels_json | jq -r ".[$i].name") + echo label ${i}: $label + if [[ "$label" == "Approved for ${{ matrix.branch }} Branch" ]];then + create_pr=1 + fi + if [[ "$label" == "Created PR to ${{ matrix.branch }} Branch" ]];then + echo "already has tag: Created PR to ${{ matrix.branch }} Branch, exit" + exit 0 + fi + if [[ "$label" == "Included in ${{ matrix.branch }} Branch" ]];then + echo "already has tag: Included in ${{ matrix.branch }} Branch, exit" + exit 0 + fi + done + if [[ "$create_pr" != "1" ]];then + echo "Didn't find 'Approved for ${{ matrix.branch }} Branch' tag." + exit 0 + fi + + # Begin to cherry-pick PR + git checkout -b ${{ matrix.branch }} --track origin/${{ matrix.branch }} + git checkout . + git clean -xdff + + git config user.name mssonicbld + git config user.email sonicbuildadmin@microsoft.com + + git config credential.https://github.com.username mssonicbld + git remote add mssonicbld https://mssonicbld:${TOKEN}@github.com/mssonicbld/sonic-buildimage + git fetch mssonicbld + git remote -vv + + if ! git cherry-pick $sha;then + echo 'cherry-pick failed.' + # Add label + gh pr edit $pr_url --add-label "Cherry Pick Conflict_${{ matrix.branch }}" + echo 'Add label "Cherry Pick Conflict_${{ matrix.branch }}" success' + author=$(gh pr view $pr_url --json author | jq '.author.login' | sed 's/"//g') + gh pr comment $pr_url --body "@${author} PR conflicts with ${{ matrix.branch }} branch" + echo 'Add commnet "@${author} PR conflicts with ${{ matrix.branch }} branch"' + else + # Create PR to release branch + git push mssonicbld HEAD:${{ matrix.branch }}-${pr_id} -f + result=$(gh pr create -R ${repository} -H mssonicbld:${{ matrix.branch }}-${pr_id} -B ${{ matrix.branch }} -t "[action] Auto cherry-pick PR:#${pr_id} into ${{ matrix.branch }} branch" -b '') + if [[ "$(echo $result | grep github.com)" == "" ]];then echo $result; exit 1;fi + + new_pr_rul=$(echo $result | grep github.com) + echo new_pr_rul: $new_pr_rul + + # Add label to old PR + gh pr edit $pr_url --add-label "Created PR to ${{ matrix.branch }} Branch" + echo Add label Created PR to ${{ matrix.branch }} Branch + # Add comment to old PR + gh pr comment $pr_url --body "Cherry-pick PR to ${{ matrix.branch }}: ${new_pr_rul}" + echo Add comment to old PR + + # Add label to new PR + gh pr edit $new_pr_rul --add-label "automerge" + echo Add label Created PR to ${{ matrix.branch }} Branch + # Add comment to new PR + gh pr comment $new_pr_rul --body "Original PR: ${pr_url}" + echo Add comment to new PR + fi