|
| 1 | +name: PreCherryPick |
| 2 | +on: |
| 3 | + pull_request_target: |
| 4 | + types: |
| 5 | + - labeled |
| 6 | + - closed |
| 7 | + branches: |
| 8 | + - master-test |
| 9 | + |
| 10 | +jobs: |
| 11 | + pre_cherry_pick: |
| 12 | + if: github.event.pull_request.merged == true && ( (github.event.action == 'closed' && contains(join(github.event.pull_request.labels.*.name, ','), 'Approved for 20')) || (github.event.action == 'labeled' && startsWith(github.event.label.name, 'Approved for 20')) ) |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - name: Checkout |
| 16 | + uses: actions/checkout@v3 |
| 17 | + with: |
| 18 | + fetch-depth: 0 |
| 19 | + persist-credentials: false |
| 20 | + - name: Debug |
| 21 | + env: |
| 22 | + GITHUB_CONTEXT: ${{ toJson(github) }} |
| 23 | + run: echo $GITHUB_CONTEXT | jq |
| 24 | + - name: Main |
| 25 | + env: |
| 26 | + GITHUB_CONTEXT: ${{ toJson(github) }} |
| 27 | + TOKEN: ${{ secrets.TOKEN }} |
| 28 | + run: | |
| 29 | + set -e |
| 30 | +
|
| 31 | + sha=$(echo $GITHUB_CONTEXT | jq -r ".event.pull_request.merge_commit_sha") |
| 32 | + pr_id=$(echo $GITHUB_CONTEXT | jq -r ".event.number") |
| 33 | + pr_url=$(echo $GITHUB_CONTEXT | jq -r ".event.pull_request._links.html.href") |
| 34 | + repository=$(echo $GITHUB_CONTEXT | jq -r ".repository") |
| 35 | + labels=$(echo $GITHUB_CONTEXT | jq -r ".event.pull_request.labels[].name") |
| 36 | + author=$(echo $GITHUB_CONTEXT | jq -r ".event.pull_request.base.user.login") |
| 37 | + branches=$(git branch -a --list 'origin/20????' | awk -F/ '{print$3}' | grep -E "202[0-9]{3}") |
| 38 | + if [[ $(echo $GITHUB_CONTEXT | jq -r ".event.action") == "labeled" ]];then |
| 39 | + labels=$(echo $GITHUB_CONTEXT | jq -r ".event.label.name") |
| 40 | + fi |
| 41 | + title=$(echo $GITHUB_CONTEXT | jq -r ".event.pull_request.title") |
| 42 | + echo ============================= |
| 43 | + echo SHA: $sha |
| 44 | + echo PRID: $pr_id |
| 45 | + echo pr_url: $pr_url |
| 46 | + echo repository: $repository |
| 47 | + echo branches: $branches |
| 48 | + echo labels: |
| 49 | + echo "$labels" |
| 50 | + echo ${TOKEN} | gh auth login --with-token |
| 51 | + echo author: $author |
| 52 | + echo title: $title |
| 53 | + echo ============================= |
| 54 | +
|
| 55 | + git config user.name mssonicbld |
| 56 | + git config user.email sonicbld@microsoft.com |
| 57 | + git config credential.https://github.com.username mssonicbld |
| 58 | + git remote add mssonicbld https://mssonicbld:${TOKEN}@github.com/mssonicbld/sonic-buildimage |
| 59 | + git fetch mssonicbld |
| 60 | + git remote -vv |
| 61 | +
|
| 62 | + cherry_pick(){ |
| 63 | + set -e |
| 64 | + local create_pr='' |
| 65 | + while read label |
| 66 | + do |
| 67 | + echo label: $label |
| 68 | + if [[ "$label" == "Approved for $branch Branch" ]];then |
| 69 | + create_pr=1 |
| 70 | + fi |
| 71 | + if [[ "$label" == "Created PR to $branch Branch" ]];then |
| 72 | + echo "already has tag: Created PR to $branch Branch, return" |
| 73 | + return 0 |
| 74 | + fi |
| 75 | + if [[ "$label" == "Included in $branch Branch" ]];then |
| 76 | + echo "already has tag: Included in $branch Branch, return" |
| 77 | + return 0 |
| 78 | + fi |
| 79 | + if [[ "$label" == "Cherry Pick Conflict_$branch" ]];then |
| 80 | + echo "already has tag: Cherry Pick Conflict_$branch, return" |
| 81 | + return 0 |
| 82 | + fi |
| 83 | + done <<< "$labels" |
| 84 | +
|
| 85 | + if [[ "$create_pr" != "1" ]];then |
| 86 | + echo "Didn't find 'Approved for $branch Branch' tag." |
| 87 | + return 0 |
| 88 | + fi |
| 89 | + # Begin to cherry-pick PR |
| 90 | + git cherry-pick --abort 2>/dev/null || true |
| 91 | + git clean -xdff 2>/dev/null || true |
| 92 | + git reset HEAD --hard || true |
| 93 | + git checkout -b $branch --track origin/$branch |
| 94 | + git status | grep "working tree clean" |
| 95 | +
|
| 96 | + if ! git cherry-pick $sha;then |
| 97 | + echo 'cherry-pick failed.' |
| 98 | + git cherry-pick --abort |
| 99 | + git status | grep "working tree clean" |
| 100 | + # Add label |
| 101 | + gh pr edit $pr_url --add-label "Cherry Pick Conflict_$branch" |
| 102 | + echo 'Add label "Cherry Pick Conflict_$branch" success' |
| 103 | + gh pr comment $pr_url --body "@${author} PR conflicts with $branch branch" |
| 104 | + echo 'Add commnet "@${author} PR conflicts with $branch branch"' |
| 105 | + else |
| 106 | + # Create PR to release branch |
| 107 | + git push mssonicbld HEAD:$branch-${pr_id} -f |
| 108 | + result=$(gh pr create -R ${repository} -H mssonicbld:$branch-${pr_id} -B $branch -t "[action] [PR:$pr_id] $title" -b '' 2>&1) |
| 109 | + echo $result | grep "already exists" && { echo $result; return 0; } |
| 110 | + echo $result | grep github.com || { echo $result; return 1; } |
| 111 | + new_pr_rul=$(echo $result | grep github.com) |
| 112 | + echo new_pr_rul: $new_pr_rul |
| 113 | +
|
| 114 | + # Add label to old PR |
| 115 | + gh pr edit $pr_url --add-label "Created PR to $branch Branch" |
| 116 | + echo Add label Created PR to $branch Branch |
| 117 | + # Add comment to old PR |
| 118 | + gh pr comment $pr_url --body "Cherry-pick PR to $branch: ${new_pr_rul}" |
| 119 | + echo Add comment to old PR |
| 120 | +
|
| 121 | + # Add label to new PR |
| 122 | + gh pr edit $new_pr_rul --add-label "automerge" |
| 123 | + echo Add label automerge to new PR |
| 124 | + # Add comment to new PR |
| 125 | + gh pr comment $new_pr_rul --body "Original PR: ${pr_url}" |
| 126 | + echo Add comment to new PR |
| 127 | + fi |
| 128 | + } |
| 129 | +
|
| 130 | + for branch in $branches |
| 131 | + do |
| 132 | + echo ------------------------------------------- |
| 133 | + echo Begin to parse Branch: $branch |
| 134 | + cherry_pick |
| 135 | + done |
| 136 | +
|
0 commit comments