copyright-updates.yml 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. name: Update copyright years
  2. on:
  3. schedule:
  4. - cron: '42 13 3 1 *' # 3rd of January at 13:42 UTC
  5. workflow_dispatch:
  6. inputs:
  7. year:
  8. type: number
  9. required: true
  10. description: 'The (new) year to update the copyright to'
  11. permissions:
  12. contents: write
  13. pull-requests: write
  14. jobs:
  15. update-copyrights:
  16. runs-on: ubuntu-latest
  17. steps:
  18. - uses: actions/checkout@v4
  19. with:
  20. fetch-depth: 0
  21. - name: Determine years
  22. id: years
  23. env:
  24. TRIGGER_NAME: ${{ github.event_name }}
  25. YEAR_INPUT: ${{ inputs.year }}
  26. run: |
  27. if [ "${TRIGGER_NAME}" = 'workflow_dispatch' ]; then
  28. NEW_YEAR=${YEAR_INPUT}
  29. else
  30. NEW_YEAR=$(date '+%Y')
  31. fi
  32. echo "new-year=${NEW_YEAR}" >> "${GITHUB_OUTPUT}"
  33. echo "old-year=$((NEW_YEAR - 1))" >> "${GITHUB_OUTPUT}"
  34. - name: Checkout branch
  35. id: branch
  36. env:
  37. OLD_YEAR: ${{ steps.years.outputs.old-year }}
  38. NEW_YEAR: ${{ steps.years.outputs.new-year }}
  39. run: |
  40. BRANCH_NAME="update-copyright/${OLD_YEAR}-to-${NEW_YEAR}"
  41. echo "branch-name=${BRANCH_NAME}" >> "${GITHUB_OUTPUT}"
  42. git checkout -B "${BRANCH_NAME}"
  43. - name: Update copyrights
  44. run: ./Scripts/update-copyright.sh
  45. env:
  46. OLD_YEAR: ${{ steps.years.outputs.old-year }}
  47. NEW_YEAR: ${{ steps.years.outputs.new-year }}
  48. - name: Commit changes if needed
  49. id: commit
  50. env:
  51. ACTOR: ${{ github.actor }}
  52. OLD_YEAR: ${{ steps.years.outputs.old-year }}
  53. NEW_YEAR: ${{ steps.years.outputs.new-year }}
  54. BRANCH_NAME: ${{ steps.branch.outputs.branch-name }}
  55. run: |
  56. if [ -n "$(git status --porcelain)" ]; then
  57. git config --local user.email "${ACTOR}@noreply.github.com"
  58. git config --local user.name "${ACTOR}"
  59. git add .
  60. git commit -m "Update copyright from ${OLD_YEAR} to ${NEW_YEAR}"
  61. git push --set-upstream origin "${BRANCH_NAME}"
  62. echo 'has-changes=true' >> "${GITHUB_OUTPUT}"
  63. else
  64. echo 'has-changes=false' >> "${GITHUB_OUTPUT}"
  65. fi
  66. - name: Create PR if needed
  67. if: ${{ steps.commit.outputs.has-changes == 'true' }}
  68. env:
  69. OLD_YEAR: ${{ steps.years.outputs.old-year }}
  70. NEW_YEAR: ${{ steps.years.outputs.new-year }}
  71. REPO: ${{ github.repository }}
  72. SOURCE_BRANCH: ${{ steps.branch.outputs.branch-name }}
  73. GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  74. run: |
  75. TARGET_BRANCH="$(gh repo view "${REPO}" --json 'defaultBranchRef' --jq '.defaultBranchRef.name')"
  76. PR_COUNT="$(gh pr list \
  77. --repo "${REPO}" \
  78. --state 'open' \
  79. --head "${SOURCE_BRANCH}" \
  80. --base "${TARGET_BRANCH}" \
  81. --json 'number' \
  82. --jq 'length')"
  83. if [ "${PR_COUNT}" -eq 0 ]; then
  84. gh pr create \
  85. --repo "${REPO}" \
  86. --head "${SOURCE_BRANCH}" \
  87. --base "${TARGET_BRANCH}" \
  88. --title "Update copyright years from ${OLD_YEAR} to ${NEW_YEAR}" \
  89. --body "This is an automated PR that updates the copyright years from \`${OLD_YEAR}\` to \`${NEW_YEAR}\`."
  90. fi