Update License #20
This file contains 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: Update License | |
on: | |
schedule: | |
- cron: '0 0 1 1 *' | |
workflow_dispatch: | |
env: | |
BRANCH: auto/update-license | |
REGEX: ^(((\/\/|#) )?Copyright \(c\) )[0-9]{4}(, WNProject Authors(\. All rights reserved.)?)$ | |
jobs: | |
update-license: | |
name: Update License | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/[email protected] | |
- name: Determine year | |
id: year | |
run: echo "value=$(date +%Y)" >> $GITHUB_OUTPUT | |
- name: Switch to 'auto/update-license' | |
run: | | |
git fetch origin +refs/heads/$BRANCH:refs/heads/$BRANCH || true | |
git checkout $BRANCH || git checkout -b $BRANCH | |
- name: Update license | |
run: > | |
find . -type f -not -path './.git/*' -and | |
-iregex '^.*\(\.\(h\|c\|cpp\|inl\|h\.in\|s\|py\|wns\)\|LICENSE\)$' | |
-exec sed -i -E "s/$REGEX/\1$YEAR\4/" {} \; | |
env: | |
YEAR: ${{steps.year.outputs.value}} | |
- name: Check for changes | |
id: changes | |
run: > | |
git diff --exit-code && | |
echo 'exist=false' >> $GITHUB_OUTPUT || | |
echo 'exist=true' >> $GITHUB_OUTPUT | |
- name: Create commit | |
if: steps.changes.outputs.exist == 'true' | |
run: | | |
git reset --hard origin/master | |
find . -type f -not -path './.git/*' -and \ | |
-iregex '^.*\(\.\(h\|c\|cpp\|inl\|h\.in\|s\|py\|wns\)\|LICENSE\)$' \ | |
-exec sed -i -E "s/$REGEX/\1$YEAR\4/" {} \; | |
git config user.name $COMMIT_USERNAME | |
git config user.email $COMMIT_EMAIL | |
git add -u | |
git commit -m "Update license year to $YEAR" | |
git push --force origin $BRANCH | |
env: | |
COMMIT_USERNAME: ${{secrets.UPDATE_LICENSE_USER}} | |
COMMIT_EMAIL: ${{secrets.UPDATE_LICENSE_EMAIL}} | |
YEAR: ${{steps.year.outputs.value}} | |
- name: Create pull request | |
if: steps.changes.outputs.exist == 'true' | |
uses: actions/github-script@v5 | |
with: | |
script: | | |
const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/'); | |
const base = 'refs/heads/master'; | |
const head = `refs/heads/${process.env.BRANCH}`; | |
const labels = ['auto']; | |
const title = `Update license year to ${process.env.YEAR}`; | |
const pullRequests = await github.rest.pulls.list({ | |
head: `${owner}:${head}`, | |
owner, repo, base | |
}); | |
if (pullRequests.data.length === 0) { | |
const createResult = await github.rest.pulls.create({ | |
title, owner, repo, head, base | |
}); | |
github.rest.issues.addLabels({ | |
issue_number: createResult.data.number, | |
owner, repo, labels | |
}); | |
console.log('Created new pull request'); | |
return createResult.data.number; | |
} else { | |
const prNumber = pullRequests.data[0].number; | |
github.rest.issues.update({ | |
issue_number: prNumber, | |
title, owner, repo, labels | |
}); | |
console.log(`Pull request already exists, updated #${prNumber}`); | |
return prNumber; | |
} | |
result-encoding: string | |
github-token: ${{secrets.UPDATE_LICENSE_TOKEN}} | |
env: | |
YEAR: ${{steps.year.outputs.value}} |