-
Notifications
You must be signed in to change notification settings - Fork 0
153 lines (119 loc) · 5.9 KB
/
checkForUpdates.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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;
}
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 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);
}