/
activity.go
62 lines (51 loc) · 1.28 KB
/
activity.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
package endpoint
import (
// Stdlib
"encoding/json"
"net/http"
// Internal
httputil "github.com/salsaflow/salsaflow-daemon/internal/http"
"github.com/salsaflow/salsaflow-daemon/internal/log"
)
type Activity struct {
Changes []*Change `json:"changes"`
Project struct {
Id int `json:"id"`
} `json:"project"`
}
type Change struct {
ResourceKind string `json:"kind"`
ResourceID int `json:"id"`
NewValues Values `json:"new_values"`
}
type Values struct {
State string `json:"current_state"`
}
type activityHandlerFunc func(r *http.Request, projectId int, change *Change) error
var activityHandlers = []activityHandlerFunc{
handleRejectedStories,
}
func handleActivity(rw http.ResponseWriter, r *http.Request) {
// Decode the activity object.
var activity Activity
if err := json.NewDecoder(r.Body).Decode(&activity); err != nil {
httputil.Error(rw, r, err)
return
}
// Process the changes.
var errorOccured bool
pid := activity.Project.Id
for _, change := range activity.Changes {
for _, handler := range activityHandlers {
if err := handler(r, pid, change); err != nil {
log.Error(r, err)
errorOccured = true
}
}
}
if errorOccured {
httputil.Status(rw, http.StatusInternalServerError)
} else {
httputil.Status(rw, http.StatusAccepted)
}
}