mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-11-23 10:21:00 +00:00
52d3652f2e
Use IDs of the forge to fetch repositories instead of their names and owner names. This improves handling of renamed and transferred repos. TODO - [ ] try to support as many forges as possible - [x] Gogs (no API) - [ ] Bitbucket Server - [x] Coding (no API?) - [x] update repo every time it is fetched or received from the forge - [x] if repo remote IDs are not available, use owner / name to get it - [x] handle redirections (redirect a renamed repo to its new path) - [x] ~~pull all repos once during migration to update ID (?)~~ issue fixed by on-demand loading of remote IDs - [x] handle redirections in web UI - [ ] improve handling of hooks after a repo was renamed (currently it checks for a redirection to the repo) - [x] tests - [x] `UNIQUE` constraint for remote IDs after migration shouldn't work (all repos have an empty string as remote ID) close #854 close #648 partial close https://codeberg.org/Codeberg-CI/feedback/issues/46 Possible follow-up PRs - apply the same scheme on everything fetched from the remote (currently only users) Co-authored-by: 6543 <6543@obermui.de>
89 lines
2.3 KiB
Go
89 lines
2.3 KiB
Go
// 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 (
|
|
"fmt"
|
|
|
|
"xorm.io/xorm"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
|
)
|
|
|
|
func (s storage) PermFind(user *model.User, repo *model.Repo) (*model.Perm, error) {
|
|
perm := &model.Perm{
|
|
UserID: user.ID,
|
|
RepoID: repo.ID,
|
|
}
|
|
return perm, wrapGet(s.engine.Get(perm))
|
|
}
|
|
|
|
func (s storage) PermUpsert(perm *model.Perm) error {
|
|
sess := s.engine.NewSession()
|
|
defer sess.Close()
|
|
if err := sess.Begin(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := s.permUpsert(sess, perm); err != nil {
|
|
return err
|
|
}
|
|
|
|
return sess.Commit()
|
|
}
|
|
|
|
func (s storage) permUpsert(sess *xorm.Session, perm *model.Perm) error {
|
|
if perm.RepoID == 0 && perm.Repo == nil {
|
|
return fmt.Errorf("could not determine repo for permission: %v", perm)
|
|
}
|
|
|
|
// lookup repo based on name or remote ID if possible
|
|
if perm.RepoID == 0 && perm.Repo != nil {
|
|
r, err := s.getRepoNameFallback(sess, perm.Repo.RemoteID, perm.Repo.FullName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
perm.RepoID = r.ID
|
|
}
|
|
|
|
exist, err := sess.Where("perm_user_id = ? AND perm_repo_id = ?", perm.UserID, perm.RepoID).
|
|
Exist(new(model.Perm))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if exist {
|
|
_, err = sess.Where("perm_user_id = ? AND perm_repo_id = ?", perm.UserID, perm.RepoID).
|
|
AllCols().Update(perm)
|
|
} else {
|
|
// only Insert set auto created ID back to object
|
|
_, err = sess.Insert(perm)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (s storage) PermDelete(perm *model.Perm) error {
|
|
_, err := s.engine.
|
|
Where("perm_user_id = ? AND perm_repo_id = ?", perm.UserID, perm.RepoID).
|
|
Delete(new(model.Perm))
|
|
return err
|
|
}
|
|
|
|
func (s storage) PermFlush(user *model.User, before int64) error {
|
|
_, err := s.engine.
|
|
Where("perm_user_id = ? AND perm_synced < ?", user.ID, before).
|
|
Delete(new(model.Perm))
|
|
return err
|
|
}
|