This repository has been archived by the owner on Nov 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
helpers.go
101 lines (91 loc) · 2.76 KB
/
helpers.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
package githelpers
import (
"fmt"
"io/ioutil"
"net/url"
"os"
"github.com/google/uuid"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
)
// AddAndCommitFiles add and commits files
func AddAndCommitFiles(gitter gits.Gitter, dir string, message string) (bool, error) {
err := gitter.Add(dir, "*")
if err != nil {
return false, errors.Wrapf(err, "failed to add files to git")
}
changes, err := gitter.HasChanges(dir)
if err != nil {
if err != nil {
return changes, errors.Wrapf(err, "failed to check if there are changes")
}
}
if !changes {
return changes, nil
}
err = gitter.CommitDir(dir, message)
if err != nil {
return changes, errors.Wrapf(err, "failed to git commit initial code changes")
}
return changes, nil
}
// CreateBranch creates a dynamic branch name and branch
func CreateBranch(gitter gits.Gitter, dir string) (string, error) {
branchName := fmt.Sprintf("pr-%s", uuid.New().String())
gitRef := branchName
err := gitter.CreateBranch(dir, branchName)
if err != nil {
return branchName, errors.Wrapf(err, "create branch %s from %s", branchName, gitRef)
}
err = gitter.Checkout(dir, branchName)
if err != nil {
return branchName, errors.Wrapf(err, "checkout branch %s", branchName)
}
return branchName, nil
}
// GitCloneToTempDir clones the git repository to either the given directory or create a temporary
func GitCloneToTempDir(gitter gits.Gitter, gitURL string, dir string) (string, error) {
var err error
if dir != "" {
err = os.MkdirAll(dir, util.DefaultWritePermissions)
if err != nil {
return "", errors.Wrapf(err, "failed to create directory %s", dir)
}
} else {
dir, err = ioutil.TempDir("", "helmboot-")
if err != nil {
return "", errors.Wrap(err, "failed to create temporary directory")
}
}
log.Logger().Debugf("cloning %s to directory %s", util.ColorInfo(gitURL), util.ColorInfo(dir))
err = gitter.Clone(gitURL, dir)
if err != nil {
return "", errors.Wrapf(err, "failed to clone repository %s to directory: %s", gitURL, dir)
}
return dir, nil
}
// AddUserTokenToURLIfRequired ensures we have a user and token in the given git URL
func AddUserTokenToURLIfRequired(gitURL, username, token string) (string, error) {
u, err := url.Parse(gitURL)
if err != nil {
return "", errors.Wrapf(err, "failed to parse git URL %s", gitURL)
}
// lets check if we've already got a user and password
if u.User != nil {
user := u.User
pwd, f := user.Password()
if user.Username() != "" && pwd != "" && f {
return gitURL, nil
}
}
if username == "" {
return "", fmt.Errorf("missing git username")
}
if token == "" {
return "", fmt.Errorf("missing git token")
}
u.User = url.UserPassword(username, token)
return u.String(), nil
}