This repository has been archived by the owner on Apr 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgit.go
182 lines (152 loc) · 4.37 KB
/
git.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"regexp"
"sort"
"strings"
"github.com/sirupsen/logrus"
"github.com/blang/semver"
)
const (
templateTagsPrefix = "<!-- start autogeneration tags -->"
templateTagsSuffix = "<!-- end autogeneration tags -->"
)
var (
reDockerfileVersion = regexp.MustCompile(`(VERSION=)(\d+\.\d+\.\d+)`)
reDockerfileSHA256 = regexp.MustCompile(`(SHA256=)([a-z0-9]+)`)
)
func gitSetupCredentials() error {
cmd := exec.Command("git", "config", "--global", "user.email", os.Getenv("GIT_EMAIL"))
output, err := cmd.CombinedOutput()
if err != nil {
return errors.New(string(output))
}
cmd = exec.Command("git", "config", "--global", "user.name", os.Getenv("GIT_NAME"))
output, err = cmd.CombinedOutput()
if err != nil {
return errors.New(string(output))
}
return nil
}
func gitCloneRepo(path string) error {
logrus.Debugln("git clone", fmt.Sprintf("https://github.com/%s/%s.git", githubRepoOwner, githubRepoName))
cmd := exec.Command("git", "clone", fmt.Sprintf("https://%s:%s@github.com/%s/%s.git", githubUser, githubToken, githubRepoOwner, githubRepoName), path)
output, err := cmd.CombinedOutput()
if err != nil {
return errors.New(string(output))
}
return nil
}
func gitCheckoutBranch(path string, branch string) error {
cmd := exec.Command("git", "checkout", "-b", branch)
cmd.Dir = path
output, err := cmd.CombinedOutput()
if err != nil {
return errors.New(string(output))
}
return nil
}
func gitCreateCommit(path string, commitMessage string) error {
logrus.Debugln("git add")
cmd := exec.Command("git", "add", ".")
cmd.Dir = path
output, err := cmd.CombinedOutput()
if err != nil {
return errors.New(string(output))
}
logrus.Debugln("git commit", commitMessage)
cmd = exec.Command("git", "commit", "-am", commitMessage)
cmd.Dir = path
output, err = cmd.CombinedOutput()
if err != nil {
return errors.New(string(output))
}
return nil
}
func gitPush(path string, branch string) error {
args := []string{"push"}
if branch != "" {
args = append(args, "--set-upstream", "origin", branch)
}
cmd := exec.Command("git", args...)
cmd.Dir = path
output, err := cmd.CombinedOutput()
if err != nil {
return errors.New(string(output))
}
return nil
}
func gitTagAndPush(path string, tagName string) error {
cmd := exec.Command("git", "tag", tagName)
cmd.Dir = path
output, err := cmd.CombinedOutput()
if err != nil {
return errors.New(string(output))
}
cmd = exec.Command("git", "push", "origin", tagName)
cmd.Dir = path
output, err = cmd.CombinedOutput()
if err != nil {
return errors.New(string(output))
}
return nil
}
func editDockerfile(path string, version semver.Version, checksum string) error {
filename := fmt.Sprintf("%s/%d.%d/Dockerfile", path, version.Major, version.Minor)
file, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
file = reDockerfileVersion.ReplaceAll(file, []byte("${1}"+version.String()))
file = reDockerfileSHA256.ReplaceAll(file, []byte("${1}"+checksum))
err = ioutil.WriteFile(filename, file, 0666)
if err != nil {
return err
}
return nil
}
func editReadme(path string, version BuildInfo) error {
file, err := ioutil.ReadFile(path + "/README.md")
if err != nil {
return err
}
startIndex := bytes.Index(file, []byte(templateTagsPrefix))
endIndex := bytes.Index(file, []byte(templateTagsSuffix))
if startIndex == -1 || endIndex == -1 {
return errors.New("unable to find start or end tags in README.md")
}
var versions []string
for k := range version.Versions {
versions = append(versions, k)
}
sort.Sort(sort.Reverse(sort.StringSlice(versions)))
generatedTags := ""
for _, v := range versions {
tags := make([]string, len(version.Versions[v].Tags))
for i, tag := range version.Versions[v].Tags {
tags[i] = fmt.Sprintf("`%s`", tag)
}
generatedTags += fmt.Sprintf("* %s\n", strings.Join(tags, ", "))
}
content := string(file[:startIndex]) + templateTagsPrefix + "\n" + generatedTags + string(file[endIndex:])
err = ioutil.WriteFile(path+"/README.md", []byte(content), 0666)
if err != nil {
return err
}
return nil
}
func editBuildinfo(path string, buildinfo BuildInfo) error {
f, err := os.OpenFile(path+"/buildinfo.json", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
if err != nil {
return err
}
encoder := json.NewEncoder(f)
encoder.SetIndent("", " ")
return encoder.Encode(buildinfo.Versions)
}