2022-10-18 03:24:12 +02:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
2018-02-20 00:24:10 +02:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02: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 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02: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-09-22 20:48:01 +02:00
|
|
|
package api
|
2015-09-30 03:21:17 +02:00
|
|
|
|
|
|
|
import (
|
2016-05-03 02:47:58 +02:00
|
|
|
"encoding/base32"
|
2023-03-22 00:01:59 +02:00
|
|
|
"errors"
|
2015-09-30 03:21:17 +02:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2017-07-14 21:58:38 +02:00
|
|
|
"strconv"
|
2023-03-22 00:01:59 +02:00
|
|
|
"time"
|
2015-09-30 03:21:17 +02:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2016-05-03 02:47:58 +02:00
|
|
|
"github.com/gorilla/securecookie"
|
2021-11-23 16:36:52 +02:00
|
|
|
"github.com/rs/zerolog/log"
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2021-09-22 20:48:01 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server"
|
2021-09-27 19:51:55 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2021-09-22 22:41:32 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/router/middleware/session"
|
2021-09-23 13:33:59 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store"
|
2023-03-22 00:01:59 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store/types"
|
2021-05-25 14:08:27 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/shared/token"
|
2015-09-30 03:21:17 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func PostRepo(c *gin.Context) {
|
2022-11-05 01:35:06 +02:00
|
|
|
forge := server.Config.Services.Forge
|
2021-12-01 15:22:06 +02:00
|
|
|
_store := store.FromContext(c)
|
2015-09-30 03:21:17 +02:00
|
|
|
user := session.User(c)
|
|
|
|
|
2023-03-22 00:01:59 +02:00
|
|
|
owner := c.Param("owner")
|
|
|
|
name := c.Param("name")
|
|
|
|
repo, err := _store.GetRepoName(owner + "/" + name)
|
|
|
|
enabledOnce := err == nil // if there's no error, the repo was found and enabled once already
|
|
|
|
if enabledOnce && repo.IsActive {
|
2021-11-27 17:06:00 +02:00
|
|
|
c.String(http.StatusConflict, "Repository is already active.")
|
2015-09-30 03:21:17 +02:00
|
|
|
return
|
2023-03-22 00:01:59 +02:00
|
|
|
} else if err != nil && !errors.Is(err, types.RecordNotExist) {
|
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
|
|
|
|
2023-03-22 00:01:59 +02:00
|
|
|
from, err := forge.Repo(c, user, "0", owner, name)
|
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusInternalServerError, "Could not fetch repository from forge.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !from.Perm.Admin {
|
|
|
|
c.String(http.StatusForbidden, "User has to be a admin of this repository")
|
|
|
|
}
|
|
|
|
|
|
|
|
if enabledOnce {
|
|
|
|
repo.Update(from)
|
|
|
|
} else {
|
|
|
|
repo = from
|
|
|
|
repo.AllowPull = true
|
|
|
|
repo.NetrcOnlyTrusted = true
|
|
|
|
repo.CancelPreviousPipelineEvents = server.Config.Pipeline.DefaultCancelPreviousPipelineEvents
|
|
|
|
}
|
2017-07-14 21:58:38 +02:00
|
|
|
repo.IsActive = true
|
|
|
|
repo.UserID = user.ID
|
2021-09-18 16:28:35 +02:00
|
|
|
|
2017-07-14 21:58:38 +02:00
|
|
|
if repo.Visibility == "" {
|
|
|
|
repo.Visibility = model.VisibilityPublic
|
2021-11-22 13:55:13 +02:00
|
|
|
if repo.IsSCMPrivate {
|
2017-07-14 21:58:38 +02:00
|
|
|
repo.Visibility = model.VisibilityPrivate
|
|
|
|
}
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
2021-09-18 16:28:35 +02:00
|
|
|
|
2017-07-14 21:58:38 +02:00
|
|
|
if repo.Timeout == 0 {
|
2023-03-19 21:24:43 +02:00
|
|
|
repo.Timeout = server.Config.Pipeline.DefaultTimeout
|
|
|
|
} else if repo.Timeout > server.Config.Pipeline.MaxTimeout {
|
|
|
|
repo.Timeout = server.Config.Pipeline.MaxTimeout
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
2021-09-18 16:28:35 +02:00
|
|
|
|
2017-07-14 21:58:38 +02:00
|
|
|
if repo.Hash == "" {
|
|
|
|
repo.Hash = base32.StdEncoding.EncodeToString(
|
|
|
|
securecookie.GenerateRandomKey(32),
|
|
|
|
)
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
|
|
|
|
2017-07-14 21:58:38 +02:00
|
|
|
// creates the jwt token used to verify the repository
|
|
|
|
t := token.New(token.HookToken, repo.FullName)
|
|
|
|
sig, err := t.Sign(repo.Hash)
|
2015-09-30 03:21:17 +02:00
|
|
|
if err != nil {
|
2021-11-27 17:06:00 +02:00
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
2015-09-30 03:21:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
link := fmt.Sprintf(
|
|
|
|
"%s/hook?access_token=%s",
|
2021-09-22 20:48:01 +02:00
|
|
|
server.Config.Server.Host,
|
2015-09-30 03:21:17 +02:00
|
|
|
sig,
|
|
|
|
)
|
|
|
|
|
2022-11-05 01:35:06 +02:00
|
|
|
err = forge.Activate(c, user, repo, link)
|
2015-09-30 03:21:17 +02:00
|
|
|
if err != nil {
|
2021-11-27 17:06:00 +02:00
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
2015-09-30 03:21:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-22 00:01:59 +02:00
|
|
|
if enabledOnce {
|
|
|
|
err = _store.UpdateRepo(repo)
|
|
|
|
} else {
|
|
|
|
err = _store.CreateRepo(repo)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
repo.Perm = from.Perm
|
|
|
|
repo.Perm.Synced = time.Now().Unix()
|
|
|
|
repo.Perm.UserID = user.ID
|
|
|
|
repo.Perm.RepoID = repo.ID
|
|
|
|
repo.Perm.Repo = repo
|
|
|
|
err = _store.PermUpsert(repo.Perm)
|
2015-09-30 03:21:17 +02:00
|
|
|
if err != nil {
|
2021-11-27 17:06:00 +02:00
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
2015-09-30 03:21:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-27 17:06:00 +02:00
|
|
|
c.JSON(http.StatusOK, repo)
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func PatchRepo(c *gin.Context) {
|
2021-12-01 15:22:06 +02:00
|
|
|
_store := store.FromContext(c)
|
2015-09-30 03:21:17 +02:00
|
|
|
repo := session.Repo(c)
|
|
|
|
user := session.User(c)
|
|
|
|
|
2017-04-12 14:12:21 +02:00
|
|
|
in := new(model.RepoPatch)
|
2015-09-30 03:21:17 +02:00
|
|
|
if err := c.Bind(in); err != nil {
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(http.StatusBadRequest, err)
|
2015-09-30 03:21:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-19 21:24:43 +02:00
|
|
|
if in.Timeout != nil && *in.Timeout > server.Config.Pipeline.MaxTimeout && !user.Admin {
|
|
|
|
c.String(http.StatusForbidden, fmt.Sprintf("Timeout is not allowed to be higher than max timeout (%dmin)", server.Config.Pipeline.MaxTimeout))
|
2021-11-27 17:06:00 +02:00
|
|
|
}
|
|
|
|
if in.IsTrusted != nil && *in.IsTrusted != repo.IsTrusted && !user.Admin {
|
|
|
|
log.Trace().Msgf("user '%s' wants to make repo trusted without being an instance admin ", user.Login)
|
|
|
|
c.String(http.StatusForbidden, "Insufficient privileges")
|
2017-04-11 19:06:45 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
if in.AllowPull != nil {
|
|
|
|
repo.AllowPull = *in.AllowPull
|
|
|
|
}
|
2017-04-11 19:06:45 +02:00
|
|
|
if in.IsGated != nil {
|
|
|
|
repo.IsGated = *in.IsGated
|
|
|
|
}
|
|
|
|
if in.IsTrusted != nil {
|
2015-09-30 03:21:17 +02:00
|
|
|
repo.IsTrusted = *in.IsTrusted
|
|
|
|
}
|
2017-04-11 19:06:45 +02:00
|
|
|
if in.Timeout != nil {
|
2015-09-30 03:21:17 +02:00
|
|
|
repo.Timeout = *in.Timeout
|
|
|
|
}
|
2017-04-12 14:12:21 +02:00
|
|
|
if in.Config != nil {
|
|
|
|
repo.Config = *in.Config
|
|
|
|
}
|
2022-05-09 11:26:09 +02:00
|
|
|
if in.CancelPreviousPipelineEvents != nil {
|
|
|
|
repo.CancelPreviousPipelineEvents = *in.CancelPreviousPipelineEvents
|
|
|
|
}
|
2023-03-20 22:17:49 +02:00
|
|
|
if in.NetrcOnlyTrusted != nil {
|
|
|
|
repo.NetrcOnlyTrusted = *in.NetrcOnlyTrusted
|
|
|
|
}
|
2017-05-23 00:44:58 +02:00
|
|
|
if in.Visibility != nil {
|
|
|
|
switch *in.Visibility {
|
2021-11-22 13:55:13 +02:00
|
|
|
case string(model.VisibilityInternal), string(model.VisibilityPrivate), string(model.VisibilityPublic):
|
2023-04-30 14:01:11 +02:00
|
|
|
repo.Visibility = model.RepoVisibility(*in.Visibility)
|
2017-05-23 00:44:58 +02:00
|
|
|
default:
|
2021-11-27 17:06:00 +02:00
|
|
|
c.String(http.StatusBadRequest, "Invalid visibility type")
|
2017-05-23 00:44:58 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2021-12-01 15:22:06 +02:00
|
|
|
err := _store.UpdateRepo(repo)
|
2015-09-30 03:21:17 +02:00
|
|
|
if err != nil {
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
2015-09-30 03:21:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-30 22:15:28 +02:00
|
|
|
c.JSON(http.StatusOK, repo)
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
|
|
|
|
2016-06-14 23:05:53 +02:00
|
|
|
func ChownRepo(c *gin.Context) {
|
2021-12-01 15:22:06 +02:00
|
|
|
_store := store.FromContext(c)
|
2016-06-14 23:05:53 +02:00
|
|
|
repo := session.Repo(c)
|
|
|
|
user := session.User(c)
|
|
|
|
repo.UserID = user.ID
|
|
|
|
|
2021-12-01 15:22:06 +02:00
|
|
|
err := _store.UpdateRepo(repo)
|
2016-06-14 23:05:53 +02:00
|
|
|
if err != nil {
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
2016-06-14 23:05:53 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, repo)
|
|
|
|
}
|
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
func GetRepo(c *gin.Context) {
|
2016-03-30 22:15:28 +02:00
|
|
|
c.JSON(http.StatusOK, session.Repo(c))
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
|
|
|
|
2021-10-13 14:16:26 +02:00
|
|
|
func GetRepoPermissions(c *gin.Context) {
|
|
|
|
perm := session.Perm(c)
|
|
|
|
c.JSON(http.StatusOK, perm)
|
|
|
|
}
|
|
|
|
|
2021-10-27 02:47:55 +02:00
|
|
|
func GetRepoBranches(c *gin.Context) {
|
|
|
|
repo := session.Repo(c)
|
|
|
|
user := session.User(c)
|
2022-11-05 01:35:06 +02:00
|
|
|
f := server.Config.Services.Forge
|
2021-10-27 02:47:55 +02:00
|
|
|
|
2023-04-30 03:40:13 +02:00
|
|
|
branches, err := f.Branches(c, user, repo, session.Pagination(c))
|
2021-10-27 02:47:55 +02:00
|
|
|
if err != nil {
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
2021-10-27 02:47:55 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, branches)
|
|
|
|
}
|
|
|
|
|
2023-03-19 11:43:57 +02:00
|
|
|
func GetRepoPullRequests(c *gin.Context) {
|
|
|
|
repo := session.Repo(c)
|
|
|
|
user := session.User(c)
|
|
|
|
f := server.Config.Services.Forge
|
|
|
|
|
2023-04-30 03:40:13 +02:00
|
|
|
prs, err := f.PullRequests(c, user, repo, session.Pagination(c))
|
2023-03-19 11:43:57 +02:00
|
|
|
if err != nil {
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, prs)
|
|
|
|
}
|
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
func DeleteRepo(c *gin.Context) {
|
2017-07-14 21:58:38 +02:00
|
|
|
remove, _ := strconv.ParseBool(c.Query("remove"))
|
2021-12-01 15:22:06 +02:00
|
|
|
_store := store.FromContext(c)
|
2021-10-28 11:12:58 +02:00
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
repo := session.Repo(c)
|
2015-10-05 03:34:06 +02:00
|
|
|
user := session.User(c)
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2017-07-14 21:58:38 +02:00
|
|
|
repo.IsActive = false
|
|
|
|
repo.UserID = 0
|
|
|
|
|
2021-12-11 17:03:14 +02:00
|
|
|
if err := _store.UpdateRepo(repo); err != nil {
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
2015-10-05 03:34:06 +02:00
|
|
|
return
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
2015-10-05 03:34:06 +02:00
|
|
|
|
2017-07-14 21:58:38 +02:00
|
|
|
if remove {
|
2021-12-11 17:03:14 +02:00
|
|
|
if err := _store.DeleteRepo(repo); err != nil {
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
2017-07-14 21:58:38 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-05 01:35:06 +02:00
|
|
|
if err := server.Config.Services.Forge.Deactivate(c, user, repo, server.Config.Server.Host); err != nil {
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
2021-09-28 12:56:59 +02:00
|
|
|
return
|
|
|
|
}
|
2023-03-22 00:01:59 +02:00
|
|
|
c.JSON(http.StatusOK, repo)
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
2017-04-12 15:32:44 +02:00
|
|
|
|
|
|
|
func RepairRepo(c *gin.Context) {
|
2022-11-05 01:35:06 +02:00
|
|
|
forge := server.Config.Services.Forge
|
2021-12-01 15:22:06 +02:00
|
|
|
_store := store.FromContext(c)
|
2017-04-12 15:32:44 +02:00
|
|
|
repo := session.Repo(c)
|
|
|
|
user := session.User(c)
|
|
|
|
|
2017-08-21 23:56:37 +02:00
|
|
|
// creates the jwt token used to verify the repository
|
2017-04-12 15:32:44 +02:00
|
|
|
t := token.New(token.HookToken, repo.FullName)
|
|
|
|
sig, err := t.Sign(repo.Hash)
|
|
|
|
if err != nil {
|
2023-03-19 14:52:58 +02:00
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
2017-04-12 15:32:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// reconstruct the link
|
2021-09-22 20:48:01 +02:00
|
|
|
host := server.Config.Server.Host
|
2017-04-12 15:32:44 +02:00
|
|
|
link := fmt.Sprintf(
|
|
|
|
"%s/hook?access_token=%s",
|
|
|
|
host,
|
|
|
|
sig,
|
|
|
|
)
|
|
|
|
|
2022-11-15 16:01:23 +02:00
|
|
|
from, err := forge.Repo(c, user, repo.ForgeRemoteID, repo.Owner, repo.Name)
|
2021-11-23 16:36:52 +02:00
|
|
|
if err != nil {
|
2022-11-05 01:35:06 +02:00
|
|
|
log.Error().Err(err).Msgf("get repo '%s/%s' from forge", repo.Owner, repo.Name)
|
2021-11-23 16:36:52 +02:00
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2022-09-05 17:08:51 +02:00
|
|
|
|
|
|
|
if repo.FullName != from.FullName {
|
|
|
|
// create a redirection
|
|
|
|
err = _store.CreateRedirection(&model.Redirection{RepoID: repo.ID, FullName: repo.FullName})
|
|
|
|
if err != nil {
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2021-11-23 16:36:52 +02:00
|
|
|
}
|
2022-09-05 17:08:51 +02:00
|
|
|
|
|
|
|
repo.Update(from)
|
2021-12-01 15:22:06 +02:00
|
|
|
if err := _store.UpdateRepo(repo); err != nil {
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
2017-08-27 00:52:57 +02:00
|
|
|
}
|
2023-03-22 00:01:59 +02:00
|
|
|
repo.Perm.Pull = from.Perm.Pull
|
|
|
|
repo.Perm.Push = from.Perm.Push
|
|
|
|
repo.Perm.Admin = from.Perm.Admin
|
|
|
|
if err := _store.PermUpsert(repo.Perm); err != nil {
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2017-08-27 00:52:57 +02:00
|
|
|
|
2022-11-05 01:35:06 +02:00
|
|
|
if err := forge.Deactivate(c, user, repo, host); err != nil {
|
2022-09-05 17:08:51 +02:00
|
|
|
log.Trace().Err(err).Msgf("deactivate repo '%s' to repair failed", repo.FullName)
|
|
|
|
}
|
2022-11-05 01:35:06 +02:00
|
|
|
if err := forge.Activate(c, user, repo, link); err != nil {
|
2023-03-19 14:52:58 +02:00
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
2022-09-05 17:08:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-12 15:32:44 +02:00
|
|
|
c.Writer.WriteHeader(http.StatusOK)
|
|
|
|
}
|
2017-08-21 23:56:37 +02:00
|
|
|
|
|
|
|
func MoveRepo(c *gin.Context) {
|
2022-11-05 01:35:06 +02:00
|
|
|
forge := server.Config.Services.Forge
|
2021-12-01 15:22:06 +02:00
|
|
|
_store := store.FromContext(c)
|
2017-08-21 23:56:37 +02:00
|
|
|
repo := session.Repo(c)
|
|
|
|
user := session.User(c)
|
|
|
|
|
|
|
|
to, exists := c.GetQuery("to")
|
|
|
|
if !exists {
|
|
|
|
err := fmt.Errorf("Missing required to query value")
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
2017-08-22 00:49:09 +02:00
|
|
|
return
|
2017-08-21 23:56:37 +02:00
|
|
|
}
|
|
|
|
|
2017-08-22 00:49:09 +02:00
|
|
|
owner, name, errParse := model.ParseRepo(to)
|
2017-08-21 23:56:37 +02:00
|
|
|
if errParse != nil {
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, errParse)
|
2017-08-22 00:49:09 +02:00
|
|
|
return
|
2017-08-21 23:56:37 +02:00
|
|
|
}
|
|
|
|
|
2022-11-05 01:35:06 +02:00
|
|
|
from, err := forge.Repo(c, user, "", owner, name)
|
2017-08-21 23:56:37 +02:00
|
|
|
if err != nil {
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
2017-08-22 00:49:09 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !from.Perm.Admin {
|
|
|
|
c.AbortWithStatus(http.StatusUnauthorized)
|
|
|
|
return
|
2017-08-21 23:56:37 +02:00
|
|
|
}
|
2017-08-22 00:49:09 +02:00
|
|
|
|
2022-09-05 17:08:51 +02:00
|
|
|
err = _store.CreateRedirection(&model.Redirection{RepoID: repo.ID, FullName: repo.FullName})
|
|
|
|
if err != nil {
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
2017-08-22 00:49:09 +02:00
|
|
|
}
|
2017-08-21 23:56:37 +02:00
|
|
|
|
2022-09-05 17:08:51 +02:00
|
|
|
repo.Update(from)
|
2021-12-01 15:22:06 +02:00
|
|
|
errStore := _store.UpdateRepo(repo)
|
2017-08-21 23:56:37 +02:00
|
|
|
if errStore != nil {
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, errStore)
|
2017-08-21 23:56:37 +02:00
|
|
|
return
|
|
|
|
}
|
2023-03-22 00:01:59 +02:00
|
|
|
repo.Perm = from.Perm
|
|
|
|
errStore = _store.PermUpsert(repo.Perm)
|
|
|
|
if errStore != nil {
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, errStore)
|
|
|
|
return
|
|
|
|
}
|
2017-08-21 23:56:37 +02:00
|
|
|
|
2017-08-27 00:52:57 +02:00
|
|
|
// creates the jwt token used to verify the repository
|
|
|
|
t := token.New(token.HookToken, repo.FullName)
|
|
|
|
sig, err := t.Sign(repo.Hash)
|
|
|
|
if err != nil {
|
2023-03-19 14:52:58 +02:00
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
2017-08-27 00:52:57 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// reconstruct the link
|
2021-09-22 20:48:01 +02:00
|
|
|
host := server.Config.Server.Host
|
2017-08-27 00:52:57 +02:00
|
|
|
link := fmt.Sprintf(
|
|
|
|
"%s/hook?access_token=%s",
|
|
|
|
host,
|
|
|
|
sig,
|
|
|
|
)
|
2017-08-21 23:56:37 +02:00
|
|
|
|
2022-11-05 01:35:06 +02:00
|
|
|
if err := forge.Deactivate(c, user, repo, host); err != nil {
|
2021-11-23 16:36:52 +02:00
|
|
|
log.Trace().Err(err).Msgf("deactivate repo '%s' for move to activate later, got an error", repo.FullName)
|
|
|
|
}
|
2022-11-05 01:35:06 +02:00
|
|
|
if err := forge.Activate(c, user, repo, link); err != nil {
|
2023-03-19 14:52:58 +02:00
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
2017-08-27 00:52:57 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Writer.WriteHeader(http.StatusOK)
|
|
|
|
}
|