From 5e03e6e795a65d0b3507560f82381d3f70a165e8 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Mon, 6 Sep 2021 11:14:31 +0200 Subject: [PATCH] Replace outdated manage-labels.yml with sync-labels.yml (#130) --- .github/workflows/manage-labels.yml | 42 --------- .github/workflows/sync-labels.yml | 132 ++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+), 42 deletions(-) delete mode 100644 .github/workflows/manage-labels.yml create mode 100644 .github/workflows/sync-labels.yml diff --git a/.github/workflows/manage-labels.yml b/.github/workflows/manage-labels.yml deleted file mode 100644 index c21c6aa8..00000000 --- a/.github/workflows/manage-labels.yml +++ /dev/null @@ -1,42 +0,0 @@ -name: Manage Repository Labels - -on: - push: - paths: - - ".github/workflows/manage-labels.yml" - - ".github/repository-labels-data/*.json" - schedule: - # run every Tuesday at 3 AM UTC - - cron: "0 3 * * 2" - # workflow_dispatch event allows the workflow to be triggered manually - # See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows#workflow_dispatch - workflow_dispatch: - -jobs: - manage-labels: - runs-on: ubuntu-latest - - env: - # All JSON files in this folder will be merged. - SOURCE_DATA_FOLDER_PATH: .github/repository-labels-data - # See: https://github.com/lannonbr/issue-label-manager-action#usage - MERGED_DATA_FILE_PATH: .github/labels.json - - steps: - - uses: actions/checkout@v2 - - - name: Download universal labels data file - uses: carlosperate/download-file-action@v1.0.3 - with: - file-url: https://raw.githubusercontent.com/107-systems/.github/main/universal-repository-labels.json - location: ${{ env.SOURCE_DATA_FOLDER_PATH }} - - - name: Merge labels data files - run: | - # Merge all data files and output to the location used by lannonbr/issue-label-manager-action - jq -s '.=.|add|.' "${{ env.SOURCE_DATA_FOLDER_PATH }}"/*.json > "${{ env.MERGED_DATA_FILE_PATH }}" - - - name: Update labels - uses: lannonbr/issue-label-manager-action@2.0.0 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/sync-labels.yml b/.github/workflows/sync-labels.yml new file mode 100644 index 00000000..90939cc6 --- /dev/null +++ b/.github/workflows/sync-labels.yml @@ -0,0 +1,132 @@ +# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/sync-labels.md +name: Sync Labels + +# See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows +on: + push: + paths: + - ".github/workflows/sync-labels.ya?ml" + - ".github/label-configuration-files/*.ya?ml" + pull_request: + paths: + - ".github/workflows/sync-labels.ya?ml" + - ".github/label-configuration-files/*.ya?ml" + schedule: + # run every Tuesday at 3 AM UTC + - cron: "0 3 * * 2" + workflow_dispatch: + repository_dispatch: + +env: + CONFIGURATIONS_FOLDER: .github/label-configuration-files + CONFIGURATIONS_ARTIFACT: label-configuration-files + +jobs: + check: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Download JSON schema for labels configuration file + id: download-schema + uses: carlosperate/download-file-action@v1.0.3 + with: + file-url: https://raw.githubusercontent.com/arduino/tooling-project-assets/main/workflow-templates/assets/sync-labels/arduino-tooling-gh-label-configuration-schema.json + location: ${{ runner.temp }}/label-configuration-schema + + - name: Install JSON schema validator + run: | + sudo npm install \ + --global \ + ajv-cli \ + ajv-formats + + - name: Validate local labels configuration + run: | + # See: https://github.com/ajv-validator/ajv-cli#readme + ajv validate \ + --all-errors \ + -c ajv-formats \ + -s "${{ steps.download-schema.outputs.file-path }}" \ + -d "${{ env.CONFIGURATIONS_FOLDER }}/*.{yml,yaml}" + + download: + needs: check + runs-on: ubuntu-latest + + strategy: + matrix: + filename: + # Filenames of the shared configurations to apply to the repository in addition to the local configuration. + # https://github.com/107-systems/.github/blob/main/workflow-templates/assets/sync-labels + - universal.yml + + steps: + - name: Download + uses: carlosperate/download-file-action@v1.0.3 + with: + file-url: https://raw.githubusercontent.com/107-systems/.github/main/workflow-templates/assets/sync-labels/${{ matrix.filename }} + + - name: Pass configuration files to next job via workflow artifact + uses: actions/upload-artifact@v2 + with: + path: | + *.yaml + *.yml + if-no-files-found: error + name: ${{ env.CONFIGURATIONS_ARTIFACT }} + + sync: + needs: download + runs-on: ubuntu-latest + + steps: + - name: Set environment variables + run: | + # See: https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable + echo "MERGED_CONFIGURATION_PATH=${{ runner.temp }}/labels.yml" >> "$GITHUB_ENV" + + - name: Determine whether to dry run + id: dry-run + if: > + github.event == 'pull_request' || + github.ref != format('refs/heads/{0}', github.event.repository.default_branch) + run: | + # Use of this flag in the github-label-sync command will cause it to only check the validity of the + # configuration. + echo "::set-output name=flag::--dry-run" + + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Download configuration files artifact + uses: actions/download-artifact@v2 + with: + name: ${{ env.CONFIGURATIONS_ARTIFACT }} + path: ${{ env.CONFIGURATIONS_FOLDER }} + + - name: Remove unneeded artifact + uses: geekyeggo/delete-artifact@v1 + with: + name: ${{ env.CONFIGURATIONS_ARTIFACT }} + + - name: Merge label configuration files + run: | + # Merge all configuration files + shopt -s extglob + cat "${{ env.CONFIGURATIONS_FOLDER }}"/*.@(yml|yaml) > "${{ env.MERGED_CONFIGURATION_PATH }}" + + - name: Install github-label-sync + run: sudo npm install --global github-label-sync + + - name: Sync labels + env: + GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + # See: https://github.com/Financial-Times/github-label-sync + github-label-sync \ + --labels "${{ env.MERGED_CONFIGURATION_PATH }}" \ + ${{ steps.dry-run.outputs.flag }} \ + ${{ github.repository }}