gateway: add badges endpoint
Currently we generate badges only for projects branches. In future this could be extended to also generate badges for tags and PRs
This commit is contained in:
parent
c523bcba4e
commit
0c94386149
|
@ -0,0 +1,81 @@
|
|||
// Copyright 2019 Sorint.lab
|
||||
//
|
||||
// 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 action
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"path"
|
||||
|
||||
"github.com/sorintlab/agola/internal/services/gateway/common"
|
||||
rstypes "github.com/sorintlab/agola/internal/services/runservice/types"
|
||||
)
|
||||
|
||||
// GetBadge return a badge for a project branch
|
||||
// TODO(sgotti) also handle tags and PRs
|
||||
func (h *ActionHandler) GetBadge(ctx context.Context, projectRef, branch string) (string, error) {
|
||||
project, resp, err := h.configstoreClient.GetProject(ctx, projectRef)
|
||||
if err != nil {
|
||||
return "", ErrFromRemote(resp, err)
|
||||
}
|
||||
|
||||
// if branch is empty we get the latest run for every branch.
|
||||
group := path.Join("/", string(common.GroupTypeProject), project.ID, string(common.GroupTypeBranch), url.PathEscape(branch))
|
||||
runResp, resp, err := h.runserviceClient.GetGroupLastRun(ctx, group, nil)
|
||||
if err != nil {
|
||||
return "", ErrFromRemote(resp, err)
|
||||
}
|
||||
if len(runResp.Runs) == 0 {
|
||||
return badgeUnknown, nil
|
||||
}
|
||||
run := runResp.Runs[0]
|
||||
|
||||
var badge string
|
||||
switch run.Result {
|
||||
case rstypes.RunResultUnknown:
|
||||
switch run.Phase {
|
||||
case rstypes.RunPhaseSetupError:
|
||||
badge = badgeError
|
||||
case rstypes.RunPhaseQueued:
|
||||
badge = badgeInProgress
|
||||
case rstypes.RunPhaseRunning:
|
||||
badge = badgeInProgress
|
||||
case rstypes.RunPhaseCancelled:
|
||||
badge = badgeFailed
|
||||
}
|
||||
case rstypes.RunResultSuccess:
|
||||
badge = badgeSuccess
|
||||
case rstypes.RunResultFailed:
|
||||
badge = badgeFailed
|
||||
case rstypes.RunResultStopped:
|
||||
badge = badgeFailed
|
||||
}
|
||||
|
||||
return badge, nil
|
||||
}
|
||||
|
||||
// svg images generated from shields.io
|
||||
const (
|
||||
// https://img.shields.io/badge/run-unknown-inactive.svg
|
||||
badgeUnknown = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="90" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="a"><rect width="90" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#a)"><path fill="#555" d="M0 0h29v20H0z"/><path fill="#9f9f9f" d="M29 0h61v20H29z"/><path fill="url(#b)" d="M0 0h90v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="110"> <text x="155" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="190">run</text><text x="155" y="140" transform="scale(.1)" textLength="190">run</text><text x="585" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="510">unknown</text><text x="585" y="140" transform="scale(.1)" textLength="510">unknown</text></g> </svg>`
|
||||
// https://img.shields.io/badge/run-success-success.svg
|
||||
badgeSuccess = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="82" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="a"><rect width="82" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#a)"><path fill="#555" d="M0 0h29v20H0z"/><path fill="#4c1" d="M29 0h53v20H29z"/><path fill="url(#b)" d="M0 0h82v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="110"> <text x="155" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="190">run</text><text x="155" y="140" transform="scale(.1)" textLength="190">run</text><text x="545" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="430">success</text><text x="545" y="140" transform="scale(.1)" textLength="430">success</text></g> </svg>`
|
||||
// https://img.shields.io/badge/run-failed-critical.svg
|
||||
badgeFailed = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="68" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="a"><rect width="68" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#a)"><path fill="#555" d="M0 0h29v20H0z"/><path fill="#e05d44" d="M29 0h39v20H29z"/><path fill="url(#b)" d="M0 0h68v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="110"> <text x="155" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="190">run</text><text x="155" y="140" transform="scale(.1)" textLength="190">run</text><text x="475" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="290">failed</text><text x="475" y="140" transform="scale(.1)" textLength="290">failed</text></g> </svg>`
|
||||
// https://img.shields.io/badge/run-inprogress-informational.svg
|
||||
badgeInProgress = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="96" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="a"><rect width="96" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#a)"><path fill="#555" d="M0 0h29v20H0z"/><path fill="#007ec6" d="M29 0h67v20H29z"/><path fill="url(#b)" d="M0 0h96v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="110"> <text x="155" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="190">run</text><text x="155" y="140" transform="scale(.1)" textLength="190">run</text><text x="615" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="570">inprogress</text><text x="615" y="140" transform="scale(.1)" textLength="570">inprogress</text></g> </svg>`
|
||||
// https://img.shields.io/badge/run-error-yellow.svg
|
||||
badgeError = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="66" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="a"><rect width="66" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#a)"><path fill="#555" d="M0 0h29v20H0z"/><path fill="#dfb317" d="M29 0h37v20H29z"/><path fill="url(#b)" d="M0 0h66v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="110"> <text x="155" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="190">run</text><text x="155" y="140" transform="scale(.1)" textLength="190">run</text><text x="465" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="270">error</text><text x="465" y="140" transform="scale(.1)" textLength="270">error</text></g> </svg>`
|
||||
)
|
|
@ -0,0 +1,65 @@
|
|||
// Copyright 2019 Sorint.lab
|
||||
//
|
||||
// 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 api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/sorintlab/agola/internal/services/gateway/action"
|
||||
"github.com/sorintlab/agola/internal/util"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type BadgeRequest struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type BadgeHandler struct {
|
||||
log *zap.SugaredLogger
|
||||
ah *action.ActionHandler
|
||||
}
|
||||
|
||||
func NewBadgeHandler(logger *zap.Logger, ah *action.ActionHandler) *BadgeHandler {
|
||||
return &BadgeHandler{log: logger.Sugar(), ah: ah}
|
||||
}
|
||||
|
||||
func (h *BadgeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
vars := mux.Vars(r)
|
||||
query := r.URL.Query()
|
||||
|
||||
projectRef, err := url.PathUnescape(vars["projectref"])
|
||||
if err != nil {
|
||||
httpError(w, util.NewErrBadRequest(err))
|
||||
return
|
||||
}
|
||||
branch := query.Get("branch")
|
||||
|
||||
badge, err := h.ah.GetBadge(ctx, projectRef, branch)
|
||||
if httpError(w, err) {
|
||||
h.log.Errorf("err: %+v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// TODO(sgotti) return some caching headers
|
||||
w.Header().Set("Content-Type", "image/svg+xml")
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
|
||||
if _, err := w.Write([]byte(badge)); err != nil {
|
||||
h.log.Errorf("err: %+v", err)
|
||||
}
|
||||
}
|
|
@ -200,9 +200,12 @@ func (g *Gateway) Run(ctx context.Context) error {
|
|||
|
||||
logsHandler := api.NewLogsHandler(logger, g.ah)
|
||||
|
||||
reposHandler := api.NewReposHandler(logger, g.c.GitserverURL)
|
||||
userRemoteReposHandler := api.NewUserRemoteReposHandler(logger, g.ah, g.configstoreClient)
|
||||
|
||||
badgeHandler := api.NewBadgeHandler(logger, g.ah)
|
||||
|
||||
reposHandler := api.NewReposHandler(logger, g.c.GitserverURL)
|
||||
|
||||
loginUserHandler := api.NewLoginUserHandler(logger, g.ah)
|
||||
authorizeHandler := api.NewAuthorizeHandler(logger, g.ah)
|
||||
registerHandler := api.NewRegisterUserHandler(logger, g.ah)
|
||||
|
@ -275,11 +278,13 @@ func (g *Gateway) Run(ctx context.Context) error {
|
|||
apirouter.Handle("/runs/{runid}/tasks/{taskid}/actions", authForcedHandler(runTaskActionsHandler)).Methods("PUT")
|
||||
apirouter.Handle("/runs", authForcedHandler(runsHandler)).Methods("GET")
|
||||
|
||||
apirouter.Handle("/user/remoterepos/{remotesourceref}", authForcedHandler(userRemoteReposHandler)).Methods("GET")
|
||||
|
||||
apirouter.Handle("/badges/{projectref}", badgeHandler).Methods("GET")
|
||||
|
||||
// TODO(sgotti) add auth to these requests
|
||||
router.Handle("/repos/{rest:.*}", reposHandler).Methods("GET", "POST")
|
||||
|
||||
apirouter.Handle("/user/remoterepos/{remotesourceref}", authForcedHandler(userRemoteReposHandler)).Methods("GET")
|
||||
|
||||
router.Handle("/login", loginUserHandler).Methods("POST")
|
||||
router.Handle("/authorize", authorizeHandler).Methods("POST")
|
||||
router.Handle("/register", registerHandler).Methods("POST")
|
||||
|
|
|
@ -214,6 +214,10 @@ func (c *Client) GetGroupFirstQueuedRuns(ctx context.Context, group string, chan
|
|||
return c.GetRuns(ctx, []string{"queued"}, []string{group}, false, changeGroups, "", 1, true)
|
||||
}
|
||||
|
||||
func (c *Client) GetGroupLastRun(ctx context.Context, group string, changeGroups []string) (*GetRunsResponse, *http.Response, error) {
|
||||
return c.GetRuns(ctx, nil, []string{group}, false, changeGroups, "", 1, false)
|
||||
}
|
||||
|
||||
func (c *Client) CreateRun(ctx context.Context, req *RunCreateRequest) (*RunResponse, *http.Response, error) {
|
||||
reqj, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue