forked from ArchCI/archci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builds.go
138 lines (115 loc) · 3.96 KB
/
builds.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
package models
import (
"fmt"
"time"
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
"github.com/ArchCI/archci/githubutil"
"github.com/ArchCI/archci/gitlabutil"
)
const (
BUILD_STATUS_NOT_START = 0
BUILD_STATUS_BUILDING = 1
BUILD_STATUS_SUCCESS = 2
BUILD_STATUS_FAIL = 3
BUILD_STATUS_CANCELED = 4
)
// Build contains all the information of the build task.
type Build struct {
Id int64 `orm:"pk;auto"`
ProjectId int64
UserName string `orm:"size(1024);null"`
ProjectName string `orm:"size(1024);null"`
RepoUrl string `orm:"size(1024);null"`
Branch string `orm:"size(1024);null"`
Commit string `orm:"size(1024);null"`
CommitTime time.Time `orm:"null"`
Committer string `orm:"size(1024);null"`
BuildTime time.Time `orm:"null"`
FinishTime time.Time `orm:"null"`
Worker string `orm:"size(1024);null"`
Status int `orm:"null"`
}
// GetAllBuilds return all builds from database.
func GetAllBuilds() []*Build {
o := orm.NewOrm()
var builds []*Build
// o.QueryTable("build").Filter("name", "slene").All(&builds) to filter with build status
num, err := o.QueryTable("build").All(&builds)
fmt.Printf("Returned Rows Num: %s, %s", num, err)
return builds
}
// GetBuildsWithProjectName takes project name and return its builds.
func GetBuildsWithProjectName(projectName string) []*Build {
o := orm.NewOrm()
var builds []*Build
num, err := o.QueryTable("build").Filter("project_name", projectName).All(&builds)
fmt.Printf("Returned Rows Num: %s, %s", num, err)
return builds
}
// GetBuildWithId takes build is and return the build.
func GetBuildWithId(buildId int64) Build {
o := orm.NewOrm()
build := Build{Id: buildId}
err := o.Read(&build)
fmt.Printf("ERR: %v\n", err)
return build
}
// AddBuildWithProject take project object to create build.
func AddBuildWithProject(project Project) error {
o := orm.NewOrm()
build := Build{UserName: project.UserName, ProjectName: project.ProjectName, RepoUrl: project.RepoUrl, Branch: "master", BuildTime: time.Now(), CommitTime: time.Now()}
_, err := o.Insert(&build)
fmt.Printf("ERR: %v\n", err)
return err
}
// AddGithubBuild takes github webhook to create build.
func AddGithubBuild(projectId int64, data githubutil.GithubPushHook) error {
o := orm.NewOrm()
// TODO(tobe): could not get commit id, commit time and committer
build := Build{
UserName: data.Repository.Owner.Login,
ProjectId: projectId,
ProjectName: data.Repository.Name,
RepoUrl: data.Repository.URL,
Branch: data.Repository.DefaultBranch,
Commit: "ffffffffff",
CommitTime: time.Now(),
Committer: "unknown",
BuildTime: time.Now(),
FinishTime: time.Now(),
Status: BUILD_STATUS_NOT_START}
_, err := o.Insert(&build)
fmt.Println("ERR: %v\n", err)
return err
}
// AddGitlabBuild takes gitlab webhook to create build.
func AddGitlabBuild(projectId int64, data gitlabutil.GitlabPushHook) error {
o := orm.NewOrm()
// TODO(tobe): need to find project id by user name and project name
// TODO(tobe): no branch data from gitlab webhook
// TODO(tobe): should choose the latest commit
// TODO(tobe): build time and finish time should be null
build := Build{
UserName: data.UserName,
ProjectId: projectId,
ProjectName: data.Repository.Name,
RepoUrl: data.Repository.URL,
Branch: "master",
Commit: data.Commits[0].ID,
CommitTime: data.Commits[0].Timestamp,
Committer: data.Commits[0].Author.Name,
BuildTime: time.Now(),
FinishTime: time.Now(),
Status: BUILD_STATUS_NOT_START}
_, err := o.Insert(&build)
fmt.Println("ERR: %v\n", err)
return err
}
// AddBuild takes basic information to create build.
func AddBuild(projectName string, branch string, commit string, commitTime time.Time, committer string) error {
o := orm.NewOrm()
build := Build{ProjectName: projectName, Branch: branch, Commit: commit, CommitTime: commitTime, Committer: committer}
_, err := o.Insert(&build)
return err
}