2016-05-24 11:55:48 +02:00
|
|
|
package api
|
2016-04-16 21:42:57 +02:00
|
|
|
|
|
|
|
import (
|
2016-05-24 11:55:48 +02:00
|
|
|
database "github.com/ansible-semaphore/semaphore/db"
|
2016-04-16 21:42:57 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/models"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/masterminds/squirrel"
|
|
|
|
)
|
|
|
|
|
2017-02-22 23:17:36 +01:00
|
|
|
func getEvents(w http.ResponseWriter, r *http.Request) {
|
2016-04-16 21:42:57 +02:00
|
|
|
user := c.MustGet("user").(*models.User)
|
|
|
|
|
|
|
|
q := squirrel.Select("event.*, p.name as project_name").
|
|
|
|
From("event").
|
|
|
|
LeftJoin("project as p on event.project_id=p.id").
|
|
|
|
OrderBy("created desc")
|
|
|
|
|
2016-04-17 02:24:32 +02:00
|
|
|
projectObj, exists := c.Get("project")
|
2016-04-16 21:42:57 +02:00
|
|
|
if exists == true {
|
|
|
|
// limit query to project
|
|
|
|
project := projectObj.(models.Project)
|
2016-04-17 02:24:32 +02:00
|
|
|
q = q.Where("event.project_id=?", project.ID)
|
2016-04-16 21:42:57 +02:00
|
|
|
} else {
|
|
|
|
q = q.LeftJoin("project__user as pu on pu.project_id=p.id").
|
|
|
|
Where("p.id IS NULL or pu.user_id=?", user.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
var events []models.Event
|
|
|
|
|
|
|
|
query, args, _ := q.ToSql()
|
|
|
|
if _, err := database.Mysql.Select(&events, query, args...); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, evt := range events {
|
|
|
|
if evt.ObjectID == nil || evt.ObjectType == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var q squirrel.SelectBuilder
|
|
|
|
|
|
|
|
switch *evt.ObjectType {
|
|
|
|
case "task":
|
2016-12-21 11:24:27 +01:00
|
|
|
q = squirrel.Select("case when length(task.playbook) > 0 then task.playbook else tpl.playbook end").
|
2016-04-16 21:42:57 +02:00
|
|
|
From("task").
|
2016-12-21 11:24:27 +01:00
|
|
|
Join("project__template as tpl on task.template_id=tpl.id").
|
|
|
|
Where("task.id=?", evt.ObjectID)
|
2016-04-16 21:42:57 +02:00
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
query, args, _ := q.ToSql()
|
|
|
|
name, err := database.Mysql.SelectNullStr(query, args...)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if name.Valid == true {
|
|
|
|
events[i].ObjectName = name.String
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(200, events)
|
|
|
|
}
|