Skip to content

Commit

Permalink
update dates GitHub Action
Browse files Browse the repository at this point in the history
  • Loading branch information
juanmaguitar committed Dec 15, 2024
1 parent c0e674e commit f0b2fce
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
53 changes: 53 additions & 0 deletions .github/workflows/scripts/update_dates.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash

echo "Starting dates update process..."

# Read examples.json
EXAMPLES_FILE="_data/examples.json"
TEMP_FILE="_data/examples.temp.json"

# Create temp file if needed
if [ ! -f "$TEMP_FILE" ]; then
cp "$EXAMPLES_FILE" "$TEMP_FILE"
fi

# Function to get Git dates for a folder
get_git_dates() {
local folder=$1

# Get first commit date (creation date)
local created=$(git log --reverse --format=%aI -- "$folder" | head -n 1)

# Get last commit date (modification date)
local lastModified=$(git log -1 --format=%aI -- "$folder")

# Return both dates in JSON format
echo "{\"created\":\"$created\",\"lastModified\":\"$lastModified\"}"
}

# Read examples.json into a variable
EXAMPLES=$(cat "$EXAMPLES_FILE")

# Process each example
UPDATED_EXAMPLES=$(echo "$EXAMPLES" | jq -c '.[]' | while read -r example; do
slug=$(echo "$example" | jq -r '.slug')
folder="plugins/$slug"
if [ -d "$folder" ]; then
# Get dates from Git history
dates=$(get_git_dates "$folder")
created=$(echo "$dates" | jq -r '.created')
lastModified=$(echo "$dates" | jq -r '.lastModified')
# Update the example with new dates
echo "$example" | jq --arg created "$created" --arg lastModified "$lastModified" \
'. + {created: $created, lastModified: $lastModified}'
else
echo "$example"
fi
done | jq -s '.')

# Write the updated content back to examples.json
echo "$UPDATED_EXAMPLES" > "$EXAMPLES_FILE"

echo "Finished dates update process. ✅"
38 changes: 38 additions & 0 deletions .github/workflows/update-dates.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Update Creation and Modification Dates

on:
workflow_dispatch:
schedule:
- cron: '0 0 1 * *' # Run monthly on the first day at midnight

jobs:
update-dates:
runs-on: ubuntu-latest
permissions:
contents: write

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for all branches and tags

- name: Make script executable
run: chmod +x .github/workflows/scripts/update_dates.sh

- name: Update dates
env:
EXAMPLES_FILE: '_data/examples.json'
run: ./.github/workflows/scripts/update_dates.sh

- name: Commit changes
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add _data/examples.json
git diff --quiet && git diff --staged --quiet || git commit -m "Update creation and modification dates"
- name: Push changes
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ github.ref }}

0 comments on commit f0b2fce

Please sign in to comment.