Skip to content

Sync Label

Sync Label #195

Workflow file for this run

# When a label in the neurobagel/planning repo is changed
# this workflow will push the change to all repos owned by
# the neurobagel organization.
#
# This workflow has 3 stages:
# 1. Check if the label has been renamed.
# Github does not provide an explicit hook for
# edit events that rename a label. Because syncing
# a renamed label requires additional steps, we have
# to check if the label has been renamed using the check_rename job.
# 2. Identify all repos owned by neurobagel with the
# get_repos job.
# 3. Use a job matrix to call the label_apply_change
# job for each repository owned by neurobagel and
# provide provide the rename status as a input.
name: Sync Label
on:
label:
types: [edited, created, deleted]
jobs:
check_rename:
runs-on: ubuntu-latest
name: Was label "${{ github.event.label.name }}" renamed?
outputs:
renamed: ${{ steps.renamed.outcome }}
steps:
# Check if the label was renamed
#
# This makes use of the github steps context outcome attribute: https://docs.github.com/en/actions/learn-github-actions/contexts#steps-context
# outcome will be
# - "success" if the exit code was 0 - i.e. the label was renamed
# - "failure" if the exit code was not 0 - i.e. the label was not renamed
# This is determined before the job is forced to "succeed" by continue-on-error: true
# in order to not crash the workflow.
- name: suceeds if label was renamed
id: renamed
continue-on-error: true
run: |
# See https://docs.github.com/en/webhooks-and-events/webhooks/webhook-events-and-payloads?actionType=edited#label
if [ -n "${{ github.event.changes.name.from }}" ];then
echo Label ${{ github.event.label.name }} has been renamed
exit 0
else
echo Label ${{ github.event.label.name }} has not been renamed
exit 1
fi
get_repos:
name: Get all repositories
runs-on: ubuntu-latest
outputs:
repos: ${{ steps.get_repo.outputs.repositories }}
steps:
- name: Generate a token
id: generate-token
uses: actions/create-github-app-token@v1
with:
app-id: ${{ vars.NB_BOT_ID }}
private-key: ${{ secrets.NB_BOT_KEY }}
# Note: We need to explicitly set the owner field here because this workflow only lives in planning but also
# makes changes in other repos.
# See https://github.com/marketplace/actions/create-github-app-token#create-a-token-for-multiple-repositories-in-the-current-owners-installation
# and https://github.com/orgs/community/discussions/69154#discussioncomment-7191057
owner: ${{ github.repository_owner }}
# To avoid setting the GH_TOKEN in every step, we set it as an environment variable
- name: Set GH_TOKEN
run: echo "GH_TOKEN=${{ steps.generate-token.outputs.token }}" >> $GITHUB_ENV
- name: Get current repositories
id: get_repo
env:
MAX_REPO: 50
run: |
repo_list=$(gh repo list $GITHUB_REPOSITORY_OWNER --no-archived -L ${MAX_REPO} --json owner,name --jq '.[] | "\(.owner.login)/\(.name)"' | jq -cnR '[inputs | select(length>0)]')
echo "repositories=${repo_list}" >> $GITHUB_OUTPUT
sync:
# This job creates a job matrix using the input computed by the two previous jobs
# and then launches the label_apply_change job for each repo.
uses: neurobagel/planning/.github/workflows/label_apply_change.yml@main
name: ${{ matrix.repo }} <- ${{ github.event.label.name }} (${{github.event.action}})
needs: [check_rename, get_repos]
secrets: inherit
strategy:
matrix:
repo: ${{fromJSON(needs.get_repos.outputs.repos)}}
with:
repo: ${{ matrix.repo }}
rename: ${{ needs.check_rename.outputs.renamed == 'success' }}