chore: move docs-util to www (#7232)
* reorganize docs apps * add README * fix directory * add condition for old docs * move docs-util to www * remove remaining docs-util * fixes of paths * fix scripts * path fixes * fix github actions * add build packages script
This commit is contained in:
33
www/utils/packages/scripts/check-pr-files-count.ts
Normal file
33
www/utils/packages/scripts/check-pr-files-count.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { Octokit } from "@octokit/core"
|
||||
import * as core from "@actions/core"
|
||||
|
||||
const octokit = new Octokit({
|
||||
auth: process.env.GH_TOKEN,
|
||||
})
|
||||
|
||||
let prNumber = process.argv.length >= 3 ? process.argv[2] : null
|
||||
const threshold = process.argv.length >= 4 ? parseInt(process.argv[3]) : 300
|
||||
|
||||
async function getPrFilesCount() {
|
||||
if (!prNumber) {
|
||||
throw new Error("Commit SHA is required.")
|
||||
}
|
||||
|
||||
prNumber = prNumber.replace("/merge", "")
|
||||
|
||||
const { data: pr } = await octokit.request(
|
||||
"GET /repos/{owner}/{repo}/pulls/{pull_number}",
|
||||
{
|
||||
owner: process.env.GIT_OWNER || "medusajs",
|
||||
repo: process.env.GIT_REPO || "medusa",
|
||||
pull_number: parseInt(prNumber),
|
||||
headers: {
|
||||
"X-GitHub-Api-Version": "2022-11-28",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
core.setOutput("files_lt_threshold", pr.changed_files < threshold)
|
||||
}
|
||||
|
||||
void getPrFilesCount()
|
||||
38
www/utils/packages/scripts/check-release-commit.ts
Normal file
38
www/utils/packages/scripts/check-release-commit.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { Octokit } from "@octokit/core"
|
||||
import * as core from "@actions/core"
|
||||
|
||||
const commitSha = process.argv.length >= 3 ? process.argv[2] : null
|
||||
|
||||
const octokit = new Octokit({
|
||||
auth: process.env.GH_TOKEN,
|
||||
})
|
||||
|
||||
async function checkReleaseCommit() {
|
||||
if (!commitSha) {
|
||||
throw new Error("Commit SHA is required.")
|
||||
}
|
||||
|
||||
// retrieve commit by the SHA
|
||||
const { data: commit } = await octokit.request(
|
||||
"GET /repos/{owner}/{repo}/commits/{ref}",
|
||||
{
|
||||
owner: process.env.GIT_OWNER || "",
|
||||
repo: process.env.GIT_REPO || "",
|
||||
ref: commitSha,
|
||||
headers: {
|
||||
"X-GitHub-Api-Version": "2022-11-28",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
if (!commit) {
|
||||
throw new Error("Commit doesn't exist.")
|
||||
}
|
||||
|
||||
core.setOutput(
|
||||
"is_release_commit",
|
||||
commit.commit.message === "chore: Release"
|
||||
)
|
||||
}
|
||||
|
||||
void checkReleaseCommit()
|
||||
167
www/utils/packages/scripts/freshness-check.ts
Normal file
167
www/utils/packages/scripts/freshness-check.ts
Normal file
@@ -0,0 +1,167 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { LinearClient } from "@linear/sdk"
|
||||
import { Octokit } from "@octokit/core"
|
||||
import fs from "fs"
|
||||
import path from "path"
|
||||
import { fileURLToPath } from "url"
|
||||
|
||||
const octokit = new Octokit({
|
||||
auth: process.env.GH_TOKEN,
|
||||
})
|
||||
|
||||
const linearClient = new LinearClient({
|
||||
apiKey: process.env.LINEAR_API_KEY,
|
||||
})
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url)
|
||||
const __dirname = path.dirname(__filename)
|
||||
|
||||
const repoPath = path.join(__dirname, "..", "..", "..", "apps", "book", "app")
|
||||
let freshnessCheckLabelId = ""
|
||||
let documentationTeamId = ""
|
||||
|
||||
async function scanDirectory(startPath: string) {
|
||||
const files = fs.readdirSync(path.join(startPath), {
|
||||
withFileTypes: true,
|
||||
})
|
||||
|
||||
for (const file of files) {
|
||||
const filePath = path.join(startPath, file.name)
|
||||
if (file.isDirectory()) {
|
||||
//if it's references directory, skip
|
||||
if (file.name !== "references" && file.name !== "upgrade-guides") {
|
||||
await scanDirectory(filePath)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
//check that the file is a markdown file
|
||||
if (file.name.indexOf(".md") === -1 && file.name.indexOf(".mdx") === -1) {
|
||||
continue
|
||||
}
|
||||
|
||||
//if it is a file, check its commits in GitHub
|
||||
const commitResponse = await octokit.request(
|
||||
"GET /repos/{owner}/{repo}/commits",
|
||||
{
|
||||
owner: "medusajs",
|
||||
repo: "medusa",
|
||||
path: filePath,
|
||||
per_page: 1,
|
||||
}
|
||||
)
|
||||
|
||||
if (
|
||||
!commitResponse.data.length ||
|
||||
!commitResponse.data[0].commit.committer?.date
|
||||
) {
|
||||
continue
|
||||
}
|
||||
|
||||
const today = new Date()
|
||||
const lastEditedDate = new Date(
|
||||
commitResponse.data[0].commit.committer.date
|
||||
)
|
||||
const monthsSinceEdited = getMonthDifference(lastEditedDate, today)
|
||||
const monthsThreshold = 2
|
||||
|
||||
if (monthsSinceEdited > monthsThreshold) {
|
||||
//file was edited more than 6 months ago.
|
||||
//check if there's an issue created for this file since the commit date
|
||||
const existingIssue = await linearClient.issues({
|
||||
filter: {
|
||||
createdAt: {
|
||||
gte: subtractMonths(monthsSinceEdited - monthsThreshold, today),
|
||||
},
|
||||
title: {
|
||||
containsIgnoreCase: `Freshness check for ${filePath}`,
|
||||
},
|
||||
labels: {
|
||||
some: {
|
||||
id: {
|
||||
eq: freshnessCheckLabelId,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
first: 1,
|
||||
})
|
||||
|
||||
if (existingIssue.nodes.length) {
|
||||
//an issue has been created for the past 6 months. Don't create an issue for it.
|
||||
continue
|
||||
}
|
||||
|
||||
console.log(`Creating an issue for ${filePath}...`)
|
||||
|
||||
//there are no issues in the past 6 months. Create an issue
|
||||
await linearClient.issueCreate({
|
||||
teamId: documentationTeamId,
|
||||
title: `Freshness check for ${filePath}`,
|
||||
labelIds: [freshnessCheckLabelId],
|
||||
description: `File \`${filePath}\` was last edited on ${lastEditedDate.toDateString()}.`,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
//fetch documentation team ID from linear
|
||||
const documentationTeam = await linearClient.teams({
|
||||
filter: {
|
||||
name: {
|
||||
eqIgnoreCase: "Documentation",
|
||||
},
|
||||
},
|
||||
first: 1,
|
||||
})
|
||||
|
||||
if (!documentationTeam.nodes.length) {
|
||||
console.log("Please add Documentation team in Linear first then try again")
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
documentationTeamId = documentationTeam.nodes[0].id
|
||||
|
||||
//fetch freshness check label ID from linear
|
||||
const freshnessCheckLabel = await linearClient.issueLabels({
|
||||
filter: {
|
||||
name: {
|
||||
eqIgnoreCase: "type: freshness-check",
|
||||
},
|
||||
team: {
|
||||
id: {
|
||||
eq: documentationTeamId,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if (!freshnessCheckLabel.nodes.length) {
|
||||
console.log(
|
||||
"Please add freshness check label in Linear under the documentation team first then try again"
|
||||
)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
freshnessCheckLabelId = freshnessCheckLabel.nodes[0].id
|
||||
|
||||
await scanDirectory(repoPath)
|
||||
}
|
||||
|
||||
function getMonthDifference(startDate: Date, endDate: Date) {
|
||||
return (
|
||||
endDate.getMonth() -
|
||||
startDate.getMonth() +
|
||||
12 * (endDate.getFullYear() - startDate.getFullYear())
|
||||
)
|
||||
}
|
||||
|
||||
function subtractMonths(numOfMonths: number, date = new Date()) {
|
||||
date.setMonth(date.getMonth() - numOfMonths)
|
||||
|
||||
return date
|
||||
}
|
||||
|
||||
void main()
|
||||
27
www/utils/packages/scripts/get-generate-diff-message.ts
Normal file
27
www/utils/packages/scripts/get-generate-diff-message.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { execSync } from "child_process"
|
||||
import * as core from "@actions/core"
|
||||
|
||||
const command = `git --no-pager diff --minimal --name-only ../../../apps/resources/references`
|
||||
const diffOutput = execSync(command).toString()
|
||||
|
||||
const files = diffOutput.toString().split("\n").filter(Boolean)
|
||||
|
||||
const referenceNames: Set<string> = new Set([])
|
||||
|
||||
files.forEach((file) => {
|
||||
const referenceName = file
|
||||
.replace("www/apps/resources/references/", "")
|
||||
.split("/")[0]
|
||||
|
||||
if (referenceName) {
|
||||
referenceNames.add(referenceName)
|
||||
}
|
||||
})
|
||||
|
||||
let strOutput = "Generated the following references:\n"
|
||||
|
||||
referenceNames.forEach((referenceName) => {
|
||||
strOutput += `- \`${referenceName}\`\n`
|
||||
})
|
||||
|
||||
core.setOutput("body", strOutput)
|
||||
33
www/utils/packages/scripts/package.json
Normal file
33
www/utils/packages/scripts/package.json
Normal file
@@ -0,0 +1,33 @@
|
||||
{
|
||||
"name": "scripts",
|
||||
"private": true,
|
||||
"license": "MIT",
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"generate:announcement": "ts-node ./doc-change-release.ts",
|
||||
"check:freshness": "ts-node ./freshness-check.ts",
|
||||
"generate:message": "ts-node ./get-generate-diff-message.ts",
|
||||
"check:release-commit": "node ./dist/check-release-commit.js",
|
||||
"generate:changeset": "node ./dist/run-changeset.js",
|
||||
"check:pr-files-count": "node ./dist/check-pr-files-count.js"
|
||||
},
|
||||
"version": "0.0.0",
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"@actions/core": "^1.10.1",
|
||||
"@linear/sdk": "^1.22.0",
|
||||
"@octokit/core": "^4.0.5",
|
||||
"@types/node": "^20.8.3",
|
||||
"chalk": "^5.3.0",
|
||||
"glob": "^10.3.10",
|
||||
"randomcolor": "^0.6.2",
|
||||
"ts-node": "^10.9.1",
|
||||
"typescript": "5.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/randomcolor": "^0.5.8"
|
||||
}
|
||||
}
|
||||
26
www/utils/packages/scripts/run-changeset.ts
Normal file
26
www/utils/packages/scripts/run-changeset.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import util from "node:util"
|
||||
import { exec } from "child_process"
|
||||
import path from "node:path"
|
||||
|
||||
const promiseExec = util.promisify(exec)
|
||||
|
||||
async function main() {
|
||||
// check if there are any changes in diff
|
||||
const diffFiles = (await promiseExec(`git diff --name-only`)).stdout
|
||||
.toString()
|
||||
.split("\n")
|
||||
.filter(Boolean)
|
||||
|
||||
if (!diffFiles.length) {
|
||||
console.log("No files were changed, skipping generating changeset...")
|
||||
return
|
||||
}
|
||||
|
||||
// run changeset
|
||||
await promiseExec(`yarn changeset --empty`, {
|
||||
cwd: path.resolve("..", "..", ".."),
|
||||
})
|
||||
console.log("Generated changeset.")
|
||||
}
|
||||
|
||||
void main()
|
||||
15
www/utils/packages/scripts/tsconfig.json
Normal file
15
www/utils/packages/scripts/tsconfig.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"extends": "../../tsconfig",
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist",
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "node",
|
||||
"rootDir": ".",
|
||||
},
|
||||
"include": ["*.ts"],
|
||||
"ts-node": {
|
||||
"esm": true,
|
||||
"experimentalSpecifierResolution": "node",
|
||||
"transpileOnly": true
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user