Skip to content

Check Branches State #117

Check Branches State

Check Branches State #117

name: Check Branches State
on:
workflow_dispatch:
schedule:
- cron: '0 9 * * 2'
jobs:
check_branches_state:
name: 'Check Branches State'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
token: ${{ secrets.AUTOMATION_GITHUB_TOKEN }}
fetch-depth: 0
- name: Step 1 - ⚙️ Setup GitHub Config
run: |
git config user.name "🤖 DailyBot"
git config user.email "ops@dailybot.com"
gh auth login --with-token <<< "${{ secrets.AUTOMATION_GITHUB_TOKEN }}"
- name: Step 2 - Get all branches of the repository and check if they are up to date
run: |
BRANCHES=$(git branch -r)
BRANCHES_OUTDATED_FOUND=0
NOTIFICATION_MESSAGE="*👉 <https://github.com/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID|#$GITHUB_RUN_NUMBER> ($GITHUB_REF_NAME) - ${{ vars.USERS_TO_NOTIFY }} Check branches without updates for more than 5 days.*"$'\n'
NOTIFICATION_MESSAGE+="*Repository:* <https://github.com/$GITHUB_REPOSITORY|$GITHUB_REPOSITORY>"$'\n'
for BRANCH in $BRANCHES
do
BRANCH_NAME="${BRANCH//origin\//}"
AUTHOR=$(git show --no-patch --format='%an' "$BRANCH" | awk '{print $1, $2}')
LAST_COMMIT_DATE=$(git show --no-patch --format='%ci' "$BRANCH")
DAYS_SINCE_LAST_COMMIT=$(( ( $(date +%s) - $(date -d "$LAST_COMMIT_DATE" +%s) ) / 86400 ))
DAYS_FROM_LAST_COMMIT_TO_NOTIFY=7
if [ $DAYS_SINCE_LAST_COMMIT -gt $DAYS_FROM_LAST_COMMIT_TO_NOTIFY ] && [ "$BRANCH_NAME" != "dev" ] && [ "$BRANCH_NAME" != "staging" ] && [ "$BRANCH_NAME" != "main" ]; then
BRANCHES_OUTDATED_FOUND=1
NOTIFICATION_MESSAGE+="> • *\`$BRANCH_NAME\`* created by *$AUTHOR*"$'\n'
fi
done
if [ $BRANCHES_OUTDATED_FOUND -eq 0 ]; then
echo "No outdated branches found"
exit 0
fi
JSON_DATA=$(jq -n \
--arg msg "$NOTIFICATION_MESSAGE" \
--arg channel "${{ vars.DAILYBOT_WORKFLOWS_NOTIFICATION_CHANNEL }}" \
'{message: $msg, target_channels: [$channel]}')
curl --location 'https://api.dailybot.com/v1/send-message/' \
--header "X-API-KEY: ${{ secrets.DAILYBOT_API_KEY }}" \
--header 'Content-Type: application/json' \
--data "$JSON_DATA"