|
| 1 | +name: Update Labels Across All Repos in Org |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + paths: |
| 8 | + - .github/labels.yml # Only trigger if the label configuration changes |
| 9 | + |
| 10 | +jobs: |
| 11 | + update-labels: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + steps: |
| 14 | + - name: Checkout the repository |
| 15 | + uses: actions/checkout@v4 |
| 16 | + |
| 17 | + - name: Set up GitHub Token for API Authentication |
| 18 | + uses: actions/setup-python@v4 |
| 19 | + with: |
| 20 | + python-version: '3.x' |
| 21 | + |
| 22 | + - name: Install dependencies |
| 23 | + run: | |
| 24 | + python -m pip install --upgrade pip |
| 25 | + pip install requests yq |
| 26 | +
|
| 27 | + - name: Get all repos in the organization |
| 28 | + id: get_repos |
| 29 | + run: | |
| 30 | + ORGANIZATION="queens-autodrive" # Replace with your organization name |
| 31 | + REPOS=$(curl -s \ |
| 32 | + -H "Authorization: token $GITHUB_TOKEN" \ |
| 33 | + "https://api.github.com/orgs/$ORGANIZATION/repos?per_page=100" \ |
| 34 | + | jq -r '.[].name') |
| 35 | + echo "REPOS=$REPOS" >> $GITHUB_ENV # Set repos as an environment variable for later use |
| 36 | +
|
| 37 | + - name: Update Labels for All Repos |
| 38 | + env: |
| 39 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # GitHub token for authentication |
| 40 | + run: | |
| 41 | + LABELS=$(cat .github/labels.yml) # Path to your labels configuration file |
| 42 | +
|
| 43 | + # Loop through the repositories listed in the environment variable |
| 44 | + for REPO in $REPOS; do |
| 45 | + echo "Updating labels in $REPO" |
| 46 | +
|
| 47 | + # Loop through the labels and update them |
| 48 | + for LABEL in $(echo "$LABELS" | yq e '.labels[]' -); do |
| 49 | + # Extract label name and color from the YAML file |
| 50 | + LABEL_NAME=$(echo "$LABEL" | yq e '.name' -) |
| 51 | + LABEL_COLOR=$(echo "$LABEL" | yq e '.color' -) |
| 52 | + LABEL_DESCRIPTION=$(echo "$LABEL" | yq e '.description' -) |
| 53 | +
|
| 54 | + echo "Label: $LABEL_NAME, Color: $LABEL_COLOR, Description: $LABEL_DESCRIPTION" # Print label info |
| 55 | +
|
| 56 | + # Make the API request to update the label |
| 57 | + curl -X PUT \ |
| 58 | + -H "Authorization: token $GITHUB_TOKEN" \ |
| 59 | + -H "Accept: application/vnd.github.v3+json" \ |
| 60 | + -d '{"name": "'"$LABEL_NAME"'", "color": "'"$LABEL_COLOR"'"}' \ |
| 61 | + "https://api.github.com/repos/$ORGANIZATION/$REPO/labels/$LABEL_NAME" |
| 62 | + done |
| 63 | + done |
0 commit comments