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-09-30 03:21:17 +02:00
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
2021-09-28 12:56:59 +02:00
|
|
|
"context"
|
2015-10-30 23:47:46 +02:00
|
|
|
"crypto/tls"
|
2015-09-30 03:21:17 +02:00
|
|
|
"fmt"
|
2016-05-03 02:47:58 +02:00
|
|
|
"net"
|
2015-09-30 03:21:17 +02:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2016-03-30 02:05:28 +02:00
|
|
|
"regexp"
|
2015-09-30 03:21:17 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2021-08-20 16:32:52 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server"
|
2021-09-27 19:51:55 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2021-09-23 18:25:51 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/remote"
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2021-09-29 07:59:46 +02:00
|
|
|
"github.com/google/go-github/v39/github"
|
2016-05-03 02:47:58 +02:00
|
|
|
"golang.org/x/oauth2"
|
2015-09-30 03:21:17 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-10-07 09:49:45 +02:00
|
|
|
defaultURL = "https://github.com" // Default GitHub URL
|
|
|
|
defaultAPI = "https://api.github.com/" // Default GitHub API URL
|
2015-09-30 03:21:17 +02:00
|
|
|
)
|
|
|
|
|
2016-05-03 02:47:58 +02:00
|
|
|
// Opts defines configuration options.
|
|
|
|
type Opts struct {
|
|
|
|
URL string // GitHub server url.
|
2016-05-27 20:22:32 +02:00
|
|
|
Context string // Context to display in status check
|
2016-05-03 02:47:58 +02:00
|
|
|
Client string // GitHub oauth client id.
|
|
|
|
Secret string // GitHub oauth client secret.
|
|
|
|
Scopes []string // GitHub oauth scopes
|
|
|
|
Username string // Optional machine account username.
|
|
|
|
Password string // Optional machine account password.
|
|
|
|
PrivateMode bool // GitHub is running in private mode.
|
|
|
|
SkipVerify bool // Skip ssl verification.
|
|
|
|
MergeRef bool // Clone pull requests using the merge ref.
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a Remote implementation that integrates with a GitHub Cloud or
|
|
|
|
// GitHub Enterprise version control hosting provider.
|
|
|
|
func New(opts Opts) (remote.Remote, error) {
|
2021-09-24 16:29:26 +02:00
|
|
|
u, err := url.Parse(opts.URL)
|
2016-05-03 02:47:58 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-24 16:29:26 +02:00
|
|
|
host, _, err := net.SplitHostPort(u.Host)
|
2016-05-03 02:47:58 +02:00
|
|
|
if err == nil {
|
2021-09-24 16:29:26 +02:00
|
|
|
u.Host = host
|
2016-05-03 02:47:58 +02:00
|
|
|
}
|
2021-09-24 16:29:26 +02:00
|
|
|
r := &client{
|
2016-05-03 02:47:58 +02:00
|
|
|
API: defaultAPI,
|
|
|
|
URL: defaultURL,
|
2016-05-27 20:22:32 +02:00
|
|
|
Context: opts.Context,
|
2016-05-03 02:47:58 +02:00
|
|
|
Client: opts.Client,
|
|
|
|
Secret: opts.Secret,
|
2016-05-11 16:32:11 +02:00
|
|
|
Scopes: opts.Scopes,
|
2016-05-03 02:47:58 +02:00
|
|
|
PrivateMode: opts.PrivateMode,
|
|
|
|
SkipVerify: opts.SkipVerify,
|
|
|
|
MergeRef: opts.MergeRef,
|
2021-09-24 16:29:26 +02:00
|
|
|
Machine: u.Host,
|
2016-05-03 02:47:58 +02:00
|
|
|
Username: opts.Username,
|
|
|
|
Password: opts.Password,
|
|
|
|
}
|
|
|
|
if opts.URL != defaultURL {
|
2021-09-24 16:29:26 +02:00
|
|
|
r.URL = strings.TrimSuffix(opts.URL, "/")
|
|
|
|
r.API = r.URL + "/api/v3/"
|
2016-05-03 02:47:58 +02:00
|
|
|
}
|
2016-08-26 00:00:07 +02:00
|
|
|
|
2021-09-24 16:29:26 +02:00
|
|
|
return r, nil
|
2016-05-03 02:47:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type client struct {
|
2015-09-30 03:21:17 +02:00
|
|
|
URL string
|
2016-05-27 20:22:32 +02:00
|
|
|
Context string
|
2015-09-30 03:21:17 +02:00
|
|
|
API string
|
|
|
|
Client string
|
|
|
|
Secret string
|
2016-05-11 16:32:11 +02:00
|
|
|
Scopes []string
|
2016-05-03 02:47:58 +02:00
|
|
|
Machine string
|
|
|
|
Username string
|
|
|
|
Password string
|
2015-09-30 03:21:17 +02:00
|
|
|
PrivateMode bool
|
|
|
|
SkipVerify bool
|
2016-05-03 02:47:58 +02:00
|
|
|
MergeRef bool
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
|
|
|
|
2016-04-29 21:39:56 +02:00
|
|
|
// Login authenticates the session and returns the remote user details.
|
2021-09-28 12:56:59 +02:00
|
|
|
func (c *client) Login(ctx context.Context, res http.ResponseWriter, req *http.Request) (*model.User, error) {
|
2019-06-27 08:25:00 +02:00
|
|
|
config := c.newConfig(req)
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2016-12-19 07:42:56 +02:00
|
|
|
// get the OAuth errors
|
|
|
|
if err := req.FormValue("error"); err != "" {
|
2016-12-19 18:22:11 +02:00
|
|
|
return nil, &remote.AuthError{
|
|
|
|
Err: err,
|
|
|
|
Description: req.FormValue("error_description"),
|
|
|
|
URI: req.FormValue("error_uri"),
|
2016-12-19 07:42:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the OAuth code
|
2016-05-03 02:47:58 +02:00
|
|
|
code := req.FormValue("code")
|
2015-09-30 03:21:17 +02:00
|
|
|
if len(code) == 0 {
|
2016-05-03 22:01:16 +02:00
|
|
|
// TODO(bradrydzewski) we really should be using a random value here and
|
|
|
|
// storing in a cookie for verification in the next stage of the workflow.
|
|
|
|
|
2021-10-02 10:59:34 +02:00
|
|
|
http.Redirect(res, req, config.AuthCodeURL("woodpecker"), http.StatusSeeOther)
|
2016-04-29 21:39:56 +02:00
|
|
|
return nil, nil
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
|
|
|
|
2021-09-28 12:56:59 +02:00
|
|
|
token, err := config.Exchange(c.newContext(ctx), code)
|
2015-09-30 03:21:17 +02:00
|
|
|
if err != nil {
|
2016-05-03 02:47:58 +02:00
|
|
|
return nil, err
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
|
|
|
|
2021-09-28 12:56:59 +02:00
|
|
|
client := c.newClientToken(ctx, token.AccessToken)
|
2021-09-29 07:59:46 +02:00
|
|
|
user, _, err := client.Users.Get(ctx, "")
|
2016-05-03 02:47:58 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
|
|
|
|
2021-09-29 07:59:46 +02:00
|
|
|
emails, _, err := client.Users.ListEmails(ctx, nil)
|
2016-05-03 22:01:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
email := matchingEmail(emails, c.API)
|
|
|
|
if email == nil {
|
|
|
|
return nil, fmt.Errorf("No verified Email address for GitHub account")
|
|
|
|
}
|
|
|
|
|
2016-05-03 02:47:58 +02:00
|
|
|
return &model.User{
|
2016-05-03 22:01:16 +02:00
|
|
|
Login: *user.Login,
|
|
|
|
Email: *email.Email,
|
2016-05-03 02:47:58 +02:00
|
|
|
Token: token.AccessToken,
|
2016-05-03 22:01:16 +02:00
|
|
|
Avatar: *user.AvatarURL,
|
2016-05-03 02:47:58 +02:00
|
|
|
}, nil
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
|
|
|
|
2016-05-03 02:47:58 +02:00
|
|
|
// Auth returns the GitHub user login for the given access token.
|
2021-09-28 12:56:59 +02:00
|
|
|
func (c *client) Auth(ctx context.Context, token, secret string) (string, error) {
|
|
|
|
client := c.newClientToken(ctx, token)
|
2021-09-29 07:59:46 +02:00
|
|
|
user, _, err := client.Users.Get(ctx, "")
|
2015-09-30 03:21:17 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return *user.Login, nil
|
|
|
|
}
|
|
|
|
|
2016-05-03 02:47:58 +02:00
|
|
|
// Teams returns a list of all team membership for the GitHub account.
|
2021-09-28 12:56:59 +02:00
|
|
|
func (c *client) Teams(ctx context.Context, u *model.User) ([]*model.Team, error) {
|
|
|
|
client := c.newClientToken(ctx, u.Token)
|
2016-05-03 02:47:58 +02:00
|
|
|
|
|
|
|
opts := new(github.ListOptions)
|
|
|
|
opts.Page = 1
|
2016-04-29 21:39:56 +02:00
|
|
|
|
|
|
|
var teams []*model.Team
|
2016-05-03 02:47:58 +02:00
|
|
|
for opts.Page > 0 {
|
2021-09-29 07:59:46 +02:00
|
|
|
list, resp, err := client.Organizations.List(ctx, "", opts)
|
2016-05-03 02:47:58 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
teams = append(teams, convertTeamList(list)...)
|
|
|
|
opts.Page = resp.NextPage
|
2016-04-29 21:39:56 +02:00
|
|
|
}
|
|
|
|
return teams, nil
|
|
|
|
}
|
|
|
|
|
2016-05-03 02:47:58 +02:00
|
|
|
// Repo returns the named GitHub repository.
|
2021-09-28 12:56:59 +02:00
|
|
|
func (c *client) Repo(ctx context.Context, u *model.User, owner, name string) (*model.Repo, error) {
|
|
|
|
client := c.newClientToken(ctx, u.Token)
|
2021-09-29 07:59:46 +02:00
|
|
|
repo, _, err := client.Repositories.Get(ctx, owner, name)
|
2015-09-30 03:21:17 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-05-03 02:47:58 +02:00
|
|
|
return convertRepo(repo, c.PrivateMode), nil
|
|
|
|
}
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2016-05-03 02:47:58 +02:00
|
|
|
// Repos returns a list of all repositories for GitHub account, including
|
|
|
|
// organization repositories.
|
2021-09-28 12:56:59 +02:00
|
|
|
func (c *client) Repos(ctx context.Context, u *model.User) ([]*model.Repo, error) {
|
|
|
|
client := c.newClientToken(ctx, u.Token)
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2016-05-03 02:47:58 +02:00
|
|
|
opts := new(github.RepositoryListOptions)
|
|
|
|
opts.PerPage = 100
|
|
|
|
opts.Page = 1
|
2015-10-03 01:02:37 +02:00
|
|
|
|
2017-07-14 21:58:38 +02:00
|
|
|
var repos []*model.Repo
|
2016-05-03 02:47:58 +02:00
|
|
|
for opts.Page > 0 {
|
2021-09-29 07:59:46 +02:00
|
|
|
list, resp, err := client.Repositories.List(ctx, "", opts)
|
2016-05-03 02:47:58 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-14 21:58:38 +02:00
|
|
|
repos = append(repos, convertRepoList(list, c.PrivateMode)...)
|
2016-05-03 02:47:58 +02:00
|
|
|
opts.Page = resp.NextPage
|
2015-10-03 01:02:37 +02:00
|
|
|
}
|
2016-05-03 02:47:58 +02:00
|
|
|
return repos, nil
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
|
|
|
|
2016-05-03 02:47:58 +02:00
|
|
|
// Perm returns the user permissions for the named GitHub repository.
|
2021-09-28 12:56:59 +02:00
|
|
|
func (c *client) Perm(ctx context.Context, u *model.User, owner, name string) (*model.Perm, error) {
|
|
|
|
client := c.newClientToken(ctx, u.Token)
|
2021-09-29 07:59:46 +02:00
|
|
|
repo, _, err := client.Repositories.Get(ctx, owner, name)
|
2015-09-30 03:21:17 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-05-03 02:47:58 +02:00
|
|
|
return convertPerm(repo), nil
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
|
|
|
|
2017-03-18 13:25:53 +02:00
|
|
|
// File fetches the file from the GitHub repository and returns its contents.
|
2021-09-28 12:56:59 +02:00
|
|
|
func (c *client) File(ctx context.Context, u *model.User, r *model.Repo, b *model.Build, f string) ([]byte, error) {
|
|
|
|
client := c.newClientToken(ctx, u.Token)
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2016-05-03 02:47:58 +02:00
|
|
|
opts := new(github.RepositoryContentGetOptions)
|
2019-06-03 08:49:11 +02:00
|
|
|
opts.Ref = b.Commit
|
2021-09-29 07:59:46 +02:00
|
|
|
content, _, _, err := client.Repositories.GetContents(ctx, r.Owner, r.Name, f, opts)
|
2015-09-30 03:21:17 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-29 07:59:46 +02:00
|
|
|
if content == nil {
|
2019-06-04 15:04:18 +02:00
|
|
|
return nil, fmt.Errorf("%s is a folder not a file use Dir(..)", f)
|
|
|
|
}
|
2021-09-29 07:59:46 +02:00
|
|
|
data, err := content.GetContent()
|
|
|
|
return []byte(data), err
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
|
|
|
|
2021-09-28 12:56:59 +02:00
|
|
|
func (c *client) Dir(ctx context.Context, u *model.User, r *model.Repo, b *model.Build, f string) ([]*remote.FileMeta, error) {
|
|
|
|
client := c.newClientToken(ctx, u.Token)
|
2019-06-03 09:16:15 +02:00
|
|
|
|
|
|
|
opts := new(github.RepositoryContentGetOptions)
|
|
|
|
opts.Ref = b.Commit
|
2021-09-29 07:59:46 +02:00
|
|
|
_, data, _, err := client.Repositories.GetContents(ctx, r.Owner, r.Name, f, opts)
|
2019-06-03 09:16:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-04 15:04:18 +02:00
|
|
|
fc := make(chan *remote.FileMeta)
|
|
|
|
errc := make(chan error)
|
|
|
|
|
2019-06-03 09:16:15 +02:00
|
|
|
for _, file := range data {
|
2019-06-04 15:04:18 +02:00
|
|
|
go func(path string) {
|
2021-09-28 12:56:59 +02:00
|
|
|
content, err := c.File(ctx, u, r, b, path)
|
2019-06-04 15:04:18 +02:00
|
|
|
if err != nil {
|
|
|
|
errc <- err
|
2019-06-05 10:08:49 +02:00
|
|
|
} else {
|
|
|
|
fc <- &remote.FileMeta{
|
|
|
|
Name: path,
|
|
|
|
Data: content,
|
|
|
|
}
|
2019-06-04 15:04:18 +02:00
|
|
|
}
|
|
|
|
}(f + "/" + *file.Name)
|
2019-06-03 09:16:15 +02:00
|
|
|
}
|
|
|
|
|
2019-06-04 15:04:18 +02:00
|
|
|
var files []*remote.FileMeta
|
|
|
|
var errors []error
|
|
|
|
|
2019-07-17 13:58:47 +02:00
|
|
|
for i := 0; i < len(data); i++ {
|
|
|
|
select {
|
|
|
|
case err, _ := <-errc:
|
|
|
|
errors = append(errors, err)
|
|
|
|
case fileMeta, _ := <-fc:
|
|
|
|
files = append(files, fileMeta)
|
2019-06-04 15:04:18 +02:00
|
|
|
}
|
2019-07-17 13:58:47 +02:00
|
|
|
}
|
2019-06-04 15:04:18 +02:00
|
|
|
|
|
|
|
close(fc)
|
|
|
|
close(errc)
|
|
|
|
|
2019-06-03 09:16:15 +02:00
|
|
|
return files, nil
|
|
|
|
}
|
|
|
|
|
2016-05-03 02:47:58 +02:00
|
|
|
// Netrc returns a netrc file capable of authenticating GitHub requests and
|
|
|
|
// cloning GitHub repositories. The netrc will use the global machine account
|
|
|
|
// when configured.
|
|
|
|
func (c *client) Netrc(u *model.User, r *model.Repo) (*model.Netrc, error) {
|
|
|
|
if c.Password != "" {
|
|
|
|
return &model.Netrc{
|
|
|
|
Login: c.Username,
|
|
|
|
Password: c.Password,
|
|
|
|
Machine: c.Machine,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return &model.Netrc{
|
|
|
|
Login: u.Token,
|
|
|
|
Password: "x-oauth-basic",
|
|
|
|
Machine: c.Machine,
|
|
|
|
}, nil
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
|
|
|
|
2016-05-03 22:01:16 +02:00
|
|
|
// Deactivate deactives the repository be removing registered push hooks from
|
|
|
|
// the GitHub repository.
|
2021-09-28 12:56:59 +02:00
|
|
|
func (c *client) Deactivate(ctx context.Context, u *model.User, r *model.Repo, link string) error {
|
|
|
|
client := c.newClientToken(ctx, u.Token)
|
2021-09-29 07:59:46 +02:00
|
|
|
hooks, _, err := client.Repositories.ListHooks(ctx, r.Owner, r.Name, nil)
|
2016-05-03 22:01:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
match := matchingHooks(hooks, link)
|
|
|
|
if match == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-29 07:59:46 +02:00
|
|
|
_, err = client.Repositories.DeleteHook(ctx, r.Owner, r.Name, *match.ID)
|
2016-05-03 22:01:16 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function to return the GitHub oauth2 context using an HTTPClient that
|
|
|
|
// disables TLS verification if disabled in the remote settings.
|
2021-09-28 12:56:59 +02:00
|
|
|
func (c *client) newContext(ctx context.Context) context.Context {
|
2016-05-03 22:01:16 +02:00
|
|
|
if !c.SkipVerify {
|
2021-09-28 12:56:59 +02:00
|
|
|
return ctx
|
2016-05-03 22:01:16 +02:00
|
|
|
}
|
2021-09-28 12:56:59 +02:00
|
|
|
return context.WithValue(ctx, oauth2.HTTPClient, &http.Client{
|
2016-05-03 22:01:16 +02:00
|
|
|
Transport: &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function to return the GitHub oauth2 config
|
2019-06-27 08:25:00 +02:00
|
|
|
func (c *client) newConfig(req *http.Request) *oauth2.Config {
|
|
|
|
var redirect string
|
|
|
|
|
|
|
|
intendedURL := req.URL.Query()["url"]
|
|
|
|
if len(intendedURL) > 0 {
|
2021-08-20 16:32:52 +02:00
|
|
|
redirect = fmt.Sprintf("%s/authorize?url=%s", server.Config.Server.Host, intendedURL[0])
|
2019-06-27 08:25:00 +02:00
|
|
|
} else {
|
2021-08-20 16:32:52 +02:00
|
|
|
redirect = fmt.Sprintf("%s/authorize", server.Config.Server.Host)
|
2019-06-27 08:25:00 +02:00
|
|
|
}
|
|
|
|
|
2016-05-03 02:47:58 +02:00
|
|
|
return &oauth2.Config{
|
|
|
|
ClientID: c.Client,
|
|
|
|
ClientSecret: c.Secret,
|
2016-05-27 20:22:32 +02:00
|
|
|
Scopes: c.Scopes,
|
2016-05-03 02:47:58 +02:00
|
|
|
Endpoint: oauth2.Endpoint{
|
|
|
|
AuthURL: fmt.Sprintf("%s/login/oauth/authorize", c.URL),
|
|
|
|
TokenURL: fmt.Sprintf("%s/login/oauth/access_token", c.URL),
|
|
|
|
},
|
2019-06-27 08:25:00 +02:00
|
|
|
RedirectURL: redirect,
|
2016-05-03 02:47:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 22:01:16 +02:00
|
|
|
// helper function to return the GitHub oauth2 client
|
2021-09-28 12:56:59 +02:00
|
|
|
func (c *client) newClientToken(ctx context.Context, token string) *github.Client {
|
2016-05-03 02:47:58 +02:00
|
|
|
ts := oauth2.StaticTokenSource(
|
|
|
|
&oauth2.Token{AccessToken: token},
|
|
|
|
)
|
2021-09-28 12:56:59 +02:00
|
|
|
tc := oauth2.NewClient(ctx, ts)
|
2016-05-03 02:47:58 +02:00
|
|
|
if c.SkipVerify {
|
|
|
|
tc.Transport.(*oauth2.Transport).Base = &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2021-09-24 16:29:26 +02:00
|
|
|
client := github.NewClient(tc)
|
|
|
|
client.BaseURL, _ = url.Parse(c.API)
|
|
|
|
return client
|
2016-05-03 02:47:58 +02:00
|
|
|
}
|
|
|
|
|
2016-05-03 22:01:16 +02:00
|
|
|
// helper function to return matching user email.
|
2021-09-29 07:59:46 +02:00
|
|
|
func matchingEmail(emails []*github.UserEmail, rawURL string) *github.UserEmail {
|
2016-05-03 22:01:16 +02:00
|
|
|
for _, email := range emails {
|
|
|
|
if email.Email == nil || email.Primary == nil || email.Verified == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if *email.Primary && *email.Verified {
|
2021-09-29 07:59:46 +02:00
|
|
|
return email
|
2016-05-03 22:01:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// github enterprise does not support verified email addresses so instead
|
|
|
|
// we'll return the first email address in the list.
|
2021-09-29 07:59:46 +02:00
|
|
|
if len(emails) != 0 && rawURL != defaultAPI {
|
|
|
|
return emails[0]
|
2016-05-03 22:01:16 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function to return matching hook.
|
2021-09-29 07:59:46 +02:00
|
|
|
func matchingHooks(hooks []*github.Hook, rawurl string) *github.Hook {
|
2016-05-03 22:01:16 +02:00
|
|
|
link, err := url.Parse(rawurl)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, hook := range hooks {
|
|
|
|
if hook.ID == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
v, ok := hook.Config["url"]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s, ok := v.(string)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2021-09-29 07:59:46 +02:00
|
|
|
hookURL, err := url.Parse(s)
|
|
|
|
if err == nil && hookURL.Host == link.Host {
|
|
|
|
return hook
|
2016-05-03 22:01:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-03 02:47:58 +02:00
|
|
|
//
|
|
|
|
// TODO(bradrydzewski) refactor below functions
|
|
|
|
//
|
|
|
|
|
2015-09-30 03:21:17 +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
|
|
|
func (c *client) Status(ctx context.Context, u *model.User, r *model.Repo, b *model.Build, link string, proc *model.Proc) error {
|
|
|
|
client := c.newClientToken(ctx, u.Token)
|
2016-05-03 02:47:58 +02:00
|
|
|
switch b.Event {
|
|
|
|
case "deployment":
|
2021-09-29 07:59:46 +02:00
|
|
|
return deploymentStatus(ctx, client, r, b, link)
|
2016-05-03 02:47:58 +02:00
|
|
|
default:
|
2021-09-29 07:59:46 +02:00
|
|
|
return repoStatus(ctx, client, r, b, link, c.Context, proc)
|
2016-03-29 20:07:50 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2021-09-29 07:59:46 +02:00
|
|
|
func repoStatus(c context.Context, client *github.Client, r *model.Repo, b *model.Build, link, ctx string, proc *model.Proc) error {
|
2016-05-12 18:34:30 +02:00
|
|
|
switch b.Event {
|
|
|
|
case model.EventPull:
|
2021-09-24 16:29:26 +02:00
|
|
|
ctx += "/pr"
|
2016-05-12 18:34:30 +02:00
|
|
|
default:
|
|
|
|
if len(b.Event) > 0 {
|
2021-09-24 16:29:26 +02:00
|
|
|
ctx += "/" + b.Event
|
2016-05-12 18:34:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-17 10:48:40 +02:00
|
|
|
status := github.String(convertStatus(b.Status))
|
|
|
|
desc := github.String(convertDesc(b.Status))
|
|
|
|
|
|
|
|
if proc != nil {
|
2021-09-24 16:29:26 +02:00
|
|
|
ctx += "/" + proc.Name
|
2019-06-17 10:48:40 +02:00
|
|
|
status = github.String(convertStatus(proc.State))
|
|
|
|
desc = github.String(convertDesc(proc.State))
|
|
|
|
}
|
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
data := github.RepoStatus{
|
2021-09-24 16:29:26 +02:00
|
|
|
Context: github.String(ctx),
|
2019-06-17 10:48:40 +02:00
|
|
|
State: status,
|
|
|
|
Description: desc,
|
2015-09-30 03:21:17 +02:00
|
|
|
TargetURL: github.String(link),
|
|
|
|
}
|
2021-09-29 07:59:46 +02:00
|
|
|
_, _, err := client.Repositories.CreateStatus(c, r.Owner, r.Name, b.Commit, &data)
|
2015-09-30 03:21:17 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-29 21:39:56 +02:00
|
|
|
var reDeploy = regexp.MustCompile(".+/deployments/(\\d+)")
|
|
|
|
|
2021-09-29 07:59:46 +02:00
|
|
|
func deploymentStatus(ctx context.Context, client *github.Client, r *model.Repo, b *model.Build, link string) error {
|
2016-04-29 21:39:56 +02:00
|
|
|
matches := reDeploy.FindStringSubmatch(b.Link)
|
2016-03-30 02:05:28 +02:00
|
|
|
if len(matches) != 2 {
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-30 13:58:24 +02:00
|
|
|
id, _ := strconv.Atoi(matches[1])
|
2016-05-03 02:47:58 +02:00
|
|
|
|
2016-03-29 20:07:50 +02:00
|
|
|
data := github.DeploymentStatusRequest{
|
2016-05-03 02:47:58 +02:00
|
|
|
State: github.String(convertStatus(b.Status)),
|
|
|
|
Description: github.String(convertDesc(b.Status)),
|
2021-09-29 07:59:46 +02:00
|
|
|
LogURL: github.String(link),
|
2016-03-29 20:07:50 +02:00
|
|
|
}
|
2021-09-29 07:59:46 +02:00
|
|
|
_, _, err := client.Repositories.CreateDeploymentStatus(ctx, r.Owner, r.Name, int64(id), &data)
|
2016-03-29 20:07:50 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
// Activate activates a repository by creating the post-commit hook and
|
|
|
|
// adding the SSH deploy key, if applicable.
|
2021-09-28 12:56:59 +02:00
|
|
|
func (c *client) Activate(ctx context.Context, u *model.User, r *model.Repo, link string) error {
|
|
|
|
if err := c.Deactivate(ctx, u, r, link); err != nil {
|
2016-05-03 22:01:16 +02:00
|
|
|
return err
|
|
|
|
}
|
2021-09-28 12:56:59 +02:00
|
|
|
client := c.newClientToken(ctx, u.Token)
|
2016-05-03 22:01:16 +02:00
|
|
|
hook := &github.Hook{
|
|
|
|
Name: github.String("web"),
|
|
|
|
Events: []string{
|
|
|
|
"push",
|
|
|
|
"pull_request",
|
|
|
|
"deployment",
|
|
|
|
},
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"url": link,
|
|
|
|
"content_type": "form",
|
|
|
|
},
|
|
|
|
}
|
2021-09-29 07:59:46 +02:00
|
|
|
_, _, err := client.Repositories.CreateHook(ctx, r.Owner, r.Name, hook)
|
2015-09-30 03:21:17 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hook parses the post-commit hook from the Request body
|
|
|
|
// and returns the required data in a standard format.
|
2016-05-03 02:47:58 +02:00
|
|
|
func (c *client) Hook(r *http.Request) (*model.Repo, *model.Build, error) {
|
2016-05-03 22:01:16 +02:00
|
|
|
return parseHook(r, c.MergeRef)
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|