diff --git a/internal/pipe/project/project.go b/internal/pipe/project/project.go index 60b1ec30c45..9713ab3c329 100644 --- a/internal/pipe/project/project.go +++ b/internal/pipe/project/project.go @@ -6,6 +6,7 @@ import ( "os/exec" "strings" + "github.com/goreleaser/goreleaser/internal/git" "github.com/goreleaser/goreleaser/pkg/context" ) @@ -27,6 +28,7 @@ func (Pipe) Default(ctx *context.Context) error { ctx.Config.Release.GitLab.Name, ctx.Config.Release.Gitea.Name, moduleName(), + gitRemote(ctx), } { if candidate == "" { continue @@ -55,3 +57,14 @@ func moduleName() string { parts := strings.Split(mod, "/") return strings.TrimSpace(parts[len(parts)-1]) } + +func gitRemote(ctx *context.Context) string { + repo, err := git.ExtractRepoFromConfig(ctx) + if err != nil { + return "" + } + if err := repo.CheckSCM(); err != nil { + return "" + } + return repo.Name +} diff --git a/internal/pipe/project/project_test.go b/internal/pipe/project/project_test.go index e676854b89a..3435ada399e 100644 --- a/internal/pipe/project/project_test.go +++ b/internal/pipe/project/project_test.go @@ -76,6 +76,23 @@ func TestEmptyProjectName_DefaultsToGoModPath(t *testing.T) { require.Equal(t, "bar", ctx.Config.ProjectName) } +func TestEmptyProjectName_DefaultsToGitURL(t *testing.T) { + _ = testlib.Mktmp(t) + ctx := testctx.New() + testlib.GitInit(t) + testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git") + require.NoError(t, Pipe{}.Default(ctx)) + require.Equal(t, "bar", ctx.Config.ProjectName) +} + +func TestEmptyProjectName_DefaultsToNonSCMGitURL(t *testing.T) { + _ = testlib.Mktmp(t) + ctx := testctx.New() + testlib.GitInit(t) + testlib.GitRemoteAdd(t, "git@myhost.local:bar.git") + require.EqualError(t, Pipe{}.Default(ctx), "couldn't guess project_name, please add it to your config") +} + func TestEmptyProjectNameAndRelease(t *testing.T) { _ = testlib.Mktmp(t) ctx := testctx.NewWithCfg(config.Project{