forked from fallenhitokiri/leeroyci
/
command.go
116 lines (92 loc) · 2.35 KB
/
command.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
// Package database provides a wrapper between the database and stucts
package database
import (
"errors"
"time"
)
const (
// CommandKindTest is used when a command runs tests.
CommandKindTest = "test"
// CommandKindBuild is used when a command builds a package or project.
CommandKindBuild = "build"
// CommandKindDeploy is used when a command deploys a branch.
CommandKindDeploy = "deploy"
)
// Command stores a short name and the path or command to execute when a users
// pushes to a repository.
type Command struct {
ID int64
Name string
Kind string
Branch string
Execute string
RepositoryID int64
CreatedAt time.Time
UpdatedAt time.Time
}
// CommandLog stored a finnished command and the output of the task.
type CommandLog struct {
ID int64
Name string // we only keep the name, no reference to the command, in case it changes.
Return string
Output string `sql:"type:text"`
JobID int64
}
// CreateCommand adds a new command to a repository.
func CreateCommand(repo *Repository, name, execute, branch, kind string) (*Command, error) {
if kind != CommandKindTest && kind != CommandKindBuild && kind != CommandKindDeploy {
return nil, errors.New("wrong kind")
}
c := Command{
Name: name,
Execute: execute,
Kind: kind,
RepositoryID: repo.ID,
Branch: branch,
}
db.Save(&c)
return &c, nil
}
// GetCommand returns a command for a specific ID.
func GetCommand(id int64) (*Command, error) {
c := &Command{}
db.Where("ID = ?", id).First(&c)
return c, nil
}
// Update a command.
func (c *Command) Update(name, kind, branch, execute string) error {
c.Name = name
c.Kind = kind
c.Branch = branch
c.Execute = execute
db.Save(c)
return nil
}
// Delete deletes a command.
func (c *Command) Delete() {
db.Delete(c)
}
// CreateCommandLog adds a new log.
func CreateCommandLog(command *Command, job *Job, ret, out string) *CommandLog {
log := CommandLog{
Name: command.Name,
Return: ret,
Output: out,
JobID: job.ID,
}
db.Save(&log)
return &log
}
// GetCommandLogsForJob returns all logs for a job.
func GetCommandLogsForJob(id int64) []*CommandLog {
var logs []*CommandLog
db.Where("job_id = ?", id).Find(&logs)
return logs
}
// Passed returns true if the command completed successfully.
func (t *CommandLog) Passed() bool {
if t.Return == "" {
return true
}
return false
}