Initial commit

This commit is contained in:
2024-03-17 01:40:38 +08:00
commit ebe9244c0a
Signed by: LuoRain
GPG key ID: 16B4D3D5372966A6
5 changed files with 2296 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
node_modules
dist
.env

27
package.json Normal file
View file

@ -0,0 +1,27 @@
{
"name": "youtrack-cert-updater",
"version": "1.0.0",
"description": "Automatically updates YouTrack domain certificate",
"main": "dist/app.cjs",
"repository": "https://git.rainplus.org/rain-plus/youtrack-cert-updater.git",
"author": "LuoRain",
"license": "MIT",
"private": false,
"type": "module",
"scripts": {
"build": "yarn webpack --mode production",
"dev": "yarn webpack --mode development --watch",
"start": "node dist/app.cjs"
},
"devDependencies": {
"@babel/core": "^7.24.0",
"@babel/preset-env": "^7.24.0",
"@babel/preset-typescript": "^7.23.3",
"babel-loader": "^9.1.3",
"dotenv": "^16.4.5",
"node-fetch": "^3.3.2",
"typescript": "^5.4.2",
"webpack": "^5.90.3",
"webpack-cli": "^5.1.4"
}
}

67
src/app.ts Normal file
View file

@ -0,0 +1,67 @@
import { readFileSync } from "fs";
import { config } from "dotenv";
import { default as fetch, Headers } from "node-fetch";
config();
let endpoint = process.env.YOUTRACK_URL;
if (!endpoint) {
throw "YouTrack's base URL is required.";
}
if (!endpoint.endsWith("/")) {
endpoint += "/";
}
const token = process.env.YOUTRACK_TOKEN
if (!token) {
throw "A valid YouTrack access token is required.";
}
let cert = process.env.CERTIFICATE;
if (!cert) {
const certPath = process.env.CERTIFICATE_FILE;
if (!certPath) {
throw "A certificate is required.";
}
cert = readFileSync(certPath, { encoding: "utf-8" });
}
let privKey = process.env.PRIVATE_KEY;
if (!privKey) {
const privKeyPath = process.env.PRIVATE_KEY_FILE;
if (!privKeyPath) {
throw "A private key is required.";
}
privKey = readFileSync(privKeyPath, { encoding: "utf-8" });
}
const apiEndpoint = endpoint + "api/admin/domainSettings";
function makeAPIHeaders() {
return {
Authorization: "Bearer " + token,
Accept: "application/json",
"Cache-Control": "no-cache",
"Content-Type": "application/json"
};
}
async function execute() {
const json = JSON.stringify({
certificates: cert,
privateKey: privKey
});
const res = await fetch(apiEndpoint, {
method: "POST",
headers: new Headers(makeAPIHeaders()),
body: json
});
if (res.ok) {
console.log("Success");
} else {
console.error("Unable to update the certificate", res.body);
}
}
execute();

29
webpack.config.js Normal file
View file

@ -0,0 +1,29 @@
import { dirname } from 'path';
import { fileURLToPath } from 'url';
/**
* @type import("webpack").Configuration
*/
export default {
target: "node",
entry: "./src/app.ts",
output: {
path: dirname(fileURLToPath(import.meta.url)),
filename: "dist/app.cjs",
chunkFormat: false
},
resolve: {
extensions: [".js", ".ts"]
},
module: {
rules: [
{
test: /\.(?:js|ts)$/,
exclude: /node_modules/,
use: [
"babel-loader"
]
}
]
}
}

2170
yarn.lock Normal file

File diff suppressed because it is too large Load diff