dalgo2ghingitdb

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 4, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package dalgo2ghingitdb provides a DALgo database adapter for reading inGitDB repositories from GitHub using the GitHub API. It supports read-only access to public repositories with no authentication required. Authenticated access is configured either with a static Config.Token or, for rotating credentials such as short-lived GitHub App installation tokens, with a Config.TokenProvider that is consulted on every request.

Index

Constants

View Source
const DatabaseID = "ingitdb-github"

Variables

This section is empty.

Functions

func NewGitHubDB

func NewGitHubDB(cfg Config) (dal.DB, error)

NewGitHubDB creates a GitHub repository adapter. Note: Definition is required for most operations, so prefer NewGitHubDBWithDef.

func NewGitHubDBWithDef

func NewGitHubDBWithDef(cfg Config, def *ingitdb.Definition) (dal.DB, error)

Types

type BatchingGitHubDB

type BatchingGitHubDB struct {
	// contains filtered or unexported fields
}

BatchingGitHubDB wraps a githubDB so that RunReadwriteTransaction buffers every tx.Set / tx.Insert / tx.Delete call inside the worker, then emits exactly one commit via the Git Data API when the worker returns nil.

Reads (Get, RunReadonlyTransaction, ExecuteQuery*) delegate to the underlying githubDB unchanged — they read the pre-tx state from remote and do not observe buffered writes (set-mode callers fetch matches in a separate read-only pass before opening the write tx, so this limitation does not affect them).

Use this in place of the per-file github DB when an operation may touch multiple records — `update --from --where`, `delete --from --where`, `update --from --all`, `delete --from --all`. Single-record operations already produce one commit through the Contents API and should keep using the plain githubDB.

func NewBatchingGitHubDB

func NewBatchingGitHubDB(cfg Config, def *ingitdb.Definition, commitMessage string) (*BatchingGitHubDB, error)

NewBatchingGitHubDB builds a BatchingGitHubDB for the given Config + def. commitMessage is the message used when flushing buffered changes; callers supply something human-readable like "ingitdb: update countries (batch)".

func (BatchingGitHubDB) Adapter

func (db BatchingGitHubDB) Adapter() dal.Adapter

func (BatchingGitHubDB) ExecuteQueryToRecordsReader

func (db BatchingGitHubDB) ExecuteQueryToRecordsReader(ctx context.Context, query dal.Query) (dal.RecordsReader, error)

func (BatchingGitHubDB) ExecuteQueryToRecordsetReader

func (db BatchingGitHubDB) ExecuteQueryToRecordsetReader(ctx context.Context, query dal.Query, options ...recordset.Option) (dal.RecordsetReader, error)

func (BatchingGitHubDB) Exists

func (db BatchingGitHubDB) Exists(ctx context.Context, key *dal.Key) (bool, error)

func (BatchingGitHubDB) Get

func (db BatchingGitHubDB) Get(ctx context.Context, record dal.Record) error

func (BatchingGitHubDB) GetMulti

func (db BatchingGitHubDB) GetMulti(ctx context.Context, records []dal.Record) error

func (BatchingGitHubDB) ID

func (db BatchingGitHubDB) ID() string

func (BatchingGitHubDB) RunReadonlyTransaction

func (db BatchingGitHubDB) RunReadonlyTransaction(ctx context.Context, f dal.ROTxWorker, options ...dal.TransactionOption) error

func (*BatchingGitHubDB) RunReadwriteTransaction

func (db *BatchingGitHubDB) RunReadwriteTransaction(ctx context.Context, f dal.RWTxWorker, options ...dal.TransactionOption) error

RunReadwriteTransaction overrides githubDB.RunReadwriteTransaction with a batching variant. Every Set / Insert / Delete inside f is buffered; when f returns nil, all buffered changes are flushed to GitHub as one commit. If f returns an error, no changes are committed (the remote is untouched).

func (BatchingGitHubDB) Schema

func (db BatchingGitHubDB) Schema() dal.Schema

type Config

