-
Notifications
You must be signed in to change notification settings - Fork 0
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
7 changed files
with
373 additions
and
35 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 |
---|---|---|
|
@@ -23,40 +23,21 @@ jobs: | |
git config --global user.email '[email protected]' | ||
git config --global core.quotePath false | ||
- name: Get dates meta | ||
- name: get inputs for job | ||
run: | | ||
# Load existing metadata.json into a variable | ||
if [[ -f "metadata.json" ]]; then | ||
json=$(<metadata.json) | ||
else | ||
json="{}" # Start with an empty JSON if metadata.json doesn't exist | ||
fi | ||
# Use jq to ensure json is formatted correctly | ||
json=$(echo "$json" | jq '.') | ||
# Get modified files in the current commit | ||
modified_files=$(git diff --name-only HEAD~1 HEAD) | ||
# Echo modified files | ||
echo "Modified files in the current commit:" | ||
echo "$modified_files" | ||
# Update dates for modified files only | ||
for filename in $modified_files; do | ||
# Only proceed if file exists in repo and isn't hidden or in hidden folders | ||
if [[ -f "$filename" ]] && [[ ! "$filename" =~ ^\. ]] && [[ ! "$filename" =~ /\. ]]; then | ||
date=$(git log -1 --format="%aI" -- "$filename") | ||
# Update the JSON object with the new date for the modified file | ||
json=$(echo "$json" | jq --arg file "$filename" --arg date "$date" '.[$file] = $date') | ||
fi | ||
done | ||
# Save the updated JSON to metadata.json | ||
echo "$json" > metadata.json | ||
- name: Display metadata.json content | ||
run: cat metadata.json | ||
all_files=$(git ls-tree -r --name-only HEAD) | ||
files_modified=$(git diff --name-only HEAD~1 HEAD) | ||
existing_meta=$(cat metadata.json) | ||
echo "all_files=$all_files" >> $GITHUB_ENV | ||
echo "files_modified=$files_modified" >> $GITHUB_ENV | ||
echo "existing_meta=$existing_meta" >> $GITHUB_ENV | ||
- name: Run dates.js | ||
uses: ./actions/dates | ||
with: | ||
token: ${{ secrets.GITHUB_TOKEN }} | ||
all-files: ${{ env.all_files }} | ||
files-modified: ${{ env.files_modified }} | ||
existing-meta: ${{ env.existing_meta }} | ||
|
||
- name: Commit if different | ||
run: | | ||
|
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,3 +1,4 @@ | ||
.DS_Store | ||
.ignore | ||
~$* | ||
~$* | ||
node_modules |
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,20 @@ | ||
name: "dates-meta" | ||
description: "Maintain metadata of files in the repository" | ||
author: "Wk" | ||
|
||
runs: | ||
using: "node20" | ||
main: "dates.js" | ||
inputs: | ||
token: | ||
description: "Repo Access Token" | ||
required: true | ||
all-files: | ||
description: "All files in the repo" | ||
required: true | ||
files-modified: | ||
description: "Files modified in the current commit" | ||
required: true | ||
existing-meta: | ||
description: "Existing metadata.json" | ||
required: true |
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,50 @@ | ||
const core = require("@actions/core"); | ||
const github = require("@actions/github"); | ||
|
||
async function run() { | ||
try { | ||
// `who-to-greet` input defined in action metadata file | ||
const accessToken = core.getInput("token"); | ||
const octokit = github.getOctokit(accessToken); | ||
const allFilesInGit = core.getInput("all-files"); | ||
const filesModifiedInCommit = core.getInput("files-modified"); | ||
const exisitingMetadataJson = core.getInput("existing-meta"); | ||
|
||
const metaParsed = JSON.parse(exisitingMetadataJson); //Record<string string> | ||
const filesModifiedArr = filesModifiedInCommit.split("\n"); | ||
const allFilesInGitArr = filesModifiedInCommit.split("\n"); | ||
|
||
const metadataUpdated = Object.entries(allFilesInGitArr).reduce( | ||
(acc, [key, value]) => { | ||
if (!allFilesInGit.includes(key)) { | ||
// If this key is in the metadata but not our project any more, don't return it; | ||
return acc; | ||
} | ||
if (filesModifiedArr.includes(key)) { | ||
//file has been updated; | ||
acc[key] = new Date().toISOString(); | ||
return acc; | ||
} | ||
// file has not been updated | ||
acc[key] = metaParsed[key]; | ||
return acc; | ||
}, | ||
{} | ||
); | ||
const stringified = JSON.stringify(metadataUpdated); | ||
if (stringified !== exisitingMetadataJson) { | ||
// commit if the metadata has changed | ||
await octokit.rest.repos.createOrUpdateFileContents({ | ||
...github.context.repo, | ||
path: "metadata.json", | ||
message: "Automated Update of metadata.json with latest file dates", | ||
content: JSON.stringify(metadataUpdated), | ||
sha: github.context.sha, | ||
}); | ||
} | ||
} catch (error) { | ||
core.setFailed(error.message); | ||
} | ||
} | ||
|
||
run(); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.