-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaction.yml
187 lines (172 loc) · 6.6 KB
/
action.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
187
name: 'Check DB Migration'
description: 'Check the migration files if there is any potential lock to the tables on MySQL DB.'
author: 'Mahmoud Almasri'
inputs:
FILE_LOCATION:
required: false
default: "src/migrations/"
description: "The location of migration files"
GUIDE_DOC:
required: false
default: "https://dev.mysql.com/doc/refman/8.0/en/innodb-online-ddl-operations.html"
description: "Any documentation that you need to reference here."
LANGUAGE:
required: false
default: "NODEJS"
description: "The programing language, we support now PHP and Nodejs"
GITHUB_TOKEN:
description: "Your Github token"
required: true
outputs:
NEW_MIGRATIONS:
description: "The list of the new migration files"
value: ${{steps.list_migrations.outputs.new_migrations}}
runs:
using: "composite"
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: List new migration files using GitHub API
id: list_migrations
uses: actions/github-script@v6
with:
github-token: ${{ inputs.GITHUB_TOKEN }}
script: |
const prNumber = context.payload.pull_request.number;
const { data: files } = await github.rest.pulls.listFiles({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
});
const newMigrations = files
.filter(file => file.status === 'added' && file.filename.startsWith('${{ inputs.FILE_LOCATION }}'))
.map(file => file.filename)
.join('\n');
core.setOutput("new_migrations", newMigrations);
- name: Save migration file list
run: echo "${{ steps.list_migrations.outputs.new_migrations }}" > new_migrations.txt
shell: bash
- name: Display migration file list
run: cat new_migrations.txt
shell: bash
- name: Extract SQL from migration files
run: |
touch extracted_sql.txt
echo "LANGUAGE is: ${{ inputs.LANGUAGE }}"
# Loop through each new migration file and extract lines with SQL statements
if [[ "${{ inputs.LANGUAGE }}" == "NODEJS" ]]; then
echo "Processing NODEJS files"
while IFS= read -r file; do
echo "Processing file: $file"
if [ -f "$file" ]; then
awk '/queryRunner\.query/,/);/' "$file" | sed -E 's/.*queryRunner\.query\((.*)\);.*/\1/' >> extracted_sql.txt || echo "Failed to process $file"
else
echo "File not found: $file"
fi
done < new_migrations.txt
else
echo "Processing Non-NODEJS files"
while IFS= read -r file; do
echo "Processing file: $file"
if [ -f "$file" ]; then
awk '/->addSql/,/);/' "$file" | sed 's/.*->addSql(\"\(.*\)\".*/\1/' >> extracted_sql.txt || echo "Failed to process $file"
else
echo "File not found: $file"
fi
done < new_migrations.txt
fi
shell: bash
- name: Check for potential table locks
run: |
touch potential_locks.txt
echo "Checking for potential table locks"
echo "Extracted SQL statements:"
cat extracted_sql.txt
echo "Starting to check each SQL statement:"
# Check each SQL statement for potential table locks
while IFS= read -r sql; do
echo "Checking SQL: $sql"
if echo "$sql" | grep -iqE 'update|insert|delete|alter|create|drop|truncate'; then
echo "Potential lock detected: $sql"
echo "$sql" >> potential_locks.txt
fi
done < extracted_sql.txt
echo "Extracted SQL statements:"
cat extracted_sql.txt
echo "Potential locks detected:"
cat potential_locks.txt
found_locks=false
if [ -s potential_locks.txt ]; then
echo "Potential table locks detected:"
cat potential_locks.txt
found_locks=true
else
echo "No potential table locks detected."
fi
echo "found_locks=$found_locks" >> $GITHUB_ENV
shell: bash
- name: Display Potential Locks
run: |
found_locks=false
if [ -s potential_locks.txt ]; then
echo "Potential table locks detected:"
cat potential_locks.txt
found_locks=true
else
echo "No potential table locks detected."
fi
echo "found_locks=$found_locks" >> $GITHUB_ENV
shell: bash
- name: Add "DB-Lock" label if potential table locks are found
if: env.found_locks == 'true'
uses: actions/github-script@v6
with:
github-token: ${{ inputs.GITHUB_TOKEN }}
script: |
const prNumber = context.payload.pull_request.number;
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
labels: ['DB-Lock']
});
- name: Comment on PR with potential table locks
if: env.found_locks == 'true'
uses: actions/github-script@v6
with:
github-token: ${{ inputs.GITHUB_TOKEN }}
script: |
const prNumber = context.payload.pull_request.number;
const fs = require('fs');
const potentialLocks = fs.readFileSync('potential_locks.txt', 'utf8');
const commentIdentifier = "<!-- db-lock-comment -->";
const commentBody = `${commentIdentifier}\n### Potential Table Locks Detected\nThe following SQL statements in the migration files might lock the tables:\n\`\`\`\n${potentialLocks}\n\`\`\`\nFor more information on how to avoid DB locks, please refer to [this guide](${{ inputs.GUIDE_DOC }}).`;
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const existingComment = comments.find(comment => comment.body.includes(commentIdentifier));
if (existingComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body: commentBody,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: commentBody,
});
}
- name: Make the check status fail
if: env.found_locks == 'true'
run: |
exit 1
shell: bash
branding:
icon: 'database'
color: 'purple'