type Config struct {
	Owner string
	Repo  string
	Ref   string

	// Token is a static GitHub token (e.g. a classic/fine-grained PAT).
	// If TokenProvider is nil and Token is non-empty, Token is wrapped in
	// StaticTokenProvider automatically, preserving legacy behavior.
	Token string

	// TokenProvider, when set, supplies the token per request and takes
	// precedence over Token. Use it to inject rotating credentials such as
	// short-lived GitHub App installation tokens.
	TokenProvider TokenProvider

	APIBaseURL string
	HTTPClient *http.Client
}

Config defines connection settings for reading an inGitDB repository from GitHub.

type FileReader

type FileReader interface {
	ReadFile(ctx context.Context, path string) (content []byte, found bool, err error)
	ListDirectory(ctx context.Context, dirPath string) (entries []string, err error)
}

FileReader reads repository files by path from GitHub.

func NewGitHubFileReader

func NewGitHubFileReader(cfg Config) (FileReader, error)

type TokenProvider added in v0.1.0

type TokenProvider interface {
	Token(ctx context.Context) (string, error)
}

TokenProvider supplies the GitHub token used to authorize API calls. It is invoked once per outgoing HTTP request, so implementations can rotate tokens (e.g. mint short-lived GitHub App installation tokens on demand) without rebuilding the adapter. Returning an empty token with a nil error sends the request unauthenticated (fine for public-repo reads).

See docs/roadmaps/ovdb-access-tokens-grants.md in sneat-co/backstage (Decision 3.4): the sneat-go backend injects a provider that mints 1-hour installation tokens with an in-memory cache; CLI/dev injects StaticTokenProvider with a PAT.

func StaticTokenProvider added in v0.1.0

func StaticTokenProvider(token string) TokenProvider

StaticTokenProvider returns a TokenProvider that always yields the given fixed token (e.g. a personal access token for CLI/dev usage). It preserves the legacy Config.Token behavior: a non-empty Config.Token is wrapped in a StaticTokenProvider automatically when Config.TokenProvider is nil.

type TokenProviderFunc added in v0.1.0

type TokenProviderFunc func(ctx context.Context) (string, error)

TokenProviderFunc adapts a plain function to the TokenProvider interface.

func (TokenProviderFunc) Token added in v0.1.0

func (f TokenProviderFunc) Token(ctx context.Context) (string, error)

Token implements TokenProvider.

type TreeChange

type TreeChange struct {
	Path    string
	Content []byte
}

TreeChange describes a single path modification within a tree. Content nil means "delete the file at Path"; non-nil Content means "set the file at Path to this content" (creating it if absent).

type TreeWriter

type TreeWriter struct {
	// contains filtered or unexported fields
}

TreeWriter performs atomic multi-file commits on a remote GitHub repository via the Git Data API (blobs / trees / commits / refs). Unlike the per-file Contents API used by FileReader.writeFile / deleteFile (which each produce their own commit), TreeWriter bundles arbitrary file modifications into a single commit — satisfying spec REQ:one-commit-per-write for multi-file operations such as `drop collection`.

func NewTreeWriter

func NewTreeWriter(cfg Config) (*TreeWriter, error)

NewTreeWriter builds a TreeWriter for the given Config. It does not perform any network I/O; the first request fires on the first method call.

func (*TreeWriter) CommitChanges

func (w *TreeWriter) CommitChanges(ctx context.Context, message string, changes []TreeChange) (string, error)

CommitChanges atomically applies the given changes in a single commit on the target branch and returns the new commit SHA. The repository ends in exactly one of two states: either every change is applied as part of the new commit, or no change is applied at all (the previous head is unchanged).

Changes with nil Content delete the path; non-nil Content creates or updates a blob at the path with that content. Mode is set to "100644" (non-executable file) for all non-delete entries.

func (*TreeWriter) ListFilesUnder

func (w *TreeWriter) ListFilesUnder(ctx context.Context, dir string) ([]string, error)

ListFilesUnder returns the paths of every blob in the current tree of the target branch whose path equals dir or has dir+"/" as a prefix. Empty dir returns every blob in the tree. Returned paths are relative to the repo root. Used to enumerate the files to delete when dropping a collection.

Errors if the upstream tree is truncated (too large to fetch in one call); drop semantics require an atomic view of the directory.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL