-
Notifications
You must be signed in to change notification settings - Fork 1
/
repo.go
146 lines (125 loc) · 3.31 KB
/
repo.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
package app
import (
"io/ioutil"
"log"
"path"
D "github.com/NeoJRotary/describe-go"
exec "github.com/NeoJRotary/exec-go"
)
// InitRepo init repo
func InitRepo() {
// prepare git folder
_, err := exec.RunCmd("/", "mkdir", "-p", GitFolder)
if D.IsErr(err) {
log.Fatal(err)
}
}
// Repo repo struct
type Repo struct {
Event string
InstallationID string
RepositoryNodeID string
Dir string
FullName string
Branch string
AssociatedBases []string
BaseBranch string
Tag string
BeforeSHA string
AfterSHA string
BuildFilePath string
Removed bool
}
// LogError repo log error
func (repo *Repo) LogError(title string, err error) bool {
text := `----- app.Repo LogError -----
InstallationID: ` + repo.InstallationID + `
RepositoryNodeID: ` + repo.RepositoryNodeID + `
FullName: ` + repo.FullName + `
Branch: ` + repo.Branch + `
Tag: ` + repo.Tag + `
CommitSHA: ` + repo.BeforeSHA + ` >>> ` + repo.AfterSHA + `
`
log.Println(text, title, err)
return false
}
// Init init repo
func (repo *Repo) Init() bool {
if repo.Branch == "" && repo.Tag == "" {
return repo.LogError("Init", D.NewErr("need one of branch/tag"))
}
cloneTarget := ""
if repo.Branch != "" {
cloneTarget = repo.Branch
}
if repo.Tag != "" {
cloneTarget = repo.Tag
}
tempDir, err := ioutil.TempDir(GitFolder, "")
if D.IsErr(err) {
return repo.LogError("Init", err)
}
_, err = exec.RunCmd(tempDir, "git", "clone", "-b", cloneTarget, "https://x-access-token:"+GetAccessToken(repo.InstallationID)+"@github.com/"+repo.FullName+".git", ".")
if D.IsErr(err) {
return repo.LogError("Init", err)
}
buildFile := path.Join(tempDir, CloudBuildFileName)
if !D.FileExist(buildFile) {
repo.Remove()
return repo.LogError("Init", D.NewErr(CloudBuildFileName+" Not found."))
}
// setup
repo.Dir = tempDir
repo.BuildFilePath = buildFile
if repo.Branch != "" {
// get initial commit as beforeSHA
if repo.BeforeSHA == "" {
out, err := exec.RunCmd(repo.Dir, "git", "rev-list", "--max-parents=0", "HEAD")
if D.IsErr(err) {
return repo.LogError("Init", err)
}
repo.BeforeSHA = D.String(out).Trim("\n").TrimSpace().Get()
}
// get HEAD as afterSHA
if repo.AfterSHA == "" {
out, err := exec.RunCmd(repo.Dir, "git", "rev-parse", "HEAD")
if D.IsErr(err) {
return repo.LogError("Init", err)
}
repo.AfterSHA = D.String(out).Trim("\n").TrimSpace().Get()
}
}
if repo.Tag != "" {
// setup both BeforeSHA and AfterSHA to tag commit
out, err := exec.RunCmd(repo.Dir, "git", "rev-parse", "HEAD")
if D.IsErr(err) {
return repo.LogError("Init", err)
}
sha := D.String(out).Trim("\n").TrimSpace().Get()
repo.BeforeSHA = sha
repo.AfterSHA = sha
}
return true
}
// GetChanges get changes filename between BeforeSHA and AfterSHA
func (repo *Repo) GetChanges() []string {
out, err := exec.RunCmd(repo.Dir, "git", "diff", "--name-only", repo.BeforeSHA, repo.AfterSHA)
if D.IsErr(err) {
repo.LogError("GetChanges", err)
return nil
}
list := D.String(out).Trim("\n").Split("\n").ElmTrimSpace().Get()
for i, f := range list {
list[i] = path.Join(repo.Dir, f)
}
return list
}
// Remove remove repo
func (repo *Repo) Remove() {
if repo.Removed {
return
}
repo.Removed = true
exec.RunCmd("/", "rm", "-rf", repo.Dir)
repo.Dir = ""
}