-
Notifications
You must be signed in to change notification settings - Fork 516
163 lines (148 loc) · 6.06 KB
/
verify-lib-on-pr-open.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
name: Verify package library
on:
pull_request:
# by default, this will run on [opened, synchronize, reopened] events
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request
# only consider PRs with changes to rust files
paths:
- '**.rs'
permissions:
id-token: write
pull-requests: write # make comment on PR
env:
NODE_VERSION: 17.0.1
ANCHOR_VERSION: 0.26.0
SOLANA_VERSION_STABLE: 1.14.13
RUST_TOOLCHAIN: stable
jobs:
dump-context:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Dump GitHub context
env:
GITHUB_CONTEXT: ${{ toJson(github) }}
run: echo "$GITHUB_CONTEXT"
get-changes-scope:
# only listen for PRs opened against master
if: contains(github.base_ref, 'master')
runs-on: ubuntu-latest
outputs:
packages: ${{ steps.filter-out-excluded-pkgs.outputs.result }}
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
- name: List changed files
uses: ./.github/actions/list-changed-files
id: list-changed-files
with:
pull-number: ${{ github.event.pull_request.number }}
# map fetched changed files to package / lang (list)
- name: Get scope of changed packages
uses: actions/github-script@v4
id: map-changed-files-to-pkg
with:
script: |
const files = ${{ steps.list-changed-files.outputs.changed-files }}
const isRustFile = /.*\.rs$/g;
const uniqueFilesObj = files
.filter(f => isRustFile.test(f))
.reduce((p, file) => {
const pkgForFile = file.split("/")[0];
if (!p[pkgForFile]) p[pkgForFile] = 1
return p
}, {})
return Array.from(Object.keys(uniqueFilesObj))
- name: Exclude packages
uses: actions/github-script@v4
id: filter-out-excluded-pkgs
with:
# exclude packages either do not have `js` dirs or do not have `api:gen` scripts in their local package.json
script: |
const exclude = ['core']
const files = ${{ steps.map-changed-files-to-pkg.outputs.result }}
const result = files
.filter(f => !exclude.includes(f))
.join(" ")
return result.length > 0 ? result : null
- name: Print packages to verify
run: echo "${{ steps.filter-out-excluded-pkgs.outputs.result }}"
shell: bash
verify-package-library:
needs: [get-changes-scope]
# note: checking for empty string just doesn't work, so we explicitly return and check null in the case that there's nothing to verify
if: ${{ needs.get-changes-scope.outputs.packages != 'null' }}
runs-on: ubuntu-latest-16-cores
steps:
# branch should be the branch from which the PR is opened
- uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
- uses: ./.github/actions/install-linux-build-deps
- uses: ./.github/actions/install-rust
with:
toolchain: ${{ env.RUST_TOOLCHAIN }}
- uses: ./.github/actions/install-solana
with:
solana_version: ${{ env.SOLANA_VERSION_STABLE }}
- name: Generate library for modified packages
id: generate-package-lib
# YARN_ENABLE_IMMUTABLE_INSTALLS=false is needed otherwise, we get this error:
# `The lockfile would have been modified by this install, which is explicitly forbidden.`
# Additionally, we aren't committing changes here.
run: |
pkgs=${{ needs.get-changes-scope.outputs.packages }}
pkgs=(${pkgs//\"})
for pkg in "${pkgs[@]}"; do
if [ ! -d "$pkg/js" ]; then
echo "$pkg/js does not exist - skipping"
continue
fi
echo ">> changing dir $pkg/js"
cd "$pkg/js"
echo ">> setup local pcakage dependencies with yarn"
YARN_ENABLE_IMMUTABLE_INSTALLS=false yarn install
echo ">> generate library"
yarn api:gen
echo ">> git status"
# ignore any possible yarn.lock changes
git status
if [[ $(git diff --stat) != '' ]]; then
echo ">> git diff"
git diff
echo ">> $pkg needs changes - breaking early"
echo "::set-output name=failed::true"
echo "::set-output name=failed-package::$pkg"
break
else
echo "::set-output name=failed::false"
fi
echo ">> regress 2 dirs"
cd ../..
done
- name: Print step outputs on success
if: steps.generate-package-lib.outputs.failed == 'false'
run: |
echo "${{ steps.generate-package-lib.outputs.failed }}"
shell: bash
- name: Print step outputs on failure
if: steps.generate-package-lib.outputs.failed == 'true'
run: |
echo "${{ steps.generate-package-lib.outputs.failed }}"
echo "${{ steps.generate-package-lib.outputs.failed-package }}"
shell: bash
# we don't want failing job to necessarily block a PR, so add an informative comment to the PR
- uses: actions/github-script@v4
if: steps.generate-package-lib.outputs.failed == 'true'
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'Workflow `Verify package library` found differences when running `yarn api:gen` in the JS lib for `${{ steps.generate-package-lib.outputs.failed-package }}`. Please see the job for more details: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}.'
})