@@ -2,9 +2,12 @@ name: Update JSON Date in PR
22
33on :
44 pull_request :
5- paths :
6- - ' json/*.json'
7- types : [opened, synchronize, reopened]
5+ branches :
6+ - main
7+ types :
8+ - opened
9+ - synchronize
10+ - reopened
811
912jobs :
1013 update_json :
@@ -13,29 +16,53 @@ jobs:
1316 steps :
1417 - name : Check out repository
1518 uses : actions/checkout@v4
16- with :
17- ref : ${{ github.head_ref }}
1819
1920 - name : Configure Git user
2021 run : |
2122 git config --global user.email "github-actions[bot]@users.noreply.github.com"
2223 git config --global user.name "github-actions[bot]"
2324
24- - name : Make script executable
25- run : chmod +x .github/workflows/scripts/update_json_date.sh
25+ - name : Get list of changed files in PR
26+ id : files
27+ uses : actions/github-script@v7
28+ with :
29+ script : |
30+ const prNumber = context.payload.pull_request.number;
31+ const prFiles = await github.rest.pulls.listFiles({
32+ owner: context.repo.owner,
33+ repo: context.repo.repo,
34+ pull_number: prNumber,
35+ });
36+
37+ // Filter for JSON files only
38+ const changedJsonFiles = prFiles.data
39+ .filter(file => file.filename.endsWith('.json'))
40+ .map(file => file.filename);
41+
42+ core.setOutput('changed_files', changedJsonFiles.join('\n'));
43+ console.log('Changed JSON files:', changedJsonFiles);
44+
45+ - name : Update dates in changed JSON files
46+ run : |
47+ changed_files="${{ steps.files.outputs.changed_files }}"
48+ if [[ -z "$changed_files" ]]; then
49+ echo "No JSON files changed in this PR. Exiting."
50+ exit 0
51+ fi
2652
27- - name : Run the update script
28- run : ./.github/workflows/scripts/update_json_date.sh
53+ for file in $changed_files; do
54+ echo "Updating $file with current date."
55+ # Your logic to update the file
56+ jq '.date_created = "'"$(date +%Y-%m-%d)"'"' "$file" > tmp.$$.json && mv tmp.$$.json "$file"
57+ done
2958
3059 - name : Commit changes if updated
3160 run : |
3261 git add *.json
3362 git diff --cached --quiet || git commit -m "Update JSON dates"
3463
35- - name : Push changes with API keys
64+ - name : Push changes
3665 env :
37- JSON_API_ID : ${{ secrets.JSON_API_ID }}
38- JSON_API_KEY : ${{ secrets.JSON_API_KEY }}
66+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
3967 run : |
40- git remote set-url origin https://$JSON_API_ID:[email protected] /${{ github.repository }}.git 4168 git push
0 commit comments