forked from samcontesse/gitlab-merge-request-resource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.go
72 lines (61 loc) · 2.07 KB
/
models.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
package resource
import (
"net/url"
"os"
"regexp"
"time"
)
type Source struct {
URI string `json:"uri"`
PrivateToken string `json:"private_token"`
Insecure bool `json:"insecure"`
SkipWorkInProgress bool `json:"skip_work_in_progress,omitempty"`
SkipNotMergeable bool `json:"skip_not_mergeable,omitempty"`
SkipTriggerComment bool `json:"skip_trigger_comment,omitempty"`
ConcourseUrl string `json:"concourse_url,omitempty"`
PipelineName string `json:"pipeline_name,omitempty"`
Labels []string `json:"labels,omitempty"`
TargetBranch string `json:"target_branch,omitempty"`
}
type Version struct {
ID int `json:"id,string"`
UpdatedAt *time.Time `json:"updated_at"`
}
type Metadata []MetadataField
type MetadataField struct {
Name string `json:"name"`
Value string `json:"value"`
}
// GetBaseURL extracts host from URI (repository URL) and appends the v3 API suffix.
func (source *Source) GetBaseURL() string {
r, _ := regexp.Compile("https?://[^/]+")
host := r.FindString(source.URI)
return host + "/api/v4"
}
// GetProjectPath extracts project path from URI (repository URL).
func (source *Source) GetProjectPath() string {
r, _ := regexp.Compile("(https?|ssh)://([^/]*)/(.*)\\.git$")
return r.FindStringSubmatch(source.URI)[3]
}
func (source *Source) GetTargetURL() string {
target, _ := url.Parse(source.GetCoucourseUrl())
target.Path += "/teams/" + url.QueryEscape(os.Getenv("BUILD_TEAM_NAME"))
target.Path += "/pipelines/" + url.QueryEscape(os.Getenv("BUILD_PIPELINE_NAME"))
target.Path += "/jobs/" + url.QueryEscape(os.Getenv("BUILD_JOB_NAME"))
target.Path += "/builds/" + url.QueryEscape(os.Getenv("BUILD_NAME"))
return target.String()
}
func (source *Source) GetCoucourseUrl() string {
if source.ConcourseUrl != "" {
return source.ConcourseUrl
} else {
return os.Getenv("ATC_EXTERNAL_URL")
}
}
func (source *Source) GetPipelineName() string {
if source.PipelineName != "" {
return source.PipelineName
} else {
return os.Getenv("BUILD_PIPELINE_NAME")
}
}