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.
|
|
|
|
|
2016-05-02 21:21:25 +02:00
|
|
|
package server
|
2015-09-30 03:21:17 +02:00
|
|
|
|
|
|
|
import (
|
2016-05-03 02:47:58 +02:00
|
|
|
"encoding/base32"
|
2015-09-30 03:21:17 +02:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2017-07-14 21:58:38 +02:00
|
|
|
"strconv"
|
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"
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2021-05-25 14:08:27 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/model"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/remote"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/router/middleware/session"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/shared/token"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/store"
|
2015-09-30 03:21:17 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func PostRepo(c *gin.Context) {
|
2015-10-22 01:14:02 +02:00
|
|
|
remote := remote.FromContext(c)
|
2015-09-30 03:21:17 +02:00
|
|
|
user := session.User(c)
|
2017-07-14 21:58:38 +02:00
|
|
|
repo := session.Repo(c)
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2017-07-14 21:58:38 +02:00
|
|
|
if repo.IsActive {
|
|
|
|
c.String(409, "Repository is already active.")
|
2015-09-30 03:21:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
if repo.IsPrivate {
|
|
|
|
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 {
|
|
|
|
repo.Timeout = 60 // 1 hour default build time
|
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 {
|
2015-10-29 22:47:46 +02:00
|
|
|
c.String(500, err.Error())
|
2015-09-30 03:21:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
link := fmt.Sprintf(
|
|
|
|
"%s/hook?access_token=%s",
|
2021-08-20 16:32:52 +02:00
|
|
|
Config.Server.Host,
|
2015-09-30 03:21:17 +02:00
|
|
|
sig,
|
|
|
|
)
|
|
|
|
|
2017-07-14 21:58:38 +02:00
|
|
|
err = remote.Activate(user, repo, link)
|
2015-09-30 03:21:17 +02:00
|
|
|
if err != nil {
|
2015-10-29 22:47:46 +02:00
|
|
|
c.String(500, err.Error())
|
2015-09-30 03:21:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-21 17:53:11 +02:00
|
|
|
from, err := remote.Repo(user, repo.Owner, repo.Name)
|
|
|
|
if err == nil {
|
|
|
|
repo.Update(from)
|
|
|
|
}
|
|
|
|
|
2017-07-14 21:58:38 +02:00
|
|
|
err = store.UpdateRepo(c, repo)
|
2015-09-30 03:21:17 +02:00
|
|
|
if err != nil {
|
2015-10-29 22:47:46 +02:00
|
|
|
c.String(500, err.Error())
|
2015-09-30 03:21:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-14 21:58:38 +02:00
|
|
|
c.JSON(200, repo)
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func PatchRepo(c *gin.Context) {
|
|
|
|
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 {
|
|
|
|
c.AbortWithError(http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-01 21:10:00 +02:00
|
|
|
if (in.IsTrusted != nil || in.Timeout != nil) && !user.Admin {
|
2017-04-11 19:06:45 +02:00
|
|
|
c.String(403, "Insufficient privileges")
|
|
|
|
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
|
|
|
|
}
|
2017-05-23 00:44:58 +02:00
|
|
|
if in.Visibility != nil {
|
|
|
|
switch *in.Visibility {
|
2017-05-23 00:54:04 +02:00
|
|
|
case model.VisibilityInternal, model.VisibilityPrivate, model.VisibilityPublic:
|
|
|
|
repo.Visibility = *in.Visibility
|
2017-05-23 00:44:58 +02:00
|
|
|
default:
|
|
|
|
c.String(400, "Invalid visibility type")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2017-08-13 02:01:10 +02:00
|
|
|
if in.BuildCounter != nil {
|
|
|
|
repo.Counter = *in.BuildCounter
|
|
|
|
}
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2015-10-22 01:14:02 +02:00
|
|
|
err := store.UpdateRepo(c, repo)
|
2015-09-30 03:21:17 +02:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
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) {
|
|
|
|
repo := session.Repo(c)
|
|
|
|
user := session.User(c)
|
|
|
|
repo.UserID = user.ID
|
|
|
|
|
|
|
|
err := store.UpdateRepo(c, repo)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteRepo(c *gin.Context) {
|
2017-07-14 21:58:38 +02:00
|
|
|
remove, _ := strconv.ParseBool(c.Query("remove"))
|
2015-10-22 01:14:02 +02:00
|
|
|
remote := remote.FromContext(c)
|
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
|
|
|
|
|
|
|
|
err := store.UpdateRepo(c, repo)
|
2015-09-30 03:21:17 +02:00
|
|
|
if err != nil {
|
|
|
|
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 {
|
|
|
|
err := store.DeleteRepo(c, repo)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-20 16:32:52 +02:00
|
|
|
remote.Deactivate(user, repo, Config.Server.Host)
|
2017-08-25 02:03:11 +02:00
|
|
|
c.JSON(200, repo)
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
2017-04-12 15:32:44 +02:00
|
|
|
|
|
|
|
func RepairRepo(c *gin.Context) {
|
|
|
|
remote := remote.FromContext(c)
|
|
|
|
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 {
|
|
|
|
c.String(500, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// reconstruct the link
|
2021-08-20 16:32:52 +02:00
|
|
|
host := Config.Server.Host
|
2017-04-12 15:32:44 +02:00
|
|
|
link := fmt.Sprintf(
|
|
|
|
"%s/hook?access_token=%s",
|
|
|
|
host,
|
|
|
|
sig,
|
|
|
|
)
|
|
|
|
|
|
|
|
remote.Deactivate(user, repo, host)
|
|
|
|
err = remote.Activate(user, repo, link)
|
|
|
|
if err != nil {
|
|
|
|
c.String(500, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2017-08-27 00:52:57 +02:00
|
|
|
|
|
|
|
from, err := remote.Repo(user, repo.Owner, repo.Name)
|
|
|
|
if err == nil {
|
|
|
|
repo.Name = from.Name
|
|
|
|
repo.Owner = from.Owner
|
|
|
|
repo.FullName = from.FullName
|
|
|
|
repo.Avatar = from.Avatar
|
|
|
|
repo.Link = from.Link
|
|
|
|
repo.Clone = from.Clone
|
|
|
|
repo.IsPrivate = from.IsPrivate
|
|
|
|
if repo.IsPrivate != from.IsPrivate {
|
|
|
|
repo.ResetVisibility()
|
|
|
|
}
|
|
|
|
store.UpdateRepo(c, repo)
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
remote := remote.FromContext(c)
|
|
|
|
repo := session.Repo(c)
|
|
|
|
user := session.User(c)
|
|
|
|
|
|
|
|
to, exists := c.GetQuery("to")
|
|
|
|
if !exists {
|
|
|
|
err := fmt.Errorf("Missing required to query value")
|
|
|
|
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 {
|
|
|
|
c.AbortWithError(http.StatusInternalServerError, errParse)
|
2017-08-22 00:49:09 +02:00
|
|
|
return
|
2017-08-21 23:56:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
from, err := remote.Repo(user, owner, name)
|
|
|
|
if err != nil {
|
|
|
|
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
|
|
|
|
2017-08-21 23:56:37 +02:00
|
|
|
repo.Name = from.Name
|
|
|
|
repo.Owner = from.Owner
|
|
|
|
repo.FullName = from.FullName
|
|
|
|
repo.Avatar = from.Avatar
|
|
|
|
repo.Link = from.Link
|
|
|
|
repo.Clone = from.Clone
|
|
|
|
repo.IsPrivate = from.IsPrivate
|
2017-08-22 00:49:09 +02:00
|
|
|
if repo.IsPrivate != from.IsPrivate {
|
|
|
|
repo.ResetVisibility()
|
|
|
|
}
|
2017-08-21 23:56:37 +02:00
|
|
|
|
|
|
|
errStore := store.UpdateRepo(c, repo)
|
|
|
|
if errStore != nil {
|
|
|
|
c.AbortWithError(http.StatusInternalServerError, errStore)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
c.String(500, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// reconstruct the link
|
2021-08-20 16:32:52 +02:00
|
|
|
host := 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
|
|
|
|
2017-08-27 00:52:57 +02:00
|
|
|
remote.Deactivate(user, repo, host)
|
|
|
|
err = remote.Activate(user, repo, link)
|
|
|
|
if err != nil {
|
|
|
|
c.String(500, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Writer.WriteHeader(http.StatusOK)
|
|
|
|
}
|