-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add file modification history update script #121
Add file modification history update script #121
Conversation
@shivammathur would you possibly know how to do the git commit hooks? |
To create a pre-commit hook, we have to create a executable But to enforce that this file is there on all clones, we have a couple of options. If we can add composer to this project, we can run a post install script that copies the pre-commit file to git/hooks/pre-commit. Another alternative is using https://pre-commit.com/ |
@shivammathur Thanks for the explanation. Maybe a pre-commit hook wasn't a good idea for this problem after all. I think there is a way to achieve the same thing in the language repos by the workflow below. I do have some questions though: Would this workflow trigger for every merge/revert on the repo? on:
push:
branches:
- master
jobs:
if_merged:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
steps:
- name: Checkout documentation
uses: actions/checkout@v4
- name: Update fileModHistory.php
run: |
# generate the updates to fileModHistory.php
php updateModHistory.php
# add and amend generated changes to the last commit
git add fileModHistory.php
git commit --amend |
It will be better to run it on the pull_request closed event, and then check to make sure it was merged when it was closed. I would suggest making a new commit and pushing it to master from CI instead of amending the PR commit. Also to push it to master it will need write permission on the on:
pull_request:
types:
- closed
branches:
- master
permissions:
contents: write
jobs:
if_merged:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
steps:
- name: Checkout documentation
uses: actions/checkout@v4
- name: Update fileModHistory.php
env:
GITHUB_TOKEN
run: |
# generate the updates to fileModHistory.php
php updateModHistory.php
# Configure git
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
# add and amend generated changes to the last commit
git add fileModHistory.php
git commit -m "Update fileModHistory.php"
- name: Push changes
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: master |
I've discussed the adding of new commits instead of amending commits with Girgias and the I do have one question regarding the action/workflow: would this also run the update script when commits are reverted? It would be great if the reverts could use the same workflow even though there probably aren't too many PRs reverted in the |
@haszi Yes, the workflow with |
2a154af
to
ee8e71c
Compare
@shivammathur I've tested the workflow and it works like a charm. I had to add with:
fetch-depth: 0 under the "Checkout documentation" step to make my script work (it needs the entire git history) and the file modification info file is now being generated perfectly in the same repo. Could you also point me in the right direction on how to check out the |
@Girgias I opened this PR up for review as the script itself is ready. The changes in the last commit make the script accept both the path of the documentation and that of the "history file" as command line parameters, so this can be called (directly or from a GitHub workflow) from within the same or another repo. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
scripts/updateModHistory.php
Outdated
} | ||
echo "documentation path supplied\n"; | ||
if (isset($commandLineOptions["history-path"])) { | ||
echo " mod history file path supplied\n"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe have a variable for the space indent?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used strlen()
in my last commit. Will that work too or would a variable be better in this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I do not understand the purpose of having a fileModHistory.php file.
My bad! The idea behind this file and the script is that every time a PR is merged into any of the documentations, the script generates a file ( The script in this PR is for generating the file (
|
Thanks @haszi for the explanation! While this is low priority for me, I understand that it will be more important for others, so I'm generally fine with the approach. |
$newModHistoryString = ' "' . $fileName . "\" => [\n"; | ||
$newModHistoryString .= " \"modified\" => \"" . ($fileProps["modified"] ?? "") . "\",\n"; | ||
$newModHistoryString .= " \"contributors\" => [\n"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use sprintf or var_export here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like this?
$newModHistoryString = sprintf("%4s\"%s\" => [\n%8s\"modified\" => \"%s\",\n%8s\"contributors\" => [\n",
" ",
$fileName,
" ",
$fileProps["modified"] ?? "",
" "
);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possibly, but this really minor actually.
$newModHistoryString .= " ],\n"; | ||
$newModHistoryString .= " ],\n"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this work?
$newModHistoryString .= sprintf("%8s],\n%4s],\n", " ", " ");
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is good; can't test anyway, since I cannot afford a bash. :)
Add file modification history update script. This script can be used to update the
fileModHistory.php
files in the documentation repos which contain the last modification date/time and the contributor list for every file in the repo.The ideal way to use this script would be something like a pre-commit hook which would generate the update to the history file, and be merged and potentially reverted together with its parent commit. If anyone knows how to do this on GitHub, please let me know.