docs: refactoring of docblock generator tool (#6261)
small refactoring of the docblock generator tool that moves all git operations and requests into the `GitManager`
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import { Octokit } from "octokit"
|
||||
import promiseExec from "../utils/promise-exec.js"
|
||||
import getMonorepoRoot from "../utils/get-monorepo-root.js"
|
||||
import filterFiles from "../utils/filter-files.js"
|
||||
|
||||
type Options = {
|
||||
owner?: string
|
||||
@@ -52,19 +55,7 @@ export class GitManager {
|
||||
|
||||
await Promise.all(
|
||||
commits.map(async (commit) => {
|
||||
const {
|
||||
data: { files: commitFiles },
|
||||
} = await this.octokit.request(
|
||||
"GET /repos/{owner}/{repo}/commits/{ref}",
|
||||
{
|
||||
owner: this.owner,
|
||||
repo: this.repo,
|
||||
ref: commit.sha,
|
||||
headers: {
|
||||
"X-GitHub-Api-Version": this.gitApiVersion,
|
||||
},
|
||||
}
|
||||
)
|
||||
const commitFiles = await this.getCommitFiles(commit.sha)
|
||||
|
||||
commitFiles?.forEach((commitFile) => files.add(commitFile.filename))
|
||||
})
|
||||
@@ -72,4 +63,33 @@ export class GitManager {
|
||||
|
||||
return [...files]
|
||||
}
|
||||
|
||||
async getDiffFiles(): Promise<string[]> {
|
||||
const childProcess = await promiseExec(
|
||||
`git diff --name-only -- "packages/**/**.ts" "packages/**/*.js" "packages/**/*.tsx" "packages/**/*.jsx"`,
|
||||
{
|
||||
cwd: getMonorepoRoot(),
|
||||
}
|
||||
)
|
||||
|
||||
return filterFiles(
|
||||
childProcess.stdout.toString().split("\n").filter(Boolean)
|
||||
)
|
||||
}
|
||||
|
||||
async getCommitFiles(commitSha: string) {
|
||||
const {
|
||||
data: { files },
|
||||
} = await this.octokit.request("GET /repos/{owner}/{repo}/commits/{ref}", {
|
||||
owner: "medusajs",
|
||||
repo: "medusa",
|
||||
ref: commitSha,
|
||||
headers: {
|
||||
"X-GitHub-Api-Version": "2022-11-28",
|
||||
},
|
||||
per_page: 3000,
|
||||
})
|
||||
|
||||
return files
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,13 @@
|
||||
import path from "path"
|
||||
import DocblockGenerator from "../classes/docblock-generator.js"
|
||||
import getMonorepoRoot from "../utils/get-monorepo-root.js"
|
||||
import promiseExec from "../utils/promise-exec.js"
|
||||
import filterFiles from "../utils/filter-files.js"
|
||||
import { GitManager } from "../classes/git-manager.js"
|
||||
|
||||
export default async function runGitChanges() {
|
||||
const monorepoPath = getMonorepoRoot()
|
||||
// retrieve the changed files under `packages` in the monorepo root.
|
||||
const childProcess = await promiseExec(
|
||||
`git diff --name-only -- "packages/**/**.ts" "packages/**/*.js" "packages/**/*.tsx" "packages/**/*.jsx"`,
|
||||
{
|
||||
cwd: monorepoPath,
|
||||
}
|
||||
)
|
||||
|
||||
let files = filterFiles(
|
||||
childProcess.stdout.toString().split("\n").filter(Boolean)
|
||||
)
|
||||
const gitManager = new GitManager()
|
||||
let files = await gitManager.getDiffFiles()
|
||||
|
||||
if (!files.length) {
|
||||
console.log(`No file changes detected.`)
|
||||
|
||||
@@ -1,26 +1,15 @@
|
||||
import { Octokit } from "@octokit/core"
|
||||
import filterFiles from "../utils/filter-files.js"
|
||||
import path from "path"
|
||||
import getMonorepoRoot from "../utils/get-monorepo-root.js"
|
||||
import DocblockGenerator from "../classes/docblock-generator.js"
|
||||
import { GitManager } from "../classes/git-manager.js"
|
||||
|
||||
export default async function (commitSha: string) {
|
||||
const monorepoPath = getMonorepoRoot()
|
||||
// retrieve the files changed in the commit
|
||||
const octokit = new Octokit({
|
||||
auth: process.env.GH_TOKEN,
|
||||
})
|
||||
const gitManager = new GitManager()
|
||||
|
||||
const {
|
||||
data: { files },
|
||||
} = await octokit.request("GET /repos/{owner}/{repo}/commits/{ref}", {
|
||||
owner: "medusajs",
|
||||
repo: "medusa",
|
||||
ref: commitSha,
|
||||
headers: {
|
||||
"X-GitHub-Api-Version": "2022-11-28",
|
||||
},
|
||||
})
|
||||
const files = await gitManager.getCommitFiles(commitSha)
|
||||
|
||||
// filter changed files
|
||||
let filteredFiles = filterFiles(files?.map((file) => file.filename) || [])
|
||||
|
||||
Reference in New Issue
Block a user