Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP
Browse files Browse the repository at this point in the history
liushuyu committed Dec 8, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent f56d6c7 commit 59c8d01
Showing 5 changed files with 212 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/tum-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Generate Update Manifests

on:
push:
paths:
- "topics/*.toml"
pull_request:
paths:
- "topics/*.toml"
workflow_dispatch: {}

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install npm dependencies
run: npm install toml @cfworker/json-schema
- name: Generate manifests
uses: actions/github-script@v7
with:
script: |
const {generateTopicUpdateData} = require('${{ github.workspace }}/.github/workflows/tum-process.js');
const topic = context.ref?.replace('refs/heads/', '');
generateTopicUpdateData(require, topic, '/tmp/dists/');
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: manifests
path: /tmp/dists/
97 changes: 97 additions & 0 deletions .github/workflows/tum-process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
const fs = require("node:fs");

/**
* @typedef TopicConfig
* @type { { name: { [key: string]: string }, security: boolean, caution?: { [key: string]: string }, topics?: string[], packages?: { [key: string]: string | boolean | null } } }
*/

/**
* Translates a TOML topic configuration into a structured JSON object.
* @param {import("toml")} toml
* @param {import("@cfworker/json-schema").Validator} schemaValidator
* @param {string} content
* @returns
*/
function translateTopic(toml, schemaValidator, filePath) {
/**
* @type { TopicConfig }
*/
console.info(`Reading ${filePath}`);
const content = fs.readFileSync(filePath, "utf8");
let topic;

try {
topic = toml.parse(content);
} catch (error) {
console.error(`Error parsing TOML file at ${filePath}:`, error);
console.log(`::error file=${filePath}::Error parsing TOML file::${error}`);
throw error;
}

if (schemaValidator.validate(topic).valid) {
console.log("TOML file parsed successfully.");
} else {
const errors = schemaValidator.validate(topic).errors;
console.error("Invalid TOML:", errors);
console.log(
`::error file=${filePath}::${errors.map((e) => e.error).join(" ")}`
);
throw new Error("Invalid TOML");
}
// rewrite package versions
if (topic.packages) {
Object.keys(topic.packages).forEach((pkg) => {
if (!topic.packages[pkg]) {
topic.packages[pkg] = null;
}
});
}

topic.type = topic.packages ? "conventional" : "cumulative";
return topic;
}

/**
*
* @param {(name: string) => import(name)} require
* @param {string | null} topic
* @param {string} outputPath
*/
function generateTopicUpdateData(require, topic, outputPath) {
const toml = require("toml");
const validator = require("@cfworker/json-schema");
const schema = require("./topics/tum.schema.json");
const schemaValidator = new validator.Validator(schema);

/**
* @type { { [key: string]: { type: "conventional" | "cumulative", name: { [key: string]: string }, security: boolean, caution?: { [key: string]: string }, topics?: string[], packages?: { [key: string]: string | null } } } }
*/
let result = {};
if (!topic) {
console.error("No topic specified. Use 'stable' or a topic name.");
return;
}
console.info(`Generating updates for topic "${topic}"`);
if (topic === "stable") {
fs.readdirSync("topics").forEach((file) => {
if (!file.endsWith(".toml")) {
return;
}
const filePath = `topics/${file}`;
const topicName = file.replace(/\.toml$/, "");
result[topicName] = translateTopic(toml, schemaValidator, filePath);
});
} else {
result[topic] = translateTopic(
toml,
schemaValidator,
`topics/${topic}.toml`
);
}

const outputDirPath = `${outputPath}/${topic}`;
fs.mkdirSync(outputDirPath, { recursive: true });
fs.writeFileSync(`${outputDirPath}/updates.json`, JSON.stringify(result));
}

module.exports = { generateTopicUpdateData };
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -4,3 +4,4 @@ repo-spec/last_db_update
.*.swp
*.save
*.acbs-ckpt
/node_modules
15 changes: 15 additions & 0 deletions topics/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name = { default = "KDE Updates (Winter 2023)", zh_CN = "KDE 更新(2023 年冬季)" }
# Security update (true/false)?
security = true
# OPTIONAL: PSA message for users.
caution = { default = """
This topic may use significantly more memory after reboot. Our testing finds
that the new KDE version may use up to 16GiB of RAM.
""", zh_CN = """
本次更新重启后可能会需要更多内存。据我社维护者测试,新版 KDE 可能需要接近 16GiB 内存。""" }

[packages]
konsole = "23.04.1-1"
dolphin = "23.04.1"
# Package removed as part of the topic.
pykde = false
68 changes: 68 additions & 0 deletions topics/tum.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/AOSC-Dev/aosc-os-abbs/blob/stable/topics/tum.schema.json",
"title": "",
"type": "object",
"properties": {
"security": {
"type": "boolean",
"description": "This topic contains security updates"
},
"name": {
"type": "object",
"properties": {
"default": {
"type": "string",
"description": "The name of the topic in English"
}
},
"patternProperties": {
"^[a-z]+_[A-Z]+": {
"type": "string",
"description": "The name of the topic in the language specified by the key"
}
},
"required": ["default"]
},
"caution": {
"type": "object",
"properties": {
"default": {
"type": "string",
"description": "PSA message to alert users about potential issues in English"
}
},
"patternProperties": {
"^[a-z]+_[A-Z]+": {
"type": "string",
"description": "PSA message to alert users about potential issues in the language specified by the key"
}
},
"required": ["default"]
},
"packages": {
"type": "object",
"patternProperties": {
"^[a-z0-9][a-z0-9+-.]+": {
"type": ["string", "boolean"],
"description": "Package version to update to or false to note that the package will be removed"
}
},
"minProperties": 1
},
"topics": {
"type": "array",
"items": {
"type": "string",
"description": "The name of the topic to be included in this cummulative update"
},
"minItems": 1
}
},
"oneOf": [
{"required": ["packages"]},
{"required": ["topics"]}
],
"additionalProperties": false,
"required": ["security", "name"]
}

0 comments on commit 59c8d01

Please sign in to comment.