2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2021-06-28 17:28:18 +00:00
|
|
|
// Copyright 2021 Informatyka Boguslawski sp. z o.o. sp.k., http://www.ib.pl/
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// 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
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// 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.
|
2021-06-28 17:28:18 +00:00
|
|
|
//
|
|
|
|
// This file has been modified by Informatyka Boguslawski sp. z o.o. sp.k.
|
2018-02-19 22:24:10 +00:00
|
|
|
|
2021-09-22 18:48:01 +00:00
|
|
|
package api
|
2015-04-08 22:43:59 +00:00
|
|
|
|
|
|
|
import (
|
2017-03-16 10:14:02 +00:00
|
|
|
"context"
|
2017-05-05 16:59:37 +00:00
|
|
|
"crypto/sha256"
|
2017-03-16 10:14:02 +00:00
|
|
|
"encoding/json"
|
2015-09-30 01:21:17 +00:00
|
|
|
"fmt"
|
2017-05-12 13:02:24 +00:00
|
|
|
"math/rand"
|
2019-06-28 06:29:57 +00:00
|
|
|
"net/http"
|
2017-03-16 10:14:02 +00:00
|
|
|
"regexp"
|
2016-09-26 05:39:28 +00:00
|
|
|
"strconv"
|
2017-03-16 10:14:02 +00:00
|
|
|
"time"
|
2016-01-10 23:19:48 +00:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2015-04-08 22:43:59 +00:00
|
|
|
|
2021-06-22 10:34:35 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-09-27 17:51:55 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2021-05-25 12:08:27 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/shared/token"
|
2017-03-16 10:14:02 +00:00
|
|
|
|
2021-09-24 11:18:34 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/rpc"
|
2021-09-22 18:48:01 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server"
|
2021-09-23 20:29:09 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/pubsub"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/queue"
|
2021-09-23 16:25:51 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/remote"
|
2021-09-22 18:48:01 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/shared"
|
2021-09-23 11:33:59 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store"
|
2015-04-08 22:43:59 +00:00
|
|
|
)
|
|
|
|
|
2017-03-16 10:14:02 +00:00
|
|
|
var skipRe = regexp.MustCompile(`\[(?i:ci *skip|skip *ci)\]`)
|
|
|
|
|
2017-05-12 13:02:24 +00:00
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
}
|
|
|
|
|
2017-03-16 10:14:02 +00:00
|
|
|
func GetQueueInfo(c *gin.Context) {
|
|
|
|
c.IndentedJSON(200,
|
2021-09-22 18:48:01 +00:00
|
|
|
server.Config.Services.Queue.Info(c),
|
2017-03-16 10:14:02 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-06-28 06:29:57 +00:00
|
|
|
func PauseQueue(c *gin.Context) {
|
2021-09-22 18:48:01 +00:00
|
|
|
server.Config.Services.Queue.Pause()
|
2019-06-28 06:29:57 +00:00
|
|
|
c.Status(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ResumeQueue(c *gin.Context) {
|
2021-09-22 18:48:01 +00:00
|
|
|
server.Config.Services.Queue.Resume()
|
2019-06-28 06:29:57 +00:00
|
|
|
c.Status(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2019-06-28 06:42:06 +00:00
|
|
|
func BlockTilQueueHasRunningItem(c *gin.Context) {
|
|
|
|
for {
|
2021-09-22 18:48:01 +00:00
|
|
|
info := server.Config.Services.Queue.Info(c)
|
2019-06-28 06:42:06 +00:00
|
|
|
if info.Stats.Running == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.Status(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2015-04-08 22:43:59 +00:00
|
|
|
func PostHook(c *gin.Context) {
|
2015-10-21 23:14:02 +00:00
|
|
|
remote_ := remote.FromContext(c)
|
2015-04-08 22:43:59 +00:00
|
|
|
|
2021-09-27 21:32:08 +00:00
|
|
|
tmpRepo, build, err := remote_.Hook(c.Request)
|
2015-04-08 22:43:59 +00:00
|
|
|
if err != nil {
|
2017-03-16 10:14:02 +00:00
|
|
|
logrus.Errorf("failure to parse hook. %s", err)
|
2015-09-30 01:21:17 +00:00
|
|
|
c.AbortWithError(400, err)
|
2015-04-08 22:43:59 +00:00
|
|
|
return
|
|
|
|
}
|
2015-09-30 01:21:17 +00:00
|
|
|
if build == nil {
|
2015-04-08 22:43:59 +00:00
|
|
|
c.Writer.WriteHeader(200)
|
|
|
|
return
|
|
|
|
}
|
2021-09-27 21:32:08 +00:00
|
|
|
if tmpRepo == nil {
|
2017-03-16 10:14:02 +00:00
|
|
|
logrus.Errorf("failure to ascertain repo from hook.")
|
2015-04-08 22:43:59 +00:00
|
|
|
c.Writer.WriteHeader(400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-11 00:44:14 +00:00
|
|
|
// skip the build if any case-insensitive combination of the words "skip" and "ci"
|
|
|
|
// wrapped in square brackets appear in the commit message
|
2015-12-11 01:12:27 +00:00
|
|
|
skipMatch := skipRe.FindString(build.Message)
|
|
|
|
if len(skipMatch) > 0 {
|
2017-03-16 10:14:02 +00:00
|
|
|
logrus.Infof("ignoring hook. %s found in %s", skipMatch, build.Commit)
|
2015-04-08 22:43:59 +00:00
|
|
|
c.Writer.WriteHeader(204)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-27 21:32:08 +00:00
|
|
|
repo, err := store.GetRepoOwnerName(c, tmpRepo.Owner, tmpRepo.Name)
|
2015-04-08 22:43:59 +00:00
|
|
|
if err != nil {
|
2021-09-27 21:32:08 +00:00
|
|
|
logrus.Errorf("failure to find repo %s/%s from hook. %s", tmpRepo.Owner, tmpRepo.Name, err)
|
2015-09-30 01:21:17 +00:00
|
|
|
c.AbortWithError(404, err)
|
2015-04-08 22:43:59 +00:00
|
|
|
return
|
|
|
|
}
|
2017-07-14 19:58:38 +00:00
|
|
|
if !repo.IsActive {
|
2021-09-27 21:32:08 +00:00
|
|
|
logrus.Errorf("ignoring hook. %s/%s is inactive.", tmpRepo.Owner, tmpRepo.Name)
|
2017-07-14 19:58:38 +00:00
|
|
|
c.AbortWithError(204, err)
|
|
|
|
return
|
|
|
|
}
|
2015-04-08 22:43:59 +00:00
|
|
|
|
2015-08-11 08:36:07 +00:00
|
|
|
// get the token and verify the hook is authorized
|
2015-09-09 21:05:52 +00:00
|
|
|
parsed, err := token.ParseRequest(c.Request, func(t *token.Token) (string, error) {
|
|
|
|
return repo.Hash, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2017-03-16 10:14:02 +00:00
|
|
|
logrus.Errorf("failure to parse token from hook for %s. %s", repo.FullName, err)
|
2015-09-30 01:21:17 +00:00
|
|
|
c.AbortWithError(400, err)
|
2015-09-09 21:05:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if parsed.Text != repo.FullName {
|
2017-03-16 10:14:02 +00:00
|
|
|
logrus.Errorf("failure to verify token from hook. Expected %s, got %s", repo.FullName, parsed.Text)
|
2015-08-11 08:36:07 +00:00
|
|
|
c.AbortWithStatus(403)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
if repo.UserID == 0 {
|
2017-03-16 10:14:02 +00:00
|
|
|
logrus.Warnf("ignoring hook. repo %s has no owner.", repo.FullName)
|
2015-04-11 22:46:30 +00:00
|
|
|
c.Writer.WriteHeader(204)
|
|
|
|
return
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
|
2021-09-18 14:28:35 +00:00
|
|
|
if build.Event == model.EventPull && !repo.AllowPull {
|
|
|
|
logrus.Infof("ignoring hook. repo %s is disabled for pull requests.", repo.FullName)
|
2021-09-27 21:32:08 +00:00
|
|
|
c.Writer.Write([]byte("pulls are disabled on woodpecker for this repo"))
|
2015-04-08 22:43:59 +00:00
|
|
|
c.Writer.WriteHeader(204)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-21 23:14:02 +00:00
|
|
|
user, err := store.GetUser(c, repo.UserID)
|
2015-04-08 22:43:59 +00:00
|
|
|
if err != nil {
|
2017-03-16 10:14:02 +00:00
|
|
|
logrus.Errorf("failure to find repo owner %s. %s", repo.FullName, err)
|
2015-09-30 01:21:17 +00:00
|
|
|
c.AbortWithError(500, err)
|
2015-04-08 22:43:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-05 00:40:27 +00:00
|
|
|
// if the remote has a refresh token, the current access token
|
|
|
|
// may be stale. Therefore, we should refresh prior to dispatching
|
2017-04-02 14:13:26 +00:00
|
|
|
// the build.
|
2015-10-05 00:40:27 +00:00
|
|
|
if refresher, ok := remote_.(remote.Refresher); ok {
|
2021-09-28 10:56:59 +00:00
|
|
|
ok, err := refresher.Refresh(c, user)
|
2021-09-27 21:32:08 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("failed to refresh oauth2 token: %s", err)
|
|
|
|
} else if ok {
|
|
|
|
if err := store.UpdateUser(c, user); err != nil {
|
|
|
|
logrus.Errorf("error while updating user: %s", err)
|
|
|
|
// move forward
|
|
|
|
}
|
2015-10-05 00:40:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-01 10:52:02 +00:00
|
|
|
// fetch the build file from the remote
|
2021-09-22 18:48:01 +00:00
|
|
|
configFetcher := shared.NewConfigFetcher(remote_, user, repo, build)
|
2021-09-28 10:56:59 +00:00
|
|
|
remoteYamlConfigs, err := configFetcher.Fetch(c)
|
2015-04-08 22:43:59 +00:00
|
|
|
if err != nil {
|
2017-07-31 19:15:05 +00:00
|
|
|
logrus.Errorf("error: %s: cannot find %s in %s: %s", repo.FullName, repo.Config, build.Ref, err)
|
2015-09-30 01:21:17 +00:00
|
|
|
c.AbortWithError(404, err)
|
2015-04-08 22:43:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-09 07:53:54 +00:00
|
|
|
filtered, err := branchFiltered(build, remoteYamlConfigs)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("failure to parse yaml from hook for %s. %s", repo.FullName, err)
|
|
|
|
c.AbortWithError(400, err)
|
|
|
|
}
|
|
|
|
if filtered {
|
2019-06-19 07:36:54 +00:00
|
|
|
c.String(200, "Branch does not match restrictions defined in yaml")
|
|
|
|
return
|
|
|
|
}
|
2019-06-04 13:04:18 +00:00
|
|
|
|
2019-07-22 11:43:18 +00:00
|
|
|
if zeroSteps(build, remoteYamlConfigs) {
|
2019-07-19 08:13:02 +00:00
|
|
|
c.String(200, "Step conditions yield zero runnable steps")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-01 10:59:29 +00:00
|
|
|
// update some build fields
|
|
|
|
build.RepoID = repo.ID
|
|
|
|
build.Verified = true
|
|
|
|
build.Status = model.StatusPending
|
|
|
|
|
2019-07-18 12:21:05 +00:00
|
|
|
if repo.IsGated && build.Sender != user.Login {
|
|
|
|
build.Status = model.StatusBlocked
|
|
|
|
}
|
|
|
|
|
2017-04-11 17:06:45 +00:00
|
|
|
err = store.CreateBuild(c, build, build.Procs...)
|
|
|
|
if err != nil {
|
2017-03-16 10:14:02 +00:00
|
|
|
logrus.Errorf("failure to save commit for %s. %s", repo.FullName, err)
|
2015-09-30 01:21:17 +00:00
|
|
|
c.AbortWithError(500, err)
|
2015-04-24 21:25:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-11 08:50:50 +00:00
|
|
|
// persist the build config for historical correctness, restarts, etc
|
|
|
|
for _, remoteYamlConfig := range remoteYamlConfigs {
|
2021-08-29 20:25:41 +00:00
|
|
|
_, err := findOrPersistPipelineConfig(repo, build, remoteYamlConfig)
|
2019-06-11 08:50:50 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("failure to find or persist build config for %s. %s", repo.FullName, err)
|
|
|
|
c.AbortWithError(500, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 03:45:08 +00:00
|
|
|
c.JSON(200, build)
|
2015-05-06 03:59:07 +00:00
|
|
|
|
2017-04-11 17:06:45 +00:00
|
|
|
if build.Status == model.StatusBlocked {
|
|
|
|
return
|
|
|
|
}
|
2017-03-18 11:25:53 +00:00
|
|
|
|
2019-06-01 10:59:29 +00:00
|
|
|
netrc, err := remote_.Netrc(user, repo)
|
|
|
|
if err != nil {
|
|
|
|
c.String(500, "Failed to generate netrc file. %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-26 19:27:53 +00:00
|
|
|
envs := map[string]string{}
|
2021-09-22 18:48:01 +00:00
|
|
|
if server.Config.Services.Environ != nil {
|
|
|
|
globals, _ := server.Config.Services.Environ.EnvironList(repo)
|
2017-06-26 19:27:53 +00:00
|
|
|
for _, global := range globals {
|
|
|
|
envs[global.Name] = global.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-22 18:48:01 +00:00
|
|
|
secs, err := server.Config.Services.Secrets.SecretListBuild(repo, build)
|
2017-05-19 21:36:08 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Debugf("Error getting secrets for %s#%d. %s", repo.FullName, build.Number, err)
|
|
|
|
}
|
|
|
|
|
2021-09-22 18:48:01 +00:00
|
|
|
regs, err := server.Config.Services.Registries.RegistryList(repo)
|
2017-05-19 21:36:08 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Debugf("Error getting registry credentials for %s#%d. %s", repo.FullName, build.Number, err)
|
|
|
|
}
|
|
|
|
|
2019-06-01 10:59:29 +00:00
|
|
|
// get the previous build so that we can send status change notifications
|
2015-10-21 23:14:02 +00:00
|
|
|
last, _ := store.GetBuildLastBefore(c, repo, build.Branch, build.ID)
|
2015-09-10 17:39:18 +00:00
|
|
|
|
2021-09-22 18:48:01 +00:00
|
|
|
b := shared.ProcBuilder{
|
2017-03-16 10:14:02 +00:00
|
|
|
Repo: repo,
|
|
|
|
Curr: build,
|
|
|
|
Last: last,
|
|
|
|
Netrc: netrc,
|
|
|
|
Secs: secs,
|
2017-04-06 16:04:25 +00:00
|
|
|
Regs: regs,
|
2017-06-26 19:27:53 +00:00
|
|
|
Envs: envs,
|
2021-09-22 18:48:01 +00:00
|
|
|
Link: server.Config.Server.Host,
|
2019-06-13 15:38:19 +00:00
|
|
|
Yamls: remoteYamlConfigs,
|
2017-03-16 10:14:02 +00:00
|
|
|
}
|
2019-06-01 08:08:41 +00:00
|
|
|
buildItems, err := b.Build()
|
2017-03-16 10:14:02 +00:00
|
|
|
if err != nil {
|
2021-09-22 18:48:01 +00:00
|
|
|
if _, err = shared.UpdateToStatusError(store.FromContext(c), *build, err); err != nil {
|
2019-09-14 12:12:04 +00:00
|
|
|
logrus.Errorf("Error setting error status of build for %s#%d. %s", repo.FullName, build.Number, err)
|
|
|
|
}
|
2017-03-16 10:14:02 +00:00
|
|
|
return
|
|
|
|
}
|
2021-09-22 18:48:01 +00:00
|
|
|
build = shared.SetBuildStepsOnBuild(b.Curr, buildItems)
|
2017-03-16 10:14:02 +00:00
|
|
|
|
2019-06-01 08:08:41 +00:00
|
|
|
err = store.FromContext(c).ProcCreate(build.Procs)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error persisting procs %s/%d: %s", repo.FullName, build.Number, err)
|
|
|
|
}
|
|
|
|
|
2019-06-17 08:48:40 +00:00
|
|
|
defer func() {
|
|
|
|
for _, item := range buildItems {
|
2021-09-22 18:48:01 +00:00
|
|
|
uri := fmt.Sprintf("%s/%s/%d", server.Config.Server.Host, repo.FullName, build.Number)
|
2019-06-17 08:48:40 +00:00
|
|
|
if len(buildItems) > 1 {
|
2021-09-28 10:56:59 +00:00
|
|
|
err = remote_.Status(c, user, repo, build, uri, item.Proc)
|
2019-06-17 08:48:40 +00:00
|
|
|
} else {
|
2021-09-28 10:56:59 +00:00
|
|
|
err = remote_.Status(c, user, repo, build, uri, nil)
|
2019-06-17 08:48:40 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error setting commit status for %s/%d: %v", repo.FullName, build.Number, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-09-16 13:18:15 +00:00
|
|
|
publishToTopic(c, build, repo, model.Enqueued)
|
2019-06-01 08:08:41 +00:00
|
|
|
queueBuild(build, repo, buildItems)
|
|
|
|
}
|
|
|
|
|
2021-09-27 21:32:08 +00:00
|
|
|
// TODO: parse yaml once and not for each filter function
|
2019-09-09 07:53:54 +00:00
|
|
|
func branchFiltered(build *model.Build, remoteYamlConfigs []*remote.FileMeta) (bool, error) {
|
2021-09-27 21:32:08 +00:00
|
|
|
logrus.Tracef("hook.branchFiltered(): build branch: '%s' build event: '%s' config count: %d", build.Branch, build.Event, len(remoteYamlConfigs))
|
2019-06-19 07:36:54 +00:00
|
|
|
for _, remoteYamlConfig := range remoteYamlConfigs {
|
|
|
|
parsedPipelineConfig, err := yaml.ParseString(string(remoteYamlConfig.Data))
|
2019-09-09 07:53:54 +00:00
|
|
|
if err != nil {
|
2021-09-27 21:32:08 +00:00
|
|
|
logrus.Tracef("parse config '%s': %s", remoteYamlConfig.Name, err)
|
2019-09-09 07:53:54 +00:00
|
|
|
return false, err
|
|
|
|
}
|
2021-09-27 21:32:08 +00:00
|
|
|
logrus.Tracef("config '%s': %#v", remoteYamlConfig.Name, parsedPipelineConfig)
|
2019-09-09 07:53:54 +00:00
|
|
|
|
|
|
|
if !parsedPipelineConfig.Branches.Match(build.Branch) && build.Event != model.EventTag && build.Event != model.EventDeploy {
|
|
|
|
} else {
|
|
|
|
return false, nil
|
2019-06-19 07:36:54 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-09 07:53:54 +00:00
|
|
|
return true, nil
|
2019-06-19 07:36:54 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 11:43:18 +00:00
|
|
|
func zeroSteps(build *model.Build, remoteYamlConfigs []*remote.FileMeta) bool {
|
2021-09-22 18:48:01 +00:00
|
|
|
b := shared.ProcBuilder{
|
2019-07-19 08:13:02 +00:00
|
|
|
Repo: &model.Repo{},
|
2019-07-22 11:43:18 +00:00
|
|
|
Curr: build,
|
2019-07-19 08:13:02 +00:00
|
|
|
Last: &model.Build{},
|
|
|
|
Netrc: &model.Netrc{},
|
|
|
|
Secs: []*model.Secret{},
|
|
|
|
Regs: []*model.Registry{},
|
|
|
|
Link: "",
|
|
|
|
Yamls: remoteYamlConfigs,
|
|
|
|
}
|
|
|
|
|
|
|
|
buildItems, err := b.Build()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(buildItems) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-08-29 20:25:41 +00:00
|
|
|
func findOrPersistPipelineConfig(repo *model.Repo, build *model.Build, remoteYamlConfig *remote.FileMeta) (*model.Config, error) {
|
2019-06-13 15:38:19 +00:00
|
|
|
sha := shasum(remoteYamlConfig.Data)
|
2021-09-22 18:48:01 +00:00
|
|
|
conf, err := server.Config.Storage.Config.ConfigFindIdentical(build.RepoID, sha)
|
2019-06-01 10:52:02 +00:00
|
|
|
if err != nil {
|
|
|
|
conf = &model.Config{
|
2019-06-11 08:50:50 +00:00
|
|
|
RepoID: build.RepoID,
|
2019-06-13 15:38:19 +00:00
|
|
|
Data: string(remoteYamlConfig.Data),
|
2019-06-01 10:52:02 +00:00
|
|
|
Hash: sha,
|
2021-09-22 18:48:01 +00:00
|
|
|
Name: shared.SanitizePath(remoteYamlConfig.Name),
|
2019-06-01 10:52:02 +00:00
|
|
|
}
|
2021-09-22 18:48:01 +00:00
|
|
|
err = server.Config.Storage.Config.ConfigCreate(conf)
|
2019-06-01 10:52:02 +00:00
|
|
|
if err != nil {
|
|
|
|
// retry in case we receive two hooks at the same time
|
2021-09-22 18:48:01 +00:00
|
|
|
conf, err = server.Config.Storage.Config.ConfigFindIdentical(build.RepoID, sha)
|
2019-06-01 10:52:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 08:50:50 +00:00
|
|
|
buildConfig := &model.BuildConfig{
|
|
|
|
ConfigID: conf.ID,
|
|
|
|
BuildID: build.ID,
|
|
|
|
}
|
2021-09-22 18:48:01 +00:00
|
|
|
err = server.Config.Storage.Config.BuildConfigCreate(buildConfig)
|
2019-06-11 08:50:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-01 10:52:02 +00:00
|
|
|
return conf, nil
|
|
|
|
}
|
|
|
|
|
2019-06-13 15:38:19 +00:00
|
|
|
// publishes message to UI clients
|
2019-09-16 13:18:15 +00:00
|
|
|
func publishToTopic(c *gin.Context, build *model.Build, repo *model.Repo, event model.EventType) {
|
2017-03-16 10:14:02 +00:00
|
|
|
message := pubsub.Message{
|
|
|
|
Labels: map[string]string{
|
|
|
|
"repo": repo.FullName,
|
|
|
|
"private": strconv.FormatBool(repo.IsPrivate),
|
|
|
|
},
|
|
|
|
}
|
2017-04-04 09:30:06 +00:00
|
|
|
buildCopy := *build
|
|
|
|
buildCopy.Procs = model.Tree(buildCopy.Procs)
|
2017-03-16 10:14:02 +00:00
|
|
|
message.Data, _ = json.Marshal(model.Event{
|
2016-09-28 01:30:28 +00:00
|
|
|
Type: model.Enqueued,
|
2016-09-26 05:39:28 +00:00
|
|
|
Repo: *repo,
|
2017-04-04 09:30:06 +00:00
|
|
|
Build: buildCopy,
|
2017-03-16 10:14:02 +00:00
|
|
|
})
|
2021-09-22 18:48:01 +00:00
|
|
|
server.Config.Services.Pubsub.Publish(c, "topic/events", message)
|
2019-06-01 08:08:41 +00:00
|
|
|
}
|
2016-09-26 05:39:28 +00:00
|
|
|
|
2021-09-22 18:48:01 +00:00
|
|
|
func queueBuild(build *model.Build, repo *model.Repo, buildItems []*shared.BuildItem) {
|
2019-06-13 15:38:19 +00:00
|
|
|
var tasks []*queue.Task
|
2019-06-01 08:08:41 +00:00
|
|
|
for _, item := range buildItems {
|
2019-06-19 07:36:54 +00:00
|
|
|
if item.Proc.State == model.StatusSkipped {
|
|
|
|
continue
|
|
|
|
}
|
2017-03-16 10:14:02 +00:00
|
|
|
task := new(queue.Task)
|
2017-04-02 14:13:26 +00:00
|
|
|
task.ID = fmt.Sprint(item.Proc.ID)
|
2017-03-16 10:14:02 +00:00
|
|
|
task.Labels = map[string]string{}
|
|
|
|
for k, v := range item.Labels {
|
|
|
|
task.Labels[k] = v
|
|
|
|
}
|
2017-09-08 22:28:25 +00:00
|
|
|
task.Labels["platform"] = item.Platform
|
2019-06-01 08:08:41 +00:00
|
|
|
task.Labels["repo"] = repo.FullName
|
2019-06-13 15:38:19 +00:00
|
|
|
task.Dependencies = taskIds(item.DependsOn, buildItems)
|
2019-06-17 07:06:36 +00:00
|
|
|
task.RunOn = item.RunsOn
|
2019-07-22 10:43:59 +00:00
|
|
|
task.DepStatus = make(map[string]string)
|
2017-03-16 10:14:02 +00:00
|
|
|
|
|
|
|
task.Data, _ = json.Marshal(rpc.Pipeline{
|
2017-04-02 14:13:26 +00:00
|
|
|
ID: fmt.Sprint(item.Proc.ID),
|
2017-03-16 10:14:02 +00:00
|
|
|
Config: item.Config,
|
2019-06-01 08:08:41 +00:00
|
|
|
Timeout: repo.Timeout,
|
2017-03-16 10:14:02 +00:00
|
|
|
})
|
|
|
|
|
2021-09-22 18:48:01 +00:00
|
|
|
server.Config.Services.Logs.Open(context.Background(), task.ID)
|
2019-06-13 15:38:19 +00:00
|
|
|
tasks = append(tasks, task)
|
|
|
|
}
|
2021-09-22 18:48:01 +00:00
|
|
|
server.Config.Services.Queue.PushAtOnce(context.Background(), tasks)
|
2019-06-13 15:38:19 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 14:29:26 +00:00
|
|
|
func taskIds(dependsOn []string, buildItems []*shared.BuildItem) (taskIds []string) {
|
2019-06-13 15:38:19 +00:00
|
|
|
for _, dep := range dependsOn {
|
|
|
|
for _, buildItem := range buildItems {
|
|
|
|
if buildItem.Proc.Name == dep {
|
|
|
|
taskIds = append(taskIds, fmt.Sprint(buildItem.Proc.ID))
|
|
|
|
}
|
|
|
|
}
|
2016-04-18 19:07:01 +00:00
|
|
|
}
|
2021-09-24 14:29:26 +00:00
|
|
|
return
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
2017-03-16 11:00:56 +00:00
|
|
|
|
2017-05-05 16:59:37 +00:00
|
|
|
func shasum(raw []byte) string {
|
|
|
|
sum := sha256.Sum256(raw)
|
|
|
|
return fmt.Sprintf("%x", sum)
|
|
|
|
}
|