use local git repo - basic features seem to work
This commit is contained in:
@@ -1,45 +1,44 @@
|
||||
import {
|
||||
createDatabase,
|
||||
createLocalDatabase,
|
||||
FilesystemBridge,
|
||||
IsomorphicBridge,
|
||||
} from "@tinacms/datalayer";
|
||||
import { RedisLevel } from "upstash-redis-level";
|
||||
import { Redis } from "@upstash/redis";
|
||||
import { GiteaGitProvider } from "./gitea-git-provider";
|
||||
|
||||
const isLocal = process.env.TINA_PUBLIC_IS_LOCAL === "true";
|
||||
const branch = process.env.TINA_GIT_BRANCH || "main";
|
||||
|
||||
function createProductionDatabase() {
|
||||
const giteaUrl = process.env.GITEA_URL;
|
||||
const giteaToken = process.env.GITEA_TOKEN;
|
||||
const giteaOwner = process.env.GITEA_OWNER;
|
||||
const giteaRepo = process.env.GITEA_REPO;
|
||||
const kvUrl = process.env.KV_REST_API_URL;
|
||||
const kvToken = process.env.KV_REST_API_TOKEN;
|
||||
|
||||
if (!giteaUrl || !giteaToken || !giteaOwner || !giteaRepo) {
|
||||
if (!kvUrl || !kvToken) {
|
||||
// During tinacms build (schema generation), env vars may not be available.
|
||||
// Fall back to local database for the build step.
|
||||
return createLocalDatabase();
|
||||
}
|
||||
|
||||
return createDatabase({
|
||||
gitProvider: new GiteaGitProvider({
|
||||
owner: giteaOwner,
|
||||
repo: giteaRepo,
|
||||
token: giteaToken,
|
||||
branch,
|
||||
baseUrl: giteaUrl,
|
||||
}),
|
||||
gitProvider: {
|
||||
onPut: async () => {},
|
||||
onDelete: async () => {},
|
||||
},
|
||||
databaseAdapter: new RedisLevel<string, Record<string, unknown>>({
|
||||
redis: new Redis({
|
||||
url: kvUrl || "http://localhost:8079",
|
||||
token: kvToken || "example_token",
|
||||
url: kvUrl,
|
||||
token: kvToken,
|
||||
}) as any,
|
||||
debug: process.env.DEBUG === "true" || false,
|
||||
}),
|
||||
bridge: new FilesystemBridge(process.cwd()),
|
||||
bridge: new IsomorphicBridge(process.cwd(), {
|
||||
gitRoot: process.cwd(),
|
||||
author: {
|
||||
name: "TinaCMS",
|
||||
email: "cms@linumiq.com",
|
||||
},
|
||||
commitMessage: "Content updated via TinaCMS",
|
||||
}),
|
||||
namespace: branch,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,123 +0,0 @@
|
||||
import { Base64 } from "js-base64";
|
||||
import type { GitProvider } from "@tinacms/datalayer";
|
||||
|
||||
export interface GiteaGitProviderOptions {
|
||||
owner: string;
|
||||
repo: string;
|
||||
token: string;
|
||||
branch: string;
|
||||
baseUrl: string;
|
||||
commitMessage?: string;
|
||||
rootPath?: string;
|
||||
}
|
||||
|
||||
export class GiteaGitProvider implements GitProvider {
|
||||
owner: string;
|
||||
repo: string;
|
||||
branch: string;
|
||||
baseUrl: string;
|
||||
commitMessage: string;
|
||||
rootPath?: string;
|
||||
private token: string;
|
||||
|
||||
constructor(args: GiteaGitProviderOptions) {
|
||||
this.owner = args.owner;
|
||||
this.repo = args.repo;
|
||||
this.branch = args.branch;
|
||||
this.baseUrl = args.baseUrl.replace(/\/$/, "");
|
||||
this.commitMessage = args.commitMessage || "Edited with TinaCMS";
|
||||
this.rootPath = args.rootPath;
|
||||
this.token = args.token;
|
||||
}
|
||||
|
||||
private getApiUrl(path: string): string {
|
||||
return `${this.baseUrl}/api/v1/repos/${this.owner}/${this.repo}/contents/${path}`;
|
||||
}
|
||||
|
||||
private getHeaders(): Record<string, string> {
|
||||
return {
|
||||
Authorization: `token ${this.token}`,
|
||||
"Content-Type": "application/json",
|
||||
Accept: "application/json",
|
||||
};
|
||||
}
|
||||
|
||||
private getKeyWithPath(key: string): string {
|
||||
return this.rootPath ? `${this.rootPath}/${key}` : key;
|
||||
}
|
||||
|
||||
private async getFileSha(
|
||||
keyWithPath: string
|
||||
): Promise<string | undefined> {
|
||||
try {
|
||||
const url = `${this.getApiUrl(keyWithPath)}?ref=${encodeURIComponent(this.branch)}`;
|
||||
const response = await fetch(url, {
|
||||
method: "GET",
|
||||
headers: this.getHeaders(),
|
||||
});
|
||||
if (!response.ok) {
|
||||
return undefined;
|
||||
}
|
||||
const data = await response.json();
|
||||
return data.sha;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
async onPut(key: string, value: string): Promise<void> {
|
||||
const keyWithPath = this.getKeyWithPath(key);
|
||||
const sha = await this.getFileSha(keyWithPath);
|
||||
|
||||
const body: Record<string, unknown> = {
|
||||
message: this.commitMessage,
|
||||
content: Base64.encode(value),
|
||||
branch: this.branch,
|
||||
};
|
||||
|
||||
if (sha) {
|
||||
body.sha = sha;
|
||||
}
|
||||
|
||||
const response = await fetch(this.getApiUrl(keyWithPath), {
|
||||
method: sha ? "PUT" : "POST",
|
||||
headers: this.getHeaders(),
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
throw new Error(
|
||||
`Failed to save ${keyWithPath}: ${response.status} ${errorText}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async onDelete(key: string): Promise<void> {
|
||||
const keyWithPath = this.getKeyWithPath(key);
|
||||
const sha = await this.getFileSha(keyWithPath);
|
||||
|
||||
if (!sha) {
|
||||
throw new Error(
|
||||
`Could not find file ${keyWithPath} in repo ${this.owner}/${this.repo}`
|
||||
);
|
||||
}
|
||||
|
||||
const response = await fetch(this.getApiUrl(keyWithPath), {
|
||||
method: "DELETE",
|
||||
headers: this.getHeaders(),
|
||||
body: JSON.stringify({
|
||||
message: this.commitMessage,
|
||||
branch: this.branch,
|
||||
sha,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
throw new Error(
|
||||
`Failed to delete ${keyWithPath}: ${response.status} ${errorText}`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user