mirror of
https://github.com/ivuorinen/actions.git
synced 2026-01-26 11:34:00 +00:00
chore(ci): simplify labels syncing
This commit is contained in:
226
.github/workflows/sync-labels.yml
vendored
226
.github/workflows/sync-labels.yml
vendored
@@ -10,6 +10,8 @@ on:
|
|||||||
paths:
|
paths:
|
||||||
- '.github/labels.yml'
|
- '.github/labels.yml'
|
||||||
- '.github/workflows/sync-labels.yml'
|
- '.github/workflows/sync-labels.yml'
|
||||||
|
- 'sync-labels/action.yml'
|
||||||
|
- 'sync-labels/labels.yml'
|
||||||
schedule:
|
schedule:
|
||||||
- cron: '34 5 * * *' # Run every day at 05:34 AM UTC
|
- cron: '34 5 * * *' # Run every day at 05:34 AM UTC
|
||||||
workflow_call:
|
workflow_call:
|
||||||
@@ -28,228 +30,8 @@ jobs:
|
|||||||
timeout-minutes: 10
|
timeout-minutes: 10
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
|
||||||
issues: write
|
issues: write
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout Repository
|
- name: ⤵️ Sync Latest Labels Definitions
|
||||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
uses: ./sync-labels/action.yml
|
||||||
with:
|
|
||||||
fetch-depth: 1
|
|
||||||
|
|
||||||
- name: Validate Labels File
|
|
||||||
id: validate
|
|
||||||
shell: bash
|
|
||||||
run: |
|
|
||||||
LABELS_URL="https://raw.githubusercontent.com/ivuorinen/actions/refs/heads/main/sync-labels/labels.yml"
|
|
||||||
LABELS_FILE="labels.yml"
|
|
||||||
|
|
||||||
echo "Downloading labels file..."
|
|
||||||
if ! curl -s --retry 3 --retry-delay 5 -o "$LABELS_FILE" "$LABELS_URL"; then
|
|
||||||
echo "::error::Failed to download labels file"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "Validating YAML format..."
|
|
||||||
if ! yq eval "$LABELS_FILE" > /dev/null 2>&1; then
|
|
||||||
echo "::error::Invalid YAML format in labels file"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Check for required fields in each label
|
|
||||||
echo "Validating label definitions..."
|
|
||||||
INVALID=0
|
|
||||||
HAS_NAME=0
|
|
||||||
HAS_COLOR=0
|
|
||||||
HAS_DESCRIPTION=0
|
|
||||||
CURRENT_LABEL=""
|
|
||||||
|
|
||||||
check_label_completion() {
|
|
||||||
if [[ $HAS_NAME -eq 1 && $HAS_COLOR -eq 1 && $HAS_DESCRIPTION -eq 1 ]]; then
|
|
||||||
echo "✓ Valid label: $CURRENT_LABEL"
|
|
||||||
else
|
|
||||||
echo "✗ Invalid label: $CURRENT_LABEL (missing:"
|
|
||||||
[[ $HAS_NAME -eq 0 ]] && echo " - name"
|
|
||||||
[[ $HAS_COLOR -eq 0 ]] && echo " - color"
|
|
||||||
[[ $HAS_DESCRIPTION -eq 0 ]] && echo " - description"
|
|
||||||
echo ")"
|
|
||||||
INVALID=$((INVALID + 1))
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
while IFS= read -r line || [ -n "$line" ]; do
|
|
||||||
# Skip empty lines and comments
|
|
||||||
[[ -z "$line" || "$line" =~ ^[[:space:]]*# ]] && continue
|
|
||||||
|
|
||||||
if [[ "$line" =~ ^-.*$ ]]; then
|
|
||||||
# Check previous label completion before starting new one
|
|
||||||
if [[ -n "$CURRENT_LABEL" ]]; then
|
|
||||||
check_label_completion
|
|
||||||
fi
|
|
||||||
# New label definition found, reset checks
|
|
||||||
HAS_NAME=0
|
|
||||||
HAS_COLOR=0
|
|
||||||
HAS_DESCRIPTION=0
|
|
||||||
CURRENT_LABEL="(new label)"
|
|
||||||
elif [[ "$line" =~ ^[[:space:]]+name:[[:space:]]*(.+)$ ]]; then
|
|
||||||
HAS_NAME=1
|
|
||||||
CURRENT_LABEL="${BASH_REMATCH[1]}"
|
|
||||||
elif [[ "$line" =~ ^[[:space:]]+color:[[:space:]]*([0-9A-Fa-f]{6})$ ]]; then
|
|
||||||
HAS_COLOR=1
|
|
||||||
elif [[ "$line" =~ ^[[:space:]]+description:[[:space:]]+.+$ ]]; then
|
|
||||||
HAS_DESCRIPTION=1
|
|
||||||
fi
|
|
||||||
done < "$LABELS_FILE"
|
|
||||||
|
|
||||||
# Check the last label
|
|
||||||
if [[ -n "$CURRENT_LABEL" ]]; then
|
|
||||||
check_label_completion
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "Validation complete. Found $INVALID invalid label(s)."
|
|
||||||
|
|
||||||
if [ $INVALID -ne 0 ]; then
|
|
||||||
echo "::error::Found $INVALID invalid label definition(s)"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "Labels file validated successfully"
|
|
||||||
echo "labels_file=$LABELS_FILE" >> "$GITHUB_OUTPUT"
|
|
||||||
|
|
||||||
# Create validation summary
|
|
||||||
{
|
|
||||||
echo "## Label Validation Results"
|
|
||||||
echo
|
|
||||||
echo "- Total invalid labels: $INVALID"
|
|
||||||
echo "- File: \`$LABELS_FILE\`"
|
|
||||||
echo "- Timestamp: $(date -u '+%Y-%m-%d %H:%M:%S UTC')"
|
|
||||||
echo
|
|
||||||
echo "All labels have required fields:"
|
|
||||||
echo "- name"
|
|
||||||
echo "- color (6-digit hex)"
|
|
||||||
echo "- description"
|
|
||||||
} > validation-report.md
|
|
||||||
|
|
||||||
- name: Run Label Syncer
|
|
||||||
id: sync
|
|
||||||
uses: micnncim/action-label-syncer@3abd5ab72fda571e69fffd97bd4e0033dd5f495c # v1.3.0
|
|
||||||
env:
|
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
with:
|
|
||||||
manifest: ${{ steps.validate.outputs.labels_file }}
|
|
||||||
prune: true
|
|
||||||
|
|
||||||
- name: Verify Label Sync
|
|
||||||
id: verify
|
|
||||||
if: success()
|
|
||||||
shell: bash
|
|
||||||
run: |
|
|
||||||
echo "Verifying labels..."
|
|
||||||
|
|
||||||
# Get current labels from GitHub
|
|
||||||
CURRENT_LABELS=$(gh api repos/${{ github.repository }}/labels --jq '.[].name')
|
|
||||||
|
|
||||||
# Get expected labels from file
|
|
||||||
EXPECTED_LABELS=$(yq eval '.[] | .name' "${{ steps.validate.outputs.labels_file }}")
|
|
||||||
|
|
||||||
# Compare labels
|
|
||||||
MISSING_LABELS=0
|
|
||||||
while IFS= read -r label; do
|
|
||||||
if ! echo "$CURRENT_LABELS" | grep -q "^${label}$"; then
|
|
||||||
echo "::warning::Label not synced: $label"
|
|
||||||
MISSING_LABELS=1
|
|
||||||
fi
|
|
||||||
done <<< "$EXPECTED_LABELS"
|
|
||||||
|
|
||||||
if [ $MISSING_LABELS -eq 1 ]; then
|
|
||||||
echo "::error::Some labels failed to sync"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "All labels verified successfully"
|
|
||||||
|
|
||||||
- name: Generate Label Report
|
|
||||||
if: always()
|
|
||||||
shell: bash
|
|
||||||
run: |
|
|
||||||
{
|
|
||||||
echo "# Label Sync Report"
|
|
||||||
echo "## Current Labels"
|
|
||||||
echo
|
|
||||||
# shellcheck disable=SC2016
|
|
||||||
gh api repos/${{ github.repository }}/labels --jq '.[] | "- **" + .name + "** (`#" + .color + "`): " + .description' | sort
|
|
||||||
echo
|
|
||||||
echo "## Sync Status"
|
|
||||||
echo "- ✅ Success: ${{ steps.sync.outcome == 'success' }}"
|
|
||||||
echo "- 🔍 Verified: ${{ steps.verify.outcome == 'success' }}"
|
|
||||||
echo
|
|
||||||
echo "Generated at: $(date -u '+%Y-%m-%d %H:%M:%S UTC')"
|
|
||||||
} > label-report.md
|
|
||||||
|
|
||||||
- name: Upload Label Report
|
|
||||||
if: always()
|
|
||||||
uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4.6.0
|
|
||||||
with:
|
|
||||||
name: label-sync-report
|
|
||||||
path: label-report.md
|
|
||||||
retention-days: 7
|
|
||||||
|
|
||||||
- name: Notify on Failure
|
|
||||||
if: failure()
|
|
||||||
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
|
|
||||||
with:
|
|
||||||
script: |
|
|
||||||
const fs = require('fs');
|
|
||||||
|
|
||||||
try {
|
|
||||||
const { repo, owner } = context.repo;
|
|
||||||
const runUrl = `${process.env.GITHUB_SERVER_URL}/${owner}/${repo}/actions/runs/${process.env.GITHUB_RUN_ID}`;
|
|
||||||
|
|
||||||
const body = `## ⚠️ Label Sync Failed
|
|
||||||
|
|
||||||
The label synchronization workflow has failed.
|
|
||||||
|
|
||||||
### Details
|
|
||||||
- Workflow: [View Run](${runUrl})
|
|
||||||
- Repository: ${owner}/${repo}
|
|
||||||
- Timestamp: ${new Date().toISOString()}
|
|
||||||
|
|
||||||
### Status
|
|
||||||
- Validation: ${{ steps.validate.outcome }}
|
|
||||||
- Sync: ${{ steps.sync.outcome }}
|
|
||||||
- Verification: ${{ steps.verify.outcome }}
|
|
||||||
|
|
||||||
Please check the workflow logs for more details.
|
|
||||||
|
|
||||||
> This issue was automatically created by the label sync workflow.`;
|
|
||||||
|
|
||||||
await github.rest.issues.create({
|
|
||||||
owner,
|
|
||||||
repo,
|
|
||||||
title: '⚠️ Label Sync Failure',
|
|
||||||
body,
|
|
||||||
labels: ['automation', 'bug'],
|
|
||||||
assignees: ['ivuorinen'],
|
|
||||||
}).catch(error => {
|
|
||||||
if (error.status === 403) {
|
|
||||||
console.error('Permission denied while creating issue. Please check workflow permissions.');
|
|
||||||
} else if (error.status === 429) {
|
|
||||||
console.error('Rate limit exceeded. Please try again later.');
|
|
||||||
} else {
|
|
||||||
console.error(`Failed to create issue: ${error.message}`);
|
|
||||||
}
|
|
||||||
throw error;
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Failed to create issue:', error);
|
|
||||||
}
|
|
||||||
|
|
||||||
- name: Cleanup
|
|
||||||
if: always()
|
|
||||||
shell: bash
|
|
||||||
run: |
|
|
||||||
# Remove temporary files
|
|
||||||
rm -f labels.yml
|
|
||||||
|
|
||||||
# Remove lock files if they exist
|
|
||||||
find . -name ".lock" -type f -delete
|
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ runs:
|
|||||||
shell: bash
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
curl -s --retry 5 \
|
curl -s --retry 5 \
|
||||||
"https://raw.githubusercontent.com/ivuorinen/actions/refs/heads/main/sync-labels/labels.yml" \
|
"https://raw.githubusercontent.com/ivuorinen/actions/main/sync-labels/labels.yml" \
|
||||||
> ${{ inputs.labels }}
|
> ${{ inputs.labels }}
|
||||||
|
|
||||||
- name: 🚀 Run Label Syncer
|
- name: 🚀 Run Label Syncer
|
||||||
|
|||||||
Reference in New Issue
Block a user