/
repository.go
114 lines (91 loc) · 2.89 KB
/
repository.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
package repository
import (
"fmt"
"os"
"regexp"
"strings"
"github.com/pkg/errors"
)
// Repository represents a local git project repository
type Repository struct {
Owner string
Project string
CommitHash string
Tag string
}
// Interface of 'Repository'
type Interface interface {
ReadTag(*string, bool) error
ReadCommitHash() error
ReadProjectName() error
GetOwner() string
GetProject() string
GetTag() *string
GetCommitHash() *string
}
// ReadTag sets tag to the receiver and sem.ver parsed version to provided parameter
func (r *Repository) ReadTag(version *string, allowPrefix bool) error {
o := os.Getenv("GITHUB_REF")
if o == "" {
return errors.New("env.var 'GITHUB_REF' is empty or not defined")
}
semver := "(?P<major>0|[1-9]\\d*)\\.(?P<minor>0|[1-9]\\d*)\\.(?P<patch>0|[1-9]\\d*)(?:(?P<sep1>-)(?P<prerelease>(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:(?P<sep2>\\+)(?P<buildmetadata>[0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?"
var expression string
if allowPrefix {
expression = fmt.Sprintf("^refs/tags/(?P<prefix>.*?)%v$", semver)
} else {
expression = fmt.Sprintf("^refs/tags/%v$", semver)
}
regex := regexp.MustCompile(expression)
if regex.MatchString(o) {
refs := strings.Split(o, "/")
r.Tag = strings.Join(refs[2:], "/")
if allowPrefix {
*version = regex.ReplaceAllString(o, "${2}.${3}.${4}${5}${6}${7}${8}")
} else {
*version = regex.ReplaceAllString(o, "${1}.${2}.${3}${4}${5}${6}${7}${8}")
}
return nil
}
return errors.New(fmt.Sprintf("malformed env.var 'GITHUB_REF' (control tag prefix via env.var 'ALLOW_TAG_PREFIX'): expected to match regex '%s', got '%v'", expression, o))
}
// ReadCommitHash sets current commit hash
func (r *Repository) ReadCommitHash() error {
o := os.Getenv("GITHUB_SHA")
if o == "" {
return errors.New("env.var 'GITHUB_SHA' is empty or not defined")
}
r.CommitHash = o
return nil
}
// ReadProjectName sets parsed owner and project names
func (r *Repository) ReadProjectName() error {
o := os.Getenv("GITHUB_REPOSITORY")
if o == "" {
return errors.New("env.var 'GITHUB_REPOSITORY' is empty or not defined")
}
expression := "^(?P<owner>[\\w,\\-,\\_\\.]+)\\/(?P<repo>[\\w\\,\\-\\_\\.]+)$"
regex := regexp.MustCompile(expression)
if regex.MatchString(o) {
r.Owner = strings.Split(o, "/")[0]
r.Project = strings.Split(o, "/")[1]
return nil
}
return errors.New(fmt.Sprintf("malformed env.var 'GITHUB_REPOSITORY': expected to match regex '%v', got '%v'", expression, o))
}
// GetOwner returns project owner
func (r *Repository) GetOwner() string {
return r.Owner
}
// GetProject returns project name
func (r *Repository) GetProject() string {
return r.Project
}
// GetTag returns current tag
func (r *Repository) GetTag() *string {
return &r.Tag
}
// GetCommitHash returns current commit hash
func (r *Repository) GetCommitHash() *string {
return &r.CommitHash
}