agola/internal/services/notification/commitstatus.go
Simone Gotti d2b09d854f *: use new errors handling library
Implement a new error handling library based on pkg/errors. It provides
stack saving on wrapping and exports some function to add stack saving
also to external errors.
It also implements custom zerolog error formatting without adding too
much verbosity by just printing the chain error file:line without a full
stack trace of every error.

* Add a --detailed-errors options to print error with they full chain
* Wrap all error returns. Use errors.WithStack to wrap without adding a
  new messsage and error.Wrap[f] to add a message.
* Add golangci-lint wrapcheck to check that external packages errors are
  wrapped. This won't check that internal packages error are wrapped.
  But we want also to ensure this case so we'll have to find something
  else to check also these.
2022-02-28 12:49:13 +01:00

135 lines
4.0 KiB
Go

// 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 notification
import (
"context"
"fmt"
"net/url"
"agola.io/agola/internal/errors"
gitsource "agola.io/agola/internal/gitsources"
"agola.io/agola/internal/services/common"
"agola.io/agola/internal/services/gateway/action"
rstypes "agola.io/agola/services/runservice/types"
)
func (n *NotificationService) updateCommitStatus(ctx context.Context, ev *rstypes.RunEvent) error {
var commitStatus gitsource.CommitStatus
if ev.Phase == rstypes.RunPhaseSetupError {
commitStatus = gitsource.CommitStatusError
}
if ev.Phase == rstypes.RunPhaseCancelled {
commitStatus = gitsource.CommitStatusError
}
if ev.Phase == rstypes.RunPhaseRunning && ev.Result == rstypes.RunResultUnknown {
commitStatus = gitsource.CommitStatusPending
}
if ev.Phase == rstypes.RunPhaseFinished && ev.Result != rstypes.RunResultUnknown {
switch ev.Result {
case rstypes.RunResultSuccess:
commitStatus = gitsource.CommitStatusSuccess
case rstypes.RunResultStopped:
fallthrough
case rstypes.RunResultFailed:
commitStatus = gitsource.CommitStatusFailed
}
}
if commitStatus == "" {
return nil
}
run, _, err := n.runserviceClient.GetRun(ctx, ev.RunID, nil)
if err != nil {
return errors.WithStack(err)
}
groupType, groupID, err := common.GroupTypeIDFromRunGroup(run.RunConfig.Group)
if err != nil {
return errors.WithStack(err)
}
// ignore user direct runs
if groupType == common.GroupTypeUser {
return nil
}
project, _, err := n.configstoreClient.GetProject(ctx, groupID)
if err != nil {
return errors.Wrapf(err, "failed to get project %s", groupID)
}
user, _, err := n.configstoreClient.GetUserByLinkedAccount(ctx, project.LinkedAccountID)
if err != nil {
return errors.Wrapf(err, "failed to get user by linked account %q", project.LinkedAccountID)
}
la := user.LinkedAccounts[project.LinkedAccountID]
if la == nil {
return errors.Errorf("linked account %q in user %q doesn't exist", project.LinkedAccountID, user.Name)
}
rs, _, err := n.configstoreClient.GetRemoteSource(ctx, la.RemoteSourceID)
if err != nil {
return errors.Wrapf(err, "failed to get remote source %q", la.RemoteSourceID)
}
// TODO(sgotti) handle refreshing oauth2 tokens
gitSource, err := common.GetGitSource(rs, la)
if err != nil {
return errors.Wrapf(err, "failed to create gitea client")
}
targetURL, err := webRunURL(n.c.WebExposedURL, project.ID, run.Run.ID)
if err != nil {
return errors.Wrapf(err, "failed to generate commit status target url")
}
description := statusDescription(commitStatus)
context := fmt.Sprintf("%s/%s/%s", n.gc.ID, project.Name, run.RunConfig.Name)
if err := gitSource.CreateCommitStatus(project.RepositoryPath, run.Run.Annotations[action.AnnotationCommitSHA], commitStatus, targetURL, description, context); err != nil {
return errors.WithStack(err)
}
return nil
}
func webRunURL(webExposedURL, projectID, runID string) (string, error) {
u, err := url.Parse(webExposedURL + "/run")
if err != nil {
return "", errors.WithStack(err)
}
q := url.Values{}
q.Set("projectref", projectID)
q.Set("runid", runID)
u.RawQuery = q.Encode()
return u.String(), nil
}
func statusDescription(commitStatus gitsource.CommitStatus) string {
switch commitStatus {
case gitsource.CommitStatusPending:
return "The run is pending"
case gitsource.CommitStatusSuccess:
return "The run finished successfully"
case gitsource.CommitStatusError:
return "The run encountered an error"
case gitsource.CommitStatusFailed:
return "The run failed"
default:
return ""
}
}