-
Notifications
You must be signed in to change notification settings - Fork 48
186 lines (156 loc) · 7.16 KB
/
versionCheck.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
name: Version Check
on:
pull_request:
types: [opened, edited, synchronize]
jobs:
check-version:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for all branches
- name: Fetch base branch
run: git fetch origin +refs/heads/${{ github.event.pull_request.base.ref }}:refs/remotes/origin/${{ github.event.pull_request.base.ref }}
- name: Get list of modified files
id: modified_files
run: |
BASE_REF="${{ github.event.pull_request.base.ref }}"
# get all files modified by this PR
FILES=$(git diff --name-only origin/${BASE_REF} HEAD)
# make sure that modified files of this PR are available
if [[ -z $FILES ]]; then
echo "No files found. This should not happen. Please check the code of the Github action"
exit 1
fi
# Initialize empty variables
CONTRACTS=""
# go through all file names and identify facet, periphery & diamond contracts (other contracts dont have versioning)
while IFS= read -r FILE; do
if echo "$FILE" | grep -E '^src/Facets/.*\.sol$'; then
# facet found
CONTRACTS="${CONTRACTS}${FILE}"$'\n'
elif echo "$FILE" | grep -E '^src/Periphery/.*\.sol$'; then
# periphery found
CONTRACTS="${CONTRACTS}${FILE}"$'\n'
elif echo "$FILE" | grep -E '^src/.*\.sol$'; then
# diamond contract found
CONTRACTS="${CONTRACTS}${FILE}"$'\n'
fi
done <<< "$FILES"
# if none found, exit here as there is nothing to do
if [[ -z "$CONTRACTS" ]]; then
echo "No facets or periphery contracts found in files modified/added by this PR"
exit 0
fi
# Write filenames to temporary files (using variables here was causing issues due to the file names)
echo "$FILES" > modified_files.txt
echo "$CONTRACTS" > modified_contracts.txt
- name: Check version tags and update PR title
id: check_versions
run: |
BASE_REF="${{ github.event.pull_request.base.ref }}"
# Read tmp files into variables
FILES=$(cat modified_files.txt)
echo "FILES=$FILES"
CONTRACTS=$(cat modified_contracts.txt)
echo "CONTRACTS=$CONTRACTS"
# Initialize variables
MISSING_VERSION_TAG=()
MISSING_VERSION_UPDATE=()
UPDATED_CONTRACTS=()
echo "--------------------"
# Process each file separately
while IFS= read -r FILE; do
VERSION_TAG=$(grep -E '^/// @custom:version' "$FILE" || true)
# Check if version tag exists
if [[ -z "$VERSION_TAG" ]]; then
MISSING_VERSION_TAG+=("$FILE")
else
# Check if version tag was updated in this PR
if git diff -U0 origin/${BASE_REF} HEAD "$FILE" | grep -qE '^@@.*\+/// @custom:version'; then
# version was updated
CONTRACT_NAME=$(basename "$FILE" .sol)
NEW_VERSION=$(echo "$VERSION_TAG" | awk '{print $NF}')
UPDATED_CONTRACTS+=("$CONTRACT_NAME v$NEW_VERSION")
fi
fi
done <<< "$CONTRACTS"
# Check for missing version updates
while IFS= read -r FILE; do
if [[ "$FILE" == *.sol ]]; then
# extract the version from the contract
VERSION_TAG=$(grep -E '^/// @custom:version' "$FILE" || true)
VERSION=$(grep -E '^/// @custom:version' <filename> | sed -E 's/^.*/// @custom:version ([0-9]+\.[0-9]+\.[0-9]+).*$/\1/' || true)
echo "File: $FILE || Version: $VERSION_TAG || Version: $VERSION"
# make sure version was extracted successfully
if [[ -z "$VERSION" ]];
echo "Could not extract contract version from contract $FILE. Please check if the contract is OK, otherwise there is a problem in this Github action"
exit 1
fi
# if version is available but was not updated
if [[ -n "$VERSION_TAG" ]] && ! git diff -U0 origin/${BASE_REF} HEAD "$FILE" | grep -qE '^@@.*\+/// @custom:version'; then
MISSING_VERSION_UPDATE+=("$FILE")
fi
fi
done <<< "$FILES"
# Output the results
if [[ ${#MISSING_VERSION_TAG[@]} -ne 0 ]]; then
echo "Files missing version tags: ${MISSING_VERSION_TAG[*]}"
fi
if [[ ${#MISSING_VERSION_UPDATE[@]} -ne 0 ]]; then
echo "Files with version tags not updated: ${MISSING_VERSION_UPDATE[*]}"
fi
if [[ ${#UPDATED_CONTRACTS[@]} -ne 0 ]]; then
echo "Updated contracts and versions: ${UPDATED_CONTRACTS[*]}"
UPDATED_CONTRACTS_STR=$(IFS=,; echo "${UPDATED_CONTRACTS[*]}")
echo "UPDATED_CONTRACTS=$UPDATED_CONTRACTS_STR" >> $GITHUB_ENV
fi
- name: Update PR title with updated version info
uses: actions/github-script@v5
env:
PR_TITLE: ${{github.event.pull_request.title}}
with:
script: |
echo "PR_TITLE: $PR_TITLE"
const updatedContracts = ${{env.UPDATED_CONTRACTS}}
const updatedContracts = updatedContracts.split(',');
echo "updatedContracts: $updatedContracts"
let prTitle = $PR_TITLE;
updatedContracts.forEach(contract => {
if (!prTitle.includes(contract.trim())) {
prTitle += ` (${contract.trim()})`;
}
});
github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
title: prTitle
});
# - name: Post reminder comment
# if: steps.check_versions.outputs.MISSING_VERSION_UPDATE
# uses: actions/github-script@v5
# with:
# script: |
# const MISSING_VERSION_FILES = '${{ steps.check_versions.outputs.MISSING_VERSION_UPDATE }}'.split(',');
# const body = `The following files have version tags that were not updated: ${MISSING_VERSION_FILES.join(', ')}. Please update the version tags.`;
# github.rest.issues.createComment({
# issue_number: context.issue.number,
# owner: context.repo.owner,
# repo: context.repo.repo,
# body: body
# });
# - name: Post version updates comment
# if: success()
# uses: actions/github-script@v5
# with:
# script: |
# const UPDATED_CONTRACTS = '${{ steps.check_versions.outputs.UPDATED_CONTRACTS }}'.split(',');
# const body = `The following contracts have been updated with new versions: ${UPDATED_CONTRACTS.join(', ')}.`;
# github.rest.issues.createComment({
# issue_number: context.issue.number,
# owner: context.repo.owner,
# repo: context.repo.repo,
# body: body
# });