mirror of https://github.com/harness/drone.git
[MISC] Abstract URL Provider (#617)
parent
f2bf0991e3
commit
5b33258589
|
@ -60,7 +60,7 @@ type Controller struct {
|
|||
repoStore store.RepoStore
|
||||
gitReporter *eventsgit.Reporter
|
||||
pullreqStore store.PullReqStore
|
||||
urlProvider *url.Provider
|
||||
urlProvider url.Provider
|
||||
}
|
||||
|
||||
func NewController(
|
||||
|
@ -70,7 +70,7 @@ func NewController(
|
|||
repoStore store.RepoStore,
|
||||
gitReporter *eventsgit.Reporter,
|
||||
pullreqStore store.PullReqStore,
|
||||
urlProvider *url.Provider,
|
||||
urlProvider url.Provider,
|
||||
) *Controller {
|
||||
return &Controller{
|
||||
db: db,
|
||||
|
|
|
@ -31,6 +31,6 @@ var WireSet = wire.NewSet(
|
|||
|
||||
func ProvideController(db *sqlx.DB, authorizer authz.Authorizer, principalStore store.PrincipalStore,
|
||||
repoStore store.RepoStore, gitReporter *eventsgit.Reporter, pullreqStore store.PullReqStore,
|
||||
urlProvider *url.Provider) *Controller {
|
||||
urlProvider url.Provider) *Controller {
|
||||
return NewController(db, authorizer, principalStore, repoStore, gitReporter, pullreqStore, urlProvider)
|
||||
}
|
||||
|
|
|
@ -39,7 +39,7 @@ import (
|
|||
|
||||
type Controller struct {
|
||||
db *sqlx.DB
|
||||
urlProvider *url.Provider
|
||||
urlProvider url.Provider
|
||||
authorizer authz.Authorizer
|
||||
pullreqStore store.PullReqStore
|
||||
activityStore store.PullReqActivityStore
|
||||
|
@ -59,7 +59,7 @@ type Controller struct {
|
|||
|
||||
func NewController(
|
||||
db *sqlx.DB,
|
||||
urlProvider *url.Provider,
|
||||
urlProvider url.Provider,
|
||||
authorizer authz.Authorizer,
|
||||
pullreqStore store.PullReqStore,
|
||||
pullreqActivityStore store.PullReqActivityStore,
|
||||
|
|
|
@ -34,7 +34,7 @@ var WireSet = wire.NewSet(
|
|||
ProvideController,
|
||||
)
|
||||
|
||||
func ProvideController(db *sqlx.DB, urlProvider *url.Provider, authorizer authz.Authorizer,
|
||||
func ProvideController(db *sqlx.DB, urlProvider url.Provider, authorizer authz.Authorizer,
|
||||
pullReqStore store.PullReqStore, pullReqActivityStore store.PullReqActivityStore,
|
||||
codeCommentsView store.CodeCommentView,
|
||||
pullReqReviewStore store.PullReqReviewStore, pullReqReviewerStore store.PullReqReviewerStore,
|
||||
|
|
|
@ -39,7 +39,7 @@ import (
|
|||
type Controller struct {
|
||||
defaultBranch string
|
||||
db *sqlx.DB
|
||||
urlProvider *url.Provider
|
||||
urlProvider url.Provider
|
||||
uidCheck check.PathUID
|
||||
authorizer authz.Authorizer
|
||||
repoStore store.RepoStore
|
||||
|
@ -53,7 +53,7 @@ type Controller struct {
|
|||
func NewController(
|
||||
defaultBranch string,
|
||||
db *sqlx.DB,
|
||||
urlProvider *url.Provider,
|
||||
urlProvider url.Provider,
|
||||
uidCheck check.PathUID,
|
||||
authorizer authz.Authorizer,
|
||||
repoStore store.RepoStore,
|
||||
|
@ -109,7 +109,7 @@ func (c *Controller) getRepoCheckAccess(
|
|||
|
||||
// CreateRPCWriteParams creates base write parameters for gitrpc write operations.
|
||||
// IMPORTANT: session & repo are assumed to be not nil!
|
||||
func CreateRPCWriteParams(ctx context.Context, urlProvider *url.Provider,
|
||||
func CreateRPCWriteParams(ctx context.Context, urlProvider url.Provider,
|
||||
session *auth.Session, repo *types.Repository) (gitrpc.WriteParams, error) {
|
||||
// generate envars (add everything githook CLI needs for execution)
|
||||
envVars, err := githook.GenerateEnvironmentVariables(
|
||||
|
|
|
@ -32,7 +32,7 @@ var WireSet = wire.NewSet(
|
|||
ProvideController,
|
||||
)
|
||||
|
||||
func ProvideController(config *types.Config, db *sqlx.DB, urlProvider *url.Provider,
|
||||
func ProvideController(config *types.Config, db *sqlx.DB, urlProvider url.Provider,
|
||||
uidCheck check.PathUID, authorizer authz.Authorizer, repoStore store.RepoStore,
|
||||
spaceStore store.SpaceStore, pipelineStore store.PipelineStore,
|
||||
principalStore store.PrincipalStore, rpcClient gitrpc.Interface,
|
||||
|
|
|
@ -38,7 +38,7 @@ type Controller struct {
|
|||
nestedSpacesEnabled bool
|
||||
|
||||
db *sqlx.DB
|
||||
urlProvider *url.Provider
|
||||
urlProvider url.Provider
|
||||
sseStreamer sse.Streamer
|
||||
uidCheck check.PathUID
|
||||
authorizer authz.Authorizer
|
||||
|
@ -56,7 +56,7 @@ type Controller struct {
|
|||
exporter *exporter.Repository
|
||||
}
|
||||
|
||||
func NewController(config *types.Config, db *sqlx.DB, urlProvider *url.Provider,
|
||||
func NewController(config *types.Config, db *sqlx.DB, urlProvider url.Provider,
|
||||
sseStreamer sse.Streamer, uidCheck check.PathUID, authorizer authz.Authorizer,
|
||||
spacePathStore store.SpacePathStore, pipelineStore store.PipelineStore, secretStore store.SecretStore,
|
||||
connectorStore store.ConnectorStore, templateStore store.TemplateStore, spaceStore store.SpaceStore,
|
||||
|
|
|
@ -34,7 +34,7 @@ var WireSet = wire.NewSet(
|
|||
ProvideController,
|
||||
)
|
||||
|
||||
func ProvideController(config *types.Config, db *sqlx.DB, urlProvider *url.Provider, sseStreamer sse.Streamer,
|
||||
func ProvideController(config *types.Config, db *sqlx.DB, urlProvider url.Provider, sseStreamer sse.Streamer,
|
||||
uidCheck check.PathUID, authorizer authz.Authorizer, spacePathStore store.SpacePathStore,
|
||||
pipelineStore store.PipelineStore, secretStore store.SecretStore,
|
||||
connectorStore store.ConnectorStore, templateStore store.TemplateStore,
|
||||
|
|
|
@ -32,7 +32,7 @@ import (
|
|||
// IMPORTANT: session & repo are assumed to be not nil!
|
||||
// TODO: this is duplicate function from repo controller, we need to see where this
|
||||
// function will be best fit.
|
||||
func CreateRPCWriteParams(ctx context.Context, urlProvider *url.Provider,
|
||||
func CreateRPCWriteParams(ctx context.Context, urlProvider url.Provider,
|
||||
session *auth.Session, repo *types.Repository) (gitrpc.WriteParams, error) {
|
||||
// generate envars (add everything githook CLI needs for execution)
|
||||
envVars, err := githook.GenerateEnvironmentVariables(
|
||||
|
|
|
@ -103,7 +103,7 @@ func GetInfoRefs(client gitrpc.Interface, repoStore store.RepoStore, authorizer
|
|||
}
|
||||
}
|
||||
|
||||
func GetUploadPack(client gitrpc.Interface, urlProvider *url.Provider,
|
||||
func GetUploadPack(client gitrpc.Interface, urlProvider url.Provider,
|
||||
repoStore store.RepoStore, authorizer authz.Authorizer) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
const service = "upload-pack"
|
||||
|
@ -120,7 +120,7 @@ func GetUploadPack(client gitrpc.Interface, urlProvider *url.Provider,
|
|||
}
|
||||
}
|
||||
|
||||
func PostReceivePack(client gitrpc.Interface, urlProvider *url.Provider,
|
||||
func PostReceivePack(client gitrpc.Interface, urlProvider url.Provider,
|
||||
repoStore store.RepoStore, authorizer authz.Authorizer) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
const service = "receive-pack"
|
||||
|
@ -146,7 +146,7 @@ func serviceRPC(
|
|||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
client gitrpc.Interface,
|
||||
urlProvider *url.Provider,
|
||||
urlProvider url.Provider,
|
||||
repoStore store.RepoStore,
|
||||
authorizer authz.Authorizer,
|
||||
service string,
|
||||
|
|
|
@ -129,7 +129,7 @@ type Manager struct {
|
|||
Config *types.Config
|
||||
FileService file.Service
|
||||
Pipelines store.PipelineStore
|
||||
urlProvider *urlprovider.Provider
|
||||
urlProvider urlprovider.Provider
|
||||
Checks store.CheckStore
|
||||
// Converter store.ConvertService
|
||||
SSEStreamer sse.Streamer
|
||||
|
@ -152,7 +152,7 @@ func New(
|
|||
config *types.Config,
|
||||
executionStore store.ExecutionStore,
|
||||
pipelineStore store.PipelineStore,
|
||||
urlProvider *urlprovider.Provider,
|
||||
urlProvider urlprovider.Provider,
|
||||
sseStreamer sse.Streamer,
|
||||
fileService file.Service,
|
||||
logStore store.LogStore,
|
||||
|
|
|
@ -38,7 +38,7 @@ func ProvideExecutionManager(
|
|||
config *types.Config,
|
||||
executionStore store.ExecutionStore,
|
||||
pipelineStore store.PipelineStore,
|
||||
urlProvider *url.Provider,
|
||||
urlProvider url.Provider,
|
||||
sseStreamer sse.Streamer,
|
||||
fileService file.Service,
|
||||
logStore store.LogStore,
|
||||
|
|
|
@ -43,7 +43,7 @@ type GitHandler interface {
|
|||
// NewGitHandler returns a new GitHandler.
|
||||
func NewGitHandler(
|
||||
config *types.Config,
|
||||
urlProvider *url.Provider,
|
||||
urlProvider url.Provider,
|
||||
repoStore store.RepoStore,
|
||||
authenticator authn.Authenticator,
|
||||
authorizer authz.Authorizer,
|
||||
|
|
|
@ -58,7 +58,7 @@ func ProvideRouter(
|
|||
api APIHandler,
|
||||
git GitHandler,
|
||||
web WebHandler,
|
||||
urlProvider *url.Provider,
|
||||
urlProvider url.Provider,
|
||||
) *Router {
|
||||
// use url provider as it has the latest data.
|
||||
gitHostname := urlProvider.GetGITHostname()
|
||||
|
@ -76,7 +76,7 @@ func ProvideRouter(
|
|||
|
||||
func ProvideGitHandler(
|
||||
config *types.Config,
|
||||
urlProvider *url.Provider,
|
||||
urlProvider url.Provider,
|
||||
repoStore store.RepoStore,
|
||||
authenticator authn.Authenticator,
|
||||
authorizer authz.Authorizer,
|
||||
|
|
|
@ -48,7 +48,7 @@ var (
|
|||
)
|
||||
|
||||
type Repository struct {
|
||||
urlProvider *gitnessurl.Provider
|
||||
urlProvider gitnessurl.Provider
|
||||
git gitrpc.Interface
|
||||
repoStore store.RepoStore
|
||||
scheduler *job.Scheduler
|
||||
|
|
|
@ -30,7 +30,7 @@ var WireSet = wire.NewSet(
|
|||
)
|
||||
|
||||
func ProvideSpaceExporter(
|
||||
urlProvider *url.Provider,
|
||||
urlProvider url.Provider,
|
||||
git gitrpc.Interface,
|
||||
repoStore store.RepoStore,
|
||||
scheduler *job.Scheduler,
|
||||
|
|
|
@ -52,7 +52,7 @@ var (
|
|||
|
||||
type Repository struct {
|
||||
defaultBranch string
|
||||
urlProvider *gitnessurl.Provider
|
||||
urlProvider gitnessurl.Provider
|
||||
git gitrpc.Interface
|
||||
db *sqlx.DB
|
||||
repoStore store.RepoStore
|
||||
|
|
|
@ -33,7 +33,7 @@ var WireSet = wire.NewSet(
|
|||
|
||||
func ProvideRepoImporter(
|
||||
config *types.Config,
|
||||
urlProvider *url.Provider,
|
||||
urlProvider url.Provider,
|
||||
git gitrpc.Interface,
|
||||
db *sqlx.DB,
|
||||
repoStore store.RepoStore,
|
||||
|
|
|
@ -49,7 +49,7 @@ type Service struct {
|
|||
codeCommentMigrator *codecomments.Migrator
|
||||
fileViewStore store.PullReqFileViewStore
|
||||
sseStreamer sse.Streamer
|
||||
urlProvider *url.Provider
|
||||
urlProvider url.Provider
|
||||
|
||||
cancelMutex sync.Mutex
|
||||
cancelMergeability map[string]context.CancelFunc
|
||||
|
@ -73,7 +73,7 @@ func New(ctx context.Context,
|
|||
codeCommentMigrator *codecomments.Migrator,
|
||||
fileViewStore store.PullReqFileViewStore,
|
||||
bus pubsub.PubSub,
|
||||
urlProvider *url.Provider,
|
||||
urlProvider url.Provider,
|
||||
sseStreamer sse.Streamer,
|
||||
) (*Service, error) {
|
||||
service := &Service{
|
||||
|
@ -255,7 +255,7 @@ func New(ctx context.Context,
|
|||
// createSystemRPCWriteParams creates base write parameters for gitrpc write operations.
|
||||
func createSystemRPCWriteParams(
|
||||
ctx context.Context,
|
||||
urlProvider *url.Provider,
|
||||
urlProvider url.Provider,
|
||||
repoID int64,
|
||||
repoGITUID string,
|
||||
) (gitrpc.WriteParams, error) {
|
||||
|
|
|
@ -51,7 +51,7 @@ func ProvideService(ctx context.Context,
|
|||
codeCommentMigrator *codecomments.Migrator,
|
||||
fileViewStore store.PullReqFileViewStore,
|
||||
pubsub pubsub.PubSub,
|
||||
urlProvider *url.Provider,
|
||||
urlProvider url.Provider,
|
||||
sseStreamer sse.Streamer,
|
||||
) (*Service, error) {
|
||||
return New(ctx, config, gitReaderFactory, pullReqEvFactory, pullReqEvReporter, gitRPCClient,
|
||||
|
|
|
@ -78,7 +78,7 @@ func (c *Config) Prepare() error {
|
|||
type Service struct {
|
||||
webhookStore store.WebhookStore
|
||||
webhookExecutionStore store.WebhookExecutionStore
|
||||
urlProvider *url.Provider
|
||||
urlProvider url.Provider
|
||||
repoStore store.RepoStore
|
||||
pullreqStore store.PullReqStore
|
||||
principalStore store.PrincipalStore
|
||||
|
@ -98,7 +98,7 @@ func NewService(ctx context.Context, config Config,
|
|||
gitReaderFactory *events.ReaderFactory[*gitevents.Reader],
|
||||
prReaderFactory *events.ReaderFactory[*pullreqevents.Reader],
|
||||
webhookStore store.WebhookStore, webhookExecutionStore store.WebhookExecutionStore,
|
||||
repoStore store.RepoStore, pullreqStore store.PullReqStore, urlProvider *url.Provider,
|
||||
repoStore store.RepoStore, pullreqStore store.PullReqStore, urlProvider url.Provider,
|
||||
principalStore store.PrincipalStore, gitRPCClient gitrpc.Interface, encrypter encrypt.Encrypter,
|
||||
) (*Service, error) {
|
||||
if err := config.Prepare(); err != nil {
|
||||
|
|
|
@ -74,7 +74,7 @@ type RepositoryInfo struct {
|
|||
}
|
||||
|
||||
// repositoryInfoFrom gets the RespositoryInfo from a types.Repository.
|
||||
func repositoryInfoFrom(repo *types.Repository, urlProvider *url.Provider) RepositoryInfo {
|
||||
func repositoryInfoFrom(repo *types.Repository, urlProvider url.Provider) RepositoryInfo {
|
||||
return RepositoryInfo{
|
||||
ID: repo.ID,
|
||||
Path: repo.Path,
|
||||
|
|
|
@ -37,7 +37,7 @@ func ProvideService(ctx context.Context, config Config,
|
|||
gitReaderFactory *events.ReaderFactory[*gitevents.Reader],
|
||||
prReaderFactory *events.ReaderFactory[*pullreqevents.Reader],
|
||||
webhookStore store.WebhookStore, webhookExecutionStore store.WebhookExecutionStore,
|
||||
repoStore store.RepoStore, pullreqStore store.PullReqStore, urlProvider *url.Provider,
|
||||
repoStore store.RepoStore, pullreqStore store.PullReqStore, urlProvider url.Provider,
|
||||
principalStore store.PrincipalStore, gitRPCClient gitrpc.Interface, encrypter encrypt.Encrypter) (*Service, error) {
|
||||
return NewService(ctx, config, gitReaderFactory, prReaderFactory,
|
||||
webhookStore, webhookExecutionStore, repoStore, pullreqStore,
|
||||
|
|
|
@ -21,8 +21,36 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
// Provider is an abstraction of a component that provides system related URLs.
|
||||
// NOTE: Abstract to allow for custom implementation for more complex routing environments.
|
||||
type Provider interface {
|
||||
// GetAPIBaseURLInternal returns the internally reachable base url of the api server.
|
||||
// NOTE: url is guaranteed to not have any trailing '/'.
|
||||
GetAPIBaseURLInternal() string
|
||||
|
||||
// GenerateGITCloneURL generates the public git clone URL for the provided repo path.
|
||||
// NOTE: url is guaranteed to not have any trailing '/'.
|
||||
GenerateGITCloneURL(repoPath string) string
|
||||
|
||||
// GenerateGITCloneURLContainer generates a URL that can be used by CI container builds to
|
||||
// interact with gitness and clone a repo.
|
||||
GenerateGITCloneURLContainer(repoPath string) string
|
||||
|
||||
// GenerateUIPRURL returns the url for the UI screen of an existing pr.
|
||||
GenerateUIPRURL(repoPath string, prID int64) string
|
||||
|
||||
// GenerateUICompareURL returns the url for the UI screen comparing two references.
|
||||
GenerateUICompareURL(repoPath string, ref1 string, ref2 string) string
|
||||
|
||||
// GetAPIHostname returns the host for the api endpoint.
|
||||
GetAPIHostname() string
|
||||
|
||||
// GetGITHostname returns the host for the git endpoint.
|
||||
GetGITHostname() string
|
||||
}
|
||||
|
||||
// Provider provides the URLs of the gitness system.
|
||||
type Provider struct {
|
||||
type provider struct {
|
||||
// apiURL stores the raw URL the api endpoints are reachable at publicly.
|
||||
apiURL *url.URL
|
||||
|
||||
|
@ -49,7 +77,7 @@ func NewProvider(
|
|||
gitURLRaw,
|
||||
gitURLContainerRaw string,
|
||||
uiURLRaw string,
|
||||
) (*Provider, error) {
|
||||
) (Provider, error) {
|
||||
// remove trailing '/' to make usage easier
|
||||
apiURLRaw = strings.TrimRight(apiURLRaw, "/")
|
||||
apiURLInternalRaw = strings.TrimRight(apiURLInternalRaw, "/")
|
||||
|
@ -81,7 +109,7 @@ func NewProvider(
|
|||
return nil, fmt.Errorf("provided uiURLRaw '%s' is invalid: %w", uiURLRaw, err)
|
||||
}
|
||||
|
||||
return &Provider{
|
||||
return &provider{
|
||||
apiURL: apiURL,
|
||||
apiURLInternalRaw: apiURLInternalRaw,
|
||||
gitURL: gitURL,
|
||||
|
@ -90,15 +118,11 @@ func NewProvider(
|
|||
}, nil
|
||||
}
|
||||
|
||||
// GetAPIBaseURLInternal returns the internally reachable base url of the api server.
|
||||
// NOTE: url is guaranteed to not have any trailing '/'.
|
||||
func (p *Provider) GetAPIBaseURLInternal() string {
|
||||
func (p *provider) GetAPIBaseURLInternal() string {
|
||||
return p.apiURLInternalRaw
|
||||
}
|
||||
|
||||
// GenerateGITCloneURL generates the public git clone URL for the provided repo path.
|
||||
// NOTE: url is guaranteed to not have any trailing '/'.
|
||||
func (p *Provider) GenerateGITCloneURL(repoPath string) string {
|
||||
func (p *provider) GenerateGITCloneURL(repoPath string) string {
|
||||
repoPath = path.Clean(repoPath)
|
||||
if !strings.HasSuffix(repoPath, ".git") {
|
||||
repoPath += ".git"
|
||||
|
@ -107,9 +131,7 @@ func (p *Provider) GenerateGITCloneURL(repoPath string) string {
|
|||
return p.gitURL.JoinPath(repoPath).String()
|
||||
}
|
||||
|
||||
// GenerateGITCloneURLContainer generates a URL that can be used by CI container builds to
|
||||
// interact with gitness and clone a repo.
|
||||
func (p *Provider) GenerateGITCloneURLContainer(repoPath string) string {
|
||||
func (p *provider) GenerateGITCloneURLContainer(repoPath string) string {
|
||||
repoPath = path.Clean(repoPath)
|
||||
if !strings.HasSuffix(repoPath, ".git") {
|
||||
repoPath += ".git"
|
||||
|
@ -118,22 +140,18 @@ func (p *Provider) GenerateGITCloneURLContainer(repoPath string) string {
|
|||
return p.gitURLContainer.JoinPath(repoPath).String()
|
||||
}
|
||||
|
||||
// GenerateUIPRURL returns the url for the UI screen of an existing pr.
|
||||
func (p *Provider) GenerateUIPRURL(repoPath string, prID int64) string {
|
||||
func (p *provider) GenerateUIPRURL(repoPath string, prID int64) string {
|
||||
return p.uiURL.JoinPath(repoPath, "pulls", fmt.Sprint(prID)).String()
|
||||
}
|
||||
|
||||
// GenerateUICompareURL returns the url for the UI screen comparing two references.
|
||||
func (p *Provider) GenerateUICompareURL(repoPath string, ref1 string, ref2 string) string {
|
||||
func (p *provider) GenerateUICompareURL(repoPath string, ref1 string, ref2 string) string {
|
||||
return p.uiURL.JoinPath(repoPath, "pulls/compare", ref1+"..."+ref2).String()
|
||||
}
|
||||
|
||||
// GetAPIHostname returns the host for the api endpoint.
|
||||
func (p *Provider) GetAPIHostname() string {
|
||||
func (p *provider) GetAPIHostname() string {
|
||||
return p.apiURL.Hostname()
|
||||
}
|
||||
|
||||
// GetGITHostname returns the host for the git endpoint.
|
||||
func (p *Provider) GetGITHostname() string {
|
||||
func (p *provider) GetGITHostname() string {
|
||||
return p.gitURL.Hostname()
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ import (
|
|||
// WireSet provides a wire set for this package.
|
||||
var WireSet = wire.NewSet(ProvideURLProvider)
|
||||
|
||||
func ProvideURLProvider(config *types.Config) (*Provider, error) {
|
||||
func ProvideURLProvider(config *types.Config) (Provider, error) {
|
||||
return NewProvider(
|
||||
config.URL.API,
|
||||
config.URL.APIInternal,
|
||||
|
|
|
@ -8,6 +8,7 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
|
||||
check2 "github.com/harness/gitness/app/api/controller/check"
|
||||
"github.com/harness/gitness/app/api/controller/connector"
|
||||
"github.com/harness/gitness/app/api/controller/execution"
|
||||
|
|
|
@ -67,6 +67,7 @@ func (c *Config) Validate() error {
|
|||
return errors.New("config is required")
|
||||
}
|
||||
if c.Port < 0 {
|
||||
//nolint: stylecheck // that's the name of the field
|
||||
return errors.New("Port is required")
|
||||
}
|
||||
if c.GitRoot == "" {
|
||||
|
|
Loading…
Reference in New Issue