From aafaffb7d9c60bdee84477483f3bf278da0932b8 Mon Sep 17 00:00:00 2001 From: Bruno Meneguele Date: Thu, 25 Feb 2021 17:54:38 -0300 Subject: [PATCH] gitlab: fix project lookup during fork During a fork process, lab doesn't check if a fork with same name under user's namespace is a fork from the same project the user is trying to fork now. If the same name matches, it only return a reference for that project, regardless where it came from. This patch adds a check step to make sure the found project is indeed a fork for the same project being forked. Otherwise, it returns an error informing the user. Signed-off-by: Bruno Meneguele --- internal/gitlab/gitlab.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/internal/gitlab/gitlab.go b/internal/gitlab/gitlab.go index 98bbaecf..7eab72b1 100644 --- a/internal/gitlab/gitlab.go +++ b/internal/gitlab/gitlab.go @@ -246,6 +246,14 @@ func Fork(project string, opts *gitlab.ForkProjectOptions, useHTTP bool, wait bo } target, err := FindProject(namespace + name) if err == nil { + // Check if it was forked from the same project being requested + if target.ForkedFromProject != nil && + target.ForkedFromProject.PathWithNamespace != project { + errMsg := fmt.Sprintf("\"%s\" fork name already taken for a different project", name) + return "", errors.New(errMsg) + } + + // Project already forked and found urlToRepo := target.SSHURLToRepo if useHTTP { urlToRepo = target.HTTPURLToRepo