Skip to content

Commit

Permalink
Merge pull request 'build: declarations patch workflow' (#1) from pat…
Browse files Browse the repository at this point in the history
…ch-workflow into master
  • Loading branch information
vanyauhalin committed Sep 20, 2024
2 parents 02ec1de + 55fede1 commit ce4f63d
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 0 deletions.
51 changes: 51 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: publish

permissions:
contents: write

on:
workflow_dispatch:

jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Setup Git
run: |
re=$(curl --silent --header "Accept: application/json" "https://api.github.com/users/github-actions%5Bbot%5D")
lo=$(echo "$re" | jq --raw-output ".login")
id=$(echo "$re" | jq --raw-output ".id")
git config --global user.name "$lo"
git config --global user.email "[email protected]"
- name: Setup mise
uses: jdx/mise-action@v2

- name: Install Dependencies
run: pnpm install --frozen-lockfile

- name: Build Declarations
run: pnpm build

- name: Publish Declarations
working-directory: dist
run: |
un=$(git config --get user.name)
ru="${{github.server_url}}/${{github.repository}}.git"
ru=$(echo "$ru" | sed "s#https://#https://$un:${{github.token}}@#")
td=$(mktemp -d)
git clone --quiet --no-checkout --single-branch --branch dist "$ru" "$td"
mv "$td/.git" .
git add .
if git diff-index --quiet HEAD --; then
echo "No changes to commit"
else
git commit --quiet --message "$(date --utc)"
git push
fi
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
3 changes: 3 additions & 0 deletions .mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[tools]
node = "prefix:22"
pnpm = "prefix:9"
18 changes: 18 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"strict": true,
"module": "NodeNext",
"moduleResolution": "NodeNext",
"resolveJsonModule": true,
"rootDir": ".",
"noEmit": true,
"outDir": "./dist",
"allowJs": true,
"checkJs": true,
"target": "ESNext",
"skipLibCheck": true
},
"include": [
"./makefile.js"
]
}
114 changes: 114 additions & 0 deletions makefile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import {argv} from "node:process"
import {existsSync} from "node:fs"
import {fileURLToPath} from "node:url"
import {join} from "node:path"
import {mkdir, writeFile} from "node:fs/promises"
import {OpenAPIV3 as OpenApi} from "openapi-types"
import sade from "sade"

const config = {
source: {
owner: "onlyoffice",
repo: "docspace-declarations",
reference: "src",
paths: [
{name: "data", path: "asc.data.backup.swagger.json"},
{name: "files", path: "asc.files.swagger.json"},
{name: "people", path: "asc.people.swagger.json"},
{name: "web", path: "asc.web.api.swagger.json"},
],
},
}

main()

/**
* @returns {void}
*/
function main() {
sade("./makefile.js")
.command("build")
.action(build)
.parse(argv)
}

/**
* @returns {Promise<void>}
*/
async function build(){
const c = config.source

const rd = rootDir()

const dd = distDir(rd)
if (!existsSync(dd)) {
await mkdir(dd)
}
//TODO: console info?
for (const p of c.paths){
const u = `https://raw.githubusercontent.com/${c.owner}/${c.repo}/${c.reference}/${p.path}`
const r = await (await fetch(u)).json()
patch(r)
const dp = join(dd, p.path)
const ds = JSON.stringify(r, null, 2)
await writeFile(dp, ds)
}
}

/**
* @param {OpenApi.Document<{}>} d
* @returns {void}
*/
function patch(d) {
for (const pn in d.paths){
const po = d.paths[pn]
if (!po) {
continue
}

// https://github.com/ONLYOFFICE/DocSpace-server/blob/v2.0.2-server/web/ASC.Web.Api/Api/CapabilitiesController.cs#L33
if (pn.endsWith("{.format}")) {
delete d.paths[pn]
continue
}

for (const mn of Object.values(OpenApi.HttpMethods)) {
const mo = po[mn]
if (!mo) {
continue
}

if (mo.description) {
mo.description = `**Note**: ${mo.description}`
}

if (mo.summary) {
if (!mo.description) {
mo.description = mo.summary
} else {
mo.description = `${mo.summary}\n\n${mo.description}`
}
}

if ("x-shortName" in mo && typeof mo["x-shortName"] === "string") {
mo.summary = mo["x-shortName"]
}
}
}
}

/**
* @returns {string}
*/
function rootDir() {
const u = new URL(".", import.meta.url)
return fileURLToPath(u)
}

/**
* @param {string} d
* @returns {string}
*/
function distDir(d) {
return join(d, "dist")
}
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "docspace-declarations",
"private": true,
"type": "module",
"scripts": {
"build": "pnpm make build",
"make": "node ./makefile.js"
},
"devDependencies": {
"openapi-types": "^12.1.3",
"sade": "^1.8.1"
},
"engines": {
"node": "^22"
}
}
39 changes: 39 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ce4f63d

Please sign in to comment.