-
Notifications
You must be signed in to change notification settings - Fork 218
/
git_provider.go
133 lines (106 loc) · 3.33 KB
/
git_provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2024 Daytona Platforms Inc.
// SPDX-License-Identifier: Apache-2.0
package gitprovider
import (
"errors"
"fmt"
"net/url"
"regexp"
"strings"
)
const personalNamespaceId = "<PERSONAL>"
type StaticGitContext struct {
Id string `json:"id"`
Url string `json:"url"`
Name string `json:"name"`
Branch *string `json:"branch,omitempty"`
Sha *string `json:"sha,omitempty"`
Owner string `json:"owner"`
PrNumber *uint32 `json:"prNumber,omitempty"`
Source string `json:"source"`
Path *string `json:"path,omitempty"`
} // @name StaticGitContext
type GitProvider interface {
GetNamespaces() ([]*GitNamespace, error)
GetRepositories(namespace string) ([]*GitRepository, error)
GetUser() (*GitUser, error)
GetRepoBranches(repositoryId string, namespaceId string) ([]*GitBranch, error)
GetRepoPRs(repositoryId string, namespaceId string) ([]*GitPullRequest, error)
GetRepositoryFromUrl(repositoryUrl string) (*GitRepository, error)
GetLastCommitSha(staticContext *StaticGitContext) (string, error)
getPrContext(staticContext *StaticGitContext) (*StaticGitContext, error)
parseStaticGitContext(repoUrl string) (*StaticGitContext, error)
}
type AbstractGitProvider struct {
GitProvider
}
func (a *AbstractGitProvider) GetRepositoryFromUrl(repositoryUrl string) (*GitRepository, error) {
staticContext, err := a.GitProvider.parseStaticGitContext(repositoryUrl)
if err != nil {
return nil, err
}
if staticContext.PrNumber != nil {
staticContext, err = a.getPrContext(staticContext)
if err != nil {
return nil, err
}
}
lastCommitSha, err := a.GetLastCommitSha(staticContext)
if err != nil {
return nil, err
}
return &GitRepository{
Id: staticContext.Id,
Name: staticContext.Name,
Url: staticContext.Url,
Branch: staticContext.Branch,
Sha: lastCommitSha,
Owner: staticContext.Owner,
PrNumber: staticContext.PrNumber,
Source: staticContext.Source,
Path: staticContext.Path,
}, nil
}
func (a *AbstractGitProvider) parseStaticGitContext(repoUrl string) (*StaticGitContext, error) {
if strings.HasPrefix(repoUrl, "git@") {
return a.parseSshGitUrl(repoUrl)
}
if !strings.HasPrefix(repoUrl, "http") {
return nil, errors.New("can not parse git URL: " + repoUrl)
}
repoUrl = strings.TrimSuffix(repoUrl, ".git")
u, err := url.Parse(repoUrl)
if err != nil {
return nil, err
}
repo := &StaticGitContext{}
path := strings.TrimPrefix(u.Path, "/")
parts := strings.Split(path, "/")
repo.Source = u.Host
repo.Owner = parts[0]
repo.Name = parts[1]
repo.Id = parts[1]
branchPath := strings.Join(parts[2:], "/")
if branchPath != "" {
repo.Path = &branchPath
}
repo.Url = getCloneUrl(repo.Source, repo.Owner, repo.Name)
return repo, nil
}
func (a *AbstractGitProvider) parseSshGitUrl(gitURL string) (*StaticGitContext, error) {
re := regexp.MustCompile(`git@([\w\.]+):(.+?)/(.+?)(?:\.git)?$`)
matches := re.FindStringSubmatch(gitURL)
if len(matches) != 4 {
return nil, errors.New("cannot parse git URL: " + gitURL)
}
repo := &StaticGitContext{}
repo.Source = matches[1]
repo.Owner = matches[2]
repo.Name = matches[3]
repo.Id = matches[3]
repo.Url = getCloneUrl(repo.Source, repo.Owner, repo.Name)
return repo, nil
}
func getCloneUrl(source, owner, repo string) string {
return fmt.Sprintf("https://%s/%s/%s.git", source, owner, repo)
}