/ .github / workflows / update-badge.yml
update-badge.yml
 1  name: Update Badge
 2  
 3  on:
 4    workflow_run:
 5      workflows:
 6        - libp2p transport interop test
 7      types:
 8        - completed
 9      branches:
10        - master
11  
12  defaults:
13    run:
14      shell: bash
15  
16  concurrency:
17    group: ${{ github.workflow }}
18    cancel-in-progress: true
19  
20  env:
21    BADGE_NAME: Interop Dashboard
22  
23  jobs:
24    update-badge:
25      runs-on: ubuntu-latest
26      steps:
27        - id: workflow
28          env:
29            WORKFLOW_PATH: ${{ github.event.workflow.path }}
30          run: echo "name=${WORKFLOW_PATH#.github/workflows/}" >> $GITHUB_OUTPUT
31        - uses: pl-strflt/job-summary-url-action@v1
32          id: metadata
33          with:
34            workflow: ${{ steps.workflow.outputs.name }}
35            run_id: ${{ github.event.workflow_run.id }}
36            run_attempt: ${{ github.event.workflow_run.run_attempt }}
37            job: latest
38        - uses: actions/checkout@v3
39        - id: update
40          uses: actions/github-script@v6
41          env:
42            BADGE_URL: ${{ github.event.workflow.badge_url }}
43            SUMMARY_URL: ${{ steps.metadata.outputs.job_summary_url }}
44          with:
45            script: |
46              const fs = require('fs')
47  
48              const badgeName = process.env.BADGE_NAME
49              const badgeURL = process.env.BADGE_URL
50              const refName = process.env.GITHUB_REF_NAME
51              const summaryURL = process.env.SUMMARY_URL
52  
53              const searchValue = new RegExp(`\\[!\\[${badgeName}\\]\\(.*\\)\\]\\(.*\\)`, 'g')
54              const replaceValue = `[![${badgeName}](${badgeURL}?branch=${refName})](${summaryURL})`
55  
56              console.log(`Searching for: ${searchValue}`)
57              console.log(`To replace it with: ${replaceValue}`)
58  
59              const readme = fs.readFileSync('README.md').toString()
60              const updatedReadme = readme.replace(searchValue, replaceValue)
61  
62              if (readme !== updatedReadme) {
63                console.log('Updating README')
64                fs.writeFileSync('README.md', updatedReadme)
65                return true
66              } else {
67                console.log('README does not need to be updated')
68                return false
69              }
70        # https://github.com/orgs/community/discussions/26560
71        - if: steps.update.outputs.result == 'true'
72          run: |
73            git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
74            git config user.name "github-actions[bot]"
75        - if: steps.update.outputs.result == 'true'
76          run: |
77            git add README.md
78            git commit -m 'chore: update the link to the interop dashboard [skip ci]'
79            git push