woodpecker/remote/remote.go

186 lines
6.4 KiB
Go
Raw Normal View History

2018-02-19 22:24:10 +00:00
// 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.
2015-04-08 22:00:27 +00:00
package remote
//go:generate mockery -name Remote -output mock -case=underscore
2015-04-08 22:00:27 +00:00
import (
"net/http"
2016-12-14 05:45:54 +00:00
"time"
2015-04-08 22:00:27 +00:00
2015-09-30 01:21:17 +00:00
"github.com/drone/drone/model"
2015-08-06 15:54:47 +00:00
"golang.org/x/net/context"
2015-04-08 22:00:27 +00:00
)
type Remote interface {
// Login authenticates the session and returns the
// remote user details.
2016-04-29 19:39:56 +00:00
Login(w http.ResponseWriter, r *http.Request) (*model.User, error)
2015-04-08 22:00:27 +00:00
2015-09-30 01:21:17 +00:00
// Auth authenticates the session and returns the remote user
// login for the given token and secret
Auth(token, secret string) (string, error)
2016-04-29 19:39:56 +00:00
// Teams fetches a list of team memberships from the remote system.
Teams(u *model.User) ([]*model.Team, error)
2015-04-08 22:00:27 +00:00
// Repo fetches the named repository from the remote system.
2015-09-30 01:21:17 +00:00
Repo(u *model.User, owner, repo string) (*model.Repo, error)
// Repos fetches a list of repos from the remote system.
2017-07-14 19:58:38 +00:00
Repos(u *model.User) ([]*model.Repo, error)
2015-04-08 22:00:27 +00:00
// Perm fetches the named repository permissions from
// the remote system for the specified user.
2015-09-30 01:21:17 +00:00
Perm(u *model.User, owner, repo string) (*model.Perm, error)
2015-04-08 22:00:27 +00:00
// File fetches a file from the remote repository and returns in string
// format.
File(u *model.User, r *model.Repo, b *model.Build, f string) ([]byte, error)
2015-04-08 22:00:27 +00:00
2017-03-18 11:25:53 +00:00
// FileRef fetches a file from the remote repository for the given ref
// and returns in string format.
FileRef(u *model.User, r *model.Repo, ref, f string) ([]byte, error)
2015-04-08 22:00:27 +00:00
// Status sends the commit status to the remote system.
// An example would be the GitHub pull request status.
2015-09-30 01:21:17 +00:00
Status(u *model.User, r *model.Repo, b *model.Build, link string) error
2015-04-08 22:00:27 +00:00
2015-04-28 21:39:48 +00:00
// Netrc returns a .netrc file that can be used to clone
// private repositories from a remote system.
2015-09-30 01:21:17 +00:00
Netrc(u *model.User, r *model.Repo) (*model.Netrc, error)
2015-04-28 21:39:48 +00:00
2016-05-01 06:22:30 +00:00
// Activate activates a repository by creating the post-commit hook.
Activate(u *model.User, r *model.Repo, link string) error
2015-04-08 22:00:27 +00:00
2016-05-01 06:22:30 +00:00
// Deactivate deactivates a repository by removing all previously created
// post-commit hooks matching the given link.
2015-09-30 01:21:17 +00:00
Deactivate(u *model.User, r *model.Repo, link string) error
2015-04-08 22:00:27 +00:00
2016-04-29 19:39:56 +00:00
// Hook parses the post-commit hook from the Request body and returns the
// required data in a standard format.
2015-09-30 01:21:17 +00:00
Hook(r *http.Request) (*model.Repo, *model.Build, error)
2015-04-08 22:00:27 +00:00
}
2016-04-29 19:39:56 +00: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.
type Refresher interface {
Refresh(*model.User) (bool, error)
}
// Login authenticates the session and returns the
// remote user details.
2016-04-29 19:39:56 +00:00
func Login(c context.Context, w http.ResponseWriter, r *http.Request) (*model.User, error) {
return FromContext(c).Login(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(token, secret)
}
2016-04-29 19:39:56 +00:00
// 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(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(u, owner, repo)
}
// Repos fetches a list of repos from the remote system.
2017-07-14 19:58:38 +00:00
func Repos(c context.Context, u *model.User) ([]*model.Repo, error) {
return FromContext(c).Repos(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(u, owner, repo)
}
// File fetches a file from the remote repository and returns in string format.
2016-12-14 05:45:54 +00:00
func File(c context.Context, u *model.User, r *model.Repo, b *model.Build, f string) (out []byte, err error) {
for i := 0; i < 12; i++ {
2016-12-14 05:45:54 +00:00
out, err = FromContext(c).File(u, r, b, f)
if err == nil {
return
}
time.Sleep(5 * time.Second)
2016-12-14 05:45:54 +00:00
}
return
}
// 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) error {
return FromContext(c).Status(u, r, b, link)
}
// 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 06:22:30 +00:00
func Activate(c context.Context, u *model.User, r *model.Repo, link string) error {
return FromContext(c).Activate(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(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(u)
}
2017-12-01 17:22:30 +00:00
// FileBackoff fetches the file using an exponential backoff.
// TODO replace this with a proper backoff
func FileBackoff(remote Remote, u *model.User, r *model.Repo, b *model.Build, f string) (out []byte, err error) {
for i := 0; i < 5; i++ {
select {
case <-time.After(time.Second * time.Duration(i)):
out, err = remote.File(u, r, b, f)
if err == nil {
return
}
}
}
return
}