aboutsummaryrefslogtreecommitdiff
path: root/internal/repoutil
diff options
context:
space:
mode:
authorJoe Chen <jc@unknwon.io>2022-06-25 18:07:39 +0800
committerGitHub <noreply@github.com>2022-06-25 18:07:39 +0800
commit083c3ee659c6c5542687f3bafae68cbc24dbc90f (patch)
tree0103bf3b5c5ebfccd368a7cb6a425a521fd669d9 /internal/repoutil
parent9df4e3ae3c555a86f691f0d78a43834842e77d8b (diff)
db: refactor "action" table to use GORM (#7054)
Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
Diffstat (limited to 'internal/repoutil')
-rw-r--r--internal/repoutil/repoutil.go62
-rw-r--r--internal/repoutil/repoutil_test.go127
2 files changed, 189 insertions, 0 deletions
diff --git a/internal/repoutil/repoutil.go b/internal/repoutil/repoutil.go
new file mode 100644
index 00000000..658f896f
--- /dev/null
+++ b/internal/repoutil/repoutil.go
@@ -0,0 +1,62 @@
+// Copyright 2022 The Gogs Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package repoutil
+
+import (
+ "fmt"
+ "path/filepath"
+ "strings"
+
+ "gogs.io/gogs/internal/conf"
+)
+
+// CloneLink represents different types of clone URLs of repository.
+type CloneLink struct {
+ SSH string
+ HTTPS string
+}
+
+// NewCloneLink returns clone URLs using given owner and repository name.
+func NewCloneLink(owner, repo string, isWiki bool) *CloneLink {
+ if isWiki {
+ repo += ".wiki"
+ }
+
+ cl := new(CloneLink)
+ if conf.SSH.Port != 22 {
+ cl.SSH = fmt.Sprintf("ssh://%s@%s:%d/%s/%s.git", conf.App.RunUser, conf.SSH.Domain, conf.SSH.Port, owner, repo)
+ } else {
+ cl.SSH = fmt.Sprintf("%s@%s:%s/%s.git", conf.App.RunUser, conf.SSH.Domain, owner, repo)
+ }
+ cl.HTTPS = HTTPSCloneURL(owner, repo)
+ return cl
+}
+
+// HTTPSCloneURL returns HTTPS clone URL using given owner and repository name.
+func HTTPSCloneURL(owner, repo string) string {
+ return fmt.Sprintf("%s%s/%s.git", conf.Server.ExternalURL, owner, repo)
+}
+
+// HTMLURL returns HTML URL using given owner and repository name.
+func HTMLURL(owner, repo string) string {
+ return conf.Server.ExternalURL + owner + "/" + repo
+}
+
+// CompareCommitsPath returns the comparison path using given owner, repository,
+// and commit IDs.
+func CompareCommitsPath(owner, repo, oldCommitID, newCommitID string) string {
+ return fmt.Sprintf("%s/%s/compare/%s...%s", owner, repo, oldCommitID, newCommitID)
+}
+
+// UserPath returns the absolute path for storing user repositories.
+func UserPath(user string) string {
+ return filepath.Join(conf.Repository.Root, strings.ToLower(user))
+}
+
+// RepositoryPath returns the absolute path using given user and repository
+// name.
+func RepositoryPath(owner, repo string) string {
+ return filepath.Join(UserPath(owner), strings.ToLower(repo)+".git")
+}
diff --git a/internal/repoutil/repoutil_test.go b/internal/repoutil/repoutil_test.go
new file mode 100644
index 00000000..232ecc52
--- /dev/null
+++ b/internal/repoutil/repoutil_test.go
@@ -0,0 +1,127 @@
+// Copyright 2022 The Gogs Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package repoutil
+
+import (
+ "runtime"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+
+ "gogs.io/gogs/internal/conf"
+)
+
+func TestNewCloneLink(t *testing.T) {
+ conf.SetMockApp(t,
+ conf.AppOpts{
+ RunUser: "git",
+ },
+ )
+ conf.SetMockServer(t,
+ conf.ServerOpts{
+ ExternalURL: "https://example.com/",
+ },
+ )
+
+ t.Run("regular SSH port", func(t *testing.T) {
+ conf.SetMockSSH(t,
+ conf.SSHOpts{
+ Domain: "example.com",
+ Port: 22,
+ },
+ )
+
+ got := NewCloneLink("alice", "example", false)
+ want := &CloneLink{
+ SSH: "git@example.com:alice/example.git",
+ HTTPS: "https://example.com/alice/example.git",
+ }
+ assert.Equal(t, want, got)
+ })
+
+ t.Run("irregular SSH port", func(t *testing.T) {
+ conf.SetMockSSH(t,
+ conf.SSHOpts{
+ Domain: "example.com",
+ Port: 2222,
+ },
+ )
+
+ got := NewCloneLink("alice", "example", false)
+ want := &CloneLink{
+ SSH: "ssh://git@example.com:2222/alice/example.git",
+ HTTPS: "https://example.com/alice/example.git",
+ }
+ assert.Equal(t, want, got)
+ })
+
+ t.Run("wiki", func(t *testing.T) {
+ conf.SetMockSSH(t,
+ conf.SSHOpts{
+ Domain: "example.com",
+ Port: 22,
+ },
+ )
+
+ got := NewCloneLink("alice", "example", true)
+ want := &CloneLink{
+ SSH: "git@example.com:alice/example.wiki.git",
+ HTTPS: "https://example.com/alice/example.wiki.git",
+ }
+ assert.Equal(t, want, got)
+ })
+}
+
+func TestHTMLURL(t *testing.T) {
+ conf.SetMockServer(t,
+ conf.ServerOpts{
+ ExternalURL: "https://example.com/",
+ },
+ )
+
+ got := HTMLURL("alice", "example")
+ want := "https://example.com/alice/example"
+ assert.Equal(t, want, got)
+}
+
+func TestCompareCommitsPath(t *testing.T) {
+ got := CompareCommitsPath("alice", "example", "old", "new")
+ want := "alice/example/compare/old...new"
+ assert.Equal(t, want, got)
+}
+
+func TestUserPath(t *testing.T) {
+ if runtime.GOOS == "windows" {
+ t.Skip("Skipping testing on Windows")
+ return
+ }
+
+ conf.SetMockRepository(t,
+ conf.RepositoryOpts{
+ Root: "/home/git/gogs-repositories",
+ },
+ )
+
+ got := UserPath("alice")
+ want := "/home/git/gogs-repositories/alice"
+ assert.Equal(t, want, got)
+}
+
+func TestRepositoryPath(t *testing.T) {
+ if runtime.GOOS == "windows" {
+ t.Skip("Skipping testing on Windows")
+ return
+ }
+
+ conf.SetMockRepository(t,
+ conf.RepositoryOpts{
+ Root: "/home/git/gogs-repositories",
+ },
+ )
+
+ got := RepositoryPath("alice", "example")
+ want := "/home/git/gogs-repositories/alice/example.git"
+ assert.Equal(t, want, got)
+}