From 020e038e3675e56834d742f20e895f83b09ce80e Mon Sep 17 00:00:00 2001 From: shilongliu Date: Wed, 20 Jul 2022 16:37:24 +0800 Subject: [PATCH] [actions] --- .github/workflows/github_restapi.sh | 78 +++++++++++++++++ .github/workflows/post_cherry_pick.yml | 57 +++++++++++++ .github/workflows/pre_cherry_pick.yml | 113 +++++++++++++++++++++++++ 3 files changed, 248 insertions(+) create mode 100644 .github/workflows/github_restapi.sh create mode 100644 .github/workflows/post_cherry_pick.yml create mode 100644 .github/workflows/pre_cherry_pick.yml diff --git a/.github/workflows/github_restapi.sh b/.github/workflows/github_restapi.sh new file mode 100644 index 000000000000..d8e42a5a279d --- /dev/null +++ b/.github/workflows/github_restapi.sh @@ -0,0 +1,78 @@ +#!/bin/bash -e +# github restapi tools + +add_label(){ + if [ $# -ne 3 ];then + >&2 echo "example: add_label github_token https://api.github.com/repos///issues/ label_1" + exit 1 + fi + TOKEN_LOCAL=$1 + ISSUES_URL=$(echo $2| sed 's#/$##') + LABEL=$(echo $3 | sed 's#"#\\"#g') + + result=$(curl -X POST -H "Accept: application/vnd.github+json" \ + -H "Authorization: token ${TOKEN_LOCAL}" \ + ${ISSUES_URL}/labels \ + -d "{ + \"labels\": [\"${LABEL}\"] + }") + if [[ $(expr length "$result") -eq "0" ]];then + >&2 echo add_label Error: + >&2 echo $(echo $result | jq ) + exit 2 + fi +} + +add_comment(){ + if [ $# -ne 3 ];then + >&2 echo "example: add_comment github_token https://api.github.com/repos///issues/ comment" + exit 1 + fi + TOKEN_LOCAL=$1 + ISSUES_URL=$(echo $2| sed 's#/$##') + BODY=$(echo $3 | sed 's#"#\\"#g') + + result=$(curl -X POST -H "Accept: application/vnd.github+json" \ + -H "Authorization: token ${TOKEN_LOCAL}" \ + ${ISSUES_URL}/comments \ + -d "{\"body\":\"${BODY}\"}") + if [[ "$(echo $result | jq '.errors')" != "null" ]];then + >&2 echo add_comment Error: + >&2 echo $result | jq '.errors' + exit 2 + fi +} + +create_pr(){ + if [ $# -ne 5 ];then + >&2 echo "example: create_pr github_token https://api.github.com/repos// tile mssonicbld:feature-branch master" + exit 1 + fi + TOKEN_LOCAL=$1 + BASE_URL=$(echo $2| sed 's#/$##') + TITLE=$(echo $3 | sed 's#"#\\"#g') + HEAD=$(echo $4| sed 's#"#\\"#g') + BASE=$(echo $5| sed 's#"#\\"#g') + + result=$(curl -X POST -H "Accept: application/vnd.github+json" \ + -H "Authorization: token ${TOKEN_LOCAL}" \ + ${BASE_URL}/pulls \ + -d "{ + \"title\":\"${TITLE}\", + \"body\":\"\", + \"head\":\"${HEAD}\", + \"base\":\"${BASE}\" + }") + if [[ "$(echo $result | jq '.errors')" != "null" ]];then + >&2 echo create_pr Error: + >&2 echo $result | jq '.errors' + exit 2 + fi + echo $result +} + +list_comments(){ + TOKEN_LOCAL=$1 + ISSUES_URL=$(echo $2| sed 's#/$##') + curl -H "Accept: application/vnd.github+json" -H "Authorization: token ${TOKEN_LOCAL}" ${ISSUES_URL}/comments +} \ No newline at end of file diff --git a/.github/workflows/post_cherry_pick.yml b/.github/workflows/post_cherry_pick.yml new file mode 100644 index 000000000000..60d5a07ecc6a --- /dev/null +++ b/.github/workflows/post_cherry_pick.yml @@ -0,0 +1,57 @@ +name: PostCherryPick +on: + pull_request_target: + types: + - closed + branches: + - '202205' + - '202111' + - '202106' + - '202012' + +jobs: + post_cherry_pick: + if: github.event.pull_request.merged == true + 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 + source .github/workflows/github_restapi.sh + issue_url=$(echo $GITHUB_CONTEXT | jq ".event.pull_request._links.issue.href" | sed 's/"//g') + base_url=$(echo $issue_url | awk -F'/issues' '{print$1}') + repo_url=$(echo $GITHUB_CONTEXT | jq ".event.pull_request._links.html.href" | awk -F'pull' '{print$1}' | sed 's/"//g') + base_ref=$(echo $GITHUB_CONTEXT | jq ".base_ref" | sed 's/"//g') + echo ============================= + echo issue_url: $issue_url + echo base_url: $base_url + echo repo_url: $repo_url + echo base_ref: $base_ref + echo ============================= + comments=$(list_comments ${{ secrets.GITHUB_TOKEN }} ${issue_url}) + if [[ $(expr length "$comments") -lt "5" ]];then + exit 0 + fi + count=$(echo $comments | jq 'length') + echo comment count: $count + for (( i=0; i<$count; i++ )) + do + if [[ $(echo $comments | jq ".[$i].user.login" | sed 's/"//g') == "github-actions[bot]" ]];then + origin_issue_url=$(echo $comments | jq ".[$i].body" | sed 's/"//g' | sed 's/Original PR: //') + break + fi + done + echo origin_issue_url: $origin_issue_url + # Add label + add_label "${{ secrets.GITHUB_TOKEN }}" "${origin_issue_url}" "Included in ${base_ref} Branch" diff --git a/.github/workflows/pre_cherry_pick.yml b/.github/workflows/pre_cherry_pick.yml new file mode 100644 index 000000000000..e69231ce4bcb --- /dev/null +++ b/.github/workflows/pre_cherry_pick.yml @@ -0,0 +1,113 @@ +name: PreCherryPick +on: + pull_request_target: + types: + - labeled + - closed + branches: + - master + +jobs: + pre_cherry_pick: + if: github.event.pull_request.merged == true + runs-on: ubuntu-latest + strategy: + matrix: + branch: [202205,202111,202106,202012] + 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 + source .github/workflows/github_restapi.sh + sha=$(echo $GITHUB_CONTEXT | jq ".event.pull_request.merge_commit_sha" | sed 's/"//g') + pr_id=$(echo $GITHUB_CONTEXT | jq ".event.number" | sed 's/"//g') + pr_body=$(echo $GITHUB_CONTEXT | jq ".event.pull_request.body" | sed 's/"//g') + issues_url=$(echo $GITHUB_CONTEXT | jq ".event.pull_request._links.issue.href" | sed 's/"//g') + base_url=$(echo $issues_url | awk -F'/issues' '{print$1}') + repo_url=$(echo $GITHUB_CONTEXT | jq ".event.pull_request._links.html.href" | awk -F'pull' '{print$1}' | sed 's/"//g') + 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 issues_url: $issues_url + echo base_url: $base_url + echo repo_url: $repo_url + echo labels_count: $labels_count + echo ============================= + create_pr='' + for (( i=0; i<$labels_count; i++ )) + do + label=$(echo $labels_json | jq ".[$i].name" | sed 's/"//g') + echo label ${i}: $label + if echo $label | grep -oE "Request for ${{ matrix.branch }} Branch";then + create_pr=1 + fi + if echo $label | grep -oE "Created PR to ${{ matrix.branch }} Branch";then + echo "already has tag: Cherry-pick PR to ${{ matrix.branch }} Branch, exit" + exit 0 + fi + if echo $label | grep -oE "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 cherry-pick 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 + add_label "${{ secrets.GITHUB_TOKEN }}" "${issues_url}" "Cherry Pick Conflict_${{ matrix.branch }}" + echo 'Add label "Cherry Pick Conflict_${{ matrix.branch }}" success' + else + # Create PR to release branch + git push mssonicbld HEAD:${pr_id}-${{ matrix.branch }} -f + result=$(create_pr ${{ secrets.TOKEN }} ${base_url} "auto cherry-pick ${pr_id} into ${{ matrix.branch }}" "mssonicbld:${pr_id}-${{ matrix.branch }}" "${{ matrix.branch }}") + if [[ $(expr length "$result") -lt "10" ]];then + echo result: + echo $result + exit 3 + fi + new_issue_url=$(echo $result | jq '.issue_url' | sed 's/"//g') + echo new_issue_url: $new_issue_url + + # Add label + add_label "${{ secrets.GITHUB_TOKEN }}" "${issues_url}" "Created PR to ${{ matrix.branch }} Branch" + echo Add label Created PR to ${{ matrix.branch }} Branch + + # Add comment to new PR + add_comment ${{ secrets.GITHUB_TOKEN }} ${new_issue_url} "Original PR: ${issues_url}" + echo Add comment to new PR + + # Add comment to old PR + add_comment ${{ secrets.GITHUB_TOKEN }} ${issues_url} "Cherry-pick PR to ${{ matrix.branch }}: ${new_issue_url}" + echo Add comment to old PR + fi