-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
43 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const fs = require('fs'); | ||
const util = require('util'); | ||
const exec = util.promisify(require('child_process').exec); | ||
|
||
async function getChangedFiles() { | ||
const { stdout } = await exec('git log --name-only --pretty=format: --max-count=3'); | ||
const files = stdout.split('\n'); | ||
const uniqueFiles = [...new Set(files)].filter(file => file.trim() !== ''); | ||
return uniqueFiles; | ||
} | ||
|
||
function updateReadme(files) { | ||
const readmeContent = fs.readFileSync('./README.md', 'utf8'); | ||
const startIndex = readmeContent.indexOf('<!-- readme: recent-changes -start -->') + '<!-- readme: recent-changes -start -->'.length; | ||
const endIndex = readmeContent.indexOf('<!-- readme: recent-changes -end -->'); | ||
|
||
let newContent = readmeContent.substring(0, startIndex) + '\n'; | ||
files.forEach((file, index) => { | ||
newContent += `${index+1}. ${file}\n`; | ||
}); | ||
newContent += readmeContent.substring(endIndex); | ||
|
||
fs.writeFileSync('./README.md', newContent); | ||
} | ||
|
||
getChangedFiles() | ||
.then(files => updateReadme(files)) | ||
.catch(error => { | ||
console.error(error); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,21 @@ | ||
name: Recent changes | ||
name: Update README | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
- main # Or replace with your default branch | ||
|
||
jobs: | ||
update_readme: | ||
update-readme: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Check out repository | ||
uses: actions/checkout@v2 | ||
|
||
- name: Update README with recent changes | ||
run: | | ||
# Fetch the latest 3 commits | ||
recent_changes=$(git log -3 --pretty=format:"%h %s" --name-only) | ||
# Extract changed filenames from the recent_changes | ||
changed_files=$(echo "$recent_changes" | grep -P '^(?=\S)(?!\d+\s)' | uniq) | ||
# Format the changed files with numbering | ||
formatted_changes="" | ||
counter=1 | ||
while read -r line; do | ||
formatted_changes+="$counter. $line\n" | ||
counter=$((counter+1)) | ||
done <<< "$changed_files" | ||
# Update the README.md file | ||
sed -i '/<!-- readme: recent-changes -start -->/,/<!-- readme: recent-changes -end -->/c\<!-- readme: recent-changes -start -->\n'"$formatted_changes"'<!-- readme: recent-changes -end -->' README.md | ||
- name: Commit and push changes | ||
- uses: actions/checkout@v2 | ||
- name: Run update script | ||
run: node .github/scripts/update-readme.js | ||
- name: Commit and push if changed | ||
run: | | ||
git config --local user.email "[email protected]" | ||
git config --local user.name "GitHub Action" | ||
git add README.md | ||
git commit -m "Update README with recent changes" || echo "No changes to commit" | ||
git push | ||
git diff | ||
git config --global user.email "[email protected]" | ||
git config --global user.name "GitHub Action" | ||
git commit -am "Update README" -a || echo "No changes to commit" | ||
git push |