2021-11-13 19:18:06 +00:00
|
|
|
// Copyright 2021 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package datastore
|
|
|
|
|
|
|
|
import (
|
2022-10-17 23:48:04 +00:00
|
|
|
"errors"
|
2023-12-19 05:03:56 +00:00
|
|
|
"fmt"
|
2023-03-19 12:52:58 +00:00
|
|
|
"strings"
|
2022-10-17 23:48:04 +00:00
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
"xorm.io/builder"
|
|
|
|
"xorm.io/xorm"
|
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store/types"
|
2021-11-13 19:18:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s storage) GetRepo(id int64) (*model.Repo, error) {
|
|
|
|
repo := new(model.Repo)
|
|
|
|
return repo, wrapGet(s.engine.ID(id).Get(repo))
|
|
|
|
}
|
|
|
|
|
2022-11-15 14:01:23 +00:00
|
|
|
func (s storage) GetRepoForgeID(remoteID model.ForgeRemoteID) (*model.Repo, error) {
|
2022-09-05 15:08:51 +00:00
|
|
|
sess := s.engine.NewSession()
|
|
|
|
defer sess.Close()
|
2022-11-15 14:01:23 +00:00
|
|
|
return s.getRepoForgeID(sess, remoteID)
|
2022-09-05 15:08:51 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 14:01:23 +00:00
|
|
|
func (s storage) getRepoForgeID(e *xorm.Session, remoteID model.ForgeRemoteID) (*model.Repo, error) {
|
2022-09-05 15:08:51 +00:00
|
|
|
repo := new(model.Repo)
|
2022-11-15 14:01:23 +00:00
|
|
|
return repo, wrapGet(e.Where("forge_remote_id = ?", remoteID).Get(repo))
|
2022-09-05 15:08:51 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 14:01:23 +00:00
|
|
|
func (s storage) GetRepoNameFallback(remoteID model.ForgeRemoteID, fullName string) (*model.Repo, error) {
|
2022-09-05 15:08:51 +00:00
|
|
|
sess := s.engine.NewSession()
|
|
|
|
defer sess.Close()
|
2022-11-15 14:01:23 +00:00
|
|
|
return s.getRepoNameFallback(sess, remoteID, fullName)
|
2022-09-05 15:08:51 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 14:01:23 +00:00
|
|
|
func (s storage) getRepoNameFallback(e *xorm.Session, remoteID model.ForgeRemoteID, fullName string) (*model.Repo, error) {
|
|
|
|
repo, err := s.getRepoForgeID(e, remoteID)
|
2022-10-17 23:48:04 +00:00
|
|
|
if errors.Is(err, types.RecordNotExist) {
|
2022-09-05 15:08:51 +00:00
|
|
|
return s.getRepoName(e, fullName)
|
|
|
|
}
|
|
|
|
return repo, err
|
|
|
|
}
|
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
func (s storage) GetRepoName(fullName string) (*model.Repo, error) {
|
|
|
|
sess := s.engine.NewSession()
|
|
|
|
defer sess.Close()
|
2022-09-05 15:08:51 +00:00
|
|
|
repo, err := s.getRepoName(sess, fullName)
|
2022-10-17 23:48:04 +00:00
|
|
|
if errors.Is(err, types.RecordNotExist) {
|
2022-09-05 15:08:51 +00:00
|
|
|
// the repository does not exist, so look for a redirection
|
|
|
|
redirect, err := s.getRedirection(sess, fullName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return s.GetRepo(redirect.RepoID)
|
|
|
|
}
|
|
|
|
return repo, err
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s storage) getRepoName(e *xorm.Session, fullName string) (*model.Repo, error) {
|
|
|
|
repo := new(model.Repo)
|
2023-03-19 12:52:58 +00:00
|
|
|
return repo, wrapGet(e.Where("LOWER(repo_full_name) = ?", strings.ToLower(fullName)).Get(repo))
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s storage) GetRepoCount() (int64, error) {
|
|
|
|
return s.engine.Where(builder.Eq{"repo_active": true}).Count(new(model.Repo))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s storage) CreateRepo(repo *model.Repo) error {
|
2023-12-19 05:03:56 +00:00
|
|
|
switch {
|
|
|
|
case repo.Name == "":
|
|
|
|
return fmt.Errorf("repo name is empty")
|
|
|
|
case repo.Owner == "":
|
|
|
|
return fmt.Errorf("repo owner is empty")
|
|
|
|
case repo.FullName == "":
|
|
|
|
return fmt.Errorf("repo full name is empty")
|
|
|
|
}
|
2021-11-13 19:18:06 +00:00
|
|
|
// only Insert set auto created ID back to object
|
|
|
|
_, err := s.engine.Insert(repo)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s storage) UpdateRepo(repo *model.Repo) error {
|
|
|
|
_, err := s.engine.ID(repo.ID).AllCols().Update(repo)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s storage) DeleteRepo(repo *model.Repo) error {
|
2023-09-02 13:00:13 +00:00
|
|
|
return s.deleteRepo(s.engine.NewSession(), repo)
|
|
|
|
}
|
2021-12-11 15:03:14 +00:00
|
|
|
|
2023-09-02 13:00:13 +00:00
|
|
|
func (s storage) deleteRepo(sess *xorm.Session, repo *model.Repo) error {
|
|
|
|
const batchSize = perPage
|
2021-12-11 15:03:14 +00:00
|
|
|
if _, err := sess.Where("config_repo_id = ?", repo.ID).Delete(new(model.Config)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := sess.Where("perm_repo_id = ?", repo.ID).Delete(new(model.Perm)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := sess.Where("registry_repo_id = ?", repo.ID).Delete(new(model.Registry)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := sess.Where("secret_repo_id = ?", repo.ID).Delete(new(model.Secret)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-05 15:08:51 +00:00
|
|
|
if _, err := sess.Where("repo_id = ?", repo.ID).Delete(new(model.Redirection)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-11 15:03:14 +00:00
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
// delete related pipelines
|
|
|
|
for startPipelines := 0; ; startPipelines += batchSize {
|
|
|
|
pipelineIDs := make([]int64, 0, batchSize)
|
2022-10-22 13:54:43 +00:00
|
|
|
if err := sess.Limit(batchSize, startPipelines).Table("pipelines").Cols("pipeline_id").Where("pipeline_repo_id = ?", repo.ID).Find(&pipelineIDs); err != nil {
|
2021-12-11 15:03:14 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
if len(pipelineIDs) == 0 {
|
2021-12-11 15:03:14 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
for i := range pipelineIDs {
|
2023-12-12 20:30:52 +00:00
|
|
|
if err := s.deletePipeline(sess, pipelineIDs[i]); err != nil {
|
2021-12-11 15:03:14 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-02 13:00:13 +00:00
|
|
|
return wrapDelete(sess.ID(repo.ID).Delete(new(model.Repo)))
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 00:44:08 +00:00
|
|
|
// RepoList list all repos where permissions for specific user are stored
|
2021-11-13 19:18:06 +00:00
|
|
|
// TODO: paginate
|
2023-04-30 01:40:13 +00:00
|
|
|
func (s storage) RepoList(user *model.User, owned, active bool) ([]*model.Repo, error) {
|
|
|
|
repos := make([]*model.Repo, 0)
|
2021-11-13 19:18:06 +00:00
|
|
|
sess := s.engine.Table("repos").
|
|
|
|
Join("INNER", "perms", "perms.perm_repo_id = repos.repo_id").
|
|
|
|
Where("perms.perm_user_id = ?", user.ID)
|
|
|
|
if owned {
|
|
|
|
sess = sess.And(builder.Eq{"perms.perm_push": true}.Or(builder.Eq{"perms.perm_admin": true}))
|
|
|
|
}
|
2023-04-30 01:40:13 +00:00
|
|
|
if active {
|
|
|
|
sess = sess.And(builder.Eq{"repos.repo_active": true})
|
|
|
|
}
|
2021-11-13 19:18:06 +00:00
|
|
|
return repos, sess.
|
|
|
|
Asc("repo_full_name").
|
|
|
|
Find(&repos)
|
|
|
|
}
|
2023-09-08 10:26:20 +00:00
|
|
|
|
|
|
|
// RepoListAll list all repos
|
|
|
|
func (s storage) RepoListAll(active bool, p *model.ListOptions) ([]*model.Repo, error) {
|
|
|
|
repos := make([]*model.Repo, 0)
|
|
|
|
sess := s.paginate(p).Table("repos")
|
|
|
|
if active {
|
|
|
|
sess = sess.And(builder.Eq{"repos.repo_active": true})
|
|
|
|
}
|
|
|
|
return repos, sess.
|
|
|
|
Asc("repo_full_name").
|
|
|
|
Find(&repos)
|
|
|
|
}
|