mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-25 17:34:19 +01:00
5f82ead13c
## Purpose This is a refactor toward building an abstraction over managing git repositories. Afterwards, it does not matter anymore if they are stored on the local disk or somewhere remote. ## What this PR changes We used `git.OpenRepository` everywhere previously. Now, we should split them into two distinct functions: Firstly, there are temporary repositories which do not change: ```go git.OpenRepository(ctx, diskPath) ``` Gitea managed repositories having a record in the database in the `repository` table are moved into the new package `gitrepo`: ```go gitrepo.OpenRepository(ctx, repo_model.Repo) ``` Why is `repo_model.Repository` the second parameter instead of file path? Because then we can easily adapt our repository storage strategy. The repositories can be stored locally, however, they could just as well be stored on a remote server. ## Further changes in other PRs - A Git Command wrapper on package `gitrepo` could be created. i.e. `NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir: repo.RepoPath()}`, the directory should be empty before invoking this method and it can be filled in the function only. #28940 - Remove the `RepoPath()`/`WikiPath()` functions to reduce the possibility of mistakes. --------- Co-authored-by: delvh <dev.lh@web.de>
110 lines
3 KiB
Go
110 lines
3 KiB
Go
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package repository
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
"code.gitea.io/gitea/models/webhook"
|
|
"code.gitea.io/gitea/modules/gitrepo"
|
|
"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(
|
|
ctx,
|
|
builder.Gt{"id": 0},
|
|
func(ctx context.Context, repo *repo_model.Repository) error {
|
|
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 {
|
|
return fmt.Errorf("SyncRepositoryHook: %w", err)
|
|
}
|
|
if repo.HasWiki() {
|
|
if err := repo_module.CreateDelegateHooks(repo.WikiPath()); err != nil {
|
|
return fmt.Errorf("SyncRepositoryHook: %w", err)
|
|
}
|
|
}
|
|
return nil
|
|
},
|
|
); err != nil {
|
|
return err
|
|
}
|
|
|
|
log.Trace("Finished: SyncRepositoryHooks")
|
|
return nil
|
|
}
|
|
|
|
// GenerateGitHooks generates git hooks from a template repository
|
|
func GenerateGitHooks(ctx context.Context, templateRepo, generateRepo *repo_model.Repository) error {
|
|
generateGitRepo, err := gitrepo.OpenRepository(ctx, generateRepo)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer generateGitRepo.Close()
|
|
|
|
templateGitRepo, err := gitrepo.OpenRepository(ctx, templateRepo)
|
|
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
|
|
}
|
|
|
|
// GenerateWebhooks generates webhooks from a template repository
|
|
func GenerateWebhooks(ctx context.Context, templateRepo, generateRepo *repo_model.Repository) error {
|
|
templateWebhooks, err := db.Find[webhook.Webhook](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,
|
|
OwnerID: templateWebhook.OwnerID,
|
|
Events: templateWebhook.Events,
|
|
Meta: templateWebhook.Meta,
|
|
})
|
|
}
|
|
return webhook.CreateWebhooks(ctx, ws)
|
|
}
|