|
| 1 | +name: Label Current Sprint Issues |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + # Run twice daily: 8 AM and 3 PM UTC |
| 6 | + - cron: "0 8 * * *" |
| 7 | + - cron: "0 15 * * *" |
| 8 | + workflow_dispatch: # Manual trigger for testing |
| 9 | + |
| 10 | +jobs: |
| 11 | + sync-sprint-labels: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + permissions: |
| 14 | + issues: write |
| 15 | + contents: read |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Checkout repository |
| 19 | + uses: actions/checkout@v6 |
| 20 | + |
| 21 | + - name: Sync sprint labels |
| 22 | + env: |
| 23 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 24 | + PROJECT_NUMBER: 24 |
| 25 | + ORG: qbicsoftware |
| 26 | + LABEL_NAME: "sprint:current" |
| 27 | + run: | |
| 28 | + set -e |
| 29 | + |
| 30 | + echo "=== Sprint Label Sync Started ===" |
| 31 | + echo "Project: $PROJECT_NUMBER" |
| 32 | + echo "Label: $LABEL_NAME" |
| 33 | + |
| 34 | + # Create label if it doesn't exist |
| 35 | + echo "Ensuring label '$LABEL_NAME' exists..." |
| 36 | + gh label create "$LABEL_NAME" --color "0E8A16" --description "Issue is part of the current sprint" 2>/dev/null || echo "Label already exists" |
| 37 | + |
| 38 | + # Get all project items |
| 39 | + echo "Fetching project items from project $PROJECT_NUMBER..." |
| 40 | + gh project item-list $PROJECT_NUMBER \ |
| 41 | + --owner $ORG \ |
| 42 | + --format json \ |
| 43 | + --limit 1000 > project_items.json |
| 44 | + |
| 45 | + # Check if we got any data |
| 46 | + if [ ! -s project_items.json ]; then |
| 47 | + echo "ERROR: Failed to fetch project items or project is empty" |
| 48 | + exit 1 |
| 49 | + fi |
| 50 | + |
| 51 | + # Get today's date for iteration comparison |
| 52 | + TODAY=$(date +%Y-%m-%d) |
| 53 | + echo -e "\nToday: $TODAY" |
| 54 | + |
| 55 | + # Find current sprint using shell-based date math (more reliable than jq) |
| 56 | + echo -e "\nSearching for current sprint..." |
| 57 | + |
| 58 | + # Extract all sprints and check each one using shell date commands |
| 59 | + cat project_items.json | jq -r '.items[].sprint | select(. != null) | "\(.iterationId)|\(.startDate)|\(.duration)|\(.title)"' | sort -u > /tmp/sprints.txt |
| 60 | + |
| 61 | + CURRENT_SPRINT="" |
| 62 | + while IFS='|' read -r id start_date duration title; do |
| 63 | + if [ -z "$id" ] || [ -z "$start_date" ] || [ -z "$duration" ]; then |
| 64 | + continue |
| 65 | + fi |
| 66 | + |
| 67 | + # Calculate end date: start_date + duration days |
| 68 | + end_date=$(date -d "$start_date + $duration days" +%Y-%m-%d 2>/dev/null || echo "") |
| 69 | + |
| 70 | + if [ -n "$end_date" ]; then |
| 71 | + echo "Checking sprint: $title ($start_date to $end_date)" |
| 72 | + |
| 73 | + # Check if today is within the sprint range (POSIX-compliant) |
| 74 | + if [ "$TODAY" = "$start_date" ] || [ "$TODAY" \> "$start_date" ]; then |
| 75 | + if [ "$TODAY" \< "$end_date" ]; then |
| 76 | + echo " -> Current sprint found!" |
| 77 | + CURRENT_SPRINT="$id" |
| 78 | + break |
| 79 | + fi |
| 80 | + fi |
| 81 | + fi |
| 82 | + done < /tmp/sprints.txt |
| 83 | + |
| 84 | + if [ -z "$CURRENT_SPRINT" ]; then |
| 85 | + echo -e "WARNING: No active sprint found for today ($TODAY)\n" |
| 86 | + echo "All sprints in project:" |
| 87 | + cat /tmp/sprints.txt | while IFS='|' read -r id start_date duration title; do |
| 88 | + end_date=$(date -d "$start_date + $duration days" +%Y-%m-%d 2>/dev/null || echo "unknown") |
| 89 | + echo " - $title: $start_date to $end_date (ID: $id)" |
| 90 | + done |
| 91 | + echo -e "\nExiting - no active sprint to process" |
| 92 | + exit 0 |
| 93 | + fi |
| 94 | + |
| 95 | + echo -e "Current sprint ID: $CURRENT_SPRINT\n" |
| 96 | + |
| 97 | + # Get issues in current sprint |
| 98 | + echo "Finding issues in current sprint..." |
| 99 | + cat project_items.json | jq -r \ |
| 100 | + --arg sprint "$CURRENT_SPRINT" \ |
| 101 | + '.items[] | select(.sprint.iterationId == $sprint and .content.type == "Issue") | .content.number' \ |
| 102 | + | sort -n | uniq > current_sprint_issues.txt 2>/dev/null || touch current_sprint_issues.txt |
| 103 | + |
| 104 | + ISSUES_IN_SPRINT=$(wc -l < current_sprint_issues.txt | tr -d ' ') |
| 105 | + echo "Found $ISSUES_IN_SPRINT issues in current sprint" |
| 106 | + |
| 107 | + if [ "$ISSUES_IN_SPRINT" -eq 0 ]; then |
| 108 | + echo "No issues found in current sprint" |
| 109 | + else |
| 110 | + echo "Issues in sprint:" |
| 111 | + cat current_sprint_issues.txt |
| 112 | + fi |
| 113 | + |
| 114 | + # Get all open issues with the sprint label |
| 115 | + echo -e "\nFetching issues with label '$LABEL_NAME'..." |
| 116 | + gh search issues \ |
| 117 | + --repo ${{ github.repository }} \ |
| 118 | + --label "$LABEL_NAME" \ |
| 119 | + --state open \ |
| 120 | + --json number \ |
| 121 | + --jq '.[].number' 2>/dev/null | sort -n | uniq > labeled_issues.txt || touch labeled_issues.txt |
| 122 | + |
| 123 | + ISSUES_LABELED=$(wc -l < labeled_issues.txt | tr -d ' ') |
| 124 | + echo "Found $ISSUES_LABELED issues with sprint label" |
| 125 | + |
| 126 | + if [ "$ISSUES_LABELED" -gt 0 ]; then |
| 127 | + echo "Currently labeled issues:" |
| 128 | + cat labeled_issues.txt |
| 129 | + fi |
| 130 | + |
| 131 | + # Add label to issues in sprint that don't have it |
| 132 | + ADDED_COUNT=0 |
| 133 | + while read -r issue_number; do |
| 134 | + if [ -z "$issue_number" ]; then |
| 135 | + continue |
| 136 | + fi |
| 137 | + if ! grep -q "^${issue_number}$" labeled_issues.txt 2>/dev/null; then |
| 138 | + echo "Adding label '$LABEL_NAME' to issue #$issue_number" |
| 139 | + if gh issue edit "$issue_number" --add-label "$LABEL_NAME"; then |
| 140 | + ADDED_COUNT=$((ADDED_COUNT + 1)) |
| 141 | + else |
| 142 | + echo "WARNING: Failed to add label to issue #$issue_number" |
| 143 | + fi |
| 144 | + fi |
| 145 | + done < current_sprint_issues.txt |
| 146 | + |
| 147 | + # Remove label from issues not in current sprint |
| 148 | + REMOVED_COUNT=0 |
| 149 | + while read -r issue_number; do |
| 150 | + if [ -z "$issue_number" ]; then |
| 151 | + continue |
| 152 | + fi |
| 153 | + if ! grep -q "^${issue_number}$" current_sprint_issues.txt 2>/dev/null; then |
| 154 | + echo "Removing label '$LABEL_NAME' from issue #$issue_number (not in current sprint)" |
| 155 | + if gh issue edit "$issue_number" --remove-label "$LABEL_NAME"; then |
| 156 | + REMOVED_COUNT=$((REMOVED_COUNT + 1)) |
| 157 | + else |
| 158 | + echo "WARNING: Failed to remove label from issue #$issue_number" |
| 159 | + fi |
| 160 | + fi |
| 161 | + done < labeled_issues.txt |
| 162 | + |
| 163 | + echo -e "\n=== Sprint Label Sync Complete ===" |
| 164 | + echo "Added label to: $ADDED_COUNT issues" |
| 165 | + echo "Removed label from: $REMOVED_COUNT issues" |
| 166 | + echo "Issues in sprint: $ISSUES_IN_SPRINT" |
0 commit comments