Skip to content

AutoMergeScan

AutoMergeScan #8600

name: AutoMergeScan
on:
schedule:
- cron: '31 */2 * * *'
workflow_dispatch:
jobs:
automerge_scan:
# Disable workflow, and use pipeline instead to have same logic with other automation
if: github.repository_owner == 'sonic-net' && false
runs-on: ubuntu-latest
steps:
- name: Debug
env:
TOKEN: ${{ secrets.TOKEN }}
run: |
set -e
echo ${TOKEN} | gh auth login --with-token
gh pr list -R sonic-net/sonic-buildimage -A mssonicbld -L 100 -S "label:automerge" --json url,body,title,createdAt,labels,statusCheckRollup > prs.log
cat prs.log | jq
- name: Main
run: |
set -e
# PR merge run per 2 hours
# Other operation run per day.
# Cherry pick PR:
# more than 3 days, comment @author to check
# more than 10 days, stop comment.
# more than 28 days, comment @author PR will be closed
# more than 30 days, close PR
date_3d_ago=$(date --date "3 day ago" -u +"%FT%TZ")
date_10d_ago=$(date --date "10 day ago" -u +"%FT%TZ")
date_28d_ago=$(date --date "28 day ago" -u +"%FT%TZ")
date_30d_ago=$(date --date "30 day ago" -u +"%FT%TZ")
date_now=$(date -u +"%T")
operate=false
[[ "$date_now" < "02:00:00" ]] && operate=true
count=$(cat prs.log | jq 'length')
for ((i=0;i<$count;i++))
do
url=$(cat prs.log | jq -r ".[$i].url")
body=$(cat prs.log | jq -r ".[$i].body")
title=$(cat prs.log | jq -r ".[$i].title")
origin_pr_id=$(echo $title | grep -Eo "\[action\] \[PR:[0-9]*\]" | grep -Eo [0-9]* || true)
created_at=$(cat prs.log | jq -r ".[$i].createdAt")
echo PR: $(($i+1))/$count, URL: $url, origin PR: $origin_pr_id, createdAt: $created_at, operate: $operate
[[ "$url" == "" ]] && continue
[[ $created_at > $(date --date "1 hour ago" -u +"%FT%TZ") ]] && continue
checks=$(cat prs.log | jq ".[$i].statusCheckRollup")
checks_count=$(echo $checks | jq 'length')
pr_success=true
for ((j=0;j<$checks_count;j++))
do
check=$(echo $checks | jq ".[$j]")
status=$(echo $check | jq -r '.status')
conclusion=$(echo $check | jq -r '.conclusion')
name=$(echo $check | jq -r '.name')
# EasyCLA success flag: state=SUCCESS
# Others success flag: conclusion in SUCCESS,NEUTRAL
# only check Azure.sonic-buildimage currently
echo "$name" | grep -v "Azure.sonic-buildimage" > /dev/null && continue
[[ "$status" != "COMPLETED" ]] && echo "$name: $status" && continue 2
success=true
( [[ "$conclusion" == "FAILURE" ]] || [[ "$conclusion" == "CANCELLED" ]] ) && success=false && pr_success=false
! $success && echo "FAIL: $name"
done
# rerun Azure.sonic-buildimage per day
! $pr_success && $operate && gh pr comment $url --body "/azp run Azure.sonic-buildimage"
# If auto cherry pick PRs failed, comment in original PR and close cherry pick PR
if [ -n "$origin_pr_id" ] && [[ $created_at < $date_3d_ago ]] && ! $pr_success;then
origin_pr_url=https://github.com/sonic-net/sonic-buildimage/pull/$origin_pr_id
author=$(gh pr view $origin_pr_url --json author | jq .author.login -r)
echo "Original author will check."
$operate && [[ $created_at > $date_10d_ago ]] && gh pr comment $origin_pr_url --body "@$author cherry pick PR didn't pass PR checker. Please check!!!<br>$url"
$operate && [[ $created_at < $date_28d_ago ]] && gh pr comment $origin_pr_url --body "@$author cherry pick PR didn't pass PR checker. Please check!!! Auto cherry pick PR will be closed in 2 days.<br>$url"
$operate && [[ $created_at < $date_30d_ago ]] && echo "$url Closed" && gh pr close $url
fi
! $pr_success && continue
# merge the PR
echo ========Merging PR========
if echo $title | grep "^\[submodule\]";then
gh pr merge --squash --admin -R sonic-net/sonic-buildimage $url -b "$body" || true
else
gh pr merge --rebase --admin -R sonic-net/sonic-buildimage $url || true
fi
echo ========Finished PR========
done