chore(deps): update actions/cache action to v4 #574
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: create change files | |
on: | |
pull_request: | |
types: [opened, synchronize, reopened, edited] | |
jobs: | |
run: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v2 | |
with: | |
# by default, GitHub checks out a detached head on an ephemeral pull ref. Check out the actual branch since `rush change` relies on real branches: | |
repository: ${{ github.event.pull_request.head.repo.full_name }} | |
ref: ${{ github.head_ref }} | |
fetch-depth: 0 | |
# todo: use GITHUB_TOKEN, if https://github.community/t/triggering-a-new-workflow-from-another-workflow/16250/2 is ever addressed | |
token: ${{ secrets.GH_CI_TOKEN || github.token }} | |
- uses: actions/setup-node@v2 | |
with: | |
node-version: 12.x | |
- name: rush install | |
run: node common/scripts/install-run-rush install | |
- name: configure git username and email | |
run: | | |
# this needs to run before `rush change` | |
git config --global user.email "${{ github.event.pull_request.user.login }}@users.noreply.github.com" | |
git config --global user.name "${{ github.event.pull_request.user.login }}" | |
CLONE_URL_NO_SUFFIX=$(echo "${{ github.event.repository.clone_url }}" | sed -E "s/\.git$//") | |
git remote add upstream $CLONE_URL_NO_SUFFIX | |
git fetch upstream | |
- name: patch rush to allow for overwriteable changefiles | |
run: | | |
# hack: workaround https://github.com/microsoft/rushstack/issues/2195 by replacing the suffix of | |
# the generated changefile with a suffix of pr-123 where 123 is the pull request number. this | |
# makes sure only one change file is generated, and it gets updated when the pull request is updated | |
# | |
# e.g. if the pull request owner adds "BREAKING CHANGE" to the body, the change file will update | |
# to type 'major' | |
FILE_TO_BE_EDITED="common/temp/install-run/@[email protected]/node_modules/@microsoft/rush-lib/lib/api/ChangeFile.js" | |
TO_BE_REPLACED='_getTimestamp(useSeconds = false) {' | |
REPLACEMENT="$TO_BE_REPLACED \n return 'pr-${{ github.event.pull_request.number }}' // patched_code \n" | |
echo "patching $TO_BE_REPLACED in $FILE_TO_BE_EDITED" | |
sed -i "s~$TO_BE_REPLACED~$REPLACEMENT~g" $FILE_TO_BE_EDITED | |
echo "grepping for patched code, this will fail if rush code changed recently, and the sed command didn't replace anything" | |
cat $FILE_TO_BE_EDITED | grep patched_code | |
- name: create or update change files - SEE LOGS FOR COMMAND TO FIX FAILING PRs | |
env: | |
PR_BODY: ${{ github.event.pull_request.body }} | |
PR_TITLE: ${{ github.event.pull_request.title }} | |
PR_NUMBER: ${{ github.event.pull_request.number }} | |
PR_AUTHOR: ${{ github.event.pull_request.user.login }} | |
REPO_OWNER: ${{ github.repository_owner }} | |
run: | | |
# do basic pull request title/body parsing to figure out if it's a major, minor or patch change | |
# it'd probably be a good idea to use something like @commitlint for this, but it's an annoyingly | |
# big dependency, with multiple peers and required config files, for such a simple task | |
# this gist has a list of the allowed types (technically `chore` isn't one) https://gist.github.com/brianclements/841ea7bffdb01346392c | |
MAJOR_CHANGE_MATCH=$(echo "$PR_TITLE $PR_BODY" | grep 'BREAKING CHANGE' || echo '') | |
PATCH_CHANGE_MATCH=$(echo "$PR_TITLE" | grep -E '^(build|chore|ci|docs|fix|perf|refactor|style|test)[:\(]' || echo '') | |
BUMP_TYPE=minor | |
if [ -n "$MAJOR_CHANGE_MATCH" ]; then | |
BUMP_TYPE=major | |
PR_TITLE="$PR_TITLE ($MAJOR_CHANGE_MATCH)" | |
elif [ -n "$PATCH_CHANGE_MATCH" ]; then | |
BUMP_TYPE=patch | |
fi | |
MESSAGE="$PR_TITLE (#$PR_NUMBER)" | |
if [ "$PR_AUTHOR" != "$REPO_OWNER" ]; then | |
MESSAGE="$MESSAGE - @$PR_AUTHOR" | |
fi | |
node common/scripts/install-run-rush change --message "$MESSAGE" --overwrite --bulk --bump-type $BUMP_TYPE | |
GIT_STATUS=$(git status --porcelain) | |
git add -A | |
DIFF_BASE64=$(git diff --cached | base64 -w 0) | |
APPLY_PATCH_COMMAND="echo $DIFF_BASE64 | base64 --decode | git apply" | |
if [ -z "$GIT_STATUS" ]; then | |
echo "no changes made" | |
elif [ -n "${{ secrets.GH_CI_TOKEN }}" ]; then | |
echo "Attempting to push a commit to your branch. If this fails, try running the following command locally, committing the changes and pushing manually:" | |
echo "$APPLY_PATCH_COMMAND" | |
git commit -m "chore: change files" | |
git push | |
echo "::set-env name=CHANGE_HASH::$(git rev-parse --short HEAD)" | |
else | |
echo "changes were made, but can't push to the branch from this context" | |
echo "run the following commands locally from a bash-like shell, then commit and push the changes it generates to your branch:" | |
echo "git clone ${{ github.event.pull_request.head.repo.clone_url }} tmp" | |
echo "cd tmp" | |
echo "git checkout ${{ github.head_ref }}" | |
echo "$APPLY_PATCH_COMMAND" | |
echo "git commit -am 'chore: changefile'" | |
exit 1 | |
fi | |
- name: Comment on PR | |
uses: actions/github-script@v3 | |
if: ${{ env.CHANGE_HASH != null }} | |
with: | |
script: | | |
github.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: context.payload.pull_request.number, | |
body: `@${{ github.event.pull_request.user.login }} - changes were pushed to your branch: ${process.env.CHANGE_HASH}.`, | |
}) |