Skip to content

Commit

Permalink
adjust management of dates
Browse files Browse the repository at this point in the history
  • Loading branch information
wkelly17 committed Nov 19, 2024
1 parent e6aeafe commit d8b1140
Show file tree
Hide file tree
Showing 7 changed files with 373 additions and 35 deletions.
47 changes: 14 additions & 33 deletions .github/workflows/addDatesMeta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
.ignore
~$*
~$*
node_modules
20 changes: 20 additions & 0 deletions actions/dates/action.yml
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
50 changes: 50 additions & 0 deletions actions/dates/dates.js
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();
1 change: 0 additions & 1 deletion exampleFile.txt

This file was deleted.

Loading

0 comments on commit d8b1140

Please sign in to comment.