Skip to content

Update Data from Source #221

Update Data from Source

Update Data from Source #221

name: Update Data from Source
on:
schedule:
- cron: "0 0 * * *" # Runs every day at midnight UTC
workflow_dispatch:
jobs:
update-data:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout dvalin-data repo
uses: actions/checkout@v4
with:
repository: "dval-in/dvalin-data"
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0
- name: Setup Bun
uses: oven-sh/setup-bun@v1
with:
bun-version: latest
- name: Install dependencies
run: bun install
- name: Get last processed commit
id: get_last_commit
run: echo "last_commit=$(cat .last_processed_commit || echo '')" >> $GITHUB_OUTPUT
- name: Fetch commit history
id: fetch_commits
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
LAST_COMMIT="${{ steps.get_last_commit.outputs.last_commit }}"
REPO="dvaJi/genshin-data"
PER_PAGE=100
PAGE=1
ALL_COMMITS=""
while true; do
COMMITS=$(gh api "repos/$REPO/commits?per_page=$PER_PAGE&page=$PAGE" \
--jq '.[] | "\(.sha) \(.commit.message)"')
if [ -z "$COMMITS" ]; then
break
fi
FILTERED_COMMITS=$(echo "$COMMITS" | awk -v last="$LAST_COMMIT" '
$1 == last {exit}
/feat: update .*data .*(v|version).*[0-9]+\.[0-9]+$/ {
gsub(/version[[:space:]]?/, "v");
gsub(/v[[:space:]]?/, "v");
print
}
')
echo "$FILTERED_COMMITS"
ALL_COMMITS="$ALL_COMMITS
$FILTERED_COMMITS"
if echo "$COMMITS" | grep -q "$LAST_COMMIT"; then
break
fi
PAGE=$((PAGE + 1))
done
if [ -z "$ALL_COMMITS" ]; then
echo "No new commits to process."
echo "new_commits=" >> $GITHUB_OUTPUT
exit 0
fi
ESCAPED_COMMITS=$(echo "$ALL_COMMITS" | sed 's/$/\\n/' | tr -d '\n')
echo "new_commits<<EOF" >> $GITHUB_OUTPUT
echo -e "$ESCAPED_COMMITS" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
VERSIONS=$(echo "$ALL_COMMITS" | sed -n 's/.*v\([0-9]\+\.[0-9]\+\)$/\1/p' | tr '\n' ' ')
echo "versions=$VERSIONS" >> $GITHUB_OUTPUT
- name: Process new commits
if: steps.fetch_commits.outputs.new_commits != ''
env:
COMMITS: ${{ steps.fetch_commits.outputs.new_commits }}
VERSIONS: ${{ steps.fetch_commits.outputs.versions }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git config user.name "GitHub Actions Bot"
git config user.email "[email protected]"
git clone https://github.com/dvaJi/genshin-data.git
readarray -t COMMIT_ARRAY <<< "$COMMITS"
# Process commits in reverse order
for ((i=${#COMMIT_ARRAY[@]}-1; i>=0; i--)); do
# Remove line number and extract commit hash and message
LINE=$(echo "${COMMIT_ARRAY[i]}")
COMMIT=$(echo "$LINE" | awk '{print $1}')
MESSAGE=$(echo "$LINE" | cut -d' ' -f2-)
# Debug: Print extracted COMMIT and MESSAGE
echo "Extracted MESSAGE: $MESSAGE"
VERSION=$(echo "$MESSAGE" | sed -n 's/.*v\([0-9]\+\.[0-9]\+\)$/\1/p')
if [ "$COMMIT" == "388d16c6f1ad3118f38365b77dbf9bc32474c837" ]; then
VERSION="3.6"
fi
# Check if COMMIT is not empty and 40 characters long before proceeding
if [ -n "$COMMIT" ] && [ ${#COMMIT} -eq 40 ]; then
gh api "repos/dvaJi/genshin-data/git/trees/$COMMIT?recursive=1" \
--jq '.tree[] | select(.type == "blob") | .path' > changed_files.txt
bun run ./scripts/workflow/update_file.ts "$VERSION"
echo "$COMMIT" > .last_processed_commit
git add .
git commit -m "feat: update dvalin data v$VERSION"
else
echo "Warning: Invalid COMMIT for line: ${COMMIT_ARRAY[i]}"
fi
done
rm -rf genshin-data
- name: Create Pull Request
uses: peter-evans/[email protected]
with:
token: ${{ secrets.GITHUB_TOKEN }}
commit-message: "Auto-update: Dvalin Data Update"
title: "Auto-update: Dvalin Data v${{ steps.fetch_commits.outputs.versions }}"
body: |
This is an automated pull request to update Dvalin data.
Updates made in this pull request:
- Updated data to version(s): ${{ steps.fetch_commits.outputs.versions }}
Please review the changes and merge if everything looks correct.
branch: update-dvalin-data-${{ github.run_number }}
delete-branch: true