mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-26 06:16:56 +09:00
This PR introduces project workflows, allowing users to configure event-based automations directly from the UI. Users can define filters and actions for each workflow event, and duplicate existing events when needed. ### Concepts Every workflow is composed of `event`, `filters`, and `actions`. - **Event** - An Event defines when a workflow should be triggered. It represents specific activities such as an issue being added to the project, a pull request being merged, or a project column being changed. - **Filter** - A Filter refines when an event should take effect. It allows users to specify conditions like issue type, labels, or project columns to ensure actions only run in targeted scenarios. - **Action** - An Action defines what happens when the event and filters match. It performs operations such as adding labels, moving issues between columns, or changing the issue or pull request state. ### Available Events Currently, the following eight events are supported: - On issues/pull requests closed - On issues/pull requests reopened - On issues/pull requests was added to the project, including when created in it - On issues/pull requests was removed from the project - On issues/pull requests' column was changed - On code changes requested - On code review approved - On pull request merged Each event type supports its own set of filters and actions. ### Available Filters There are four types of filters that can be configured for an event: - **Apply to** - Specify the target type: `issues only`, `pull requests only`, or `issues and pull requests`. - **Labels** - Trigger the event only if the issue or pull request contains one or more specified labels. - **Move from column** - Filter events based on the original project column. - **Move to column** - Filter events based on the destination project column. ### Available Actions There are four types of actions that can be executed when an event is triggered: - **Add labels** - Automatically add one or more labels to the issue or pull request. - **Remove labels** - Remove one or more specified labels. - **Move to column** - Move the issue or pull request to a designated project column. - **Issue state** - Close or reopen the issue. Pull requests can only be closed. ### Enable/Disable an event workflow Each event workflow can be individually enabled or disabled. When disabled, the workflow will be ignored even if the event occurs. This feature is useful when you want to temporarily stop certain automated actions without deleting the workflow configuration. Workflows of closed projects don't run either. ### Clone an event workflow When you need multiple workflows for the same event, you can clone an existing event to create a new one with different filters and actions. This allows you to customize how the same event behaves under different conditions, making workflows more flexible and powerful. ### Permissions Permissions are checked when workflows are managed, not when they run: - Viewing a project's workflows needs read access to the project - Managing them needs write access to the project plus write access to both issues and pull requests of a repository project's repository, being an owner of an organization project's organization or a site administrator, or being the user of a user project. Tokens limited to public repositories can't manage owner-level projects' workflows - A workflow runs when its event happens to an item of an open project that can hold the item's repository, unless that repository is archived, no matter who caused the event, and applies its actions without further permission checks. Pull requests are never reopened - Changes show as made by "Project workflow" on behalf of the user who triggered the event and don't trigger other workflows - Only users who can read an item can put it on a board or move its card The management rule is also documented on `CanManageWorkflows` in `services/projects/workflow.go`. Fixes: #14359 Fixes: #26704 Fixes: #27990 Replaces: #28745 Documentation PR: https://gitea.com/gitea/docs/pulls/308 Co-authored-by: bircni <bircni@icloud.com> Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: Giteabot <teabot@gitea.io>
611 lines
19 KiB
Go
611 lines
19 KiB
Go
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package repository
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
actions_model "gitea.dev/models/actions"
|
|
audit_model "gitea.dev/models/audit"
|
|
"gitea.dev/models/db"
|
|
issues_model "gitea.dev/models/issues"
|
|
"gitea.dev/models/organization"
|
|
"gitea.dev/models/perm"
|
|
access_model "gitea.dev/models/perm/access"
|
|
project_model "gitea.dev/models/project"
|
|
repo_model "gitea.dev/models/repo"
|
|
user_model "gitea.dev/models/user"
|
|
"gitea.dev/modules/git"
|
|
"gitea.dev/modules/git/gitrepo"
|
|
"gitea.dev/modules/globallock"
|
|
"gitea.dev/modules/log"
|
|
"gitea.dev/modules/util"
|
|
"gitea.dev/services/audit"
|
|
notify_service "gitea.dev/services/notify"
|
|
)
|
|
|
|
type LimitReachedError struct{ Limit int }
|
|
|
|
func (LimitReachedError) Error() string {
|
|
return "Repository limit has been reached"
|
|
}
|
|
|
|
func IsRepositoryLimitReached(err error) bool {
|
|
_, ok := err.(LimitReachedError)
|
|
return ok
|
|
}
|
|
|
|
func getRepoWorkingLockKey(repoID int64) string {
|
|
return fmt.Sprintf("repo_working_%d", repoID)
|
|
}
|
|
|
|
// AcceptTransferOwnership transfers all corresponding setting from old user to new one.
|
|
func AcceptTransferOwnership(ctx context.Context, repo *repo_model.Repository, doer *user_model.User) error {
|
|
releaser, err := globallock.Lock(ctx, getRepoWorkingLockKey(repo.ID))
|
|
if err != nil {
|
|
log.Error("lock.Lock(): %v", err)
|
|
return fmt.Errorf("lock.Lock: %w", err)
|
|
}
|
|
defer releaser()
|
|
|
|
repoTransfer, err := repo_model.GetPendingRepositoryTransfer(ctx, repo)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
oldOwnerName := repo.OwnerName
|
|
|
|
if err := db.WithTx(ctx, func(ctx context.Context) error {
|
|
if err := repoTransfer.LoadAttributes(ctx); err != nil {
|
|
return err
|
|
}
|
|
|
|
if !doer.CanCreateRepoIn(repoTransfer.Recipient) {
|
|
return LimitReachedError{Limit: repoTransfer.Recipient.MaxCreationLimit()}
|
|
}
|
|
|
|
if !repoTransfer.CanUserAcceptOrRejectTransfer(ctx, doer) {
|
|
return util.ErrPermissionDenied
|
|
}
|
|
|
|
if err := repo.LoadOwner(ctx); err != nil {
|
|
return err
|
|
}
|
|
for _, team := range repoTransfer.Teams {
|
|
if repoTransfer.Recipient.ID != team.OrgID {
|
|
return fmt.Errorf("team %d does not belong to organization", team.ID)
|
|
}
|
|
}
|
|
|
|
return transferOwnership(ctx, repoTransfer.Doer, repoTransfer.Recipient.Name, repo, repoTransfer.Teams)
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
releaser()
|
|
|
|
notify_service.TransferRepository(ctx, doer, repo, oldOwnerName)
|
|
|
|
return nil
|
|
}
|
|
|
|
// isRepositoryModelOrDirExist returns true if the repository with given name under user has already existed.
|
|
func isRepositoryModelOrDirExist(ctx context.Context, u *user_model.User, repoName string) (bool, error) {
|
|
has, err := repo_model.IsRepositoryModelExist(ctx, u, repoName)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
repo := gitrepo.CodeRepoByName(u.Name, repoName)
|
|
isExist, err := git.IsRepositoryExist(ctx, repo)
|
|
return has || isExist, err
|
|
}
|
|
|
|
// transferOwnership transfers all corresponding repository items from old user to new one.
|
|
func transferOwnership(ctx context.Context, doer *user_model.User, newOwnerName string, repo *repo_model.Repository, teams []*organization.Team) (err error) {
|
|
repoRenamed := false
|
|
wikiRenamed := false
|
|
oldOwnerName := doer.Name
|
|
|
|
defer func() {
|
|
if !repoRenamed && !wikiRenamed {
|
|
return
|
|
}
|
|
|
|
recoverErr := recover()
|
|
if err == nil && recoverErr == nil {
|
|
return
|
|
}
|
|
|
|
if repoRenamed {
|
|
// revert the rename
|
|
from := gitrepo.CodeRepoByName(newOwnerName, repo.Name)
|
|
to := gitrepo.CodeRepoByName(oldOwnerName, repo.Name)
|
|
if err := git.RenameRepository(ctx, from, to); err != nil {
|
|
log.Error("Unable to revert repository %s/%s to %s/%s: %v", newOwnerName, repo.Name, oldOwnerName, repo.Name, err)
|
|
}
|
|
}
|
|
|
|
if wikiRenamed {
|
|
from := gitrepo.WikiRepoByName(newOwnerName, repo.Name)
|
|
to := gitrepo.WikiRepoByName(oldOwnerName, repo.Name)
|
|
if err := git.RenameRepository(ctx, from, to); err != nil {
|
|
log.Error("Unable to revert wiki repository %s/%s to %s/%s: %v", newOwnerName, repo.Name, oldOwnerName, repo.Name, err)
|
|
}
|
|
}
|
|
|
|
if recoverErr != nil {
|
|
log.Error("Panic within TransferOwnership: %v\n%s", recoverErr, log.Stack(2))
|
|
panic(recoverErr)
|
|
}
|
|
}()
|
|
|
|
ctx, committer, err := db.TxContext(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer committer.Close()
|
|
|
|
sess := db.GetEngine(ctx)
|
|
|
|
newOwner, err := user_model.GetUserByName(ctx, newOwnerName)
|
|
if err != nil {
|
|
return fmt.Errorf("get new owner '%s': %w", newOwnerName, err)
|
|
}
|
|
newOwnerName = newOwner.Name // ensure capitalisation matches
|
|
|
|
// Check if new owner has repository with same name.
|
|
if has, err := isRepositoryModelOrDirExist(ctx, newOwner, repo.Name); err != nil {
|
|
return fmt.Errorf("IsRepositoryExist: %w", err)
|
|
} else if has {
|
|
return repo_model.ErrRepoAlreadyExist{
|
|
Uname: newOwnerName,
|
|
Name: repo.Name,
|
|
}
|
|
}
|
|
|
|
oldOwner := repo.Owner
|
|
oldOwnerName = oldOwner.Name
|
|
|
|
// Note: we have to set value here to make sure recalculate accesses is based on
|
|
// new owner.
|
|
repo.OwnerID = newOwner.ID
|
|
repo.Owner = newOwner
|
|
repo.OwnerName = newOwner.Name
|
|
|
|
// Update repository.
|
|
if err := repo_model.UpdateRepositoryColsNoAutoTime(ctx, repo, "owner_id", "owner_name"); err != nil {
|
|
return fmt.Errorf("update owner: %w", err)
|
|
}
|
|
|
|
// Remove redundant collaborators.
|
|
collaborators, _, err := repo_model.GetCollaborators(ctx, &repo_model.FindCollaborationOptions{RepoID: repo.ID})
|
|
if err != nil {
|
|
return fmt.Errorf("GetCollaborators: %w", err)
|
|
}
|
|
|
|
// Dummy object.
|
|
collaboration := &repo_model.Collaboration{RepoID: repo.ID}
|
|
for _, c := range collaborators {
|
|
if c.IsGhost() {
|
|
collaboration.ID = c.Collaboration.ID
|
|
if _, err := sess.Delete(collaboration); err != nil {
|
|
return fmt.Errorf("remove collaborator '%d': %w", c.ID, err)
|
|
}
|
|
collaboration.ID = 0
|
|
}
|
|
|
|
if c.ID != newOwner.ID {
|
|
isMember, err := organization.IsOrganizationMember(ctx, newOwner.ID, c.ID)
|
|
if err != nil {
|
|
return fmt.Errorf("IsOrgMember: %w", err)
|
|
} else if !isMember {
|
|
continue
|
|
}
|
|
}
|
|
collaboration.UserID = c.ID
|
|
if _, err := sess.Delete(collaboration); err != nil {
|
|
return fmt.Errorf("remove collaborator '%d': %w", c.ID, err)
|
|
}
|
|
collaboration.UserID = 0
|
|
}
|
|
|
|
if oldOwner.IsOrganization() {
|
|
// Remove old team-repository relations.
|
|
if err := organization.RemoveOrgRepo(ctx, oldOwner.ID, repo.ID); err != nil {
|
|
return fmt.Errorf("removeOrgRepo: %w", err)
|
|
}
|
|
|
|
// Remove project's issues that belong to old organization's projects, this intentionally doesn't trigger project workflows
|
|
projects, err := project_model.GetAllProjectsIDsByOwnerIDAndType(ctx, oldOwner.ID, project_model.TypeOrganization)
|
|
if err != nil {
|
|
return fmt.Errorf("Unable to find old org projects: %w", err)
|
|
}
|
|
issues, err := issues_model.GetIssueIDsByRepoID(ctx, repo.ID)
|
|
if err != nil {
|
|
return fmt.Errorf("Unable to find repo's issues: %w", err)
|
|
}
|
|
err = project_model.DeleteAllProjectIssueByIssueIDsAndProjectIDs(ctx, issues, projects)
|
|
if err != nil {
|
|
return fmt.Errorf("Unable to delete project's issues: %w", err)
|
|
}
|
|
}
|
|
|
|
if newOwner.IsOrganization() {
|
|
teams, err := organization.FindOrgTeams(ctx, newOwner.ID)
|
|
if err != nil {
|
|
return fmt.Errorf("LoadTeams: %w", err)
|
|
}
|
|
for _, t := range teams {
|
|
if t.IncludesAllRepositories {
|
|
if err := addRepositoryToTeam(ctx, t, repo); err != nil {
|
|
return fmt.Errorf("AddRepository: %w", err)
|
|
}
|
|
}
|
|
}
|
|
} else if err := access_model.RecalculateAccesses(ctx, repo); err != nil {
|
|
// Organization called this in addRepository method.
|
|
return fmt.Errorf("recalculateAccesses: %w", err)
|
|
}
|
|
|
|
// Remove repository from old owner's Actions AllowedCrossRepoIDs if present
|
|
if oldActionsCfg, err := actions_model.GetOwnerActionsConfig(ctx, oldOwner.ID); err == nil {
|
|
newAllowedCrossRepoIDs := util.SliceRemoveAll(oldActionsCfg.AllowedCrossRepoIDs, repo.ID)
|
|
if len(newAllowedCrossRepoIDs) != len(oldActionsCfg.AllowedCrossRepoIDs) {
|
|
oldActionsCfg.AllowedCrossRepoIDs = newAllowedCrossRepoIDs
|
|
if err := actions_model.SetOwnerActionsConfig(ctx, oldOwner.ID, oldActionsCfg); err != nil {
|
|
return fmt.Errorf("SetOwnerActionsConfig: %w", err)
|
|
}
|
|
}
|
|
} else {
|
|
return fmt.Errorf("GetOwnerActionsConfig: %w", err)
|
|
}
|
|
|
|
// Update repository count.
|
|
if _, err := sess.Exec("UPDATE `user` SET num_repos=num_repos+1 WHERE id=?", newOwner.ID); err != nil {
|
|
return fmt.Errorf("increase new owner repository count: %w", err)
|
|
} else if _, err := sess.Exec("UPDATE `user` SET num_repos=num_repos-1 WHERE id=?", oldOwner.ID); err != nil {
|
|
return fmt.Errorf("decrease old owner repository count: %w", err)
|
|
}
|
|
|
|
if err := repo_model.WatchRepoAuto(ctx, doer, repo, true); err != nil {
|
|
return fmt.Errorf("watchRepo: %w", err)
|
|
}
|
|
|
|
if oldOwner.IsOrganization() {
|
|
// Remove watch for organization.
|
|
if err := repo_model.WatchRepoAuto(ctx, oldOwner, repo, false); err != nil {
|
|
return fmt.Errorf("watchRepo [false]: %w", err)
|
|
}
|
|
|
|
// Delete labels that belong to the old organization and comments that added these labels
|
|
if _, err := sess.Exec(`DELETE FROM issue_label WHERE issue_label.id IN (
|
|
SELECT il_too.id FROM (
|
|
SELECT il_too_too.id
|
|
FROM issue_label AS il_too_too
|
|
INNER JOIN label ON il_too_too.label_id = label.id
|
|
INNER JOIN issue on issue.id = il_too_too.issue_id
|
|
WHERE
|
|
issue.repo_id = ? AND ((label.org_id = 0 AND issue.repo_id != label.repo_id) OR (label.repo_id = 0 AND label.org_id != ?))
|
|
) AS il_too )`, repo.ID, newOwner.ID); err != nil {
|
|
return fmt.Errorf("Unable to remove old org labels: %w", err)
|
|
}
|
|
|
|
if _, err := sess.Exec(`DELETE FROM comment WHERE comment.id IN (
|
|
SELECT il_too.id FROM (
|
|
SELECT com.id
|
|
FROM comment AS com
|
|
INNER JOIN label ON com.label_id = label.id
|
|
INNER JOIN issue ON issue.id = com.issue_id
|
|
WHERE
|
|
com.type = ? AND issue.repo_id = ? AND ((label.org_id = 0 AND issue.repo_id != label.repo_id) OR (label.repo_id = 0 AND label.org_id != ?))
|
|
) AS il_too)`, issues_model.CommentTypeLabel, repo.ID, newOwner.ID); err != nil {
|
|
return fmt.Errorf("Unable to remove old org label comments: %w", err)
|
|
}
|
|
}
|
|
|
|
// Rename remote repository to new path and delete local copy.
|
|
oldCodeRepo := gitrepo.CodeRepoByName(oldOwner.Name, repo.Name)
|
|
newCodeRepo := gitrepo.CodeRepoByName(newOwner.Name, repo.Name)
|
|
if err := git.RenameRepository(ctx, oldCodeRepo, newCodeRepo); err != nil {
|
|
return fmt.Errorf("rename repository directory: %w", err)
|
|
}
|
|
repoRenamed = true
|
|
|
|
// Rename remote wiki repository to new path and delete local copy.
|
|
oldWikiRepo := gitrepo.WikiRepoByName(oldOwner.Name, repo.Name)
|
|
if isExist, err := git.IsRepositoryExist(ctx, oldWikiRepo); err != nil {
|
|
log.Error("Unable to check if wiki of repo %s/%s exists. Error: %v", oldOwner.Name, repo.Name, err)
|
|
return err
|
|
} else if isExist {
|
|
newWikiRepo := gitrepo.WikiRepoByName(newOwner.Name, repo.Name)
|
|
if err := git.RenameRepository(ctx, oldWikiRepo, newWikiRepo); err != nil {
|
|
return fmt.Errorf("rename repository wiki: %w", err)
|
|
}
|
|
wikiRenamed = true
|
|
}
|
|
|
|
if err := repo_model.DeleteRepositoryTransfer(ctx, repo.ID); err != nil {
|
|
return fmt.Errorf("deleteRepositoryTransfer: %w", err)
|
|
}
|
|
repo.Status = repo_model.RepositoryReady
|
|
if err := repo_model.UpdateRepositoryColsNoAutoTime(ctx, repo, "status"); err != nil {
|
|
return err
|
|
}
|
|
|
|
// If there was previously a redirect at this location, remove it.
|
|
if err := repo_model.DeleteRedirect(ctx, newOwner.ID, repo.Name); err != nil {
|
|
return fmt.Errorf("delete repo redirect: %w", err)
|
|
}
|
|
|
|
if err := repo_model.NewRedirect(ctx, oldOwner.ID, repo.ID, repo.Name, repo.Name); err != nil {
|
|
return fmt.Errorf("repo_model.NewRedirect: %w", err)
|
|
}
|
|
|
|
newRepo, err := repo_model.GetRepositoryByID(ctx, repo.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, team := range teams {
|
|
if err := addRepositoryToTeam(ctx, team, newRepo); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if err := committer.Commit(); err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, team := range teams {
|
|
audit.Record(ctx, audit_model.RepositoryCollaboratorTeamAdd, newRepo, "team", team.Name)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// changeRepositoryName changes all corresponding setting from old repository name to new one.
|
|
func changeRepositoryName(ctx context.Context, repo *repo_model.Repository, newRepoName string) (err error) {
|
|
oldRepoName := repo.Name
|
|
newRepoName = strings.ToLower(newRepoName)
|
|
if err = repo_model.IsUsableRepoName(newRepoName); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := repo.LoadOwner(ctx); err != nil {
|
|
return err
|
|
}
|
|
|
|
has, err := isRepositoryModelOrDirExist(ctx, repo.Owner, newRepoName)
|
|
if err != nil {
|
|
return fmt.Errorf("IsRepositoryExist: %w", err)
|
|
} else if has {
|
|
return repo_model.ErrRepoAlreadyExist{
|
|
Uname: repo.OwnerName,
|
|
Name: newRepoName,
|
|
}
|
|
}
|
|
|
|
newCodeRepo := gitrepo.CodeRepoByName(repo.OwnerName, newRepoName)
|
|
if err = git.RenameRepository(ctx, repo, newCodeRepo); err != nil {
|
|
return fmt.Errorf("rename repository directory: %w", err)
|
|
}
|
|
|
|
if HasWiki(ctx, repo) {
|
|
newWikiRepo := gitrepo.WikiRepoByName(repo.OwnerName, newRepoName)
|
|
if err = git.RenameRepository(ctx, repo.WikiStorageRepo(), newWikiRepo); err != nil {
|
|
return fmt.Errorf("rename repository wiki: %w", err)
|
|
}
|
|
}
|
|
|
|
return db.WithTx(ctx, func(ctx context.Context) error {
|
|
return repo_model.NewRedirect(ctx, repo.Owner.ID, repo.ID, oldRepoName, newRepoName)
|
|
})
|
|
}
|
|
|
|
// ChangeRepositoryName changes all corresponding setting from old repository name to new one.
|
|
func ChangeRepositoryName(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, newRepoName string) error {
|
|
log.Trace("ChangeRepositoryName: %s/%s -> %s", doer.Name, repo.Name, newRepoName)
|
|
|
|
oldRepoName := repo.Name
|
|
|
|
// Change repository directory name. We must lock the local copy of the
|
|
// repo so that we can automatically rename the repo path and updates the
|
|
// local copy's origin accordingly.
|
|
|
|
releaser, err := globallock.Lock(ctx, getRepoWorkingLockKey(repo.ID))
|
|
if err != nil {
|
|
log.Error("lock.Lock(): %v", err)
|
|
return fmt.Errorf("lock.Lock: %w", err)
|
|
}
|
|
defer releaser()
|
|
|
|
if err := changeRepositoryName(ctx, repo, newRepoName); err != nil {
|
|
return err
|
|
}
|
|
releaser()
|
|
|
|
repo.Name = newRepoName
|
|
|
|
notify_service.RenameRepository(ctx, doer, repo, oldRepoName)
|
|
|
|
return nil
|
|
}
|
|
|
|
// StartRepositoryTransfer transfer a repo from one owner to a new one.
|
|
// it make repository into pending transfer state, if doer can not create repo for new owner.
|
|
func StartRepositoryTransfer(ctx context.Context, doer, newOwner *user_model.User, repo *repo_model.Repository, teams []*organization.Team) error {
|
|
releaser, err := globallock.Lock(ctx, getRepoWorkingLockKey(repo.ID))
|
|
if err != nil {
|
|
return fmt.Errorf("lock.Lock: %w", err)
|
|
}
|
|
defer releaser()
|
|
|
|
if err := repo_model.TestRepositoryReadyForTransfer(repo.Status); err != nil {
|
|
return err
|
|
}
|
|
|
|
if !doer.CanForkRepoIn(newOwner) {
|
|
return LimitReachedError{Limit: newOwner.MaxCreationLimit()}
|
|
}
|
|
|
|
var isDirectTransfer bool
|
|
oldOwnerName := repo.OwnerName
|
|
|
|
if err := db.WithTx(ctx, func(ctx context.Context) error {
|
|
// Admin is always allowed to transfer || user transfer repo back to his account,
|
|
// then it will transfer directly without acceptance.
|
|
if doer.IsAdmin || doer.ID == newOwner.ID {
|
|
isDirectTransfer = true
|
|
return transferOwnership(ctx, doer, newOwner.Name, repo, teams)
|
|
}
|
|
|
|
if user_model.IsUserBlockedBy(ctx, doer, newOwner.ID) || user_model.IsUserBlockedBy(ctx, newOwner, repo.OwnerID) {
|
|
return user_model.ErrBlockedUser
|
|
}
|
|
|
|
// If new owner is an org and user can create repos he can transfer directly too
|
|
if newOwner.IsOrganization() {
|
|
allowed, err := organization.CanCreateOrgRepo(ctx, newOwner.ID, doer.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if allowed {
|
|
isDirectTransfer = true
|
|
return transferOwnership(ctx, doer, newOwner.Name, repo, teams)
|
|
}
|
|
}
|
|
|
|
// In case the new owner would not have sufficient access to the repo, give access rights for read
|
|
hasAccess, err := access_model.HasAnyUnitAccess(ctx, newOwner.ID, repo)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
grantRecipientTempAccess := !hasAccess
|
|
if grantRecipientTempAccess {
|
|
if err := db.Insert(ctx, &repo_model.Collaboration{RepoID: repo.ID, UserID: newOwner.ID, Mode: perm.AccessModeRead}); err != nil {
|
|
return err
|
|
}
|
|
if err := access_model.RecalculateUserAccess(ctx, repo, newOwner.ID); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Make repo as pending for transfer
|
|
repo.Status = repo_model.RepositoryPendingTransfer
|
|
return repo_model.CreatePendingRepositoryTransfer(ctx, doer, newOwner, repo.ID, teams, grantRecipientTempAccess)
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
|
|
if isDirectTransfer {
|
|
notify_service.TransferRepository(ctx, doer, repo, oldOwnerName)
|
|
} else {
|
|
// notify users who are able to accept / reject transfer
|
|
notify_service.RepoPendingTransfer(ctx, doer, newOwner, repo)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// RejectRepositoryTransfer marks the repository as ready and remove pending transfer entry,
|
|
// thus cancel the transfer process.
|
|
// The accepter can reject the transfer.
|
|
func RejectRepositoryTransfer(ctx context.Context, repo *repo_model.Repository, doer *user_model.User) error {
|
|
if err := db.WithTx(ctx, func(ctx context.Context) error {
|
|
repoTransfer, err := repo_model.GetPendingRepositoryTransfer(ctx, repo)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := repoTransfer.LoadAttributes(ctx); err != nil {
|
|
return err
|
|
}
|
|
|
|
if !repoTransfer.CanUserAcceptOrRejectTransfer(ctx, doer) {
|
|
return util.ErrPermissionDenied
|
|
}
|
|
if err := removeTransferRecipientCollaboration(ctx, repoTransfer); err != nil {
|
|
return err
|
|
}
|
|
|
|
repo.Status = repo_model.RepositoryReady
|
|
if err := repo_model.UpdateRepositoryColsNoAutoTime(ctx, repo, "status"); err != nil {
|
|
return err
|
|
}
|
|
|
|
return repo_model.DeleteRepositoryTransfer(ctx, repo.ID)
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
|
|
audit.Record(ctx, audit_model.RepositoryTransferCancel, repo)
|
|
|
|
return nil
|
|
}
|
|
|
|
func removeTransferRecipientCollaboration(ctx context.Context, repoTransfer *repo_model.RepoTransfer) error {
|
|
if !repoTransfer.RecipientAccessGranted {
|
|
return nil
|
|
}
|
|
return deleteCollaborationByMode(ctx, repoTransfer.Repo, repoTransfer.Recipient, perm.AccessModeRead)
|
|
}
|
|
|
|
func canUserCancelTransfer(ctx context.Context, r *repo_model.RepoTransfer, u *user_model.User) bool {
|
|
if u.IsAdmin || u.ID == r.DoerID {
|
|
return true
|
|
}
|
|
|
|
if err := r.LoadAttributes(ctx); err != nil {
|
|
log.Error("LoadAttributes: %v", err)
|
|
return false
|
|
}
|
|
|
|
if err := r.Repo.LoadOwner(ctx); err != nil {
|
|
log.Error("LoadOwner: %v", err)
|
|
return false
|
|
}
|
|
|
|
if !r.Repo.Owner.IsOrganization() {
|
|
return r.Repo.OwnerID == u.ID
|
|
}
|
|
|
|
perm, err := access_model.GetIndividualUserRepoPermission(ctx, r.Repo, u)
|
|
if err != nil {
|
|
log.Error("GetIndividualUserRepoPermission: %v", err)
|
|
return false
|
|
}
|
|
return perm.IsOwner()
|
|
}
|
|
|
|
// CancelRepositoryTransfer cancels the repository transfer process. The sender or
|
|
// the users who have admin permission of the original repository can cancel the transfer
|
|
func CancelRepositoryTransfer(ctx context.Context, repoTransfer *repo_model.RepoTransfer, doer *user_model.User) error {
|
|
if err := db.WithTx(ctx, func(ctx context.Context) error {
|
|
if err := repoTransfer.LoadAttributes(ctx); err != nil {
|
|
return err
|
|
}
|
|
|
|
if !canUserCancelTransfer(ctx, repoTransfer, doer) {
|
|
return util.ErrPermissionDenied
|
|
}
|
|
if err := removeTransferRecipientCollaboration(ctx, repoTransfer); err != nil {
|
|
return err
|
|
}
|
|
|
|
repoTransfer.Repo.Status = repo_model.RepositoryReady
|
|
if err := repo_model.UpdateRepositoryColsNoAutoTime(ctx, repoTransfer.Repo, "status"); err != nil {
|
|
return err
|
|
}
|
|
|
|
return repo_model.DeleteRepositoryTransfer(ctx, repoTransfer.RepoID)
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
|
|
audit.Record(ctx, audit_model.RepositoryTransferCancel, repoTransfer.Repo)
|
|
|
|
return nil
|
|
}
|