// Copyright 2018 Drone.IO Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package server
import (
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"github.com/laszlocph/drone-oss-08/model"
"github.com/laszlocph/drone-oss-08/shared/httputil"
"github.com/laszlocph/drone-oss-08/store"
)
var (
badgeSuccess = ``
badgeFailure = ``
badgeStarted = ``
badgeError = ``
badgeNone = ``
)
func GetBadge(c *gin.Context) {
repo, err := store.GetRepoOwnerName(c,
c.Param("owner"),
c.Param("name"),
)
if err != nil {
c.AbortWithStatus(404)
return
}
// an SVG response is always served, even when error, so
// we can go ahead and set the content type appropriately.
c.Writer.Header().Set("Content-Type", "image/svg+xml")
// if no commit was found then display
// the 'none' badge, instead of throwing
// an error response
branch := c.Query("branch")
if len(branch) == 0 {
branch = repo.Branch
}
build, err := store.GetBuildLast(c, repo, branch)
if err != nil {
log.Warning(err)
c.String(200, badgeNone)
return
}
switch build.Status {
case model.StatusSuccess:
c.String(200, badgeSuccess)
case model.StatusFailure:
c.String(200, badgeFailure)
case model.StatusError, model.StatusKilled:
c.String(200, badgeError)
case model.StatusPending, model.StatusRunning:
c.String(200, badgeStarted)
default:
c.String(200, badgeNone)
}
}
func GetCC(c *gin.Context) {
repo, err := store.GetRepoOwnerName(c,
c.Param("owner"),
c.Param("name"),
)
if err != nil {
c.AbortWithStatus(404)
return
}
builds, err := store.GetBuildList(c, repo, 1)
if err != nil || len(builds) == 0 {
c.AbortWithStatus(404)
return
}
url := fmt.Sprintf("%s/%s/%d", httputil.GetURL(c.Request), repo.FullName, builds[0].Number)
cc := model.NewCC(repo, builds[0], url)
c.XML(200, cc)
}