Fix gitlab hooks and simplify config extension (#2537)

- closes #2534 
- remove `IsConfigured` func from config extension.
  If `server.Config.Services.ConfigService != nil` it is always configured
This commit is contained in:
qwerty287 2023-10-07 16:41:25 +02:00 committed by GitHub
parent bb17d25963
commit 3bd53b379e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 8 additions and 13 deletions

View file

@ -69,7 +69,7 @@ func (cf *configFetcher) Fetch(ctx context.Context) (files []*types.FileMeta, er
continue continue
} }
if cf.configExtension != nil && cf.configExtension.IsConfigured() { if cf.configExtension != nil {
fetchCtx, cancel := context.WithTimeout(ctx, cf.timeout) fetchCtx, cancel := context.WithTimeout(ctx, cf.timeout)
defer cancel() // ok here as we only try http fetching once, returning on fail and success defer cancel() // ok here as we only try http fetching once, returning on fail and success

View file

@ -316,7 +316,7 @@ func TestFetch(t *testing.T) {
configFetcher := forge.NewConfigFetcher( configFetcher := forge.NewConfigFetcher(
f, f,
time.Second*3, time.Second*3,
config.NewHTTP("", ""), nil,
&model.User{Token: "xxx"}, &model.User{Token: "xxx"},
repo, repo,
&model.Pipeline{Commit: "89ab7b2d6bfb347144ac7c557e638ab402848fee"}, &model.Pipeline{Commit: "89ab7b2d6bfb347144ac7c557e638ab402848fee"},

View file

@ -447,7 +447,7 @@ func (g *GitLab) getTokenAndWebURL(link string) (token, webURL string, err error
return "", "", err return "", "", err
} }
token = uri.Query().Get("access_token") token = uri.Query().Get("access_token")
webURL = fmt.Sprintf("%s://%s/api/hook", uri.Scheme, uri.Host) webURL = fmt.Sprintf("%s://%s/%s", uri.Scheme, uri.Host, strings.TrimPrefix(uri.Path, "/"))
return token, webURL, nil return token, webURL, nil
} }

View file

@ -110,12 +110,12 @@ func Test_GitLab(t *testing.T) {
// Test activate method // Test activate method
g.Describe("Activate", func() { g.Describe("Activate", func() {
g.It("Should be success", func() { g.It("Should be success", func() {
err := client.Activate(ctx, &user, &repo, "http://example.com/api/hook/test/test?access_token=token") err := client.Activate(ctx, &user, &repo, "http://example.com/api/hook?access_token=token")
assert.NoError(t, err) assert.NoError(t, err)
}) })
g.It("Should be failed, when token not given", func() { g.It("Should be failed, when token not given", func() {
err := client.Activate(ctx, &user, &repo, "http://example.com/api/hook/test/test") err := client.Activate(ctx, &user, &repo, "http://example.com/api/hook")
g.Assert(err).IsNotNil() g.Assert(err).IsNotNil()
}) })
@ -124,7 +124,7 @@ func Test_GitLab(t *testing.T) {
// Test deactivate method // Test deactivate method
g.Describe("Deactivate", func() { g.Describe("Deactivate", func() {
g.It("Should be success", func() { g.It("Should be success", func() {
err := client.Deactivate(ctx, &user, &repo, "http://example.com/api/hook/test/test?access_token=token") err := client.Deactivate(ctx, &user, &repo, "http://example.com/api/hook?access_token=token")
g.Assert(err).IsNil() g.Assert(err).IsNil()
}) })

View file

@ -51,8 +51,8 @@ func Restart(ctx context.Context, store store.Store, lastPipeline *model.Pipelin
pipelineFiles = append(pipelineFiles, &forge_types.FileMeta{Data: y.Data, Name: y.Name}) pipelineFiles = append(pipelineFiles, &forge_types.FileMeta{Data: y.Data, Name: y.Name})
} }
// If config extension is active we should refetch the config in case something changed // If the config extension is active we should refetch the config in case something changed
if server.Config.Services.ConfigService != nil && server.Config.Services.ConfigService.IsConfigured() { if server.Config.Services.ConfigService != nil {
currentFileMeta := make([]*forge_types.FileMeta, len(configs)) currentFileMeta := make([]*forge_types.FileMeta, len(configs))
for i, cfg := range configs { for i, cfg := range configs {
currentFileMeta[i] = &forge_types.FileMeta{Name: cfg.Name, Data: cfg.Data} currentFileMeta[i] = &forge_types.FileMeta{Name: cfg.Name, Data: cfg.Data}

View file

@ -22,6 +22,5 @@ import (
) )
type Extension interface { type Extension interface {
IsConfigured() bool
FetchConfig(ctx context.Context, repo *model.Repo, pipeline *model.Pipeline, currentFileMeta []*forge_types.FileMeta, netrc *model.Netrc) (configData []*forge_types.FileMeta, useOld bool, err error) FetchConfig(ctx context.Context, repo *model.Repo, pipeline *model.Pipeline, currentFileMeta []*forge_types.FileMeta, netrc *model.Netrc) (configData []*forge_types.FileMeta, useOld bool, err error)
} }

View file

@ -50,10 +50,6 @@ func NewHTTP(endpoint string, privateKey crypto.PrivateKey) Extension {
return &http{endpoint, privateKey} return &http{endpoint, privateKey}
} }
func (cp *http) IsConfigured() bool {
return cp.endpoint != ""
}
func (cp *http) FetchConfig(ctx context.Context, repo *model.Repo, pipeline *model.Pipeline, currentFileMeta []*forge_types.FileMeta, netrc *model.Netrc) (configData []*forge_types.FileMeta, useOld bool, err error) { func (cp *http) FetchConfig(ctx context.Context, repo *model.Repo, pipeline *model.Pipeline, currentFileMeta []*forge_types.FileMeta, netrc *model.Netrc) (configData []*forge_types.FileMeta, useOld bool, err error) {
currentConfigs := make([]*config, len(currentFileMeta)) currentConfigs := make([]*config, len(currentFileMeta))
for i, pipe := range currentFileMeta { for i, pipe := range currentFileMeta {