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.
|
|
|
|
|
2015-04-09 00:00:27 +02:00
|
|
|
package remote
|
|
|
|
|
2021-09-28 12:56:59 +02:00
|
|
|
//go:generate mockery -name Remote -output mocks -case=underscore
|
2016-03-05 07:15:50 +02:00
|
|
|
|
2015-04-09 00:00:27 +02:00
|
|
|
import (
|
2021-09-28 12:56:59 +02:00
|
|
|
"context"
|
2015-04-09 00:00:27 +02:00
|
|
|
"net/http"
|
|
|
|
|
2021-09-27 19:51:55 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2015-04-09 00:00:27 +02:00
|
|
|
)
|
|
|
|
|
2021-09-27 23:32:08 +02:00
|
|
|
// TODO: use pagination
|
|
|
|
// TODO: add Driver() who return source forge back
|
|
|
|
|
2015-04-09 00:00:27 +02:00
|
|
|
type Remote interface {
|
|
|
|
// Login authenticates the session and returns the
|
|
|
|
// remote user details.
|
2021-09-28 12:56:59 +02:00
|
|
|
Login(ctx context.Context, w http.ResponseWriter, r *http.Request) (*model.User, error)
|
2015-04-09 00:00:27 +02:00
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
// Auth authenticates the session and returns the remote user
|
|
|
|
// login for the given token and secret
|
2021-09-28 12:56:59 +02:00
|
|
|
Auth(ctx context.Context, token, secret string) (string, error)
|
2015-04-15 09:20:00 +02:00
|
|
|
|
2016-04-29 21:39:56 +02:00
|
|
|
// Teams fetches a list of team memberships from the remote system.
|
2021-09-28 12:56:59 +02:00
|
|
|
Teams(ctx context.Context, u *model.User) ([]*model.Team, error)
|
2016-04-29 21:39:56 +02:00
|
|
|
|
2015-04-09 00:00:27 +02:00
|
|
|
// Repo fetches the named repository from the remote system.
|
2021-09-28 12:56:59 +02:00
|
|
|
Repo(ctx context.Context, u *model.User, owner, name string) (*model.Repo, error)
|
2015-09-30 03:21:17 +02:00
|
|
|
|
|
|
|
// Repos fetches a list of repos from the remote system.
|
2021-09-28 12:56:59 +02:00
|
|
|
Repos(ctx context.Context, u *model.User) ([]*model.Repo, error)
|
2015-04-09 00:00:27 +02:00
|
|
|
|
2015-04-22 00:48:06 +02:00
|
|
|
// Perm fetches the named repository permissions from
|
|
|
|
// the remote system for the specified user.
|
2021-09-28 12:56:59 +02:00
|
|
|
Perm(ctx context.Context, u *model.User, owner, repo string) (*model.Perm, error)
|
2015-04-09 00:00:27 +02:00
|
|
|
|
2016-03-22 12:34:33 +02:00
|
|
|
// File fetches a file from the remote repository and returns in string
|
|
|
|
// format.
|
2021-09-28 12:56:59 +02:00
|
|
|
File(ctx context.Context, u *model.User, r *model.Repo, b *model.Build, f string) ([]byte, error)
|
2015-04-09 00:00:27 +02:00
|
|
|
|
2019-06-03 09:16:15 +02:00
|
|
|
// Dir fetches a folder from the remote repository
|
2021-09-28 12:56:59 +02:00
|
|
|
Dir(ctx context.Context, u *model.User, r *model.Repo, b *model.Build, f string) ([]*FileMeta, error)
|
2019-06-03 09:16:15 +02:00
|
|
|
|
2015-04-09 00:00:27 +02:00
|
|
|
// Status sends the commit status to the remote system.
|
|
|
|
// An example would be the GitHub pull request status.
|
2021-09-28 12:56:59 +02:00
|
|
|
Status(ctx context.Context, u *model.User, r *model.Repo, b *model.Build, link string, proc *model.Proc) error
|
2015-04-09 00:00:27 +02:00
|
|
|
|
2015-04-28 23:39:48 +02:00
|
|
|
// Netrc returns a .netrc file that can be used to clone
|
|
|
|
// private repositories from a remote system.
|
2015-09-30 03:21:17 +02:00
|
|
|
Netrc(u *model.User, r *model.Repo) (*model.Netrc, error)
|
2015-04-28 23:39:48 +02:00
|
|
|
|
2016-05-01 08:22:30 +02:00
|
|
|
// Activate activates a repository by creating the post-commit hook.
|
2021-09-28 12:56:59 +02:00
|
|
|
Activate(ctx context.Context, u *model.User, r *model.Repo, link string) error
|
2015-04-09 00:00:27 +02:00
|
|
|
|
2016-05-01 08:22:30 +02:00
|
|
|
// Deactivate deactivates a repository by removing all previously created
|
|
|
|
// post-commit hooks matching the given link.
|
2021-09-28 12:56:59 +02:00
|
|
|
Deactivate(ctx context.Context, u *model.User, r *model.Repo, link string) error
|
2015-04-09 00:00:27 +02:00
|
|
|
|
2021-10-27 02:47:55 +02:00
|
|
|
// Branches returns the names of all branches for the named repository.
|
|
|
|
Branches(ctx context.Context, u *model.User, r *model.Repo) ([]string, error)
|
|
|
|
|
2016-04-29 21:39:56 +02:00
|
|
|
// Hook parses the post-commit hook from the Request body and returns the
|
|
|
|
// required data in a standard format.
|
2015-09-30 03:21:17 +02:00
|
|
|
Hook(r *http.Request) (*model.Repo, *model.Build, error)
|
2015-04-09 00:00:27 +02:00
|
|
|
}
|
2015-10-04 06:50:11 +02:00
|
|
|
|
2019-06-03 09:16:15 +02:00
|
|
|
// FileMeta represents a file in version control
|
|
|
|
type FileMeta struct {
|
|
|
|
Name string
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
2019-06-16 15:27:40 +02:00
|
|
|
type ByName []*FileMeta
|
|
|
|
|
|
|
|
func (a ByName) Len() int { return len(a) }
|
|
|
|
func (a ByName) Less(i, j int) bool { return a[i].Name < a[j].Name }
|
|
|
|
func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
|
2016-04-29 21:39:56 +02:00
|
|
|
// Refresher 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.
|
2015-10-04 06:50:11 +02:00
|
|
|
type Refresher interface {
|
2021-09-28 12:56:59 +02:00
|
|
|
Refresh(context.Context, *model.User) (bool, error)
|
2015-10-04 06:50:11 +02:00
|
|
|
}
|
2016-03-05 07:15:50 +02:00
|
|
|
|
|
|
|
// Login authenticates the session and returns the
|
|
|
|
// remote user details.
|
2016-04-29 21:39:56 +02:00
|
|
|
func Login(c context.Context, w http.ResponseWriter, r *http.Request) (*model.User, error) {
|
2021-09-28 12:56:59 +02:00
|
|
|
return FromContext(c).Login(c, w, r)
|
2016-03-05 07:15:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2021-09-28 12:56:59 +02:00
|
|
|
return FromContext(c).Auth(c, token, secret)
|
2016-03-05 07:15:50 +02:00
|
|
|
}
|
|
|
|
|
2016-04-29 21:39:56 +02:00
|
|
|
// Teams fetches a list of team memberships from the remote system.
|
|
|
|
func Teams(c context.Context, u *model.User) ([]*model.Team, error) {
|
2021-09-28 12:56:59 +02:00
|
|
|
return FromContext(c).Teams(c, u)
|
2016-04-29 21:39:56 +02:00
|
|
|
}
|
|
|
|
|
2016-03-05 07:15:50 +02:00
|
|
|
// Repo fetches the named repository from the remote system.
|
|
|
|
func Repo(c context.Context, u *model.User, owner, repo string) (*model.Repo, error) {
|
2021-09-28 12:56:59 +02:00
|
|
|
return FromContext(c).Repo(c, u, owner, repo)
|
2016-03-05 07:15:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Repos fetches a list of repos from the remote system.
|
2017-07-14 21:58:38 +02:00
|
|
|
func Repos(c context.Context, u *model.User) ([]*model.Repo, error) {
|
2021-09-28 12:56:59 +02:00
|
|
|
return FromContext(c).Repos(c, u)
|
2016-03-05 07:15:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2021-09-28 12:56:59 +02:00
|
|
|
return FromContext(c).Perm(c, u, owner, repo)
|
2016-03-05 07:15:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Status sends the commit status to the remote system.
|
|
|
|
// An example would be the GitHub pull request status.
|
2019-06-17 10:48:40 +02:00
|
|
|
func Status(c context.Context, u *model.User, r *model.Repo, b *model.Build, link string, proc *model.Proc) error {
|
2021-09-28 12:56:59 +02:00
|
|
|
return FromContext(c).Status(c, u, r, b, link, proc)
|
2016-03-05 07:15:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2016-05-01 08:22:30 +02:00
|
|
|
func Activate(c context.Context, u *model.User, r *model.Repo, link string) error {
|
2021-09-28 12:56:59 +02:00
|
|
|
return FromContext(c).Activate(c, u, r, link)
|
2016-03-05 07:15:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2021-09-28 12:56:59 +02:00
|
|
|
return FromContext(c).Deactivate(c, u, r, link)
|
2016-03-05 07:15:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2021-09-28 12:56:59 +02:00
|
|
|
return refresher.Refresh(c, u)
|
2016-03-05 07:15:50 +02:00
|
|
|
}
|