-
Notifications
You must be signed in to change notification settings - Fork 3
157 lines (134 loc) · 5.45 KB
/
update-genshin.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
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 }}