Add support for remote forges

This commit is contained in:
Mustafa Gezen 2023-08-28 05:12:22 +02:00
parent 26aeb7cc9e
commit 051589caee
Signed by: mustafa
GPG Key ID: DCDF010D946438C1
3 changed files with 67 additions and 0 deletions

View File

@ -0,0 +1,12 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "forge",
srcs = [
"caching.go",
"forge.go",
],
importpath = "go.resf.org/peridot/tools/mothership/worker_server/forge",
visibility = ["//visibility:public"],
deps = ["//vendor/github.com/go-git/go-git/v5/plumbing/transport"],
)

View File

@ -1 +1,35 @@
package forge
import "time"
type Cacher struct {
Forge
authenticator *Authenticator
}
func NewCacher(f Forge) *Cacher {
return &Cacher{
Forge: f,
}
}
func (c *Cacher) GetAuthenticator() (*Authenticator, error) {
if c.authenticator != nil {
// Check if the token is expired, if not return it
if !c.authenticator.Expires.Before(time.Now()) {
return c.authenticator, nil
}
}
// Otherwise, get a new token
c.authenticator = nil
a, err := c.Forge.GetAuthenticator()
if err != nil {
return nil, err
}
c.authenticator = a
return a, nil
}

View File

@ -1 +1,22 @@
package forge
import (
"github.com/go-git/go-git/v5/plumbing/transport"
"time"
)
type Authenticator struct {
transport.AuthMethod
AuthorName string
AuthorEmail string
// Expires is the time when the token expires.
// So it can be used to cache the token.
Expires time.Time
}
type Forge interface {
GetAuthenticator() (*Authenticator, error)
GetRemote() string
EnsureRepositoryExists(auth *Authenticator, repo string) error
}