2022-06-15 19:33:29 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// 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 (
|
2023-02-01 23:08:02 +00:00
|
|
|
"errors"
|
2022-06-15 19:33:29 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-04-16 06:04:55 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2022-06-15 19:33:29 +00:00
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/forge"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/pipeline"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store/types"
|
2022-06-15 19:33:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func handlePipelineErr(c *gin.Context, err error) {
|
2024-01-10 14:34:44 +00:00
|
|
|
switch {
|
|
|
|
case errors.Is(err, &pipeline.ErrNotFound{}):
|
2023-02-01 23:08:02 +00:00
|
|
|
c.String(http.StatusNotFound, "%s", err)
|
2024-01-10 14:34:44 +00:00
|
|
|
case errors.Is(err, &pipeline.ErrBadRequest{}):
|
2023-02-01 23:08:02 +00:00
|
|
|
c.String(http.StatusBadRequest, "%s", err)
|
2024-01-10 14:34:44 +00:00
|
|
|
case errors.Is(err, pipeline.ErrFiltered):
|
2023-12-21 10:13:25 +00:00
|
|
|
// for debugging purpose we add a header
|
|
|
|
c.Writer.Header().Add("Pipeline-Filtered", "true")
|
2023-09-02 11:31:10 +00:00
|
|
|
c.Status(http.StatusNoContent)
|
2024-01-10 14:34:44 +00:00
|
|
|
default:
|
2022-06-15 19:33:29 +00:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-10 21:56:42 +00:00
|
|
|
func handleDBError(c *gin.Context, err error) {
|
2023-03-19 12:52:58 +00:00
|
|
|
if errors.Is(err, types.RecordNotExist) {
|
|
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
|
2023-10-08 12:05:06 +00:00
|
|
|
// If the forge has a refresh token, the current access token may be stale.
|
2022-06-15 19:33:29 +00:00
|
|
|
// Therefore, we should refresh prior to dispatching the job.
|
|
|
|
func refreshUserToken(c *gin.Context, user *model.User) {
|
|
|
|
_store := store.FromContext(c)
|
2024-04-16 06:04:55 +00:00
|
|
|
_forge, err := server.Config.Services.Manager.ForgeFromUser(user)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Cannot get forge from user")
|
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2023-10-08 12:05:06 +00:00
|
|
|
forge.Refresh(c, _forge, _store, user)
|
2022-06-15 19:33:29 +00:00
|
|
|
}
|