Skip to content

Update Badges for delta-coverage-plugin #157

Update Badges for delta-coverage-plugin

Update Badges for delta-coverage-plugin #157

name: Update Badges
run-name: Update Badges for ${{ inputs.target-repo }}
on:
workflow_dispatch:
inputs:
target-repo:
description: 'Target repository name'
required: true
default: 'delta-coverage-plugin'
artifacts-source-run-id:
description: 'Run ID of the workflow that generated the artifacts'
required: true
badges-artifact-id:
description: 'Artifact ID of the badges'
required: true
permissions:
contents: write
env:
FULL_TARGET_REPO: '${{ github.repository_owner }}/${{ inputs.target-repo }}'
REL_BADGES_DIR: '${{ inputs.target-repo }}/badges'
TMP_DIR: '${{ github.workspace }}/artifacts-tmp'
jobs:
update-badges:
name: '🏷️ Update Badges on ${{ inputs.target-repo }}'
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ github.token }}
- name: Download artifact
uses: actions/download-artifact@v4
with:
path: artifacts-tmp
run-id: ${{ inputs.artifacts-source-run-id }}
artifact-ids: ${{ inputs.badges-artifact-id }}
repository: ${{ env.FULL_TARGET_REPO }}
github-token: ${{ github.token }}
- name: Move Badges to ${{ inputs.target-repo }} Badges Directory
run: |
mkdir -p ${{ env.REL_BADGES_DIR }}
find ${{ env.TMP_DIR }} -type f -exec mv {} ${{ env.REL_BADGES_DIR }} \;
- name: Commit to repository
id: commit-to-repository
shell: bash
run: |
git config user.name "Github Action of ${{ github.event.repository.name }} repo"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add ${{ env.REL_BADGES_DIR }}/*
if ! git diff --quiet HEAD; then
git commit -m "${{ inputs.target-repo }}: Update badges"
# Get current branch name
BRANCH_NAME=$(git symbolic-ref --short HEAD)
# Try to push with set-upstream first; if it fails, try pull-rebase-push
if git push --set-upstream origin ${BRANCH_NAME} 2>/dev/null; then
echo "Branch pushed successfully with --set-upstream"
else
echo "Branch may already exist on remote, trying pull-rebase-push"
# https://stackoverflow.com/q/42743809/2971192
for i in {1..5}; do
git pull --rebase origin ${BRANCH_NAME} && git push && break || sleep 5
done
fi
revision=$(git rev-parse HEAD)
echo "REVISION: ${revision}"
echo "revision=${revision}" >> $GITHUB_OUTPUT
else
echo "No changes to commit"
fi
- name: Render Badges in Summary
uses: actions/github-script@v7
with:
script: |
const thisRepo = '${{ github.repository }}';
const thisBranch = '${{ github.ref }}';
const badgesDir = '${{ env.REL_BADGES_DIR }}';
const baseBadgeUrl = `https://raw.githubusercontent.com/${thisRepo}/${thisBranch}/${badgesDir}`;
const fs = require('fs');
const files = fs.readdirSync(badgesDir);
files.map(file => {
const completeBadgeUrl = `${baseBadgeUrl}/${file}`;
const badgeMarkdown = `![${file}](${completeBadgeUrl})`;
return badgeMarkdown;
}).reduce(
(acc, badge) => {
return acc
.addRaw(badge).addEOL()
.addCodeBlock(badge).addEOL()
.addRaw('---').addEOL();
},
core.summary.addHeading('Badges', 3).addEOL()
).write();