ProxmoxVE/.github/workflows/update_json_date.yml

122 lines
4.4 KiB
YAML
Raw Normal View History

2025-02-11 08:59:09 +00:00
name: Update JSON Date via GitHub App
2025-02-11 08:43:21 +00:00
on:
schedule:
2025-02-11 08:43:21 +00:00
- cron: "0 */6 * * *" # Läuft alle 6 Stunden
workflow_dispatch:
jobs:
2025-02-11 08:43:21 +00:00
check-open-prs:
runs-on: ubuntu-latest
steps:
2025-02-11 08:43:21 +00:00
- name: Checkout Repository
uses: actions/checkout@v4
2025-02-11 08:59:09 +00:00
- name: Install Dependencies
run: |
sudo apt update && sudo apt install -y jq
- name: Authenticate GitHub App
id: auth
run: |
echo "Generating JWT for GitHub App authentication..."
# Header und Payload Base64 encodieren
HEADER_B64=$(echo -n '{"alg":"RS256","typ":"JWT"}' | openssl base64 -A | tr -d '=' | tr '/+' '_-')
NOW=$(date +%s)
EXP=$((NOW + 600)) # 10 Minuten gültig
PAYLOAD_B64=$(echo -n "{\"iat\":$NOW,\"exp\":$EXP,\"iss\":${{ secrets.JSON_APP_ID }}}" | openssl base64 -A | tr -d '=' | tr '/+' '_-')
# Signatur mit dem privaten Schlüssel erstellen
SIGNATURE=$(echo -n "$HEADER_B64.$PAYLOAD_B64" | openssl dgst -sha256 -sign <(echo "${{ secrets.JSON_APP_KEY }}") | openssl base64 -A | tr -d '=' | tr '/+' '_-')
# Komplette JWT-Token-Zeichenkette erstellen
JWT="$HEADER_B64.$PAYLOAD_B64.$SIGNATURE"
# App-Installation abrufen
INSTALLATION_ID=$(curl -s -X GET -H "Authorization: Bearer $JWT" -H "Accept: application/vnd.github+json" \
https://api.github.com/app/installations | jq -r '.[0].id')
# Access Token generieren
ACCESS_TOKEN=$(curl -s -X POST -H "Authorization: Bearer $JWT" -H "Accept: application/vnd.github+json" \
https://api.github.com/app/installations/$INSTALLATION_ID/access_tokens | jq -r '.token')
echo "GH_ACCESS_TOKEN=$ACCESS_TOKEN" >> $GITHUB_ENV
2025-02-11 08:43:21 +00:00
- name: Get Open PRs
run: |
2025-02-11 08:44:17 +00:00
echo "Fetching open PRs..."
2025-02-11 08:59:09 +00:00
PRS=$(gh pr list --state open --json number,headRepositoryOwner,headRefName \
--jq '.[] | {number: .number, repo: .headRepositoryOwner, branch: .headRefName}' || echo "")
2025-02-11 08:44:17 +00:00
if [[ -z "$PRS" ]]; then
echo "No open PRs found."
exit 0
fi
2025-02-11 08:59:09 +00:00
echo "$PRS" | jq -c '.[]' > pr_list.json
env:
2025-02-11 08:59:09 +00:00
GH_TOKEN: ${{ env.GH_ACCESS_TOKEN }}
2025-02-11 08:43:21 +00:00
- name: Process Each PR
run: |
2025-02-11 08:44:17 +00:00
TODAY=$(date -u +"%Y-%m-%d")
2025-02-11 08:46:06 +00:00
2025-02-11 08:59:09 +00:00
while read -r PR_ENTRY; do
PR_NUMBER=$(echo "$PR_ENTRY" | jq -r '.number')
PR_REPO=$(echo "$PR_ENTRY" | jq -r '.repo')
PR_BRANCH=$(echo "$PR_ENTRY" | jq -r '.branch')
2025-02-11 08:43:21 +00:00
2025-02-11 08:59:09 +00:00
echo "Processing PR #$PR_NUMBER from $PR_REPO:$PR_BRANCH"
# Fork klonen mit App-Token
git clone --depth=1 https://x-access-token:${{ env.GH_ACCESS_TOKEN }}@github.com/$PR_REPO/ProxmoxVE.git
cd ProxmoxVE || exit 1
2025-02-11 08:46:06 +00:00
2025-02-11 08:59:09 +00:00
# PR-Branch auschecken
git fetch origin "$PR_BRANCH"
git checkout "$PR_BRANCH"
2025-02-11 08:43:21 +00:00
# Get newly added JSON files
2025-02-11 08:59:09 +00:00
NEW_JSON_FILES=$(gh api repos/${{ github.repository }}/pulls/$PR_NUMBER/files \
--jq '.[] | select(.status == "added") | .filename' | grep '^json/.*\.json$' || true)
2025-02-11 08:43:21 +00:00
if [[ -z "$NEW_JSON_FILES" ]]; then
echo "No new JSON files in PR #$PR_NUMBER"
2025-02-11 08:59:09 +00:00
cd ..
rm -rf ProxmoxVE
2025-02-11 08:43:21 +00:00
continue
fi
UPDATED=false
2025-02-11 08:43:21 +00:00
for FILE in $NEW_JSON_FILES; do
2025-02-11 08:59:09 +00:00
if [[ -f "$FILE" ]]; then
DATE_IN_JSON=$(jq -r '.date_created' "$FILE")
2025-02-11 08:59:09 +00:00
if [[ "$DATE_IN_JSON" != "$TODAY" ]]; then
echo "Updating $FILE: $DATE_IN_JSON -> $TODAY"
jq --arg date "$TODAY" '.date_created = $date' "$FILE" > tmp.json && mv tmp.json "$FILE"
UPDATED=true
fi
else
echo "File $FILE not found in the forked repo"
fi
2025-02-11 08:43:21 +00:00
done
if [[ "$UPDATED" == "true" ]]; then
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git commit -am "Update date_created in new JSON files"
2025-02-11 08:59:09 +00:00
git push origin "$PR_BRANCH"
echo "Updated PR #$PR_NUMBER and pushed changes."
2025-02-11 08:43:21 +00:00
else
echo "No updates needed for PR #$PR_NUMBER"
fi
2025-02-11 08:59:09 +00:00
cd ..
rm -rf ProxmoxVE
done < pr_list.json
env:
2025-02-11 08:59:09 +00:00
GH_TOKEN: ${{ env.GH_ACCESS_TOKEN }}