This repository has been archived by the owner on Feb 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
helper.go
126 lines (97 loc) · 2.85 KB
/
helper.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
// Copyright © 2016-present Thomas Rabaix <[email protected]>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package gitlab_ci_helper
import (
"errors"
"fmt"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
gitlab "gopkg.in/plouc/go-gitlab-client.v1"
)
type Paths []string
func (p *Paths) String() string {
return fmt.Sprintf("%v", *p)
}
func (p *Paths) Set(value string) error {
*p = append(*p, value)
return nil
}
var SemVersion = regexp.MustCompile("(v|)[0-9]{1,}\\.[0-9]{1,}\\.[0-9]{1,}(-[A-Za-z]*|)")
func GetAwsCredentials(profile string) (*credentials.Credentials, error) {
sess := session.Must(session.NewSession())
chainProvider := credentials.NewChainCredentials([]credentials.Provider{
&credentials.EnvProvider{},
&credentials.SharedCredentialsProvider{
Filename: os.Getenv("HOME") + "/.aws/credentials",
Profile: profile,
},
&ec2rolecreds.EC2RoleProvider{
Client: ec2metadata.New(sess, &aws.Config{
HTTPClient: &http.Client{Timeout: 10 * time.Second},
}),
ExpiryWindow: 0,
},
})
_, err := chainProvider.Get()
if err != nil {
return nil, err
}
return chainProvider, nil
}
func GetProject(p string, client *gitlab.Gitlab) (*gitlab.Project, error) {
pId, err := strconv.ParseInt(p, 10, 32)
if err != nil {
// invalid build id, search from a project list
paths := strings.Split(p, "/")
if len(paths) != 2 {
return nil, fmt.Errorf("Error: Invalid project format, must be namespace/project-name, project=%s, err=%s", p, err)
}
projects, _ := client.Projects()
try := ""
for _, p := range projects {
if p.Name == paths[1] {
try = fmt.Sprintf("%s/%s", p.Namespace.Name, p.Name)
}
if p.Name == paths[1] && p.Namespace.Name == paths[0] {
pId = int64(p.Id)
break
}
}
if pId == 0 {
extra := ""
if len(try) > 0 {
extra = fmt.Sprintf("\nDid you mean: %s ?", try)
}
return nil, fmt.Errorf("Unable to find the project: %s/%s.%s", paths[0], paths[1], extra)
}
}
project, err := client.Project(strconv.FormatInt(pId, 10))
if err != nil {
return nil, errors.New("Error: " + err.Error())
}
return project, err
}
func GetBuild(project *gitlab.Project, buildId string, client *gitlab.Gitlab) (*gitlab.Build, error) {
build, err := client.ProjectBuild(strconv.FormatInt(int64(project.Id), 10), buildId)
if err != nil {
return nil, fmt.Errorf("Error: %s.\nUnable to find the build (projectId:%d, buildId:%s)", err.Error(), project.Id, buildId)
}
return build, err
}
func GetEnv(name, deflt string) string {
if len(os.Getenv(name)) == 0 {
return deflt
}
return os.Getenv(name)
}