2022-10-18 01:24:12 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00: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 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00: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 01:21:17 +00:00
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
2021-09-28 10:56:59 +00:00
|
|
|
"context"
|
2015-10-30 21:47:46 +00:00
|
|
|
"crypto/tls"
|
2023-12-21 10:13:25 +00:00
|
|
|
"errors"
|
2015-09-30 01:21:17 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2016-03-30 00:05:28 +00:00
|
|
|
"regexp"
|
2015-09-30 01:21:17 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2024-07-14 05:58:35 +00:00
|
|
|
"github.com/google/go-github/v63/github"
|
2022-01-17 22:46:59 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-10-12 07:25:13 +00:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/forge"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/forge/common"
|
|
|
|
forge_types "go.woodpecker-ci.org/woodpecker/v2/server/forge/types"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/shared/utils"
|
2015-09-30 01:21:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-10-07 07:49:45 +00:00
|
|
|
defaultURL = "https://github.com" // Default GitHub URL
|
|
|
|
defaultAPI = "https://api.github.com/" // Default GitHub API URL
|
2015-09-30 01:21:17 +00:00
|
|
|
)
|
|
|
|
|
2016-05-03 00:47:58 +00:00
|
|
|
// Opts defines configuration options.
|
|
|
|
type Opts struct {
|
2022-01-31 14:38:00 +00:00
|
|
|
URL string // GitHub server url.
|
|
|
|
Client string // GitHub oauth client id.
|
|
|
|
Secret string // GitHub oauth client secret.
|
|
|
|
SkipVerify bool // Skip ssl verification.
|
|
|
|
MergeRef bool // Clone pull requests using the merge ref.
|
2024-03-29 13:36:48 +00:00
|
|
|
OnlyPublic bool // Only obtain OAuth tokens with access to public repos.
|
2024-05-15 13:45:08 +00:00
|
|
|
OAuthHost string // Public url for oauth if different from url.
|
2016-05-03 00:47:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 23:35:06 +00:00
|
|
|
// New returns a Forge implementation that integrates with a GitHub Cloud or
|
2016-05-03 00:47:58 +00:00
|
|
|
// GitHub Enterprise version control hosting provider.
|
2022-11-04 23:35:06 +00:00
|
|
|
func New(opts Opts) (forge.Forge, error) {
|
2021-09-24 14:29:26 +00:00
|
|
|
r := &client{
|
2022-01-31 14:38:00 +00:00
|
|
|
API: defaultAPI,
|
2023-05-31 16:30:41 +00:00
|
|
|
url: defaultURL,
|
2022-01-31 14:38:00 +00:00
|
|
|
Client: opts.Client,
|
|
|
|
Secret: opts.Secret,
|
2024-05-15 13:45:08 +00:00
|
|
|
oAuthHost: opts.OAuthHost,
|
2022-01-31 14:38:00 +00:00
|
|
|
SkipVerify: opts.SkipVerify,
|
|
|
|
MergeRef: opts.MergeRef,
|
2024-03-29 13:36:48 +00:00
|
|
|
OnlyPublic: opts.OnlyPublic,
|
2016-05-03 00:47:58 +00:00
|
|
|
}
|
|
|
|
if opts.URL != defaultURL {
|
2023-05-31 16:30:41 +00:00
|
|
|
r.url = strings.TrimSuffix(opts.URL, "/")
|
|
|
|
r.API = r.url + "/api/v3/"
|
2016-05-03 00:47:58 +00:00
|
|
|
}
|
2016-08-25 22:00:07 +00:00
|
|
|
|
2021-09-24 14:29:26 +00:00
|
|
|
return r, nil
|
2016-05-03 00:47:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type client struct {
|
2023-05-31 16:30:41 +00:00
|
|
|
url string
|
2022-01-31 14:38:00 +00:00
|
|
|
API string
|
|
|
|
Client string
|
|
|
|
Secret string
|
|
|
|
SkipVerify bool
|
|
|
|
MergeRef bool
|
2024-03-29 13:36:48 +00:00
|
|
|
OnlyPublic bool
|
2024-05-15 13:45:08 +00:00
|
|
|
oAuthHost string
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// Name returns the string name of this driver.
|
2022-06-17 18:14:01 +00:00
|
|
|
func (c *client) Name() string {
|
|
|
|
return "github"
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// URL returns the root url of a configured forge.
|
2023-05-31 16:30:41 +00:00
|
|
|
func (c *client) URL() string {
|
|
|
|
return c.url
|
|
|
|
}
|
|
|
|
|
2022-11-04 23:35:06 +00:00
|
|
|
// Login authenticates the session and returns the forge user details.
|
2024-02-13 15:19:02 +00:00
|
|
|
func (c *client) Login(ctx context.Context, req *forge_types.OAuthRequest) (*model.User, string, error) {
|
|
|
|
config := c.newConfig()
|
2024-06-21 07:55:30 +00:00
|
|
|
redirectURL := config.AuthCodeURL(req.State)
|
2016-12-19 05:42:56 +00:00
|
|
|
|
2024-02-13 15:19:02 +00:00
|
|
|
// check the OAuth code
|
|
|
|
if len(req.Code) == 0 {
|
2016-05-03 20:01:16 +00: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.
|
|
|
|
|
2024-02-13 15:19:02 +00:00
|
|
|
return nil, redirectURL, nil
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
|
2024-02-13 15:19:02 +00:00
|
|
|
token, err := config.Exchange(c.newContext(ctx), req.Code)
|
2015-09-30 01:21:17 +00:00
|
|
|
if err != nil {
|
2024-02-13 15:19:02 +00:00
|
|
|
return nil, redirectURL, err
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 10:56:59 +00:00
|
|
|
client := c.newClientToken(ctx, token.AccessToken)
|
2021-09-29 05:59:46 +00:00
|
|
|
user, _, err := client.Users.Get(ctx, "")
|
2016-05-03 00:47:58 +00:00
|
|
|
if err != nil {
|
2024-02-13 15:19:02 +00:00
|
|
|
return nil, redirectURL, err
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 05:59:46 +00:00
|
|
|
emails, _, err := client.Users.ListEmails(ctx, nil)
|
2016-05-03 20:01:16 +00:00
|
|
|
if err != nil {
|
2024-02-13 15:19:02 +00:00
|
|
|
return nil, redirectURL, err
|
2016-05-03 20:01:16 +00:00
|
|
|
}
|
|
|
|
email := matchingEmail(emails, c.API)
|
|
|
|
if email == nil {
|
2024-02-13 15:19:02 +00:00
|
|
|
return nil, redirectURL, fmt.Errorf("no verified Email address for GitHub account")
|
2016-05-03 20:01:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 00:47:58 +00:00
|
|
|
return &model.User{
|
2023-05-11 03:19:35 +00:00
|
|
|
Login: user.GetLogin(),
|
|
|
|
Email: email.GetEmail(),
|
|
|
|
Token: token.AccessToken,
|
|
|
|
Avatar: user.GetAvatarURL(),
|
|
|
|
ForgeRemoteID: model.ForgeRemoteID(fmt.Sprint(user.GetID())),
|
2024-02-13 15:19:02 +00:00
|
|
|
}, redirectURL, nil
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 00:47:58 +00:00
|
|
|
// Auth returns the GitHub user login for the given access token.
|
2023-03-18 19:35:27 +00:00
|
|
|
func (c *client) Auth(ctx context.Context, token, _ string) (string, error) {
|
2021-09-28 10:56:59 +00:00
|
|
|
client := c.newClientToken(ctx, token)
|
2021-09-29 05:59:46 +00:00
|
|
|
user, _, err := client.Users.Get(ctx, "")
|
2015-09-30 01:21:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return *user.Login, nil
|
|
|
|
}
|
|
|
|
|
2016-05-03 00:47:58 +00:00
|
|
|
// Teams returns a list of all team membership for the GitHub account.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *client) Teams(ctx context.Context, u *model.User) ([]*model.Team, error) {
|
|
|
|
client := c.newClientToken(ctx, u.Token)
|
2016-05-03 00:47:58 +00:00
|
|
|
|
|
|
|
opts := new(github.ListOptions)
|
|
|
|
opts.Page = 1
|
2016-04-29 19:39:56 +00:00
|
|
|
|
|
|
|
var teams []*model.Team
|
2016-05-03 00:47:58 +00:00
|
|
|
for opts.Page > 0 {
|
2021-09-29 05:59:46 +00:00
|
|
|
list, resp, err := client.Organizations.List(ctx, "", opts)
|
2016-05-03 00:47:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
teams = append(teams, convertTeamList(list)...)
|
|
|
|
opts.Page = resp.NextPage
|
2016-04-29 19:39:56 +00:00
|
|
|
}
|
|
|
|
return teams, nil
|
|
|
|
}
|
|
|
|
|
2022-09-05 15:08:51 +00:00
|
|
|
// Repo returns the GitHub repository.
|
2022-11-15 14:01:23 +00:00
|
|
|
func (c *client) Repo(ctx context.Context, u *model.User, id model.ForgeRemoteID, owner, name string) (*model.Repo, error) {
|
2021-09-28 10:56:59 +00:00
|
|
|
client := c.newClientToken(ctx, u.Token)
|
2022-09-05 15:08:51 +00:00
|
|
|
|
|
|
|
if id.IsValid() {
|
|
|
|
intID, err := strconv.ParseInt(string(id), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
repo, _, err := client.Repositories.GetByID(ctx, intID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return convertRepo(repo), nil
|
|
|
|
}
|
|
|
|
|
2021-09-29 05:59:46 +00:00
|
|
|
repo, _, err := client.Repositories.Get(ctx, owner, name)
|
2015-09-30 01:21:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-31 14:38:00 +00:00
|
|
|
return convertRepo(repo), nil
|
2016-05-03 00:47:58 +00:00
|
|
|
}
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2016-05-03 00:47:58 +00:00
|
|
|
// Repos returns a list of all repositories for GitHub account, including
|
|
|
|
// organization repositories.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *client) Repos(ctx context.Context, u *model.User) ([]*model.Repo, error) {
|
|
|
|
client := c.newClientToken(ctx, u.Token)
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2023-12-02 14:19:39 +00:00
|
|
|
opts := new(github.RepositoryListByAuthenticatedUserOptions)
|
2016-05-03 00:47:58 +00:00
|
|
|
opts.PerPage = 100
|
|
|
|
opts.Page = 1
|
2015-10-02 23:02:37 +00:00
|
|
|
|
2017-07-14 19:58:38 +00:00
|
|
|
var repos []*model.Repo
|
2016-05-03 00:47:58 +00:00
|
|
|
for opts.Page > 0 {
|
2023-12-02 14:19:39 +00:00
|
|
|
list, resp, err := client.Repositories.ListByAuthenticatedUser(ctx, opts)
|
2016-05-03 00:47:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-09-06 19:40:25 +00:00
|
|
|
for _, repo := range list {
|
|
|
|
if repo.GetArchived() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
repos = append(repos, convertRepo(repo))
|
|
|
|
}
|
2016-05-03 00:47:58 +00:00
|
|
|
opts.Page = resp.NextPage
|
2015-10-02 23:02:37 +00:00
|
|
|
}
|
2016-05-03 00:47:58 +00:00
|
|
|
return repos, nil
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 11:25:53 +00:00
|
|
|
// File fetches the file from the GitHub repository and returns its contents.
|
2022-10-18 01:24:12 +00:00
|
|
|
func (c *client) File(ctx context.Context, u *model.User, r *model.Repo, b *model.Pipeline, f string) ([]byte, error) {
|
2021-09-28 10:56:59 +00:00
|
|
|
client := c.newClientToken(ctx, u.Token)
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2016-05-03 00:47:58 +00:00
|
|
|
opts := new(github.RepositoryContentGetOptions)
|
2019-06-03 06:49:11 +00:00
|
|
|
opts.Ref = b.Commit
|
2023-12-21 10:13:25 +00:00
|
|
|
content, _, resp, err := client.Repositories.GetContents(ctx, r.Owner, r.Name, f, opts)
|
|
|
|
if resp != nil && resp.StatusCode == http.StatusNotFound {
|
2024-01-10 21:56:42 +00:00
|
|
|
return nil, errors.Join(err, &forge_types.ErrConfigNotFound{Configs: []string{f}})
|
2023-12-21 10:13:25 +00:00
|
|
|
}
|
2015-09-30 01:21:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-29 05:59:46 +00:00
|
|
|
if content == nil {
|
2019-06-04 13:04:18 +00:00
|
|
|
return nil, fmt.Errorf("%s is a folder not a file use Dir(..)", f)
|
|
|
|
}
|
2021-09-29 05:59:46 +00:00
|
|
|
data, err := content.GetContent()
|
|
|
|
return []byte(data), err
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
|
2022-11-06 11:44:04 +00:00
|
|
|
func (c *client) Dir(ctx context.Context, u *model.User, r *model.Repo, b *model.Pipeline, f string) ([]*forge_types.FileMeta, error) {
|
2021-09-28 10:56:59 +00:00
|
|
|
client := c.newClientToken(ctx, u.Token)
|
2019-06-03 07:16:15 +00:00
|
|
|
|
|
|
|
opts := new(github.RepositoryContentGetOptions)
|
|
|
|
opts.Ref = b.Commit
|
2023-12-21 10:13:25 +00:00
|
|
|
_, data, resp, err := client.Repositories.GetContents(ctx, r.Owner, r.Name, f, opts)
|
|
|
|
if resp != nil && resp.StatusCode == http.StatusNotFound {
|
2024-01-10 21:56:42 +00:00
|
|
|
return nil, errors.Join(err, &forge_types.ErrConfigNotFound{Configs: []string{f}})
|
2023-12-21 10:13:25 +00:00
|
|
|
}
|
2019-06-03 07:16:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-06 11:44:04 +00:00
|
|
|
fc := make(chan *forge_types.FileMeta)
|
2024-06-04 06:30:54 +00:00
|
|
|
errChan := make(chan error)
|
2019-06-04 13:04:18 +00:00
|
|
|
|
2019-06-03 07:16:15 +00:00
|
|
|
for _, file := range data {
|
2019-06-04 13:04:18 +00:00
|
|
|
go func(path string) {
|
2021-09-28 10:56:59 +00:00
|
|
|
content, err := c.File(ctx, u, r, b, path)
|
2019-06-04 13:04:18 +00:00
|
|
|
if err != nil {
|
2024-01-10 21:56:42 +00:00
|
|
|
if errors.Is(err, &forge_types.ErrConfigNotFound{}) {
|
2023-12-21 10:13:25 +00:00
|
|
|
err = fmt.Errorf("git tree reported existence of file but we got: %s", err.Error())
|
|
|
|
}
|
2024-06-04 06:30:54 +00:00
|
|
|
errChan <- err
|
2019-06-05 08:08:49 +00:00
|
|
|
} else {
|
2022-11-06 11:44:04 +00:00
|
|
|
fc <- &forge_types.FileMeta{
|
2019-06-05 08:08:49 +00:00
|
|
|
Name: path,
|
|
|
|
Data: content,
|
|
|
|
}
|
2019-06-04 13:04:18 +00:00
|
|
|
}
|
|
|
|
}(f + "/" + *file.Name)
|
2019-06-03 07:16:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-06 11:44:04 +00:00
|
|
|
var files []*forge_types.FileMeta
|
2019-06-04 13:04:18 +00:00
|
|
|
|
2019-07-17 11:58:47 +00:00
|
|
|
for i := 0; i < len(data); i++ {
|
|
|
|
select {
|
2024-06-04 06:30:54 +00:00
|
|
|
case err := <-errChan:
|
2021-11-25 16:15:36 +00:00
|
|
|
return nil, err
|
2021-11-23 14:36:52 +00:00
|
|
|
case fileMeta := <-fc:
|
2019-07-17 11:58:47 +00:00
|
|
|
files = append(files, fileMeta)
|
2019-06-04 13:04:18 +00:00
|
|
|
}
|
2019-07-17 11:58:47 +00:00
|
|
|
}
|
2019-06-04 13:04:18 +00:00
|
|
|
|
|
|
|
close(fc)
|
2024-06-04 06:30:54 +00:00
|
|
|
close(errChan)
|
2019-06-04 13:04:18 +00:00
|
|
|
|
2019-06-03 07:16:15 +00:00
|
|
|
return files, nil
|
|
|
|
}
|
|
|
|
|
2023-04-30 01:40:13 +00:00
|
|
|
func (c *client) PullRequests(ctx context.Context, u *model.User, r *model.Repo, p *model.ListOptions) ([]*model.PullRequest, error) {
|
2023-06-03 01:03:06 +00:00
|
|
|
token := common.UserToken(ctx, r, u)
|
2023-03-19 09:43:57 +00:00
|
|
|
client := c.newClientToken(ctx, token)
|
|
|
|
|
|
|
|
pullRequests, _, err := client.PullRequests.List(ctx, r.Owner, r.Name, &github.PullRequestListOptions{
|
2023-04-30 01:40:13 +00:00
|
|
|
ListOptions: github.ListOptions{Page: p.Page, PerPage: p.PerPage},
|
2023-03-19 09:43:57 +00:00
|
|
|
State: "open",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make([]*model.PullRequest, len(pullRequests))
|
|
|
|
for i := range pullRequests {
|
|
|
|
result[i] = &model.PullRequest{
|
2023-11-25 23:52:52 +00:00
|
|
|
Index: model.ForgeRemoteID(strconv.Itoa(pullRequests[i].GetNumber())),
|
2023-03-19 09:43:57 +00:00
|
|
|
Title: pullRequests[i].GetTitle(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2016-05-03 00:47:58 +00: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) {
|
2022-01-31 14:38:00 +00:00
|
|
|
login := ""
|
|
|
|
token := ""
|
|
|
|
|
|
|
|
if u != nil {
|
|
|
|
login = u.Token
|
|
|
|
token = "x-oauth-basic"
|
2016-05-03 00:47:58 +00:00
|
|
|
}
|
2022-01-31 14:38:00 +00:00
|
|
|
|
2022-02-26 01:54:15 +00:00
|
|
|
host, err := common.ExtractHostFromCloneURL(r.Clone)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-05-03 00:47:58 +00:00
|
|
|
return &model.Netrc{
|
2022-01-31 14:38:00 +00:00
|
|
|
Login: login,
|
|
|
|
Password: token,
|
2022-02-26 01:54:15 +00:00
|
|
|
Machine: host,
|
2016-05-03 00:47:58 +00:00
|
|
|
}, nil
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 07:12:17 +00:00
|
|
|
// Deactivate deactivates the repository be removing registered push hooks from
|
2016-05-03 20:01:16 +00:00
|
|
|
// the GitHub repository.
|
2021-09-28 10:56:59 +00: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 05:59:46 +00:00
|
|
|
hooks, _, err := client.Repositories.ListHooks(ctx, r.Owner, r.Name, nil)
|
2016-05-03 20:01:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
match := matchingHooks(hooks, link)
|
|
|
|
if match == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-29 05:59:46 +00:00
|
|
|
_, err = client.Repositories.DeleteHook(ctx, r.Owner, r.Name, *match.ID)
|
2016-05-03 20:01:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-25 01:09:35 +00:00
|
|
|
// OrgMembership returns if user is member of organization and if user
|
|
|
|
// is admin/owner in this organization.
|
|
|
|
func (c *client) OrgMembership(ctx context.Context, u *model.User, owner string) (*model.OrgPerm, error) {
|
|
|
|
client := c.newClientToken(ctx, u.Token)
|
|
|
|
org, _, err := client.Organizations.GetOrgMembership(ctx, u.Login, owner)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &model.OrgPerm{Member: org.GetState() == "active", Admin: org.GetRole() == "admin"}, nil
|
|
|
|
}
|
|
|
|
|
2023-07-21 17:45:32 +00:00
|
|
|
func (c *client) Org(ctx context.Context, u *model.User, owner string) (*model.Org, error) {
|
|
|
|
client := c.newClientToken(ctx, u.Token)
|
|
|
|
|
|
|
|
user, _, err := client.Users.Get(ctx, owner)
|
2024-01-11 18:17:07 +00:00
|
|
|
log.Trace().Msgf("GitHub user for owner %s = %v", owner, user)
|
2023-07-21 17:45:32 +00:00
|
|
|
if user != nil && err == nil {
|
|
|
|
return &model.Org{
|
2023-08-03 15:30:56 +00:00
|
|
|
Name: user.GetLogin(),
|
2023-07-21 17:45:32 +00:00
|
|
|
IsUser: true,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
org, _, err := client.Organizations.Get(ctx, owner)
|
2024-01-11 18:17:07 +00:00
|
|
|
log.Trace().Msgf("GitHub organization for owner %s = %v", owner, org)
|
2023-07-21 17:45:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &model.Org{
|
2023-08-03 15:30:56 +00:00
|
|
|
Name: org.GetLogin(),
|
2023-07-21 17:45:32 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// newContext returns the GitHub oauth2 context using an HTTPClient that
|
2022-11-04 23:35:06 +00:00
|
|
|
// disables TLS verification if disabled in the forge settings.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *client) newContext(ctx context.Context) context.Context {
|
2016-05-03 20:01:16 +00:00
|
|
|
if !c.SkipVerify {
|
2021-09-28 10:56:59 +00:00
|
|
|
return ctx
|
2016-05-03 20:01:16 +00:00
|
|
|
}
|
2021-09-28 10:56:59 +00:00
|
|
|
return context.WithValue(ctx, oauth2.HTTPClient, &http.Client{
|
2016-05-03 20:01:16 +00:00
|
|
|
Transport: &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// newConfig returns the GitHub oauth2 config.
|
2024-02-13 15:19:02 +00:00
|
|
|
func (c *client) newConfig() *oauth2.Config {
|
2024-03-29 13:36:48 +00:00
|
|
|
scopes := []string{"user:email", "read:org"}
|
|
|
|
if c.OnlyPublic {
|
|
|
|
scopes = append(scopes, []string{"admin:repo_hook", "repo:status"}...)
|
|
|
|
} else {
|
|
|
|
scopes = append(scopes, "repo")
|
|
|
|
}
|
|
|
|
|
2024-05-15 13:45:08 +00:00
|
|
|
publicOAuthURL := c.oAuthHost
|
|
|
|
if publicOAuthURL == "" {
|
|
|
|
publicOAuthURL = c.url
|
|
|
|
}
|
|
|
|
|
2016-05-03 00:47:58 +00:00
|
|
|
return &oauth2.Config{
|
|
|
|
ClientID: c.Client,
|
|
|
|
ClientSecret: c.Secret,
|
2024-03-29 13:36:48 +00:00
|
|
|
Scopes: scopes,
|
2016-05-03 00:47:58 +00:00
|
|
|
Endpoint: oauth2.Endpoint{
|
2024-05-15 13:45:08 +00:00
|
|
|
AuthURL: fmt.Sprintf("%s/login/oauth/authorize", publicOAuthURL),
|
2023-05-31 16:30:41 +00:00
|
|
|
TokenURL: fmt.Sprintf("%s/login/oauth/access_token", c.url),
|
2016-05-03 00:47:58 +00:00
|
|
|
},
|
2024-02-13 15:19:02 +00:00
|
|
|
RedirectURL: fmt.Sprintf("%s/authorize", server.Config.Server.OAuthHost),
|
2016-05-03 00:47:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// newClientToken returns the GitHub oauth2 client.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *client) newClientToken(ctx context.Context, token string) *github.Client {
|
2016-05-03 00:47:58 +00:00
|
|
|
ts := oauth2.StaticTokenSource(
|
|
|
|
&oauth2.Token{AccessToken: token},
|
|
|
|
)
|
2021-09-28 10:56:59 +00:00
|
|
|
tc := oauth2.NewClient(ctx, ts)
|
2016-05-03 00:47:58 +00:00
|
|
|
if c.SkipVerify {
|
2024-01-12 01:01:02 +00:00
|
|
|
tp, _ := tc.Transport.(*oauth2.Transport)
|
|
|
|
tp.Base = &http.Transport{
|
2016-05-03 00:47:58 +00:00
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2021-09-24 14:29:26 +00:00
|
|
|
client := github.NewClient(tc)
|
|
|
|
client.BaseURL, _ = url.Parse(c.API)
|
|
|
|
return client
|
2016-05-03 00:47:58 +00:00
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// matchingEmail returns matching user email.
|
2021-09-29 05:59:46 +00:00
|
|
|
func matchingEmail(emails []*github.UserEmail, rawURL string) *github.UserEmail {
|
2016-05-03 20:01:16 +00:00
|
|
|
for _, email := range emails {
|
|
|
|
if email.Email == nil || email.Primary == nil || email.Verified == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if *email.Primary && *email.Verified {
|
2021-09-29 05:59:46 +00:00
|
|
|
return email
|
2016-05-03 20:01:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// github enterprise does not support verified email addresses so instead
|
|
|
|
// we'll return the first email address in the list.
|
2021-09-29 05:59:46 +00:00
|
|
|
if len(emails) != 0 && rawURL != defaultAPI {
|
|
|
|
return emails[0]
|
2016-05-03 20:01:16 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// matchingHooks returns matching hook.
|
2024-06-04 06:30:54 +00:00
|
|
|
func matchingHooks(hooks []*github.Hook, rawURL string) *github.Hook {
|
|
|
|
link, err := url.Parse(rawURL)
|
2016-05-03 20:01:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, hook := range hooks {
|
|
|
|
if hook.ID == nil {
|
|
|
|
continue
|
|
|
|
}
|
2024-03-16 08:37:08 +00:00
|
|
|
hookURL, err := url.Parse(hook.Config.GetURL())
|
2021-09-29 05:59:46 +00:00
|
|
|
if err == nil && hookURL.Host == link.Host {
|
|
|
|
return hook
|
2016-05-03 20:01:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:02:49 +00:00
|
|
|
var reDeploy = regexp.MustCompile(`.+/deployments/(\d+)`)
|
2016-05-03 00:47:58 +00:00
|
|
|
|
2022-11-04 23:35:06 +00:00
|
|
|
// Status sends the commit status to the forge.
|
2015-09-30 01:21:17 +00:00
|
|
|
// An example would be the GitHub pull request status.
|
2023-06-27 16:01:18 +00:00
|
|
|
func (c *client) Status(ctx context.Context, user *model.User, repo *model.Repo, pipeline *model.Pipeline, workflow *model.Workflow) error {
|
2021-12-28 16:02:49 +00:00
|
|
|
client := c.newClientToken(ctx, user.Token)
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
if pipeline.Event == model.EventDeploy {
|
2024-03-15 17:00:25 +00:00
|
|
|
// Get id from url. If not found, skip.
|
2023-11-14 16:12:12 +00:00
|
|
|
matches := reDeploy.FindStringSubmatch(pipeline.ForgeURL)
|
2024-05-13 20:58:21 +00:00
|
|
|
//nolint:mnd
|
2021-12-28 16:02:49 +00:00
|
|
|
if len(matches) != 2 {
|
|
|
|
return nil
|
2016-05-12 16:34:30 +00:00
|
|
|
}
|
2021-12-28 16:02:49 +00:00
|
|
|
id, _ := strconv.Atoi(matches[1])
|
2019-06-17 08:48:40 +00:00
|
|
|
|
2021-12-28 16:02:49 +00:00
|
|
|
_, _, err := client.Repositories.CreateDeploymentStatus(ctx, repo.Owner, repo.Name, int64(id), &github.DeploymentStatusRequest{
|
2022-10-18 01:24:12 +00:00
|
|
|
State: github.String(convertStatus(pipeline.Status)),
|
|
|
|
Description: github.String(common.GetPipelineStatusDescription(pipeline.Status)),
|
2023-11-14 16:12:12 +00:00
|
|
|
LogURL: github.String(common.GetPipelineStatusURL(repo, pipeline, nil)),
|
2021-12-28 16:02:49 +00:00
|
|
|
})
|
|
|
|
return err
|
2016-03-30 00:05:28 +00:00
|
|
|
}
|
2016-05-03 00:47:58 +00:00
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
_, _, err := client.Repositories.CreateStatus(ctx, repo.Owner, repo.Name, pipeline.Commit, &github.RepoStatus{
|
2023-06-27 16:01:18 +00:00
|
|
|
Context: github.String(common.GetPipelineStatusContext(repo, pipeline, workflow)),
|
|
|
|
State: github.String(convertStatus(workflow.State)),
|
|
|
|
Description: github.String(common.GetPipelineStatusDescription(workflow.State)),
|
2023-11-14 16:12:12 +00:00
|
|
|
TargetURL: github.String(common.GetPipelineStatusURL(repo, pipeline, workflow)),
|
2021-12-28 16:02:49 +00:00
|
|
|
})
|
2016-03-29 18:07:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
// Activate activates a repository by creating the post-commit hook and
|
|
|
|
// adding the SSH deploy key, if applicable.
|
2021-09-28 10:56:59 +00: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 20:01:16 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-09-28 10:56:59 +00:00
|
|
|
client := c.newClientToken(ctx, u.Token)
|
2016-05-03 20:01:16 +00:00
|
|
|
hook := &github.Hook{
|
|
|
|
Name: github.String("web"),
|
|
|
|
Events: []string{
|
|
|
|
"push",
|
|
|
|
"pull_request",
|
|
|
|
"deployment",
|
|
|
|
},
|
2024-03-16 08:37:08 +00:00
|
|
|
Config: &github.HookConfig{
|
|
|
|
URL: &link,
|
|
|
|
ContentType: github.String("form"),
|
2016-05-03 20:01:16 +00:00
|
|
|
},
|
|
|
|
}
|
2021-09-29 05:59:46 +00:00
|
|
|
_, _, err := client.Repositories.CreateHook(ctx, r.Owner, r.Name, hook)
|
2015-09-30 01:21:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-27 00:47:55 +00:00
|
|
|
// Branches returns the names of all branches for the named repository.
|
2023-04-30 01:40:13 +00:00
|
|
|
func (c *client) Branches(ctx context.Context, u *model.User, r *model.Repo, p *model.ListOptions) ([]string, error) {
|
2023-06-03 01:03:06 +00:00
|
|
|
token := common.UserToken(ctx, r, u)
|
2022-02-26 16:36:00 +00:00
|
|
|
client := c.newClientToken(ctx, token)
|
2021-10-27 00:47:55 +00:00
|
|
|
|
2023-04-30 01:40:13 +00:00
|
|
|
githubBranches, _, err := client.Repositories.ListBranches(ctx, r.Owner, r.Name, &github.BranchListOptions{
|
|
|
|
ListOptions: github.ListOptions{Page: p.Page, PerPage: p.PerPage},
|
|
|
|
})
|
2021-10-27 00:47:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
branches := make([]string, 0)
|
|
|
|
for _, branch := range githubBranches {
|
|
|
|
branches = append(branches, *branch.Name)
|
|
|
|
}
|
|
|
|
return branches, nil
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// BranchHead returns the sha of the head (latest commit) of the specified branch.
|
2024-02-11 09:44:50 +00:00
|
|
|
func (c *client) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (*model.Commit, error) {
|
2023-06-03 01:03:06 +00:00
|
|
|
token := common.UserToken(ctx, r, u)
|
2023-10-12 13:28:00 +00:00
|
|
|
b, _, err := c.newClientToken(ctx, token).Repositories.GetBranch(ctx, r.Owner, r.Name, branch, 1)
|
2022-08-31 22:36:32 +00:00
|
|
|
if err != nil {
|
2024-02-11 09:44:50 +00:00
|
|
|
return nil, err
|
2022-08-31 22:36:32 +00:00
|
|
|
}
|
2024-02-11 09:44:50 +00:00
|
|
|
return &model.Commit{
|
|
|
|
SHA: b.GetCommit().GetSHA(),
|
|
|
|
ForgeURL: b.GetCommit().GetHTMLURL(),
|
|
|
|
}, nil
|
2022-08-31 22:36:32 +00:00
|
|
|
}
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
// Hook parses the post-commit hook from the Request body
|
|
|
|
// and returns the required data in a standard format.
|
2022-10-18 01:24:12 +00:00
|
|
|
func (c *client) Hook(ctx context.Context, r *http.Request) (*model.Repo, *model.Pipeline, error) {
|
|
|
|
pull, repo, pipeline, err := parseHook(r, c.MergeRef)
|
2022-01-17 22:46:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2024-01-30 16:39:00 +00:00
|
|
|
if pipeline != nil && pipeline.Event == model.EventRelease && pipeline.Commit == "" {
|
|
|
|
tagName := strings.Split(pipeline.Ref, "/")[2]
|
|
|
|
sha, err := c.getTagCommitSHA(ctx, repo, tagName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
pipeline.Commit = sha
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
if pull != nil && len(pipeline.ChangedFiles) == 0 {
|
|
|
|
pipeline, err = c.loadChangedFilesFromPullRequest(ctx, pull, repo, pipeline)
|
2022-01-17 22:46:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
return repo, pipeline, nil
|
2022-01-17 22:46:59 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
func (c *client) loadChangedFilesFromPullRequest(ctx context.Context, pull *github.PullRequest, tmpRepo *model.Repo, pipeline *model.Pipeline) (*model.Pipeline, error) {
|
2022-01-17 22:46:59 +00:00
|
|
|
_store, ok := store.TryFromContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
log.Error().Msg("could not get store from context")
|
2022-10-18 01:24:12 +00:00
|
|
|
return pipeline, nil
|
2022-01-17 22:46:59 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 14:01:23 +00:00
|
|
|
repo, err := _store.GetRepoNameFallback(tmpRepo.ForgeRemoteID, tmpRepo.FullName)
|
2022-01-17 22:46:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := _store.GetUser(repo.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-30 01:40:13 +00:00
|
|
|
pipeline.ChangedFiles, err = utils.Paginate(func(page int) ([]string, error) {
|
|
|
|
opts := &github.ListOptions{Page: page}
|
|
|
|
fileList := make([]string, 0, 16)
|
|
|
|
for opts.Page > 0 {
|
|
|
|
files, resp, err := c.newClientToken(ctx, user.Token).PullRequests.ListFiles(ctx, repo.Owner, repo.Name, pull.GetNumber(), opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-17 22:46:59 +00:00
|
|
|
|
2023-04-30 01:40:13 +00:00
|
|
|
for _, file := range files {
|
|
|
|
fileList = append(fileList, file.GetFilename(), file.GetPreviousFilename())
|
|
|
|
}
|
2022-01-17 22:46:59 +00:00
|
|
|
|
2023-04-30 01:40:13 +00:00
|
|
|
opts.Page = resp.NextPage
|
|
|
|
}
|
2024-04-09 07:04:53 +00:00
|
|
|
return utils.DeduplicateStrings(fileList), nil
|
2023-04-30 01:40:13 +00:00
|
|
|
})
|
2022-01-17 22:46:59 +00:00
|
|
|
|
2023-04-30 01:40:13 +00:00
|
|
|
return pipeline, err
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
2024-01-30 16:39:00 +00:00
|
|
|
|
|
|
|
func (c *client) getTagCommitSHA(ctx context.Context, repo *model.Repo, tagName string) (string, error) {
|
|
|
|
_store, ok := store.TryFromContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
log.Error().Msg("could not get store from context")
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
repo, err := _store.GetRepoNameFallback(repo.ForgeRemoteID, repo.FullName)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := _store.GetUser(repo.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
gh := c.newClientToken(ctx, user.Token)
|
|
|
|
|
|
|
|
page := 1
|
|
|
|
var tag *github.RepositoryTag
|
|
|
|
for {
|
|
|
|
tags, _, err := gh.Repositories.ListTags(ctx, repo.Owner, repo.Name, &github.ListOptions{Page: page})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range tags {
|
|
|
|
if t.GetName() == tagName {
|
|
|
|
tag = t
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if tag != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if tag == nil {
|
|
|
|
return "", fmt.Errorf("could not find tag %s", tagName)
|
|
|
|
}
|
|
|
|
return tag.GetCommit().GetSHA(), nil
|
|
|
|
}
|