-
Notifications
You must be signed in to change notification settings - Fork 8
119 lines (103 loc) · 5.55 KB
/
changes.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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}.`,
})