Files
certd/scripts/publish-atomgit.js
2026-01-22 13:50:20 +08:00

174 lines
3.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import fs from 'fs'
import axios from 'axios'
import { getVersionContent } from './get-new-version.js'
const AtomgitAccessToken = process.env.ATOMGIT_TOKEN
/**
* 创建仓库Release
POST
https://api.atomgit.com/api/v5/repos/:owner/:repo/releases
Request
Path Parameters
owner
string
required
仓库所属空间地址企业、组织或个人的地址path
repo
string
required
仓库路径
Query Parameters
access_token
string
required
用户授权码
application/json
Body
tag_name
string
required
tag名称
name
string
required
release名称
body
string
required
release描述
target_commitish
string
分支名称或者commit SHA如果tag不存在需要新建tag则传入该参数如果不传入该参数则为默认分支的最新提交
*/
// 创建release
async function createRelease(versionTitle, content) {
const response = await axios.request({
method: 'POST',
url: `https://api.atomgit.com/api/v5/repos/certd/certd/releases`,
headers: {
"Content-Type": "application/json"
},
params: {
access_token: AtomgitAccessToken
},
data: {
tag_name: `v${versionTitle}`,
name: `v${versionTitle}`,
body: content,
target_commitish: 'v2-dev'
},
}
)
console.log("createRelease success")
return response.data
}
/**
* 获取Release附件上传地址
GET
https://api.atomgit.com/api/v5/repos/:owner/:repo/releases/:tag/upload_url
Request
Path Parameters
owner
string
required
仓库所属空间地址企业、组织或个人的地址path
repo
string
required
仓库路径
tag
string
required
tag名称
Query Parameters
access_token
string
required
用户授权码
file_name
string
required
要上传的文件名称
Responses
200
Response Headers
application/json
Schema
Example (auto)
Schema
url
string
required
上传的地址使用put请求
headers
object
*/
async function getUploadUrl(versionTitle) {
const response = await axios.request({
method: 'GET',
url: `https://api.atomgit.com/api/v5/repos/certd/certd/releases/v${versionTitle}/upload_url`,
headers: {
"Content-Type": "application/json"
},
params: {
access_token: AtomgitAccessToken,
file_name: `ui-${versionTitle}.zip`
},
}
)
console.log("getUploadUrl success:",response.data?.url)
return response.data // {url: string, headers: any}
}
async function uploadFile(url, headers, data) {
const response = await axios.request({
method: 'PUT',
url,
headers,
data,
}
)
return response.data
}
async function publishToAtomgit() {
const { versionTitle, content } = getVersionContent()
try{
const release = await createRelease(versionTitle, content)
const uploadUrl = await getUploadUrl(versionTitle)
const fileName = `./packages/ui/certd-client/ui.zip`
const fileData = fs.createReadStream(fileName)
const contentLength = fs.statSync(fileName).size
uploadUrl.headers['Content-Length'] = contentLength
const response = await uploadFile(uploadUrl.url, uploadUrl.headers, fileData)
console.log("uploadFile success:")
console.log("publishToAtomgit success")
}catch(error){
if (error?.response?.data){
throw new Error("publishToAtomgit error:",error.response.data)
}
throw new Error("publishToAtomgit error:",error)
}
}
publishToAtomgit()