// 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"
"agola.io/agola/internal/services/common"
"agola.io/agola/internal/util"
rstypes "agola.io/agola/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, _, err := h.configstoreClient.GetProject(ctx, projectRef)
if err != nil {
return "", util.NewAPIError(util.KindFromRemoteError(err), 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, _, err := h.runserviceClient.GetGroupLastRun(ctx, group, nil)
if err != nil {
return "", util.NewAPIError(util.KindFromRemoteError(err), 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 = ``
// https://img.shields.io/badge/run-success-success.svg
badgeSuccess = ``
// https://img.shields.io/badge/run-failed-critical.svg
badgeFailed = ``
// https://img.shields.io/badge/run-inprogress-informational.svg
badgeInProgress = ``
// https://img.shields.io/badge/run-error-yellow.svg
badgeError = ``
)