harness-drone/service/status/status.go

96 lines
2.6 KiB
Go
Raw Normal View History

2019-02-19 23:56:41 +00:00
// Copyright 2019 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 status
import (
"context"
"fmt"
"github.com/drone/drone/core"
"github.com/drone/go-scm/scm"
"github.com/drone/go-scm/scm/driver/github"
2019-02-19 23:56:41 +00:00
)
// Config configures the Status service.
type Config struct {
Base string
Name string
Disabled bool
}
// New returns a new StatusService
func New(client *scm.Client, renew core.Renewer, config Config) core.StatusService {
return &service{
client: client,
renew: renew,
base: config.Base,
name: config.Name,
disabled: config.Disabled,
}
}
type service struct {
renew core.Renewer
client *scm.Client
base string
name string
disabled bool
}
func (s *service) Send(ctx context.Context, user *core.User, req *core.StatusInput) error {
2019-09-04 02:25:18 +00:00
if s.disabled || req.Build.Event == core.EventCron {
2019-02-19 23:56:41 +00:00
return nil
}
err := s.renew.Renew(ctx, user, false)
if err != nil {
return err
}
ctx = context.WithValue(ctx, scm.TokenKey{}, &scm.Token{
Token: user.Token,
Refresh: user.Refresh,
})
// HACK(bradrydzewski) provides support for the github deployment API
if req.Build.DeployID != 0 && s.client.Driver == scm.DriverGithub {
// TODO(bradrydzewski) only update the deployment status when the
// build completes.
if req.Build.Finished == 0 {
return nil
}
_, _, err = s.client.Repositories.(*github.RepositoryService).CreateDeployStatus(ctx, req.Repo.Slug, &scm.DeployStatus{
Number: req.Build.DeployID,
Desc: createDesc(req.Build.Status),
State: convertStatus(req.Build.Status),
Target: fmt.Sprintf("%s/%s/%d", s.base, req.Repo.Slug, req.Build.Number),
Environment: req.Build.Target,
})
return err
}
2019-02-19 23:56:41 +00:00
_, _, err = s.client.Repositories.CreateStatus(ctx, req.Repo.Slug, req.Build.After, &scm.StatusInput{
2019-09-25 19:12:52 +00:00
Title: fmt.Sprintf("Build #%d", req.Build.Number),
2019-02-19 23:56:41 +00:00
Desc: createDesc(req.Build.Status),
Label: createLabel(s.name, req.Build.Event),
State: convertStatus(req.Build.Status),
Target: fmt.Sprintf("%s/%s/%d", s.base, req.Repo.Slug, req.Build.Number),
})
if err == scm.ErrNotSupported {
return nil
}
return err
}