Skip to content

Check for Updates

Check for Updates #9

name: Check for Updates
on:
schedule:
- cron: "00 0 * * 0" # Run every Sunday at 12:00 AM
workflow_dispatch:
jobs:
check-for-changes:
permissions:
contents: write
runs-on: ubuntu-20.04
steps:
- name: Checkout Repository
uses: actions/checkout@v3
with:
fetch-depth: 0
ref: dev
- name: Checkout SDH-CssLoader
uses: actions/checkout@v3
with:
repository: suchmememanyskill/SDH-CssLoader
path: css-loader
ref: main
- name: Check DeckThemes API
uses: actions/github-script@v6
with:
script: |
const fs = require("fs");
const child_process = require("child_process");
const path = require("path");
function sleep(ms) {
return new Promise((res) => setTimeout(res, ms));
}
async function openPullRequest(title, targetBranch, sourceBranch, body) {
return await github.rest.repos.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: title,
head: sourceBranch,
base: targetBranch,
body: body
});
}
async function mergePullRequest(pullNumber) {
await github.rest.repos.pulls.merge({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pullNumber
});
}
async function getLatestTag() {
const unsorted = await github.rest.repos.listTags({
owner: context.repo.owner,
repo: context.repo.repo
});
const sorter = (v1, v2) => {
const v1Parts = v1.substring(1).split(".");
const v2Parts = v2.substring(1).split(".");
const major = parseInt(v1Parts[0]) - parseInt(v2Parts[0]);
const minor = parseInt(v1Parts[1]) - parseInt(v2Parts[1]);
const patch = parseInt(v1Parts[2]) - parseInt(v2Parts[2]);
return major || minor || patch;
}
core.info(unsorted);
return unsorted.sort((v1, v2) => sorter(v1, v2))[0];
}
let newTargets = false;
let newManifestVersion = false;
const srcBranch = "dev";
const requestInterval = 30000;
const config = (prop, value) => exec.exec(`git config ${prop} "${value}"`);
const add = (file) => exec.exec(`git add ${file}`);
const commit = (message) => exec.exec(`git commit -m "${message}"`);
const push = (branch) => exec.exec(`git push origin ${branch} --follow-tags`);
const updateOrigin = (repo) => exec.exec(`git remote set-url origin ${repo}`);
core.setSecret(process.env.GITHUB_TOKEN);
updateOrigin(`https://x-access-token:${process.env.GITHUB_TOKEN}@github.com/${process.env.GITHUB_REPOSITORY}.git`);
config('user.email', "[email protected]");
config('user.name', "DeckThemes Auto-Update Action");
const cwdPath = path.resolve(process.cwd());
const schemaPath = path.join(cwdPath, `manifest-schema.json`);
const schemaStr = fs.readFileSync(schemaPath, 'utf8');
const schema = JSON.parse(schemaStr);
const response = await fetch('https://api.deckthemes.com/themes/filters?type=CSS');
const data = await response.json();
//? Check for New Targets
const listFromSchema = schema.properties.target.enum;
const listFromApi = Object.keys(data.filters);
//TODO: check for differences
const listsAreEqual = listFromSchema.every((elem) => listFromApi.includes(elem)) && listFromApi.every((elem) => listFromSchema.includes(elem));
if (!listsAreEqual) {
newTargets = true;
schema.properties.target.enum = listFromApi;
}
const latestTag = await getLatestTag();
const themePyPath = path.join(cwdPath, "css-loader", "css_theme.py");
const themePyContents = fs.readFileSync(themePyPath, 'utf8');
const manifestVersionPattern = /CSS_LOADER_VER = (.*)/;
//? Check for New Manifest Version
const currentVersion = schema.properties.manifest_version.const;
const newVersionStr = themePyContents.match(manifestVersionPattern)[1];
const newVersion = parseInt(newVersionStr);
if (currentVersion !== newVersion) {
newManifestVersion = true;
schema.properties.manifest_version.description = `The manifest version. This should almost always be the latest version.\n\nLatest Version: ${newVersion}`;
schema.properties.manifest_version.const = newVersion;
}
if (newTargets || newManifestVersion) {
fs.writeFileSync(schemaPath, JSON.stringify(schema, null, '\t'));
const commitMsg = (newTargets && newManifestVersion) ? `updated manifest version to ${newVersion} and updated theme targets` : (newManifestVersion ? `updated manifest version to ${newVersion}` : `updated theme targets`);
await add(".");
await commit(`feat: ${commitMsg}`);
await push(srcBranch);
const latestTag = await getLatestTag();
const latestTagParts = latestTag.subString(1).split(".");
const nextTag = `${latestTagParts[0]}.${parseInt(latestTagParts[1]) + 1}.${latestTagParts[2]}`
const releasePRTitle = `chore: build v${nextTag}`;
const releasePR = await openPullRequest(releasePRTitle, "release", srcBranch, "");
const releasePRNumber = releasePR.number;
await sleep(requestInterval);
await mergePullRequest(releasePRNumber);
}