Git action test [AllBridgeFacet v3.0.1] [@coderabbit ignore] #83
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: Install GitHub CLI | |
run: sudo apt-get install -y gh | |
# - 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 -e "$FILES" > modified_files.txt | |
echo -e "$CONTRACTS" > modified_contracts.txt | |
- name: Verify version updates on changed contracts | |
id: verify_version_changes | |
run: | | |
# Read tmp files into variables | |
FILES=$(cat modified_files.txt) | |
echo "FILES=$FILES" | |
CONTRACTS=$(cat modified_contracts.txt) | |
echo -e "CONTRACTS=$CONTRACTS" | |
# Initialize variables | |
MISSING_VERSION_TAG=() | |
MISSING_VERSION_UPDATE=() | |
UPDATED_CONTRACTS=() | |
echo "--------------------" | |
# Process each file separately | |
while IFS= read -r FILE; do | |
echo "Now checking contract: $FILE" | |
VERSION_TAG=$(grep -E '^/// @custom:version' "$FILE" || true) | |
VERSION=$(echo "$VERSION_TAG" | sed -E 's/^\/\/\/ @custom:version ([0-9]+\.[0-9]+\.[0-9]+).*$/\1/' || true) | |
# Extract the filename without extension | |
FILENAME=$(basename "$FILE" .sol) | |
echo "File: $FILENAME, Version: $VERSION, Version_Tag: $VERSION_TAG" | |
# Check if version tag exists | |
if [[ -z "$VERSION_TAG" ]]; then | |
echo "File $FILENAME does not have a version tag at all" | |
MISSING_VERSION_TAG+=("$FILE") | |
else | |
echo "File $FILENAME does have a version tag" | |
# Check if version tag was updated in this PR | |
DIFF_OUTPUT=$(git diff origin/${{ github.event.pull_request.base.ref }} HEAD "$FILE") | |
# echo "Diff output: $DIFF_OUTPUT" | |
if echo "$DIFF_OUTPUT" | grep -qE '^\+/// @custom:version'; then | |
echo "Version was updated in $FILENAME" | |
CONTRACT_NAME=$(basename "$FILE" .sol) | |
NEW_VERSION=$(echo "$VERSION_TAG" | awk '{print $NF}') | |
TARGET_STRING="${CONTRACT_NAME} v${NEW_VERSION}" | |
UPDATED_CONTRACTS+=("$TARGET_STRING") | |
else | |
echo "Version was not updated in $FILENAME" | |
MISSING_VERSION_UPDATE+=("$FILE") | |
fi | |
fi | |
echo "--------------------" | |
done <<< "$CONTRACTS" | |
# If contract files are missing a version tag, this must be corrected before continuing | |
if [[ ${#MISSING_VERSION_TAG[@]} -ne 0 ]]; then | |
echo "---------------------–" | |
echo ">>>>>>" | |
echo "The following files are missing a version tag: ${MISSING_VERSION_TAG[*]}" | |
echo "Please make sure that all contracts have a version tag and try to push your changes again." | |
exit 1 | |
fi | |
# if the version was not updated in any of the changed contracts, store the list of affected files in a tmp file | |
if [[ ${#MISSING_VERSION_UPDATE[@]} -ne 0 ]]; then | |
echo "---------------------–" | |
echo ">>>>>>" | |
echo "The following contracts have been changed but their version tags were not updated: ${MISSING_VERSION_UPDATE[*]}" | |
echo "Please make sure to update a contract's version whenever there are changes in the file." | |
exit 1 | |
# echo -e "${MISSING_VERSION_UPDATE[*]}" > missing_version_update.txt | |
fi | |
# store any contracts that were correctly updated in a tmp file so we can check the PR title after for each of those | |
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 | |
echo -e "${UPDATED_CONTRACTS_STR[*]}" > updated_contracts.txt | |
fi | |
- name: Update PR title with updated version info | |
env: | |
PR_TITLE: ${{github.event.pull_request.title}} | |
UPDATED_CONTRACTS: ${{ steps.check_versions.outputs.UPDATED_CONTRACTS }} | |
UPDATED_CONTRACTS_STR: ${{ steps.check_versions.outputs.UPDATED_CONTRACTS_STR }} | |
run: | | |
echo "PR_TITLE: $PR_TITLE" | |
echo "UPDATED_CONTRACTS: $UPDATED_CONTRACTS" | |
echo "UPDATED_CONTRACTS_STR: $UPDATED_CONTRACTS_STR" | |
echo "UPDATED_CONTRACTS_STR: ${{ env.UPDATED_CONTRACTS_STR }}" | |
echo "UPDATED_CONTRACTS_STR: ${{ github.UPDATED_CONTRACTS_STR }}" | |
echo "-----------------------------------" | |
# Read tmp files into variables | |
if [ -f updated_contracts.txt ]; then | |
UPDATED_CONTRACTS=$(cat updated_contracts.txt) | |
echo "UPDATED_CONTRACTS=$UPDATED_CONTRACTS" | |
fi | |
echo "-----------------------------------" | |
# Initialize PR title | |
PR_TITLE_UPDATED="$PR_TITLE (" | |
# Check if each target string is in the PR title | |
IFS=',' read -ra UPDATED_ARRAY <<< "$UPDATED_CONTRACTS" | |
for TARGET_STRING in "${UPDATED_ARRAY[@]}"; do | |
if [[ "$PR_TITLE_UPDATED" =~ "$TARGET_STRING" ]]; then | |
echo "PR title contains target string ($TARGET_STRING)" | |
else | |
echo "PR title does not contain target string ($TARGET_STRING)" | |
PR_TITLE_UPDATED="$PR_TITLE_UPDATED ($TARGET_STRING)" | |
fi | |
done | |
echo "Final PR Title: $PR_TITLE_UPDATED)" | |
echo "PR_TITLE=$PR_TITLE_UPDATED" >> $GITHUB_ENV | |
- name: Update PR title with updated version info | |
env: | |
PR_TITLE: ${{github.event.pull_request.title}} | |
UPDATED_PR_TITLE: ${{env.PR_TITLE}} | |
run: | | |
PR_TITLE="${{ env.PR_TITLE }}" | |
UPDATED_PR_TITLE="${{ env.UPDATED_PR_TITLE }}" | |
UPDATED_CONTRACTS="" | |
# Read tmp file | |
if [ -f updated_contracts.txt ]; then | |
UPDATED_CONTRACTS=$(cat updated_contracts.txt) | |
fi | |
# Initialize PR title | |
PR_TITLE_UPDATED="$PR_TITLE (" | |
# Check if each target string is in the PR title | |
IFS=',' read -ra UPDATED_ARRAY <<< "$UPDATED_CONTRACTS" | |
for TARGET_STRING in "${UPDATED_ARRAY[@]}"; do | |
if [[ "$PR_TITLE_UPDATED" =~ "$TARGET_STRING" ]]; then | |
continue | |
else | |
PR_TITLE_UPDATED="$PR_TITLE_UPDATED $TARGET_STRING," | |
fi | |
done | |
PR_TITLE_UPDATED="${PR_TITLE_UPDATED%,})" | |
echo "PR_TITLE=$PR_TITLE_UPDATED" >> $GITHUB_ENV | |
echo "final PR title: $PR_TITLE_UPDATED" | |
- name: Update the PR title on GitHub | |
env: | |
GH_PAT: ${{ secrets.GIT_TOKEN }} | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
PR_TITLE: ${{ env.PR_TITLE }} | |
run: | | |
unset GITHUB_TOKEN | |
echo $GH_PAT | gh auth login --with-token | |
gh pr edit ${{ github.event.pull_request.number }} --title "${{ env.PR_TITLE }}" |