2022-10-18 01:24:12 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
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-09-22 18:48:01 +00:00
|
|
|
package api
|
2015-09-30 01:21:17 +00:00
|
|
|
|
|
|
|
import (
|
2016-05-03 00:47:58 +00:00
|
|
|
"encoding/base32"
|
2023-03-21 22:01:59 +00:00
|
|
|
"errors"
|
2015-09-30 01:21:17 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2017-07-14 19:58:38 +00:00
|
|
|
"strconv"
|
2023-03-21 22:01:59 +00:00
|
|
|
"time"
|
2015-09-30 01:21:17 +00:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2016-05-03 00:47:58 +00:00
|
|
|
"github.com/gorilla/securecookie"
|
2021-11-23 14:36:52 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/router/middleware/session"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store/types"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/shared/token"
|
2015-09-30 01:21:17 +00:00
|
|
|
)
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// PostRepo
|
|
|
|
//
|
|
|
|
// @Summary Activate a repository
|
2023-07-11 16:51:03 +00:00
|
|
|
// @Router /repos [post]
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} Repo
|
|
|
|
// @Tags Repositories
|
2023-12-24 14:50:01 +00:00
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
|
|
|
// @Param forge_remote_id query string true "the id of a repository at the forge"
|
2015-09-30 01:21:17 +00:00
|
|
|
func PostRepo(c *gin.Context) {
|
2021-12-01 13:22:06 +00:00
|
|
|
_store := store.FromContext(c)
|
2015-09-30 01:21:17 +00:00
|
|
|
user := session.User(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
|
|
|
|
}
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2023-06-12 23:07:52 +00:00
|
|
|
forgeRemoteID := model.ForgeRemoteID(c.Query("forge_remote_id"))
|
2023-07-11 16:51:03 +00:00
|
|
|
if !forgeRemoteID.IsValid() {
|
|
|
|
c.String(http.StatusBadRequest, "No forge_remote_id provided")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-12 23:07:52 +00:00
|
|
|
repo, err := _store.GetRepoForgeID(forgeRemoteID)
|
2023-03-21 22:01:59 +00:00
|
|
|
enabledOnce := err == nil // if there's no error, the repo was found and enabled once already
|
|
|
|
if enabledOnce && repo.IsActive {
|
2021-11-27 15:06:00 +00:00
|
|
|
c.String(http.StatusConflict, "Repository is already active.")
|
2015-09-30 01:21:17 +00:00
|
|
|
return
|
2023-03-21 22:01:59 +00:00
|
|
|
} else if err != nil && !errors.Is(err, types.RecordNotExist) {
|
2024-01-11 18:17:07 +00:00
|
|
|
msg := "could not get repo by remote id from store."
|
2023-12-19 05:25:59 +00:00
|
|
|
log.Error().Err(err).Msg(msg)
|
|
|
|
c.String(http.StatusInternalServerError, msg)
|
2023-03-21 22:01:59 +00:00
|
|
|
return
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
|
2024-04-16 06:04:55 +00:00
|
|
|
from, err := _forge.Repo(c, user, forgeRemoteID, "", "")
|
2023-03-21 22:01:59 +00:00
|
|
|
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")
|
2023-11-12 13:39:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !server.Config.Permissions.OwnersAllowlist.IsAllowed(from) {
|
|
|
|
c.String(http.StatusForbidden, "Repo owner is not allowed")
|
|
|
|
return
|
2023-03-21 22:01:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if enabledOnce {
|
|
|
|
repo.Update(from)
|
|
|
|
} else {
|
|
|
|
repo = from
|
|
|
|
repo.AllowPull = true
|
2024-04-02 20:03:37 +00:00
|
|
|
repo.AllowDeploy = false
|
2023-03-21 22:01:59 +00:00
|
|
|
repo.NetrcOnlyTrusted = true
|
|
|
|
repo.CancelPreviousPipelineEvents = server.Config.Pipeline.DefaultCancelPreviousPipelineEvents
|
|
|
|
}
|
2017-07-14 19:58:38 +00:00
|
|
|
repo.IsActive = true
|
|
|
|
repo.UserID = user.ID
|
2021-09-18 14:28:35 +00:00
|
|
|
|
2017-07-14 19:58:38 +00:00
|
|
|
if repo.Visibility == "" {
|
|
|
|
repo.Visibility = model.VisibilityPublic
|
2021-11-22 11:55:13 +00:00
|
|
|
if repo.IsSCMPrivate {
|
2017-07-14 19:58:38 +00:00
|
|
|
repo.Visibility = model.VisibilityPrivate
|
|
|
|
}
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
2021-09-18 14:28:35 +00:00
|
|
|
|
2017-07-14 19:58:38 +00:00
|
|
|
if repo.Timeout == 0 {
|
2023-03-19 19:24:43 +00:00
|
|
|
repo.Timeout = server.Config.Pipeline.DefaultTimeout
|
|
|
|
} else if repo.Timeout > server.Config.Pipeline.MaxTimeout {
|
|
|
|
repo.Timeout = server.Config.Pipeline.MaxTimeout
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
2021-09-18 14:28:35 +00:00
|
|
|
|
2017-07-14 19:58:38 +00:00
|
|
|
if repo.Hash == "" {
|
|
|
|
repo.Hash = base32.StdEncoding.EncodeToString(
|
|
|
|
securecookie.GenerateRandomKey(32),
|
|
|
|
)
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
|
2017-07-14 19:58:38 +00:00
|
|
|
// creates the jwt token used to verify the repository
|
2024-05-27 09:52:43 +00:00
|
|
|
t := token.New(token.HookToken)
|
|
|
|
t.Set("repo-id", strconv.FormatInt(repo.ID, 10))
|
2017-07-14 19:58:38 +00:00
|
|
|
sig, err := t.Sign(repo.Hash)
|
2015-09-30 01:21:17 +00:00
|
|
|
if err != nil {
|
2024-01-11 18:17:07 +00:00
|
|
|
msg := "could not generate new jwt token."
|
2023-12-19 05:25:59 +00:00
|
|
|
log.Error().Err(err).Msg(msg)
|
|
|
|
c.String(http.StatusInternalServerError, msg)
|
2015-09-30 01:21:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-14 16:12:12 +00:00
|
|
|
hookURL := fmt.Sprintf(
|
2023-08-16 00:42:37 +00:00
|
|
|
"%s/api/hook?access_token=%s",
|
2023-06-18 12:47:40 +00:00
|
|
|
server.Config.Server.WebhookHost,
|
2015-09-30 01:21:17 +00:00
|
|
|
sig,
|
|
|
|
)
|
|
|
|
|
2023-07-21 17:45:32 +00:00
|
|
|
// find org of repo
|
|
|
|
var org *model.Org
|
|
|
|
org, err = _store.OrgFindByName(repo.Owner)
|
|
|
|
if err != nil && !errors.Is(err, types.RecordNotExist) {
|
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// create an org if it doesn't exist yet
|
|
|
|
if errors.Is(err, types.RecordNotExist) {
|
2024-04-16 06:04:55 +00:00
|
|
|
org, err = _forge.Org(c, user, repo.Owner)
|
2023-07-21 17:45:32 +00:00
|
|
|
if err != nil {
|
2024-01-11 18:17:07 +00:00
|
|
|
msg := "could not fetch organization from forge."
|
2023-12-19 05:25:59 +00:00
|
|
|
log.Error().Err(err).Msg(msg)
|
|
|
|
c.String(http.StatusInternalServerError, msg)
|
2023-07-21 17:45:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-16 06:04:55 +00:00
|
|
|
org.ForgeID = user.ForgeID
|
2023-07-21 17:45:32 +00:00
|
|
|
err = _store.OrgCreate(org)
|
|
|
|
if err != nil {
|
2024-01-11 18:17:07 +00:00
|
|
|
msg := "could not create organization in store."
|
2023-12-19 05:25:59 +00:00
|
|
|
log.Error().Err(err).Msg(msg)
|
|
|
|
c.String(http.StatusInternalServerError, msg)
|
2023-07-21 17:45:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
repo.OrgID = org.ID
|
|
|
|
|
2024-04-16 06:04:55 +00:00
|
|
|
err = _forge.Activate(c, user, repo, hookURL)
|
2015-09-30 01:21:17 +00:00
|
|
|
if err != nil {
|
2024-01-11 18:17:07 +00:00
|
|
|
msg := "could not create webhook in forge."
|
2023-12-19 05:25:59 +00:00
|
|
|
log.Error().Err(err).Msg(msg)
|
|
|
|
c.String(http.StatusInternalServerError, msg)
|
2015-09-30 01:21:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-21 22:01:59 +00:00
|
|
|
if enabledOnce {
|
|
|
|
err = _store.UpdateRepo(repo)
|
|
|
|
} else {
|
2024-04-16 06:04:55 +00:00
|
|
|
repo.ForgeID = user.ForgeID // TODO: allow to use other connected forges of the user
|
2023-03-21 22:01:59 +00:00
|
|
|
err = _store.CreateRepo(repo)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2024-01-11 18:17:07 +00:00
|
|
|
msg := "could not create/update repo in store."
|
2023-12-19 05:25:59 +00:00
|
|
|
log.Error().Err(err).Msg(msg)
|
|
|
|
c.String(http.StatusInternalServerError, msg)
|
2023-03-21 22:01:59 +00:00
|
|
|
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 01:21:17 +00:00
|
|
|
if err != nil {
|
2021-11-27 15:06:00 +00:00
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
2015-09-30 01:21:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-27 15:06:00 +00:00
|
|
|
c.JSON(http.StatusOK, repo)
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// PatchRepo
|
|
|
|
//
|
2024-05-01 09:50:41 +00:00
|
|
|
// @Summary Update a repository
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Router /repos/{repo_id} [patch]
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} Repo
|
|
|
|
// @Tags Repositories
|
2023-12-24 14:50:01 +00:00
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
|
|
|
// @Param repo_id path int true "the repository id"
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Param repo body RepoPatch true "the repository's information"
|
2015-09-30 01:21:17 +00:00
|
|
|
func PatchRepo(c *gin.Context) {
|
2021-12-01 13:22:06 +00:00
|
|
|
_store := store.FromContext(c)
|
2015-09-30 01:21:17 +00:00
|
|
|
repo := session.Repo(c)
|
|
|
|
user := session.User(c)
|
|
|
|
|
2017-04-12 12:12:21 +00:00
|
|
|
in := new(model.RepoPatch)
|
2015-09-30 01:21:17 +00:00
|
|
|
if err := c.Bind(in); err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusBadRequest, err)
|
2015-09-30 01:21:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-19 19:24:43 +00:00
|
|
|
if in.Timeout != nil && *in.Timeout > server.Config.Pipeline.MaxTimeout && !user.Admin {
|
2024-06-04 06:30:54 +00:00
|
|
|
c.String(http.StatusForbidden, fmt.Sprintf("Timeout is not allowed to be higher than max timeout (%d min)", server.Config.Pipeline.MaxTimeout))
|
2023-06-19 15:46:48 +00:00
|
|
|
return
|
2021-11-27 15:06:00 +00:00
|
|
|
}
|
|
|
|
if in.IsTrusted != nil && *in.IsTrusted != repo.IsTrusted && !user.Admin {
|
2023-09-02 11:31:10 +00:00
|
|
|
log.Trace().Msgf("user '%s' wants to make repo trusted without being an instance admin", user.Login)
|
2021-11-27 15:06:00 +00:00
|
|
|
c.String(http.StatusForbidden, "Insufficient privileges")
|
2017-04-11 17:06:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
if in.AllowPull != nil {
|
|
|
|
repo.AllowPull = *in.AllowPull
|
|
|
|
}
|
2024-04-02 20:03:37 +00:00
|
|
|
if in.AllowDeploy != nil {
|
|
|
|
repo.AllowDeploy = *in.AllowDeploy
|
|
|
|
}
|
2017-04-11 17:06:45 +00:00
|
|
|
if in.IsGated != nil {
|
|
|
|
repo.IsGated = *in.IsGated
|
|
|
|
}
|
|
|
|
if in.IsTrusted != nil {
|
2015-09-30 01:21:17 +00:00
|
|
|
repo.IsTrusted = *in.IsTrusted
|
|
|
|
}
|
2017-04-11 17:06:45 +00:00
|
|
|
if in.Timeout != nil {
|
2015-09-30 01:21:17 +00:00
|
|
|
repo.Timeout = *in.Timeout
|
|
|
|
}
|
2017-04-12 12:12:21 +00:00
|
|
|
if in.Config != nil {
|
|
|
|
repo.Config = *in.Config
|
|
|
|
}
|
2022-05-09 09:26:09 +00:00
|
|
|
if in.CancelPreviousPipelineEvents != nil {
|
|
|
|
repo.CancelPreviousPipelineEvents = *in.CancelPreviousPipelineEvents
|
|
|
|
}
|
2023-03-20 20:17:49 +00:00
|
|
|
if in.NetrcOnlyTrusted != nil {
|
|
|
|
repo.NetrcOnlyTrusted = *in.NetrcOnlyTrusted
|
|
|
|
}
|
2017-05-22 22:44:58 +00:00
|
|
|
if in.Visibility != nil {
|
|
|
|
switch *in.Visibility {
|
2021-11-22 11:55:13 +00:00
|
|
|
case string(model.VisibilityInternal), string(model.VisibilityPrivate), string(model.VisibilityPublic):
|
2023-04-30 12:01:11 +00:00
|
|
|
repo.Visibility = model.RepoVisibility(*in.Visibility)
|
2017-05-22 22:44:58 +00:00
|
|
|
default:
|
2021-11-27 15:06:00 +00:00
|
|
|
c.String(http.StatusBadRequest, "Invalid visibility type")
|
2017-05-22 22:44:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
err := _store.UpdateRepo(repo)
|
2015-09-30 01:21:17 +00:00
|
|
|
if err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
2015-09-30 01:21:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-30 20:15:28 +00:00
|
|
|
c.JSON(http.StatusOK, repo)
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// ChownRepo
|
|
|
|
//
|
2024-05-01 09:50:41 +00:00
|
|
|
// @Summary Change a repository's owner to the currently authenticated user
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Router /repos/{repo_id}/chown [post]
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} Repo
|
|
|
|
// @Tags Repositories
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Param repo_id path int true "the repository id"
|
2016-06-14 21:05:53 +00:00
|
|
|
func ChownRepo(c *gin.Context) {
|
2021-12-01 13:22:06 +00:00
|
|
|
_store := store.FromContext(c)
|
2016-06-14 21:05:53 +00:00
|
|
|
repo := session.Repo(c)
|
|
|
|
user := session.User(c)
|
|
|
|
repo.UserID = user.ID
|
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
err := _store.UpdateRepo(repo)
|
2016-06-14 21:05:53 +00:00
|
|
|
if err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
2016-06-14 21:05:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, repo)
|
|
|
|
}
|
|
|
|
|
2023-06-12 23:07:52 +00:00
|
|
|
// LookupRepo
|
|
|
|
//
|
2024-05-01 09:50:41 +00:00
|
|
|
// @Summary Lookup a repository by full name
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Router /repos/lookup/{repo_full_name} [get]
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} Repo
|
|
|
|
// @Tags Repositories
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2024-05-01 09:50:41 +00:00
|
|
|
// @Param repo_full_name path string true "the repository full name / slug"
|
2023-06-12 23:07:52 +00:00
|
|
|
func LookupRepo(c *gin.Context) {
|
2023-08-30 14:35:34 +00:00
|
|
|
c.JSON(http.StatusOK, session.Repo(c))
|
2023-06-12 23:07:52 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// GetRepo
|
|
|
|
//
|
2024-05-01 09:50:41 +00:00
|
|
|
// @Summary Get a repository
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Router /repos/{repo_id} [get]
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} Repo
|
|
|
|
// @Tags Repositories
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Param repo_id path int true "the repository id"
|
2015-09-30 01:21:17 +00:00
|
|
|
func GetRepo(c *gin.Context) {
|
2016-03-30 20:15:28 +00:00
|
|
|
c.JSON(http.StatusOK, session.Repo(c))
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// GetRepoPermissions
|
|
|
|
//
|
2024-05-01 09:50:41 +00:00
|
|
|
// @Summary Check current authenticated users access to the repository
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Description The repository permission, according to the used access token.
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Router /repos/{repo_id}/permissions [get]
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} Perm
|
|
|
|
// @Tags Repositories
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2023-12-24 14:50:01 +00:00
|
|
|
// @Param repo_id path int true "the repository id"
|
2021-10-13 12:16:26 +00:00
|
|
|
func GetRepoPermissions(c *gin.Context) {
|
|
|
|
perm := session.Perm(c)
|
|
|
|
c.JSON(http.StatusOK, perm)
|
|
|
|
}
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// GetRepoBranches
|
|
|
|
//
|
2024-05-01 09:50:41 +00:00
|
|
|
// @Summary Get branches of a repository
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Router /repos/{repo_id}/branches [get]
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {array} string
|
|
|
|
// @Tags Repositories
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Param repo_id path int true "the repository id"
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Param page query int false "for response pagination, page offset number" default(1)
|
|
|
|
// @Param perPage query int false "for response pagination, max items per page" default(50)
|
2021-10-27 00:47:55 +00:00
|
|
|
func GetRepoBranches(c *gin.Context) {
|
|
|
|
repo := session.Repo(c)
|
|
|
|
user := session.User(c)
|
2024-04-16 06:04:55 +00:00
|
|
|
_forge, err := server.Config.Services.Manager.ForgeFromRepo(repo)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Cannot get forge from repo")
|
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2021-10-27 00:47:55 +00:00
|
|
|
|
2024-04-16 06:04:55 +00:00
|
|
|
branches, err := _forge.Branches(c, user, repo, session.Pagination(c))
|
2021-10-27 00:47:55 +00:00
|
|
|
if err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
2021-10-27 00:47:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, branches)
|
|
|
|
}
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// GetRepoPullRequests
|
|
|
|
//
|
2024-05-01 09:50:41 +00:00
|
|
|
// @Summary List active pull requests of a repository
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Router /repos/{repo_id}/pull_requests [get]
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {array} PullRequest
|
|
|
|
// @Tags Repositories
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Param repo_id path int true "the repository id"
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Param page query int false "for response pagination, page offset number" default(1)
|
|
|
|
// @Param perPage query int false "for response pagination, max items per page" default(50)
|
2023-03-19 09:43:57 +00:00
|
|
|
func GetRepoPullRequests(c *gin.Context) {
|
|
|
|
repo := session.Repo(c)
|
|
|
|
user := session.User(c)
|
2024-04-16 06:04:55 +00:00
|
|
|
_forge, err := server.Config.Services.Manager.ForgeFromRepo(repo)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Cannot get forge from repo")
|
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2023-03-19 09:43:57 +00:00
|
|
|
|
2024-04-16 06:04:55 +00:00
|
|
|
prs, err := _forge.PullRequests(c, user, repo, session.Pagination(c))
|
2023-03-19 09:43:57 +00:00
|
|
|
if err != nil {
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, prs)
|
|
|
|
}
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// DeleteRepo
|
|
|
|
//
|
|
|
|
// @Summary Delete a repository
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Router /repos/{repo_id} [delete]
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} Repo
|
|
|
|
// @Tags Repositories
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Param repo_id path int true "the repository id"
|
2015-09-30 01:21:17 +00:00
|
|
|
func DeleteRepo(c *gin.Context) {
|
2017-07-14 19:58:38 +00:00
|
|
|
remove, _ := strconv.ParseBool(c.Query("remove"))
|
2021-12-01 13:22:06 +00:00
|
|
|
_store := store.FromContext(c)
|
2015-09-30 01:21:17 +00:00
|
|
|
repo := session.Repo(c)
|
2015-10-05 01:34:06 +00:00
|
|
|
user := session.User(c)
|
2024-04-16 06:04:55 +00:00
|
|
|
_forge, err := server.Config.Services.Manager.ForgeFromRepo(repo)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Cannot get forge from repo")
|
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2017-07-14 19:58:38 +00:00
|
|
|
repo.IsActive = false
|
|
|
|
repo.UserID = 0
|
|
|
|
|
2021-12-11 15:03:14 +00:00
|
|
|
if err := _store.UpdateRepo(repo); err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
2015-10-05 01:34:06 +00:00
|
|
|
return
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
2015-10-05 01:34:06 +00:00
|
|
|
|
2017-07-14 19:58:38 +00:00
|
|
|
if remove {
|
2021-12-11 15:03:14 +00:00
|
|
|
if err := _store.DeleteRepo(repo); err != nil {
|
2024-01-10 21:56:42 +00:00
|
|
|
handleDBError(c, err)
|
2017-07-14 19:58:38 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-16 06:04:55 +00:00
|
|
|
if err := _forge.Deactivate(c, user, repo, server.Config.Server.WebhookHost); err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
2021-09-28 10:56:59 +00:00
|
|
|
return
|
|
|
|
}
|
2023-03-21 22:01:59 +00:00
|
|
|
c.JSON(http.StatusOK, repo)
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
2017-04-12 13:32:44 +00:00
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// RepairRepo
|
|
|
|
//
|
|
|
|
// @Summary Repair a repository
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Router /repos/{repo_id}/repair [post]
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Produce plain
|
2023-10-24 12:12:55 +00:00
|
|
|
// @Success 204
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Tags Repositories
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Param repo_id path int true "the repository id"
|
2017-04-12 13:32:44 +00:00
|
|
|
func RepairRepo(c *gin.Context) {
|
|
|
|
repo := session.Repo(c)
|
2023-12-13 13:53:38 +00:00
|
|
|
repairRepo(c, repo, true, false)
|
|
|
|
if c.Writer.Written() {
|
|
|
|
return
|
|
|
|
}
|
2023-10-24 12:12:55 +00:00
|
|
|
c.Status(http.StatusNoContent)
|
2017-04-12 13:32:44 +00:00
|
|
|
}
|
2017-08-21 21:56:37 +00:00
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// MoveRepo
|
|
|
|
//
|
|
|
|
// @Summary Move a repository to a new owner
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Router /repos/{repo_id}/move [post]
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Produce plain
|
2023-10-28 11:37:54 +00:00
|
|
|
// @Success 204
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Tags Repositories
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Param repo_id path int true "the repository id"
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Param to query string true "the username to move the repository to"
|
2017-08-21 21:56:37 +00:00
|
|
|
func MoveRepo(c *gin.Context) {
|
2021-12-01 13:22:06 +00:00
|
|
|
_store := store.FromContext(c)
|
2017-08-21 21:56:37 +00:00
|
|
|
repo := session.Repo(c)
|
|
|
|
user := session.User(c)
|
2024-04-16 06:04:55 +00:00
|
|
|
_forge, err := server.Config.Services.Manager.ForgeFromRepo(repo)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Cannot get forge from repo")
|
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2017-08-21 21:56:37 +00:00
|
|
|
|
|
|
|
to, exists := c.GetQuery("to")
|
|
|
|
if !exists {
|
2023-09-02 11:31:10 +00:00
|
|
|
err := fmt.Errorf("missing required to query value")
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
2017-08-21 22:49:09 +00:00
|
|
|
return
|
2017-08-21 21:56:37 +00:00
|
|
|
}
|
|
|
|
|
2017-08-21 22:49:09 +00:00
|
|
|
owner, name, errParse := model.ParseRepo(to)
|
2017-08-21 21:56:37 +00:00
|
|
|
if errParse != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, errParse)
|
2017-08-21 22:49:09 +00:00
|
|
|
return
|
2017-08-21 21:56:37 +00:00
|
|
|
}
|
|
|
|
|
2024-04-16 06:04:55 +00:00
|
|
|
from, err := _forge.Repo(c, user, "", owner, name)
|
2017-08-21 21:56:37 +00:00
|
|
|
if err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
2017-08-21 22:49:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !from.Perm.Admin {
|
|
|
|
c.AbortWithStatus(http.StatusUnauthorized)
|
|
|
|
return
|
2017-08-21 21:56:37 +00:00
|
|
|
}
|
2017-08-21 22:49:09 +00:00
|
|
|
|
2022-09-05 15:08:51 +00:00
|
|
|
err = _store.CreateRedirection(&model.Redirection{RepoID: repo.ID, FullName: repo.FullName})
|
|
|
|
if err != nil {
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
2017-08-21 22:49:09 +00:00
|
|
|
}
|
2017-08-21 21:56:37 +00:00
|
|
|
|
2022-09-05 15:08:51 +00:00
|
|
|
repo.Update(from)
|
2021-12-01 13:22:06 +00:00
|
|
|
errStore := _store.UpdateRepo(repo)
|
2017-08-21 21:56:37 +00:00
|
|
|
if errStore != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, errStore)
|
2017-08-21 21:56:37 +00:00
|
|
|
return
|
|
|
|
}
|
2023-03-21 22:01:59 +00:00
|
|
|
repo.Perm = from.Perm
|
|
|
|
errStore = _store.PermUpsert(repo.Perm)
|
|
|
|
if errStore != nil {
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, errStore)
|
|
|
|
return
|
|
|
|
}
|
2017-08-21 21:56:37 +00:00
|
|
|
|
2017-08-26 22:52:57 +00:00
|
|
|
// creates the jwt token used to verify the repository
|
2024-05-27 09:52:43 +00:00
|
|
|
t := token.New(token.HookToken)
|
|
|
|
t.Set("repo-id", strconv.FormatInt(repo.ID, 10))
|
2017-08-26 22:52:57 +00:00
|
|
|
sig, err := t.Sign(repo.Hash)
|
|
|
|
if err != nil {
|
2023-03-19 12:52:58 +00:00
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
2017-08-26 22:52:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-14 16:12:12 +00:00
|
|
|
// reconstruct the hook url
|
2023-09-22 14:43:31 +00:00
|
|
|
host := server.Config.Server.WebhookHost
|
2023-11-14 16:12:12 +00:00
|
|
|
hookURL := fmt.Sprintf(
|
2023-08-16 00:42:37 +00:00
|
|
|
"%s/api/hook?access_token=%s",
|
2017-08-26 22:52:57 +00:00
|
|
|
host,
|
|
|
|
sig,
|
|
|
|
)
|
2017-08-21 21:56:37 +00:00
|
|
|
|
2024-04-16 06:04:55 +00:00
|
|
|
if err := _forge.Deactivate(c, user, repo, host); err != nil {
|
2024-05-27 09:52:43 +00:00
|
|
|
log.Trace().Err(err).Msgf("deactivate repo '%s' for move to activate later, got an error", strconv.FormatInt(repo.ID, 10))
|
2021-11-23 14:36:52 +00:00
|
|
|
}
|
2024-04-16 06:04:55 +00:00
|
|
|
if err := _forge.Activate(c, user, repo, hookURL); err != nil {
|
2023-03-19 12:52:58 +00:00
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
2017-08-26 22:52:57 +00:00
|
|
|
return
|
|
|
|
}
|
2023-10-28 11:37:54 +00:00
|
|
|
c.Status(http.StatusNoContent)
|
2017-08-26 22:52:57 +00:00
|
|
|
}
|
2023-09-08 10:26:20 +00:00
|
|
|
|
|
|
|
// GetAllRepos
|
|
|
|
//
|
2024-05-01 09:50:41 +00:00
|
|
|
// @Summary List all repositories on the server
|
|
|
|
// @Description Returns a list of all repositories. Requires admin rights.
|
2023-09-08 10:26:20 +00:00
|
|
|
// @Router /repos [get]
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {array} Repo
|
|
|
|
// @Tags Repositories
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2023-12-24 14:50:01 +00:00
|
|
|
// @Param active query bool false "only list active repos"
|
2023-09-08 10:26:20 +00:00
|
|
|
// @Param page query int false "for response pagination, page offset number" default(1)
|
|
|
|
// @Param perPage query int false "for response pagination, max items per page" default(50)
|
|
|
|
func GetAllRepos(c *gin.Context) {
|
|
|
|
_store := store.FromContext(c)
|
|
|
|
|
|
|
|
active, _ := strconv.ParseBool(c.Query("active"))
|
|
|
|
|
|
|
|
repos, err := _store.RepoListAll(active, session.Pagination(c))
|
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusInternalServerError, "Error fetching repository list. %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, repos)
|
|
|
|
}
|
2023-10-24 12:12:55 +00:00
|
|
|
|
|
|
|
// RepairAllRepos
|
|
|
|
//
|
2024-05-01 09:50:41 +00:00
|
|
|
// @Summary Repair all repositories on the server
|
|
|
|
// @Description Executes a repair process on all repositories. Requires admin rights.
|
2023-10-24 12:12:55 +00:00
|
|
|
// @Router /repos/repair [post]
|
|
|
|
// @Produce plain
|
|
|
|
// @Success 204
|
|
|
|
// @Tags Repositories
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
|
|
|
func RepairAllRepos(c *gin.Context) {
|
|
|
|
_store := store.FromContext(c)
|
|
|
|
|
|
|
|
repos, err := _store.RepoListAll(true, &model.ListOptions{All: true})
|
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusInternalServerError, "Error fetching repository list. %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range repos {
|
2023-12-13 13:53:38 +00:00
|
|
|
repairRepo(c, r, false, true)
|
2023-10-24 12:12:55 +00:00
|
|
|
if c.Writer.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Status(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
2023-12-13 13:53:38 +00:00
|
|
|
func repairRepo(c *gin.Context, repo *model.Repo, withPerms, skipOnErr bool) {
|
2023-10-24 12:12:55 +00:00
|
|
|
_store := store.FromContext(c)
|
2024-04-16 06:04:55 +00:00
|
|
|
_forge, err := server.Config.Services.Manager.ForgeFromRepo(repo)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Cannot get forge from repo")
|
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2023-10-24 13:54:43 +00:00
|
|
|
|
|
|
|
user, err := _store.GetUser(repo.UserID)
|
|
|
|
if err != nil {
|
2023-12-13 13:53:38 +00:00
|
|
|
if errors.Is(err, types.RecordNotExist) {
|
|
|
|
if !skipOnErr {
|
|
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
|
|
}
|
2024-01-10 19:57:12 +00:00
|
|
|
log.Error().Err(err).Msg("could not get user on repo repair")
|
2023-12-13 13:53:38 +00:00
|
|
|
} else {
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
}
|
2023-10-24 13:54:43 +00:00
|
|
|
return
|
|
|
|
}
|
2023-10-24 12:12:55 +00:00
|
|
|
|
|
|
|
// creates the jwt token used to verify the repository
|
2024-05-27 09:52:43 +00:00
|
|
|
t := token.New(token.HookToken)
|
|
|
|
t.Set("repo-id", strconv.FormatInt(repo.ID, 10))
|
2023-10-24 12:12:55 +00:00
|
|
|
sig, err := t.Sign(repo.Hash)
|
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-14 16:12:12 +00:00
|
|
|
// reconstruct the hook url
|
2023-10-24 12:12:55 +00:00
|
|
|
host := server.Config.Server.WebhookHost
|
2023-11-14 16:12:12 +00:00
|
|
|
hookURL := fmt.Sprintf(
|
2023-10-24 12:12:55 +00:00
|
|
|
"%s/api/hook?access_token=%s",
|
|
|
|
host,
|
|
|
|
sig,
|
|
|
|
)
|
|
|
|
|
2024-04-16 06:04:55 +00:00
|
|
|
from, err := _forge.Repo(c, user, repo.ForgeRemoteID, repo.Owner, repo.Name)
|
2023-10-24 12:12:55 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("get repo '%s/%s' from forge", repo.Owner, repo.Name)
|
2023-12-13 13:53:38 +00:00
|
|
|
if !skipOnErr {
|
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
}
|
2023-10-24 12:12:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
repo.Update(from)
|
|
|
|
if err := _store.UpdateRepo(repo); err != nil {
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if withPerms {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-16 06:04:55 +00:00
|
|
|
if err := _forge.Deactivate(c, user, repo, host); err != nil {
|
2023-10-24 12:12:55 +00:00
|
|
|
log.Trace().Err(err).Msgf("deactivate repo '%s' to repair failed", repo.FullName)
|
|
|
|
}
|
2024-04-16 06:04:55 +00:00
|
|
|
if err := _forge.Activate(c, user, repo, hookURL); err != nil {
|
2023-10-24 12:12:55 +00:00
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|