Skip to content

Update Data from Source #50

Update Data from Source

Update Data from Source #50

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
steps:
- name: Checkout dvalin-data repo
uses: actions/checkout@v4
with:
repository: "dval-in/dvalin-data"
token: ${{ secrets.GITHUB_TOKEN }}
ref: "main"
path: "dvalin-data"
- uses: actions/setup-node@v4
with:
node-version: lts/Iron
- uses: pnpm/action-setup@v3
name: Install pnpm
with:
version: 9
run_install: true
- name: Read .last_processed_commit
id: read_commit
run: |
LAST_PROCESSED_COMMIT=$(cat dvalin-data/.last_processed_commit)
echo "LAST_PROCESSED_COMMIT=$LAST_PROCESSED_COMMIT" >> $GITHUB_ENV
- name: Fetch commit history since last processed
run: |
# Initialize the variables
COMMITS=""
GI_VERSIONS=""
# Clone the repository
git clone --bare https://github.com/dvaJi/genshin-data.git genshin-data-bare
cd genshin-data-bare
# Use git log to fetch commits and process output to a single line
if [ -z "$LAST_PROCESSED_COMMIT" ]; then
COMMITS=$(git log --reverse --grep='^feat: update GI data v[0-9]\+\.[0-9]\+$' --format='%H' HEAD | tr '\n' ' ')
else
COMMITS=$(git log --reverse --grep='^feat: update GI data v[0-9]\+\.[0-9]\+$' --format='%H' $LAST_PROCESSED_COMMIT..HEAD | tr '\n' ' ')
fi
# Extract the version from each matching commit message
if [ -n "$COMMITS" ]; then
for COMMIT in $COMMITS; do
# Extract the commit message for the matching commit
COMMIT_MESSAGE=$(git log --format='%s' -n 1 $COMMIT)
# Extract the version from the commit message
VERSION=$(echo "$COMMIT_MESSAGE" | grep -o 'v[0-9]\+\.[0-9]\+' | sed 's/^v//')
if [ -n "$VERSION" ]; then
# Append the version to the GI_VERSIONS variable
GI_VERSIONS="$GI_VERSIONS $VERSION"
fi
done
# Remove leading and trailing spaces
GI_VERSIONS=$(echo $GI_VERSIONS | xargs)
fi
# Export variables to GitHub environment
echo "COMMITS=$COMMITS" >> $GITHUB_ENV
echo "GI_VERSIONS=$GI_VERSIONS" >> $GITHUB_ENV
# Print variables for debugging
echo "COMMITS: $COMMITS"
echo "GI_VERSIONS: $GI_VERSIONS"
# Clean up
cd ..
rm -rf genshin-data-bare
- name: Exit if no new commits
if: ${{ env.COMMITS == '' }}
run: echo "No new commits to process. Exiting."
continue-on-error: true
- name: create a new branch
if: ${{ env.COMMITS != '' }}
run: |
cd dvalin-data
git config --global user.email "[email protected]"
git config --global user.name "updater davlin-data"
BRANCH_NAME="update-$(echo ${{ env.GI_VERSIONS }} | tr ' ' '-')"
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
git checkout -b $BRANCH_NAME
git push origin $BRANCH_NAME
- name: Checkout dvalin-data repo on new branch
if: ${{ env.COMMITS != '' }}
uses: actions/checkout@v4
with:
repository: "dval-in/dvalin-data"
token: ${{ secrets.GITHUB_TOKEN }}
ref: ${{ env.BRANCH_NAME }}
path: "dvalin-data"
- name: Get all changed files
if: ${{ env.COMMITS != '' }}
run: |
export PATH=$HOME/.local/share/pnpm:$PATH
cd dvalin-data
pnpm install
set -e # Ensure the script exits on first error
git clone https://github.com/dvaJi/genshin-data.git genshin-data
cd genshin-data
# Convert space-separated strings into newline-separated strings for easier processing in sh
COMMITS_NEWLINE=$(echo $COMMITS | tr ' ' '\n')
VERSIONS_NEWLINE=$(echo $GI_VERSIONS | tr ' ' '\n')
# Initialize counters
COMMIT_COUNTER=1
VERSION_COUNTER=1
# Iterate over commits and corresponding versions using while loop
echo "$COMMITS_NEWLINE" | while read -r COMMIT; do
# Get the corresponding version
VERSION=$(echo "$VERSIONS_NEWLINE" | awk "NR==$VERSION_COUNTER")
# Process the commit
git diff-tree --no-commit-id --name-only --diff-filter=MA -r "$COMMIT" > changed_files.txt
if [ $? -ne 0 ]; then
echo "Failed to get changes for commit $COMMIT"
exit 1
fi
# Pass the version to the node script
cd ..
node ./scripts/workflow/update_file.js "$VERSION"
echo "$COMMIT" > ./.last_processed_commit
git add .
git commit -m "feat: update dvalin data v$VERSION"
# Increment the version counter
VERSION_COUNTER=$((VERSION_COUNTER + 1))
cd genshin-data
done
cd ..
rm -rf genshin-data
git push origin ${{ env.BRANCH_NAME }}
- name: create pull request
if: ${{ env.COMMITS != '' }}
run: |
cd dvalin-data
gh pr create -B main -H ${{ env.BRANCH_NAME }} --title 'Update repo ${{ env.BRANCH_NAME }}' --body ''
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}