refactor: split git provider adapters

This commit is contained in:
Tom Boullay
2026-05-17 14:12:09 +02:00
parent 377ed7cfb3
commit 81c513ee1f
13 changed files with 779 additions and 644 deletions
+102
View File
@@ -0,0 +1,102 @@
import { getRemoteFileEntry, getRequiredContentEntrySha, readRemoteFolder } from '../content'
import { getRepoApiPath, requestGitJson } from '../http'
import { buildLfsPointer, splitLfsFiles, uploadToLfs } from '../lfs'
import type { GitProvider, GitRemoteConfig, LfsPushFile, PushFilesParams } from '../types'
import type { PushFile } from '@/lib/types'
import { isRecord } from '@/lib/guards'
interface GiteaFileOperation {
content?: string
operation: 'create' | 'update' | 'delete'
path: string
sha?: string
}
function getGiteaCommitUrl(value: unknown, remote: GitRemoteConfig, branch: string) {
if (isRecord(value) && isRecord(value.commit)) {
if (typeof value.commit.html_url === 'string') return value.commit.html_url
if (typeof value.commit.sha === 'string') return `${remote.webUrl}/commit/${value.commit.sha}`
}
return `${remote.webUrl}/commits/branch/${branch}`
}
function buildCommittedFiles(regularFiles: PushFile[], lfsFiles: LfsPushFile[]): PushFile[] {
return [
...regularFiles,
...lfsFiles.map((file) => ({
path: file.path,
contentBase64: Buffer.from(buildLfsPointer(file.oid, file.size), 'utf-8').toString('base64'),
})),
]
}
async function buildGiteaOperations(
remote: GitRemoteConfig,
branch: string,
committedFiles: PushFile[],
deletePaths: string[],
) {
const newFilePaths = new Set(committedFiles.map((file) => file.path))
const operations: GiteaFileOperation[] = []
for (const file of committedFiles) {
const existing = await getRemoteFileEntry(remote, file.path, branch)
operations.push({
content: file.contentBase64,
operation: existing ? 'update' : 'create',
path: file.path,
sha: existing ? getRequiredContentEntrySha(existing, file.path) : undefined,
})
}
for (const path of deletePaths) {
if (newFilePaths.has(path)) continue
const existing = await getRemoteFileEntry(remote, path, branch)
if (!existing) continue
operations.push({
operation: 'delete',
path,
sha: getRequiredContentEntrySha(existing, path),
})
}
return operations
}
export function createGiteaProvider(remote: GitRemoteConfig, branch: string): GitProvider {
return {
getRemoteFolder(folderPath) {
return readRemoteFolder(remote, branch, folderPath)
},
async pushFiles({ files, deletePaths, commitMessage }: PushFilesParams) {
const { lfsFiles, regularFiles } = splitLfsFiles(files)
await uploadToLfs(
remote,
lfsFiles.map((file) => ({ oid: file.oid, size: file.size, contentBase64: file.contentBase64 })),
)
const committedFiles = buildCommittedFiles(regularFiles, lfsFiles)
const operations = await buildGiteaOperations(remote, branch, committedFiles, deletePaths)
if (operations.length === 0) {
return { commitUrl: `${remote.webUrl}/commits/branch/${branch}` }
}
const data = await requestGitJson(remote, `${getRepoApiPath(remote)}/contents`, {
method: 'POST',
body: {
branch,
files: operations,
message: commitMessage,
},
})
return { commitUrl: getGiteaCommitUrl(data, remote, branch) }
},
}
}
+106
View File
@@ -0,0 +1,106 @@
import { Octokit } from '@octokit/rest'
import { readRemoteFolder } from '../content'
import { buildLfsPointer, splitLfsFiles, uploadToLfs } from '../lfs'
import type { GitProvider, GitRemoteConfig, PushFilesParams } from '../types'
export function createGitHubProvider(remote: GitRemoteConfig, branch: string): GitProvider {
const octokit = new Octokit({
auth: remote.token,
baseUrl: remote.apiBaseUrl,
})
return {
getRemoteFolder(folderPath) {
return readRemoteFolder(remote, branch, folderPath)
},
async pushFiles({ files, deletePaths, commitMessage }: PushFilesParams) {
const { lfsFiles, regularFiles } = splitLfsFiles(files)
await uploadToLfs(
remote,
lfsFiles.map((file) => ({ oid: file.oid, size: file.size, contentBase64: file.contentBase64 })),
)
const { owner, repo } = remote
const { data: ref } = await octokit.git.getRef({
owner,
repo,
ref: `heads/${branch}`,
})
const latestCommitSha = ref.object.sha
const { data: commit } = await octokit.git.getCommit({
owner,
repo,
commit_sha: latestCommitSha,
})
const allFiles = [...regularFiles, ...lfsFiles]
const blobResults = await Promise.all(
allFiles.map((file) => {
const lfs = lfsFiles.find((lfsFile) => lfsFile.path === file.path)
if (lfs) {
const pointer = buildLfsPointer(lfs.oid, lfs.size)
return octokit.git.createBlob({
owner,
repo,
content: Buffer.from(pointer, 'utf-8').toString('base64'),
encoding: 'base64',
})
}
return octokit.git.createBlob({
owner,
repo,
content: file.contentBase64,
encoding: 'base64',
})
}),
)
const newFilePaths = new Set(files.map((file) => file.path))
const deleteEntries = deletePaths
.filter((path) => !newFilePaths.has(path))
.map((path) => ({
path,
mode: '100644' as const,
type: 'blob' as const,
sha: null,
}))
const { data: newTree } = await octokit.git.createTree({
owner,
repo,
base_tree: commit.tree.sha,
tree: [
...allFiles.map((file, index) => ({
path: file.path,
mode: '100644' as const,
type: 'blob' as const,
sha: blobResults[index].data.sha,
})),
...deleteEntries,
],
})
const { data: newCommit } = await octokit.git.createCommit({
owner,
repo,
message: commitMessage,
tree: newTree.sha,
parents: [latestCommitSha],
})
await octokit.git.updateRef({
owner,
repo,
ref: `heads/${branch}`,
sha: newCommit.sha,
})
return { commitUrl: newCommit.html_url || `${remote.webUrl}/commit/${newCommit.sha}` }
},
}
}