This repository has been archived by the owner on May 22, 2022. It is now read-only.
forked from fallenhitokiri/leeroyci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
121 lines (88 loc) · 2.35 KB
/
job.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
package web
import (
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/francoishill/leeroyci/database"
"github.com/francoishill/leeroyci/runner"
)
const limit = 20
// viewListJobs shows a paginated list of all jobs.
func viewListJobs(w http.ResponseWriter, r *http.Request) {
template := "job/list.html"
ctx := make(responseContext)
offset := 0
paramOffset := r.URL.Query().Get("offset")
if len(paramOffset) > 0 {
offset = stringToInt(paramOffset)
}
ctx["jobs"] = database.GetJobs(offset, limit)
prev, next, first := previousNextNumber(offset)
ctx["previous_offset"] = prev
ctx["next_offset"] = next
ctx["first_page"] = first
render(w, r, template, ctx)
}
// viewJobDetail shows a specific job with all related information.
func viewDetailJob(w http.ResponseWriter, r *http.Request) {
template := "job/detail.html"
ctx := make(responseContext)
vars := mux.Vars(r)
jobID, _ := strconv.Atoi(vars["jid"])
job := database.GetJob(int64(jobID))
ctx["job"] = job
render(w, r, template, ctx)
}
// viewCancelJob cancels a job.
func viewCancelJob(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
jobID, _ := strconv.Atoi(vars["jid"])
job := database.GetJob(int64(jobID))
job.Cancel()
http.Redirect(w, r, "/", 302)
}
// viewRerunJob resets a job status and enqueues it agian.
func viewRerunJob(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
jobID, _ := strconv.Atoi(vars["jid"])
old := database.GetJob(int64(jobID))
job := database.CreateJob(
&old.Repository,
old.Branch,
old.Commit,
old.CommitURL,
old.Name,
old.Email,
)
queueJob := runner.QueueJob{
JobID: job.ID,
}
queueJob.Enqueue()
http.Redirect(w, r, "/", 302)
}
// viewSearchJobs returns a list of jobs filtered by the search string.
func viewSearchJobs(w http.ResponseWriter, r *http.Request) {
template := "job/list.html"
ctx := make(responseContext)
query := r.URL.Query().Get("query")
ctx["jobs"] = database.SearchJobs(query)
ctx["query"] = query
render(w, r, template, ctx)
}
// returns the offset for the previous and next page.
func previousNextNumber(offset int) (int, int, bool) {
count := database.NumberOfJobs()
prev := offset - limit
next := offset + limit
if prev < 0 {
prev = 0
}
if next >= count {
next = -1
}
first := false
if count > limit && next != limit {
first = true
}
return prev, next, first
}