2016-04-06 11:31:22 -05:00
|
|
|
// Copyright 2016 Tim Shannon. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-04-13 11:29:17 -05:00
|
|
|
"bytes"
|
2016-04-06 11:31:22 -05:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
2016-04-13 11:29:17 -05:00
|
|
|
"time"
|
2016-04-17 20:43:27 -05:00
|
|
|
|
|
|
|
"git.townsourced.com/ironsmith/datastore"
|
2016-04-06 11:31:22 -05:00
|
|
|
)
|
|
|
|
|
2016-04-06 16:59:24 -05:00
|
|
|
// /path/<project-id>/<version>/<stage>
|
2016-04-06 11:31:22 -05:00
|
|
|
func splitPath(path string) (project, version, stage string) {
|
|
|
|
s := strings.Split(path, "/")
|
|
|
|
if len(s) < 3 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
project, _ = url.QueryUnescape(s[2])
|
|
|
|
if len(s) < 4 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
version, _ = url.QueryUnescape(s[3])
|
|
|
|
|
|
|
|
if len(s) < 5 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
stage, _ = url.QueryUnescape(s[4])
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-07 09:49:54 -05:00
|
|
|
/*
|
|
|
|
/log/ - list all projects
|
2016-04-13 11:29:17 -05:00
|
|
|
/log/<project-id> - list all versions in a project, POST triggers new builds
|
2016-04-07 09:49:54 -05:00
|
|
|
/log/<project-id>/<version> - list combined output of all stages for a given version
|
|
|
|
/log/<project-id>/<version>/<stage> - list output of a given stage of a given version
|
|
|
|
*/
|
|
|
|
func logGet(w http.ResponseWriter, r *http.Request) {
|
|
|
|
prj, ver, stg := splitPath(r.URL.Path)
|
2016-04-06 16:59:24 -05:00
|
|
|
|
|
|
|
if prj == "" {
|
2016-04-07 09:49:54 -05:00
|
|
|
///log/ - list all projects
|
2016-04-06 16:59:24 -05:00
|
|
|
pList, err := projects.webList()
|
2016-04-13 11:29:17 -05:00
|
|
|
if errHandled(err, w, r) {
|
2016-04-06 16:59:24 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
respondJsend(w, &JSend{
|
|
|
|
Status: statusSuccess,
|
|
|
|
Data: pList,
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
project, ok := projects.get(prj)
|
|
|
|
if !ok {
|
|
|
|
four04(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//project found
|
|
|
|
|
|
|
|
if ver == "" {
|
2016-04-13 11:29:17 -05:00
|
|
|
///log/<project-id> - list all versions in a project
|
2016-04-07 09:49:54 -05:00
|
|
|
|
|
|
|
vers, err := project.versions()
|
2016-04-13 11:29:17 -05:00
|
|
|
if errHandled(err, w, r) {
|
2016-04-07 09:49:54 -05:00
|
|
|
return
|
|
|
|
}
|
2016-04-17 20:43:27 -05:00
|
|
|
|
|
|
|
prjData, err := project.webData()
|
|
|
|
if errHandled(err, w, r) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-07 09:49:54 -05:00
|
|
|
respondJsend(w, &JSend{
|
|
|
|
Status: statusSuccess,
|
2016-04-17 20:43:27 -05:00
|
|
|
Data: struct {
|
|
|
|
*webProject
|
|
|
|
Versions []*datastore.Log `json:"versions"`
|
|
|
|
}{
|
|
|
|
webProject: prjData,
|
|
|
|
Versions: vers,
|
|
|
|
},
|
2016-04-07 09:49:54 -05:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//ver found
|
|
|
|
if stg == "" {
|
|
|
|
///log/<project-id>/<version> - list combined output of all stages for a given version
|
|
|
|
logs, err := project.versionLog(ver)
|
2016-04-13 11:29:17 -05:00
|
|
|
if errHandled(err, w, r) {
|
2016-04-07 09:49:54 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
respondJsend(w, &JSend{
|
|
|
|
Status: statusSuccess,
|
|
|
|
Data: logs,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//stage found
|
|
|
|
///log/<project-id>/<version>/<stage> - list output of a given stage of a given version
|
|
|
|
|
|
|
|
log, err := project.stageLog(ver, stg)
|
2016-04-13 11:29:17 -05:00
|
|
|
if errHandled(err, w, r) {
|
2016-04-07 09:49:54 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
respondJsend(w, &JSend{
|
|
|
|
Status: statusSuccess,
|
|
|
|
Data: log,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
/release/<project-id>/<version>
|
|
|
|
|
2016-04-13 11:29:17 -05:00
|
|
|
/release/<project-id> - list last release for a given project
|
|
|
|
?all returns all the releases for a project ?file returns the last release file
|
|
|
|
/release/<project-id>/<version> - list release for a given project version ?file returns the file for a given release version
|
2016-04-07 09:49:54 -05:00
|
|
|
*/
|
|
|
|
func releaseGet(w http.ResponseWriter, r *http.Request) {
|
2016-04-13 11:29:17 -05:00
|
|
|
prj, ver, _ := splitPath(r.URL.Path)
|
2016-04-07 09:49:54 -05:00
|
|
|
|
|
|
|
values := r.URL.Query()
|
|
|
|
|
2016-04-13 11:29:17 -05:00
|
|
|
_, all := values["all"]
|
|
|
|
_, file := values["file"]
|
|
|
|
|
2016-04-07 09:49:54 -05:00
|
|
|
if prj == "" {
|
|
|
|
four04(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
project, ok := projects.get(prj)
|
|
|
|
if !ok {
|
|
|
|
four04(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//project found
|
|
|
|
|
|
|
|
if ver == "" {
|
2016-04-13 11:29:17 -05:00
|
|
|
///release/<project-id> - list last release for a given project
|
|
|
|
// ?all returns all the releases for a project ?file returns the last release file
|
|
|
|
|
|
|
|
if all {
|
|
|
|
releases, err := project.releases()
|
|
|
|
if errHandled(err, w, r) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
respondJsend(w, &JSend{
|
|
|
|
Status: statusSuccess,
|
|
|
|
Data: releases,
|
|
|
|
})
|
|
|
|
return
|
2016-04-07 09:49:54 -05:00
|
|
|
|
2016-04-13 11:29:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
last, err := project.lastRelease()
|
|
|
|
if errHandled(err, w, r) {
|
2016-04-06 16:59:24 -05:00
|
|
|
return
|
|
|
|
}
|
2016-04-13 11:29:17 -05:00
|
|
|
|
|
|
|
if file {
|
|
|
|
fileData, err := project.releaseFile(last.FileKey)
|
|
|
|
if errHandled(err, w, r) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-13 15:53:49 -05:00
|
|
|
w.Header().Add("Content-disposition", `attachment; filename="`+last.FileName+`"`)
|
2016-04-13 11:29:17 -05:00
|
|
|
http.ServeContent(w, r, last.FileName, time.Time{}, bytes.NewReader(fileData))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-06 16:59:24 -05:00
|
|
|
respondJsend(w, &JSend{
|
|
|
|
Status: statusSuccess,
|
2016-04-13 11:29:17 -05:00
|
|
|
Data: last,
|
2016-04-06 16:59:24 -05:00
|
|
|
})
|
2016-04-13 11:29:17 -05:00
|
|
|
|
2016-04-06 16:59:24 -05:00
|
|
|
return
|
|
|
|
}
|
2016-04-06 11:31:22 -05:00
|
|
|
|
2016-04-07 09:49:54 -05:00
|
|
|
//ver found
|
2016-04-13 11:29:17 -05:00
|
|
|
// /release/<project-id>/<version> - list release for a given project version ?file returns the file for a given release version
|
|
|
|
|
|
|
|
release, err := project.releaseData(ver)
|
|
|
|
if errHandled(err, w, r) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if file {
|
|
|
|
fileData, err := project.releaseFile(release.FileKey)
|
|
|
|
if errHandled(err, w, r) {
|
|
|
|
return
|
|
|
|
}
|
2016-04-13 15:53:49 -05:00
|
|
|
|
|
|
|
w.Header().Add("Content-disposition", `attachment; filename="`+release.FileName+`"`)
|
2016-04-13 11:29:17 -05:00
|
|
|
http.ServeContent(w, r, release.FileName, time.Time{}, bytes.NewReader(fileData))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
respondJsend(w, &JSend{
|
|
|
|
Status: statusSuccess,
|
|
|
|
Data: release,
|
|
|
|
})
|
2016-04-06 11:31:22 -05:00
|
|
|
}
|
2016-04-13 15:53:49 -05:00
|
|
|
|
|
|
|
type triggerInput struct {
|
|
|
|
Secret string `json:"secret"`
|
|
|
|
}
|
|
|
|
|
|
|
|
/*trigger routes
|
|
|
|
/trigger/<project-id>
|
|
|
|
Triggers a project to start a cycle
|
|
|
|
*/
|
|
|
|
func triggerPost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
prj, _, _ := splitPath(r.URL.Path)
|
|
|
|
|
|
|
|
if prj == "" {
|
|
|
|
four04(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
project, ok := projects.get(prj)
|
|
|
|
if !ok {
|
|
|
|
four04(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-18 16:42:21 -05:00
|
|
|
if strings.TrimSpace(project.TriggerSecret) == "" {
|
|
|
|
four04(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-13 15:53:49 -05:00
|
|
|
input := &triggerInput{}
|
|
|
|
if errHandled(parseInput(r, input), w, r) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.Secret != project.TriggerSecret {
|
|
|
|
errHandled(&Fail{
|
|
|
|
Message: "Invalid trigger secret for this project",
|
|
|
|
HTTPStatus: http.StatusUnauthorized,
|
|
|
|
}, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
project.load()
|
|
|
|
}()
|
|
|
|
}
|