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"
|
|
|
|
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server"
|
2022-11-04 23:35:06 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/forge"
|
2022-06-15 19:33:29 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/pipeline"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store"
|
2023-03-19 17:32:19 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store/types"
|
2022-06-15 19:33:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func handlePipelineErr(c *gin.Context, err error) {
|
2023-02-01 23:08:02 +00:00
|
|
|
if errors.Is(err, &pipeline.ErrNotFound{}) {
|
|
|
|
c.String(http.StatusNotFound, "%s", err)
|
|
|
|
} else if errors.Is(err, &pipeline.ErrBadRequest{}) {
|
|
|
|
c.String(http.StatusBadRequest, "%s", err)
|
2023-10-08 12:05:06 +00:00
|
|
|
} else if errors.Is(err, pipeline.ErrFiltered) {
|
2023-09-02 11:31:10 +00:00
|
|
|
c.Status(http.StatusNoContent)
|
2022-06-15 19:33:29 +00:00
|
|
|
} else {
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-02 11:31:10 +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) {
|
2022-11-04 23:35:06 +00:00
|
|
|
_forge := server.Config.Services.Forge
|
2022-06-15 19:33:29 +00:00
|
|
|
_store := store.FromContext(c)
|
2023-10-08 12:05:06 +00:00
|
|
|
forge.Refresh(c, _forge, _store, user)
|
2022-06-15 19:33:29 +00:00
|
|
|
}
|