Skip to content

Commit

Permalink
Update workflow script.
Browse files Browse the repository at this point in the history
  • Loading branch information
hardiesoft committed Sep 9, 2024
1 parent f505d94 commit 631101a
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 94 deletions.
156 changes: 67 additions & 89 deletions .github/workflows/aggregate-versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ const repo = args[2];

const getFilesWithExtension = (path, ext) => {
const items = fs.readdirSync(path)
.filter(item => (
!item.startsWith(".") &&
!item.startsWith("_") &&
(item.endsWith(`.${ext}`) || fs.lstatSync(`${path}/${item}`).isDirectory())
))
.map(item => ({ name: item, isDir: fs.lstatSync(`${path}/${item}`).isDirectory() }));
.filter(item => (
!item.startsWith(".") &&
!item.startsWith("_") &&
(item.endsWith(`.${ext}`) || fs.lstatSync(`${path}/${item}`).isDirectory())
))
.map(item => ({ name: item, isDir: fs.lstatSync(`${path}/${item}`).isDirectory() }));

let files = [];
for (const item of items) {
if (item.isDir) {
Expand All @@ -38,103 +39,80 @@ const findKeyInObject = (obj, key) => {
}
return false;
}
const formatDate = date => {
const dateString = date.toLocaleString("en-NZ", {timeZone: "Pacific/Auckland"});
// Node on github actions only has US localization, so we need to swap day and month:
const firstPart = dateString.split(",")[0];
const pieces = firstPart.split("/");
return `${pieces[1]}/${pieces[0]}/${pieces[2]},${dateString.split(',')[1]}`;
}

const branches = ["prod", "test", "dev"];
const models = ["pi", "tc2"];
const versionsAreEqual = (prev, next) => {
for (const branch of branches) {
if (!prev[branch] || !next[branch]) {
return false;
}
for (const model of models) {
for (const softwarePackage of Object.keys(prev[branch][model])) {
if (!next[branch][model][softwarePackage]) {
return false;
}
if (next[branch][model][softwarePackage] !== prev[branch][model][softwarePackage]) {
return false;
}
}
for (const softwarePackage of Object.keys(next[branch][model])) {
if (!prev[branch][model][softwarePackage]) {
return false;
}
if (next[branch][model][softwarePackage] !== prev[branch][model][softwarePackage]) {
return false;
}
}
}
}
return true;
};
(async function () {
const versionData = {};
const now = new Date();
const separator = "____\n";
let versionOutput = "";

console.log(process.cwd());
process.chdir("../../../");
// For each branch:
const branches = ["prod", "test", "dev"];
const releaseNotesLinks = {
prod: "https://docs.cacophony.org.nz/home/release-notes-2020",
dev: "https://docs.cacophony.org.nz/home/release-notes-2"
};
console.log(process.cwd());

for (const branch of branches) {
process.chdir(`./${branch}`);
const slsFiles = getFilesWithExtension(".", "sls");
// For each branch:
versionData[branch] = {};
for (const path of slsFiles) {
const data = fs.readFileSync(path, "utf8");
try {
const yamlData = yaml.parse(data);
const versionInfo = findKeyInObject(yamlData, "cacophony.pkg_installed_from_github");
const name = versionInfo.find(item => item.hasOwnProperty("name")).name;
const version = versionInfo.find(item => item.hasOwnProperty("version")).version;
versionData[branch][name] = version;
} catch (e) {
process.chdir(`./${branch}`);
for (const model of models) {
// For each camera model:
process.chdir(`./${model}`);
const slsFiles = getFilesWithExtension(".", "sls");
versionData[branch][model] = {};
for (const path of slsFiles) {
const data = fs.readFileSync(path, "utf8");
try {
const yamlData = yaml.parse(data);
const versionInfo = findKeyInObject(yamlData, "cacophony.pkg_installed_from_github");
const name = versionInfo.find(item => item.hasOwnProperty("name")).name;
const version = versionInfo.find(item => item.hasOwnProperty("version")).version;
versionData[branch][model][name] = version;
} catch (e) {
}
}
process.chdir("../");
}
process.chdir("../");
}

// Output the text to the README.md file, if the version info has changed since last time.
for (const branch of branches) {
versionOutput += `#### Branch \`${branch}\`\n`;
for (const [key, val] of Object.entries(versionData[branch])) {
versionOutput += ` * ${key}: ${val}\n`;
}
if (releaseNotesLinks[branch]) {
versionOutput += `\n[Release notes](${releaseNotesLinks[branch]})\n`;
}
}

// Just write out on the dev branch.
const branch = "dev";
let prevVersionOutput = "";
process.chdir(`./${branch}`);
const readme = fs.readFileSync("README.md", "utf8");
const versionInfoStart = readme.indexOf("\n\n#### Version information");
let output;
if (versionInfoStart !== -1) {
output = readme.substring(0, versionInfoStart);
prevVersionOutput = readme.substring(readme.indexOf(separator) + separator.length);
} else {
output = readme;
process.chdir("./salt-version-info");
let prevVersionData;
try {
const prevVersionInfo = fs.readFileSync("./salt-version-info.json", "utf8");
prevVersionData = JSON.parse(prevVersionInfo);
} catch (e) {
console.log(e);
}
output += "\n\n#### Version information ";
output += `(_Updated ${formatDate(now)}_):\n`;
output += separator;
output += versionOutput;
if (versionOutput !== prevVersionOutput) {
fs.writeFileSync("README.md", output);
if ((prevVersionData && !versionsAreEqual(prevVersionData, versionData)) || !prevVersionData) {
versionData.updated = new Date().toISOString();
console.log("version info updated", JSON.stringify(versionData, null, '\t'));
if (repo === "TheCacophonyProject/saltops") {
console.log("Committing changes for branch", branch);
{
const {stderr, stdout} = await exec("git config user.name cacophony-bot");
console.log("1:", stderr, stdout);
}
{
const {stderr, stdout} = await exec("git config user.email bot@cacophony.org.nz");
console.log("2:", stderr, stdout);
}
{
const {stderr, stdout} = await exec("git add .");
console.log("3:", stderr, stdout);
}
{
const {stderr, stdout} = await exec("git commit -m \"updated version information\"");
console.log("4:", stderr, stdout);
}
{
console.log("Pushing");
const {stderr, stdout} = await exec("git push --force");
console.log("5:", stderr, stdout);
}
fs.writeFileSync("./salt-version-info.json", JSON.stringify(versionData, null, '\t'));
}
} else {
// Version info is unchanged.
console.log("version information unchanged");
}
process.chdir("../");

}());
51 changes: 46 additions & 5 deletions .github/workflows/aggregate-versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,63 @@
# to each branch if any of the version information in any branch has changed.

name: aggregate-version-info
env:
GITHUB_TOKEN: ${{ secrets.CACOPHONY_BOT_TOKEN }}
on: [push, workflow_dispatch]
env:
HAS_CHANGES: ${{ false }}
jobs:
aggregate-version-info:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Checkout
uses: actions/checkout@v4
with:
path: dev
ref: dev
- name: Checkout
uses: actions/checkout@v4
with:
path: prod
ref: prod
- name: Checkout
uses: actions/checkout@v4
with:
path: test
ref: test
- name: Checkout
uses: actions/checkout@v4
with:
repository: TheCacophonyProject/salt-version-info
path: salt-version-info
token: ${{ secrets.WRITE_VERSION_INFO_TOKEN }}
persist-credentials: true
- uses: actions/setup-node@v2.1.2
with:
node-version: 12.x
- shell: bash
node-version: 20.x
- name: Run version-check script
shell: bash
run: |
cd dev/.github/workflows
npm i
npm run aggregate-versions ${{ github.repository }}
- name: Check for new files
id: newFiles
shell: bash
working-directory: salt-version-info
run: |
git add -A
git status --porcelain | wc -l
if [[ $(git status --porcelain | wc -l) -gt 0 ]]; then
echo "HAS_CHANGES=true" >> "$GITHUB_ENV"
else
echo "HAS_CHANGES=false" >> "$GITHUB_ENV"
fi
- name: Push new files
if: env.HAS_CHANGES == 'true'
shell: bash
working-directory: salt-version-info
run: |
git config --global user.name "cacophony-bot"
git config --global user.email "bot@cacophony.org.nz"
git commit -m "updated version information"
git push

0 comments on commit 631101a

Please sign in to comment.