mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-20 23:46:49 +00:00
Server obtain remote from glob config not from context (#540)
This commit is contained in:
parent
8b476e7722
commit
5e6b38e0e7
13 changed files with 29 additions and 168 deletions
|
@ -98,7 +98,7 @@ func run(c *cli.Context) error {
|
|||
)
|
||||
}
|
||||
|
||||
remote_, err := SetupRemote(c)
|
||||
remote_, err := setupRemote(c)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("")
|
||||
}
|
||||
|
@ -142,7 +142,6 @@ func run(c *cli.Context) error {
|
|||
middleware.Version,
|
||||
middleware.Config(c),
|
||||
middleware.Store(c, store_),
|
||||
middleware.Remote(remote_),
|
||||
)
|
||||
|
||||
var g errgroup.Group
|
||||
|
@ -253,6 +252,9 @@ func setupEvilGlobals(c *cli.Context, v store.Store, r remote.Remote) {
|
|||
server.Config.Storage.Files = v
|
||||
server.Config.Storage.Config = v
|
||||
|
||||
// remote
|
||||
server.Config.Services.Remote = r
|
||||
|
||||
// services
|
||||
server.Config.Services.Queue = setupQueue(c, v)
|
||||
server.Config.Services.Logs = logging.New()
|
||||
|
|
|
@ -175,8 +175,8 @@ func setupEnvironService(c *cli.Context, s store.Store) model.EnvironService {
|
|||
return environments.Filesystem(c.StringSlice("environment"))
|
||||
}
|
||||
|
||||
// SetupRemote helper function to setup the remote from the CLI arguments.
|
||||
func SetupRemote(c *cli.Context) (remote.Remote, error) {
|
||||
// setupRemote helper function to setup the remote from the CLI arguments.
|
||||
func setupRemote(c *cli.Context) (remote.Remote, error) {
|
||||
switch {
|
||||
case c.Bool("github"):
|
||||
return setupGithub(c)
|
||||
|
|
|
@ -261,7 +261,7 @@ func DeleteBuild(c *gin.Context) {
|
|||
|
||||
func PostApproval(c *gin.Context) {
|
||||
var (
|
||||
remote_ = remote.FromContext(c)
|
||||
remote_ = server.Config.Services.Remote
|
||||
store_ = store.FromContext(c)
|
||||
repo = session.Repo(c)
|
||||
user = session.User(c)
|
||||
|
@ -372,7 +372,7 @@ func PostApproval(c *gin.Context) {
|
|||
|
||||
func PostDecline(c *gin.Context) {
|
||||
var (
|
||||
remote_ = remote.FromContext(c)
|
||||
remote_ = server.Config.Services.Remote
|
||||
store_ = store.FromContext(c)
|
||||
|
||||
repo = session.Repo(c)
|
||||
|
@ -415,7 +415,7 @@ func GetBuildQueue(c *gin.Context) {
|
|||
|
||||
// PostBuild restarts a build
|
||||
func PostBuild(c *gin.Context) {
|
||||
remote_ := remote.FromContext(c)
|
||||
remote_ := server.Config.Services.Remote
|
||||
store_ := store.FromContext(c)
|
||||
repo := session.Repo(c)
|
||||
|
||||
|
|
|
@ -76,7 +76,7 @@ func BlockTilQueueHasRunningItem(c *gin.Context) {
|
|||
}
|
||||
|
||||
func PostHook(c *gin.Context) {
|
||||
remote_ := remote.FromContext(c)
|
||||
remote_ := server.Config.Services.Remote
|
||||
store_ := store.FromContext(c)
|
||||
|
||||
tmpRepo, build, err := remote_.Hook(c.Request)
|
||||
|
|
|
@ -25,7 +25,6 @@ import (
|
|||
|
||||
"github.com/woodpecker-ci/woodpecker/server"
|
||||
"github.com/woodpecker-ci/woodpecker/server/model"
|
||||
"github.com/woodpecker-ci/woodpecker/server/remote"
|
||||
"github.com/woodpecker-ci/woodpecker/server/store"
|
||||
"github.com/woodpecker-ci/woodpecker/shared/httputil"
|
||||
"github.com/woodpecker-ci/woodpecker/shared/token"
|
||||
|
@ -55,7 +54,7 @@ func HandleAuth(c *gin.Context) {
|
|||
// cannot, however, remember why, so need to revisit this line.
|
||||
c.Writer.Header().Del("Content-Type")
|
||||
|
||||
tmpuser, err := remote.Login(c, c.Writer, c.Request)
|
||||
tmpuser, err := server.Config.Services.Remote.Login(c, c.Writer, c.Request)
|
||||
if err != nil {
|
||||
log.Error().Msgf("cannot authenticate user. %s", err)
|
||||
c.Redirect(303, "/login?error=oauth_error")
|
||||
|
@ -81,7 +80,7 @@ func HandleAuth(c *gin.Context) {
|
|||
// if self-registration is enabled for whitelisted organizations we need to
|
||||
// check the user's organization membership.
|
||||
if len(config.Orgs) != 0 {
|
||||
teams, terr := remote.Teams(c, tmpuser)
|
||||
teams, terr := server.Config.Services.Remote.Teams(c, tmpuser)
|
||||
if terr != nil || !config.IsMember(teams) {
|
||||
log.Error().Msgf("cannot verify team membership for %s.", u.Login)
|
||||
c.Redirect(303, "/login?error=access_denied")
|
||||
|
@ -118,7 +117,7 @@ func HandleAuth(c *gin.Context) {
|
|||
// if self-registration is enabled for whitelisted organizations we need to
|
||||
// check the user's organization membership.
|
||||
if len(config.Orgs) != 0 {
|
||||
teams, terr := remote.Teams(c, u)
|
||||
teams, terr := server.Config.Services.Remote.Teams(c, u)
|
||||
if terr != nil || !config.IsMember(teams) {
|
||||
log.Error().Msgf("cannot verify team membership for %s.", u.Login)
|
||||
c.Redirect(303, "/login?error=access_denied")
|
||||
|
@ -166,7 +165,7 @@ func GetLoginToken(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
login, err := remote.Auth(c, in.Access, in.Refresh)
|
||||
login, err := server.Config.Services.Remote.Auth(c, in.Access, in.Refresh)
|
||||
if err != nil {
|
||||
_ = c.AbortWithError(http.StatusUnauthorized, err)
|
||||
return
|
||||
|
|
|
@ -26,14 +26,13 @@ import (
|
|||
|
||||
"github.com/woodpecker-ci/woodpecker/server"
|
||||
"github.com/woodpecker-ci/woodpecker/server/model"
|
||||
"github.com/woodpecker-ci/woodpecker/server/remote"
|
||||
"github.com/woodpecker-ci/woodpecker/server/router/middleware/session"
|
||||
"github.com/woodpecker-ci/woodpecker/server/store"
|
||||
"github.com/woodpecker-ci/woodpecker/shared/token"
|
||||
)
|
||||
|
||||
func PostRepo(c *gin.Context) {
|
||||
remote_ := remote.FromContext(c)
|
||||
remote_ := server.Config.Services.Remote
|
||||
store_ := store.FromContext(c)
|
||||
user := session.User(c)
|
||||
repo := session.Repo(c)
|
||||
|
@ -177,7 +176,7 @@ func GetRepoPermissions(c *gin.Context) {
|
|||
func GetRepoBranches(c *gin.Context) {
|
||||
repo := session.Repo(c)
|
||||
user := session.User(c)
|
||||
r := remote.FromContext(c)
|
||||
r := server.Config.Services.Remote
|
||||
|
||||
branches, err := r.Branches(c, user, repo)
|
||||
if err != nil {
|
||||
|
@ -190,7 +189,7 @@ func GetRepoBranches(c *gin.Context) {
|
|||
|
||||
func DeleteRepo(c *gin.Context) {
|
||||
remove, _ := strconv.ParseBool(c.Query("remove"))
|
||||
remote_ := remote.FromContext(c)
|
||||
remote_ := server.Config.Services.Remote
|
||||
store_ := store.FromContext(c)
|
||||
|
||||
repo := session.Repo(c)
|
||||
|
@ -221,7 +220,7 @@ func DeleteRepo(c *gin.Context) {
|
|||
}
|
||||
|
||||
func RepairRepo(c *gin.Context) {
|
||||
remote_ := remote.FromContext(c)
|
||||
remote_ := server.Config.Services.Remote
|
||||
store_ := store.FromContext(c)
|
||||
repo := session.Repo(c)
|
||||
user := session.User(c)
|
||||
|
@ -275,7 +274,7 @@ func RepairRepo(c *gin.Context) {
|
|||
}
|
||||
|
||||
func MoveRepo(c *gin.Context) {
|
||||
remote_ := remote.FromContext(c)
|
||||
remote_ := server.Config.Services.Remote
|
||||
store_ := store.FromContext(c)
|
||||
repo := session.Repo(c)
|
||||
user := session.User(c)
|
||||
|
|
|
@ -24,8 +24,8 @@ import (
|
|||
"github.com/gorilla/securecookie"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/woodpecker-ci/woodpecker/server"
|
||||
"github.com/woodpecker-ci/woodpecker/server/model"
|
||||
"github.com/woodpecker-ci/woodpecker/server/remote"
|
||||
"github.com/woodpecker-ci/woodpecker/server/router/middleware/session"
|
||||
"github.com/woodpecker-ci/woodpecker/server/shared"
|
||||
"github.com/woodpecker-ci/woodpecker/server/store"
|
||||
|
@ -38,7 +38,7 @@ func GetSelf(c *gin.Context) {
|
|||
|
||||
func GetFeed(c *gin.Context) {
|
||||
store_ := store.FromContext(c)
|
||||
remote_ := remote.FromContext(c)
|
||||
remote_ := server.Config.Services.Remote
|
||||
|
||||
user := session.User(c)
|
||||
latest, _ := strconv.ParseBool(c.Query("latest"))
|
||||
|
@ -87,7 +87,7 @@ func GetFeed(c *gin.Context) {
|
|||
|
||||
func GetRepos(c *gin.Context) {
|
||||
store_ := store.FromContext(c)
|
||||
remote_ := remote.FromContext(c)
|
||||
remote_ := server.Config.Services.Remote
|
||||
|
||||
user := session.User(c)
|
||||
all, _ := strconv.ParseBool(c.Query("all"))
|
||||
|
|
|
@ -24,6 +24,7 @@ import (
|
|||
"github.com/woodpecker-ci/woodpecker/server/model"
|
||||
"github.com/woodpecker-ci/woodpecker/server/pubsub"
|
||||
"github.com/woodpecker-ci/woodpecker/server/queue"
|
||||
"github.com/woodpecker-ci/woodpecker/server/remote"
|
||||
)
|
||||
|
||||
var Config = struct {
|
||||
|
@ -35,6 +36,7 @@ var Config = struct {
|
|||
Secrets model.SecretService
|
||||
Registries model.RegistryService
|
||||
Environ model.EnvironService
|
||||
Remote remote.Remote
|
||||
}
|
||||
Storage struct {
|
||||
// Users model.UserStore
|
||||
|
|
|
@ -1,37 +0,0 @@
|
|||
// Copyright 2018 Drone.IO Inc.
|
||||
//
|
||||
// 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 remote
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
const key = "remote"
|
||||
|
||||
// Setter defines a context that enables setting values.
|
||||
type Setter interface {
|
||||
Set(string, interface{})
|
||||
}
|
||||
|
||||
// FromContext returns the Remote associated with this context.
|
||||
func FromContext(c context.Context) Remote {
|
||||
return c.Value(key).(Remote)
|
||||
}
|
||||
|
||||
// ToContext adds the Remote to this context if it supports
|
||||
// the Setter interface.
|
||||
func ToContext(c Setter, r Remote) {
|
||||
c.Set(key, r)
|
||||
}
|
|
@ -96,78 +96,3 @@ func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|||
type Refresher interface {
|
||||
Refresh(context.Context, *model.User) (bool, error)
|
||||
}
|
||||
|
||||
// Login authenticates the session and returns the
|
||||
// remote user details.
|
||||
func Login(c context.Context, w http.ResponseWriter, r *http.Request) (*model.User, error) {
|
||||
return FromContext(c).Login(c, w, r)
|
||||
}
|
||||
|
||||
// Auth authenticates the session and returns the remote user
|
||||
// login for the given token and secret
|
||||
func Auth(c context.Context, token, secret string) (string, error) {
|
||||
return FromContext(c).Auth(c, token, secret)
|
||||
}
|
||||
|
||||
// Teams fetches a list of team memberships from the remote system.
|
||||
func Teams(c context.Context, u *model.User) ([]*model.Team, error) {
|
||||
return FromContext(c).Teams(c, u)
|
||||
}
|
||||
|
||||
// Repo fetches the named repository from the remote system.
|
||||
func Repo(c context.Context, u *model.User, owner, repo string) (*model.Repo, error) {
|
||||
return FromContext(c).Repo(c, u, owner, repo)
|
||||
}
|
||||
|
||||
// Repos fetches a list of repos from the remote system.
|
||||
func Repos(c context.Context, u *model.User) ([]*model.Repo, error) {
|
||||
return FromContext(c).Repos(c, u)
|
||||
}
|
||||
|
||||
// Perm fetches the named repository permissions from
|
||||
// the remote system for the specified user.
|
||||
func Perm(c context.Context, u *model.User, owner, repo string) (*model.Perm, error) {
|
||||
return FromContext(c).Perm(c, u, owner, repo)
|
||||
}
|
||||
|
||||
// Status sends the commit status to the remote system.
|
||||
// An example would be the GitHub pull request status.
|
||||
func Status(c context.Context, u *model.User, r *model.Repo, b *model.Build, link string, proc *model.Proc) error {
|
||||
return FromContext(c).Status(c, u, r, b, link, proc)
|
||||
}
|
||||
|
||||
// Netrc returns a .netrc file that can be used to clone
|
||||
// private repositories from a remote system.
|
||||
func Netrc(c context.Context, u *model.User, r *model.Repo) (*model.Netrc, error) {
|
||||
return FromContext(c).Netrc(u, r)
|
||||
}
|
||||
|
||||
// Activate activates a repository by creating the post-commit hook and
|
||||
// adding the SSH deploy key, if applicable.
|
||||
func Activate(c context.Context, u *model.User, r *model.Repo, link string) error {
|
||||
return FromContext(c).Activate(c, u, r, link)
|
||||
}
|
||||
|
||||
// Deactivate removes a repository by removing all the post-commit hooks
|
||||
// which are equal to link and removing the SSH deploy key.
|
||||
func Deactivate(c context.Context, u *model.User, r *model.Repo, link string) error {
|
||||
return FromContext(c).Deactivate(c, u, r, link)
|
||||
}
|
||||
|
||||
// Hook parses the post-commit hook from the Request body
|
||||
// and returns the required data in a standard format.
|
||||
func Hook(c context.Context, r *http.Request) (*model.Repo, *model.Build, error) {
|
||||
return FromContext(c).Hook(r)
|
||||
}
|
||||
|
||||
// Refresh refreshes an oauth token and expiration for the given
|
||||
// user. It returns true if the token was refreshed, false if the
|
||||
// token was not refreshed, and error if it failed to refersh.
|
||||
func Refresh(c context.Context, u *model.User) (bool, error) {
|
||||
remote := FromContext(c)
|
||||
refresher, ok := remote.(Refresher)
|
||||
if !ok {
|
||||
return false, nil
|
||||
}
|
||||
return refresher.Refresh(c, u)
|
||||
}
|
||||
|
|
|
@ -1,29 +0,0 @@
|
|||
// Copyright 2018 Drone.IO Inc.
|
||||
//
|
||||
// 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 middleware
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/woodpecker-ci/woodpecker/server/remote"
|
||||
)
|
||||
|
||||
// Remote is a middleware function that initializes the Remote and attaches to
|
||||
// the context of every http.Request.
|
||||
func Remote(v remote.Remote) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
remote.ToContext(c, v)
|
||||
}
|
||||
}
|
|
@ -18,12 +18,11 @@ import (
|
|||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/woodpecker-ci/woodpecker/server"
|
||||
"github.com/woodpecker-ci/woodpecker/server/model"
|
||||
"github.com/woodpecker-ci/woodpecker/server/remote"
|
||||
"github.com/woodpecker-ci/woodpecker/server/store"
|
||||
)
|
||||
|
||||
|
@ -98,7 +97,7 @@ func SetPerm() gin.HandlerFunc {
|
|||
user.Login, repo.FullName, err)
|
||||
}
|
||||
if time.Unix(perm.Synced, 0).Add(time.Hour).Before(time.Now()) {
|
||||
perm, err = remote.FromContext(c).Perm(c, user, repo.Owner, repo.Name)
|
||||
perm, err = server.Config.Services.Remote.Perm(c, user, repo.Owner, repo.Name)
|
||||
if err == nil {
|
||||
log.Debug().Msgf("Synced user permission for %s %s", user.Login, repo.FullName)
|
||||
perm.Repo = repo.FullName
|
||||
|
|
|
@ -21,6 +21,7 @@ import (
|
|||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/woodpecker-ci/woodpecker/server"
|
||||
"github.com/woodpecker-ci/woodpecker/server/remote"
|
||||
"github.com/woodpecker-ci/woodpecker/server/router/middleware/session"
|
||||
"github.com/woodpecker-ci/woodpecker/server/store"
|
||||
|
@ -35,7 +36,7 @@ func Refresh(c *gin.Context) {
|
|||
|
||||
// check if the remote includes the ability to
|
||||
// refresh the user token.
|
||||
remote_ := remote.FromContext(c)
|
||||
remote_ := server.Config.Services.Remote
|
||||
refresher, ok := remote_.(remote.Refresher)
|
||||
if !ok {
|
||||
c.Next()
|
||||
|
|
Loading…
Reference in a new issue