Skip to content

Commit

Permalink
gitlab: improve checking for project lookup during fork
Browse files Browse the repository at this point in the history
The fork was being interrupted only when another fork, for a different
project, was found. However, there are other cases where a fork should fail:
(keep in mind the code is trying to find an identical _fork project_)

1) the fork matches the project being forked

	creates two remotes pointing to the exact same project.

2) the fork matches a non-forked project

	creates a remote pointing to a project, that just matches the
	namespace/name the user wants, but it isn't a fork.

	In general, it falls in case 1), however, lab allow the user to set a
	custom name, namespace and path for the fork, so we need this additional
	checking step.

3) (as said above) the fork matches another fork, but for a different project

	create a remote pointing to a project with same namespace/name but
	forked from another non-related project.

Signed-off-by: Bruno Meneguele <[email protected]>
  • Loading branch information
bmeneg committed Feb 26, 2021
1 parent 80edd01 commit bbca093
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,27 @@ func Fork(project string, opts *gitlab.ForkProjectOptions, useHTTP bool, wait bo
opts.Name = gitlab.String(name)
}
}

target, err := FindProject(namespace + name)
if err == nil {
// Check if it was forked from the same project being requested
// Check if it isn't the same project being requested
if target.PathWithNamespace == project {
errMsg := "not possible to fork a project from the same namespace and name"
return "", errors.New(errMsg)
}

// Check if it isn't a non-fork project, meaning the user has
// access to a project with same namespace/name
if target.ForkedFromProject == nil {
errMsg := fmt.Sprintf("\"%s\" project already taken\n", target.PathWithNamespace)
return "", errors.New(errMsg)
}

// Check if it isn't already a fork for another project
if target.ForkedFromProject != nil &&
target.ForkedFromProject.PathWithNamespace != project {
errMsg := fmt.Sprintf("\"%s\" fork name already taken for a different project", name)
errMsg := fmt.Sprintf("\"%s\" fork already taken for a different project",
target.PathWithNamespace)
return "", errors.New(errMsg)
}

Expand Down

0 comments on commit bbca093

Please sign in to comment.