-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tools] upload release artifacts on SIT-MC server
- Loading branch information
Showing
12 changed files
with
197 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { git, github } from './lib/git.mjs' | ||
import crypto from "crypto" | ||
|
||
/** | ||
* @param {({name:string,browser_download_url:string})=>boolean} filter | ||
*/ | ||
export async function searchAndGetAssetInfo(filter) { | ||
const asset = searchAsset(filter) | ||
if (!asset) return | ||
return await getAssetInfo(asset) | ||
} | ||
|
||
/** | ||
* @template {{name:string,browser_download_url:string}} T | ||
* @param {(T)=>boolean} filter | ||
* @returns {T | undefined} | ||
*/ | ||
function searchAsset(filter) { | ||
const assets = github.release.assets | ||
for (const asset of assets) { | ||
if (filter(asset)) { | ||
return asset | ||
} | ||
} | ||
return | ||
} | ||
|
||
/** | ||
* | ||
* @param {{name:string,browser_download_url:string}} payload | ||
*/ | ||
async function getAssetInfo(payload) { | ||
const url = payload.browser_download_url | ||
let sha256 = "" | ||
if (url) { | ||
sha256 = await downloadAndSha256Hash(url) | ||
} | ||
return { | ||
name: payload.name, | ||
url: url, | ||
sha256: sha256, | ||
} | ||
} | ||
|
||
|
||
/** | ||
* | ||
* @param {string} url | ||
*/ | ||
async function downloadAndSha256Hash(url) { | ||
const response = await fetch(url) | ||
const chunks = [] | ||
for await (const chunk of response.body) { | ||
chunks.push(chunk) | ||
} | ||
const buffer = Buffer.concat(chunks) | ||
const hash = crypto.createHash('sha256').update(buffer).digest('hex') | ||
return hash | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import fs from "fs" | ||
import { Readable } from "stream" | ||
import { pipeline } from "stream/promises" | ||
/** | ||
* | ||
* @param {string} raw | ||
*/ | ||
export const sanitizeNameForUri = (raw) => { | ||
return raw.replace("+", "-") | ||
} | ||
|
||
|
||
/** | ||
* | ||
* @param {string} url | ||
* @param {string} filePath | ||
*/ | ||
export const downloadFile = async (url, filePath) => { | ||
const res = await fetch(url) | ||
|
||
const s = fs.createWriteStream(filePath) | ||
// Readable.fromWeb(res.body).pipe(s) | ||
await pipeline(Readable.fromWeb(res.body), s) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
import * as fs from 'fs/promises' // For file system operations | ||
import { git, github } from './lib/git.mjs' | ||
import crypto from "crypto" | ||
import { git, github, getGitHubMirrorDownloadUrl } from './lib/git.mjs' | ||
import * as path from "path" | ||
import { getArtifactDownloadUrl } from './lib/sitmc.mjs' | ||
import esMain from 'es-main' | ||
import { searchAndGetAssetInfo } from "./lib/release.mjs" | ||
|
||
const gitUrl = 'https://github.com/Amazefcc233/mimir-docs' | ||
const deployPath = '~/deploy' | ||
const artifactPath = 'artifact/' | ||
|
||
async function main() { | ||
const main = async () => { | ||
// Get release information from environment variables (GitHub Actions context) | ||
const version = getVersion() | ||
const releaseTime = getPublishTime() | ||
|
@@ -43,11 +43,35 @@ async function main() { | |
await fs.unlink(`${artifactPath}latest.json`) // Ignore if file doesn't exist | ||
await fs.symlink(`${version}.json`, `${artifactPath}latest.json`) | ||
|
||
await addAndPush({ version }) | ||
await git.add(".") | ||
await git.commit(`Release New Version: ${version}`) | ||
await git.push("[email protected]:Amazefcc233/mimir-docs", "main:main") | ||
} | ||
|
||
function withGitHubMirror(url) { | ||
return `https://mirror.ghproxy.com/${url}` | ||
|
||
function getVersion() { | ||
// remove leading 'v' | ||
return github.release.tag_name.slice(1) | ||
} | ||
|
||
function getReleaseNote() { | ||
const text = github.release.body | ||
const startLine = text.indexOf('## 更改') | ||
const endLine = text.indexOf('## How to download') | ||
|
||
if (startLine === -1 || endLine === -1) { | ||
throw new Error('Release notes section not found') | ||
} | ||
|
||
// Extract content between start and end lines (excluding headers) | ||
const releaseNotes = text.substring(startLine + '## 更改\n'.length, endLine).trim() | ||
|
||
// Remove any leading or trailing blank lines | ||
return releaseNotes.replace(/^\s*|\s*$/g, '') | ||
} | ||
|
||
function getPublishTime() { | ||
return new Date(github.release.published_at) | ||
} | ||
|
||
function buildArtifactPayload({ version, tagName, releaseTime, releaseNote, apk, ipa }) { | ||
|
@@ -65,7 +89,7 @@ function buildArtifactPayload({ version, tagName, releaseTime, releaseNote, apk, | |
url: { | ||
official: getArtifactDownloadUrl(tagName, apk.name), | ||
github: apk.url, | ||
mirror: withGitHubMirror(apk.url), | ||
mirror: getGitHubMirrorDownloadUrl(apk.url), | ||
}, | ||
} | ||
} | ||
|
@@ -77,14 +101,14 @@ function buildArtifactPayload({ version, tagName, releaseTime, releaseNote, apk, | |
url: { | ||
official: getArtifactDownloadUrl(tagName, ipa.name), | ||
github: ipa.url, | ||
mirror: withGitHubMirror(ipa.url), | ||
mirror: getGitHubMirrorDownloadUrl(ipa.url), | ||
}, | ||
} | ||
} | ||
return payload | ||
} | ||
|
||
function validateArtifactPayload(payload) { | ||
const validateArtifactPayload = (payload) => { | ||
for (const [profile, download] in Object.entries(payload.downloads)) { | ||
if (!(download.default && download.url[download.default] !== undefined)) { | ||
if (download.url.length > 0) { | ||
|
@@ -95,96 +119,6 @@ function validateArtifactPayload(payload) { | |
} | ||
} | ||
} | ||
/** | ||
* | ||
* @param {{version:string}} param0 | ||
*/ | ||
async function addAndPush({ version }) { | ||
await git.add(".") | ||
await git.commit(`Release New Version: ${version}`) | ||
await git.push("[email protected]:Amazefcc233/mimir-docs", "main:main") | ||
} | ||
|
||
function getVersion() { | ||
// remove leading 'v' | ||
return github.release.tag_name.slice(1) | ||
} | ||
|
||
function getReleaseNote() { | ||
const text = github.release.body | ||
const startLine = text.indexOf('## 更改') | ||
const endLine = text.indexOf('## How to download') | ||
|
||
if (startLine === -1 || endLine === -1) { | ||
throw new Error('Release notes section not found') | ||
} | ||
|
||
// Extract content between start and end lines (excluding headers) | ||
const releaseNotes = text.substring(startLine + '## 更改\n'.length, endLine).trim() | ||
|
||
// Remove any leading or trailing blank lines | ||
return releaseNotes.replace(/^\s*|\s*$/g, '') | ||
} | ||
|
||
function getPublishTime() { | ||
return new Date(github.release.published_at) | ||
} | ||
|
||
/** | ||
* @param {({name:string,browser_download_url:string})=>boolean} filter | ||
*/ | ||
async function searchAndGetAssetInfo(filter) { | ||
const asset = searchAsset(filter) | ||
if (!asset) return | ||
return await getAssetInfo(asset) | ||
} | ||
|
||
/** | ||
* @template {{name:string,browser_download_url:string}} T | ||
* @param {(T)=>boolean} filter | ||
* @returns {T | undefined} | ||
*/ | ||
function searchAsset(filter) { | ||
const assets = github.release.assets | ||
for (const asset of assets) { | ||
if (filter(asset)) { | ||
return asset | ||
} | ||
} | ||
return | ||
} | ||
|
||
/** | ||
* | ||
* @param {{name:string,browser_download_url:string}} payload | ||
*/ | ||
async function getAssetInfo(payload) { | ||
const url = payload.browser_download_url | ||
let sha256 = "" | ||
if (url) { | ||
sha256 = await downloadAndSha256Hash(url) | ||
} | ||
return { | ||
name: payload.name, | ||
url: url, | ||
sha256: sha256, | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param {string} url | ||
*/ | ||
async function downloadAndSha256Hash(url) { | ||
const response = await fetch(url) | ||
const chunks = [] | ||
for await (const chunk of response.body) { | ||
chunks.push(chunk) | ||
} | ||
const buffer = Buffer.concat(chunks) | ||
const hash = crypto.createHash('sha256').update(buffer).digest('hex') | ||
return hash | ||
} | ||
|
||
if (esMain(import.meta)) { | ||
main() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.