forked from stdlib-js/stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
186 lines (159 loc) · 6.56 KB
/
lint_autofix.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#/
# @license Apache-2.0
#
# Copyright (c) 2024 The Stdlib Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#/
# Workflow name:
name: lint_autofix
# Workflow triggers:
on:
# Allow the workflow to be triggered by other workflows
workflow_call:
# Define the input parameters for the workflow:
inputs:
pull_request_number:
description: 'PR number'
required: true
type: number
# Define the secrets accessible by the workflow:
secrets:
STDLIB_BOT_GITHUB_TOKEN:
description: 'GitHub token for stdlib-bot'
required: true
REPO_GITHUB_TOKEN:
description: 'GitHub token for accessing the repository'
required: true
STDLIB_BOT_GPG_PRIVATE_KEY:
description: 'GPG private key for stdlib-bot'
required: true
STDLIB_BOT_GPG_PASSPHRASE:
description: 'GPG passphrase for stdlib-bot'
required: true
# Workflow jobs:
jobs:
# Define a job for automatically fixing lint errors:
autofix:
# Define a display name:
name: 'Fix lint errors'
# Define the type of virtual host machine:
runs-on: ubuntu-latest
# Define the sequence of job steps...
steps:
# Get PR details:
- name: 'Get PR details'
id: pr-details
run: |
pr_response=$(curl -s \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: Bearer ${{ secrets.STDLIB_BOT_GITHUB_TOKEN }}" \
"https://api.github.com/repos/stdlib-js/stdlib/pulls/${{ inputs.pull_request_number }}")
# Escape control characters:
pr_response=$(echo "$pr_response" | tr -d '\000-\031')
# Extract the needed details:
pr_branch=$(echo "$pr_response" | jq -r '.head.ref') # PR's branch
pr_repo_full_name=$(echo "$pr_response" | jq -r '.head.repo.full_name') # PR's repo full name
# Set outputs for the branch and repository:
echo "branch=$pr_branch" >> $GITHUB_OUTPUT
echo "repository=$pr_repo_full_name" >> $GITHUB_OUTPUT
# Checkout the repository:
- name: 'Checkout repository'
# Pin action to full length commit SHA
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
# Refers to the branch name of the branch being pushed:
ref: ${{ steps.pr-details.outputs.branch }}
# Refers to the repository name:
repository: ${{ steps.pr-details.outputs.repository }}
# Token for accessing the repository:
token: ${{ secrets.REPO_GITHUB_TOKEN }}
# File path to checkout to:
path: './'
# Install Node.js:
- name: 'Install Node.js'
# Pin action to full length commit SHA
uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0
with:
node-version: '20' # 'lts/*'
timeout-minutes: 5
# Install dependencies (accounting for possible network failures, etc, when installing node module dependencies):
- name: 'Install dependencies'
run: |
make install-node-modules || make install-node-modules || make install-node-modules
timeout-minutes: 15
# Initialize development environment:
- name: 'Initialize development environment'
run: |
make init
timeout-minutes: 5
# Get list of changed files:
- name: 'Get list of changed files'
id: changed-files
run: |
page=1
files=""
while true; do
changed_files=$(curl -s -H "Accept: application/vnd.github.v3+json" -H "Authorization: Bearer ${{ secrets.STDLIB_BOT_GITHUB_TOKEN
}}" "https://api.github.com/repos/stdlib-js/stdlib/pulls/${{ inputs.pull_request_number }}/files?page=$page&per_page=100" | jq -r '.[] | .filename')
if [ -z "$changed_files" ]; then
break
fi
files="$files $changed_files"
page=$((page+1))
done
files=$(echo "$files" | tr '\n' ' ' | sed 's/^ //;s/ $//')
echo "files=${files}" >> $GITHUB_OUTPUT
# Fix JavaScript lint errors:
- name: 'Fix JavaScript lint errors'
id: fix-lint-errors
run: |
files="${{ steps.changed-files.outputs.files }}"
FIX=1 . "$GITHUB_WORKSPACE/.github/workflows/scripts/lint_javascript_files" "$files" || true
# Add missing trailing newlines:
- name: 'Add missing trailing newlines'
id: add-trailing-newlines
run: |
files="${{ steps.changed-files.outputs.files }}"
. "$GITHUB_WORKSPACE/.github/workflows/scripts/add_trailing_newlines" "$files"
# Remove related packages from README.md files:
- name: 'Remove related packages from README.md files'
run: |
files="${{ steps.changed-files.outputs.files }}"
. "$GITHUB_WORKSPACE/.github/workflows/scripts/remove_related_packages" "$files"
# Disable Git hooks:
- name: 'Disable Git hooks'
run: |
rm -rf .git/hooks
# Import GPG key to sign commits:
- name: 'Import GPG key to sign commits'
# Pin action to full length commit SHA
uses: crazy-max/ghaction-import-gpg@cb9bde2e2525e640591a934b1fd28eef1dcaf5e5 # v6.2.0
with:
gpg_private_key: ${{ secrets.STDLIB_BOT_GPG_PRIVATE_KEY }}
passphrase: ${{ secrets.STDLIB_BOT_GPG_PASSPHRASE }}
git_user_signingkey: true
git_commit_gpgsign: true
# Commit and push changes:
- name: 'Commit and push changes'
env:
REPO_GITHUB_TOKEN: ${{ secrets.REPO_GITHUB_TOKEN }}
USER_NAME: stdlib-bot
BRANCH_NAME: ${{ steps.pr-details.outputs.branch }}
REPO_NAME: ${{ steps.pr-details.outputs.repository }}
run: |
git config --local user.email "[email protected]"
git config --local user.name "$USER_NAME"
git add .
git commit -m "fix: resolve lint errors"
git push "https://$USER_NAME:[email protected]/$REPO_NAME.git" $BRANCH_NAME