Pull latest direct updates to the development branch #2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: TEI editorial metadata | |
| on: | |
| pull_request: | |
| types: [closed] | |
| jobs: | |
| update-metadata: | |
| if: github.event.pull_request.merged == true | |
| runs-on: ubuntu-latest | |
| permissions: | |
| pull-requests: read | |
| contents: write | |
| steps: | |
| - name: Check approval count | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| PR_NUMBER: ${{ github.event.pull_request.number }} | |
| REPO: ${{ github.repository }} | |
| run: | | |
| approvals=$(gh api \ | |
| repos/$REPO/pulls/$PR_NUMBER/reviews \ | |
| --jq '[.[] | select(.state=="APPROVED") | .user.login] | unique | length') | |
| [ "$approvals" -ge 2 ] || exit 0 | |
| - name: Check out repository | |
| uses: actions/checkout@v4 | |
| - name: Get changed XML files | |
| id: changed_xml | |
| env: | |
| BASE_SHA: ${{ github.event.pull_request.base.sha }} | |
| HEAD_SHA: ${{ github.event.pull_request.merge_commit_sha }} | |
| run: | | |
| files=$(git diff --name-only "$BASE_SHA" "$HEAD_SHA" | grep '\.xml$' || true) | |
| echo "xml_files=$(echo "$files" | tr '\n' ' ')" >> "$GITHUB_OUTPUT" | |
| - name: Install xmlstarlet | |
| run: sudo apt-get update && sudo apt-get install -y xmlstarlet | |
| - name: Update editorial metadata | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| PR_NUMBER: ${{ github.event.pull_request.number }} | |
| REPO: ${{ github.repository }} | |
| PR_AUTHOR: ${{ github.event.pull_request.user.login }} | |
| PR_BODY: ${{ github.event.pull_request.body }} | |
| MERGE_DATE: ${{ github.event.pull_request.merged_at }} | |
| run: | | |
| set -e | |
| reviewers=$(gh api \ | |
| repos/$REPO/pulls/$PR_NUMBER/reviews \ | |
| --jq '.[] | select(.state=="APPROVED") | .user.login' | sort -u) | |
| get_name () { | |
| user=$1 | |
| text=$2 | |
| display=$(gh api users/$user --jq '.name // .login') | |
| echo "$text" | grep -qi anonymous && echo "Anonymous" || echo "$display" | |
| } | |
| for file in ${{ steps.changed_xml.outputs.xml_files }}; do | |
| ROOT=$(xmlstarlet sel -t -v "local-name(/*)" "$file") | |
| if [ "$ROOT" = "TEI" ]; then | |
| HEADER="//teiHeader" | |
| PERSON_EL="name" | |
| elif [ "$ROOT" = "mei" ]; then | |
| HEADER="//meiHead" | |
| PERSON_EL="persName" | |
| else | |
| continue | |
| fi | |
| if git cat-file -e "${{ github.event.pull_request.base.sha }}:$file" 2>/dev/null; then | |
| MODE="revision" | |
| else | |
| MODE="initial" | |
| fi | |
| editor_name=$(get_name "$PR_AUTHOR" "$PR_BODY") | |
| if [ "$MODE" = "initial" ]; then | |
| xmlstarlet ed -L \ | |
| -s "$HEADER/titleStmt" -t elem -n respStmt -v "" \ | |
| -s "$HEADER/titleStmt/respStmt[last()]" -t elem -n resp -v "Edited by" \ | |
| -s "$HEADER/titleStmt/respStmt[last()]" -t elem -n "$PERSON_EL" -v "$editor_name" \ | |
| -i "$HEADER/titleStmt/respStmt[last()]/*[last()]" -t attr -n role -v editor \ | |
| "$file" | |
| xmlstarlet ed -L \ | |
| -s "$HEADER/titleStmt" -t elem -n respStmt -v "" \ | |
| -s "$HEADER/titleStmt/respStmt[last()]" -t elem -n resp -v "Validated by" \ | |
| "$file" | |
| for r in $reviewers; do | |
| text=$(gh api repos/$REPO/pulls/$PR_NUMBER/reviews \ | |
| --jq ".[] | select(.user.login==\"$r\") | .body" | tail -n 1) | |
| name=$(get_name "$r" "$text") | |
| xmlstarlet ed -L \ | |
| -s "$HEADER/titleStmt/respStmt[last()]" -t elem -n "$PERSON_EL" -v "$name" \ | |
| -i "$HEADER/titleStmt/respStmt[last()]/*[last()]" -t attr -n role -v validator \ | |
| "$file" | |
| done | |
| else | |
| if ! xmlstarlet sel -t -c "$HEADER/revisionDesc" "$file" >/dev/null 2>&1; then | |
| xmlstarlet ed -L \ | |
| -s "$HEADER" -t elem -n revisionDesc -v "" \ | |
| "$file" | |
| fi | |
| xmlstarlet ed -L \ | |
| -s "$HEADER/revisionDesc" -t elem -n change -v "" \ | |
| -i "$HEADER/revisionDesc/change[last()]" -t attr -n when -v "${MERGE_DATE%%T*}" \ | |
| "$file" | |
| xmlstarlet ed -L \ | |
| -s "$HEADER/revisionDesc/change[last()]" -t elem -n "$PERSON_EL" -v "$editor_name" \ | |
| -i "$HEADER/revisionDesc/change[last()]/*[last()]" -t attr -n role -v editor \ | |
| "$file" | |
| for r in $reviewers; do | |
| text=$(gh api repos/$REPO/pulls/$PR_NUMBER/reviews \ | |
| --jq ".[] | select(.user.login==\"$r\") | .body" | tail -n 1) | |
| name=$(get_name "$r" "$text") | |
| xmlstarlet ed -L \ | |
| -s "$HEADER/revisionDesc/change[last()]" -t elem -n "$PERSON_EL" -v "$name" \ | |
| -i "$HEADER/revisionDesc/change[last()]/*[last()]" -t attr -n role -v validator \ | |
| "$file" | |
| done | |
| xmlstarlet ed -L \ | |
| -s "$HEADER/revisionDesc/change[last()]" -t elem -n desc -v "Change via PR #$PR_NUMBER" \ | |
| "$file" | |
| fi | |
| done | |
| - name: Commit changes | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions@users.noreply.github.com" | |
| git add *.xml | |
| git commit -m "Record editorial metadata for PR #${{ github.event.pull_request.number }}" || exit 0 | |
| git push |