2021-11-17 20:47:31 +05:30
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2022-06-06 13:31:49 +05:30
|
|
|
"code.gitea.io/gitea/models/webhook"
|
2021-12-12 21:18:20 +05:30
|
|
|
"code.gitea.io/gitea/modules/git"
|
2021-11-17 20:47:31 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
|
|
|
|
|
|
|
"xorm.io/builder"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SyncRepositoryHooks rewrites all repositories' pre-receive, update and post-receive hooks
|
|
|
|
// to make sure the binary and custom conf path are up-to-date.
|
|
|
|
func SyncRepositoryHooks(ctx context.Context) error {
|
|
|
|
log.Trace("Doing: SyncRepositoryHooks")
|
|
|
|
|
|
|
|
if err := db.Iterate(
|
2022-03-22 20:52:54 +05:30
|
|
|
ctx,
|
2021-12-10 06:57:50 +05:30
|
|
|
new(repo_model.Repository),
|
2021-11-17 20:47:31 +05:30
|
|
|
builder.Gt{"id": 0},
|
|
|
|
func(idx int, bean interface{}) error {
|
2021-12-10 06:57:50 +05:30
|
|
|
repo := bean.(*repo_model.Repository)
|
2021-11-17 20:47:31 +05:30
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return db.ErrCancelledf("before sync repository hooks for %s", repo.FullName())
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := repo_module.CreateDelegateHooks(repo.RepoPath()); err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("SyncRepositoryHook: %w", err)
|
2021-11-17 20:47:31 +05:30
|
|
|
}
|
|
|
|
if repo.HasWiki() {
|
|
|
|
if err := repo_module.CreateDelegateHooks(repo.WikiPath()); err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("SyncRepositoryHook: %w", err)
|
2021-11-17 20:47:31 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace("Finished: SyncRepositoryHooks")
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-12 21:18:20 +05:30
|
|
|
|
|
|
|
// GenerateGitHooks generates git hooks from a template repository
|
|
|
|
func GenerateGitHooks(ctx context.Context, templateRepo, generateRepo *repo_model.Repository) error {
|
2022-03-30 00:43:41 +05:30
|
|
|
generateGitRepo, err := git.OpenRepository(ctx, generateRepo.RepoPath())
|
2021-12-12 21:18:20 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer generateGitRepo.Close()
|
|
|
|
|
2022-03-30 00:43:41 +05:30
|
|
|
templateGitRepo, err := git.OpenRepository(ctx, templateRepo.RepoPath())
|
2021-12-12 21:18:20 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer templateGitRepo.Close()
|
|
|
|
|
|
|
|
templateHooks, err := templateGitRepo.Hooks()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, templateHook := range templateHooks {
|
|
|
|
generateHook, err := generateGitRepo.GetHook(templateHook.Name())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
generateHook.Content = templateHook.Content
|
|
|
|
if err := generateHook.Update(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-06-06 13:31:49 +05:30
|
|
|
|
|
|
|
// GenerateWebhooks generates webhooks from a template repository
|
|
|
|
func GenerateWebhooks(ctx context.Context, templateRepo, generateRepo *repo_model.Repository) error {
|
|
|
|
templateWebhooks, err := webhook.ListWebhooksByOpts(ctx, &webhook.ListWebhookOptions{RepoID: templateRepo.ID})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ws := make([]*webhook.Webhook, 0, len(templateWebhooks))
|
|
|
|
for _, templateWebhook := range templateWebhooks {
|
|
|
|
ws = append(ws, &webhook.Webhook{
|
|
|
|
RepoID: generateRepo.ID,
|
|
|
|
URL: templateWebhook.URL,
|
|
|
|
HTTPMethod: templateWebhook.HTTPMethod,
|
|
|
|
ContentType: templateWebhook.ContentType,
|
|
|
|
Secret: templateWebhook.Secret,
|
|
|
|
HookEvent: templateWebhook.HookEvent,
|
|
|
|
IsActive: templateWebhook.IsActive,
|
|
|
|
Type: templateWebhook.Type,
|
|
|
|
OrgID: templateWebhook.OrgID,
|
|
|
|
Events: templateWebhook.Events,
|
|
|
|
Meta: templateWebhook.Meta,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return webhook.CreateWebhooks(ctx, ws)
|
|
|
|
}
|