2019-02-21 16:58:25 +00:00
|
|
|
// Copyright 2019 Sorint.lab
|
|
|
|
//
|
|
|
|
// 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 common
|
|
|
|
|
|
|
|
import (
|
2019-07-01 09:40:20 +00:00
|
|
|
gitsource "agola.io/agola/internal/gitsources"
|
|
|
|
"agola.io/agola/internal/gitsources/gitea"
|
|
|
|
"agola.io/agola/internal/gitsources/github"
|
|
|
|
"agola.io/agola/internal/gitsources/gitlab"
|
2019-07-31 13:39:07 +00:00
|
|
|
cstypes "agola.io/agola/services/configstore/types"
|
2019-04-29 13:42:10 +00:00
|
|
|
|
2019-05-23 09:23:14 +00:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-02-21 16:58:25 +00:00
|
|
|
)
|
|
|
|
|
2019-07-31 13:17:54 +00:00
|
|
|
func newGitea(rs *cstypes.RemoteSource, accessToken string) (*gitea.Client, error) {
|
2019-02-21 16:58:25 +00:00
|
|
|
return gitea.New(gitea.Opts{
|
2019-05-15 21:46:21 +00:00
|
|
|
APIURL: rs.APIURL,
|
2019-05-09 12:14:13 +00:00
|
|
|
SkipVerify: rs.SkipVerify,
|
|
|
|
Token: accessToken,
|
|
|
|
Oauth2ClientID: rs.Oauth2ClientID,
|
|
|
|
Oauth2Secret: rs.Oauth2ClientSecret,
|
2019-02-21 16:58:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:17:54 +00:00
|
|
|
func newGitlab(rs *cstypes.RemoteSource, accessToken string) (*gitlab.Client, error) {
|
2019-02-21 16:58:25 +00:00
|
|
|
return gitlab.New(gitlab.Opts{
|
2019-05-15 21:46:21 +00:00
|
|
|
APIURL: rs.APIURL,
|
|
|
|
SkipVerify: rs.SkipVerify,
|
|
|
|
Token: accessToken,
|
|
|
|
Oauth2ClientID: rs.Oauth2ClientID,
|
|
|
|
Oauth2Secret: rs.Oauth2ClientSecret,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:17:54 +00:00
|
|
|
func newGithub(rs *cstypes.RemoteSource, accessToken string) (*github.Client, error) {
|
2019-05-15 21:46:21 +00:00
|
|
|
return github.New(github.Opts{
|
|
|
|
APIURL: rs.APIURL,
|
2019-02-21 16:58:25 +00:00
|
|
|
SkipVerify: rs.SkipVerify,
|
|
|
|
Token: accessToken,
|
|
|
|
Oauth2ClientID: rs.Oauth2ClientID,
|
|
|
|
Oauth2Secret: rs.Oauth2ClientSecret,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:17:54 +00:00
|
|
|
func GetAccessToken(rs *cstypes.RemoteSource, userAccessToken, oauth2AccessToken string) (string, error) {
|
2019-04-29 13:42:10 +00:00
|
|
|
switch rs.AuthType {
|
2019-07-31 13:17:54 +00:00
|
|
|
case cstypes.RemoteSourceAuthTypePassword:
|
2019-02-21 16:58:25 +00:00
|
|
|
return userAccessToken, nil
|
2019-07-31 13:17:54 +00:00
|
|
|
case cstypes.RemoteSourceAuthTypeOauth2:
|
2019-02-21 16:58:25 +00:00
|
|
|
return oauth2AccessToken, nil
|
|
|
|
default:
|
2019-04-29 13:42:10 +00:00
|
|
|
return "", errors.Errorf("invalid remote source auth type %q", rs.AuthType)
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:17:54 +00:00
|
|
|
func GetGitSource(rs *cstypes.RemoteSource, la *cstypes.LinkedAccount) (gitsource.GitSource, error) {
|
2019-02-21 16:58:25 +00:00
|
|
|
var accessToken string
|
|
|
|
if la != nil {
|
|
|
|
var err error
|
2019-04-29 13:42:10 +00:00
|
|
|
accessToken, err = GetAccessToken(rs, la.UserAccessToken, la.Oauth2AccessToken)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var gitSource gitsource.GitSource
|
|
|
|
var err error
|
|
|
|
switch rs.Type {
|
2019-07-31 13:17:54 +00:00
|
|
|
case cstypes.RemoteSourceTypeGitea:
|
2019-02-21 16:58:25 +00:00
|
|
|
gitSource, err = newGitea(rs, accessToken)
|
2019-07-31 13:17:54 +00:00
|
|
|
case cstypes.RemoteSourceTypeGitlab:
|
2019-02-21 16:58:25 +00:00
|
|
|
gitSource, err = newGitlab(rs, accessToken)
|
2019-07-31 13:17:54 +00:00
|
|
|
case cstypes.RemoteSourceTypeGithub:
|
2019-05-15 21:46:21 +00:00
|
|
|
gitSource, err = newGithub(rs, accessToken)
|
2019-02-21 16:58:25 +00:00
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("remote source %s isn't a valid git source", rs.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return gitSource, err
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:17:54 +00:00
|
|
|
func GetUserSource(rs *cstypes.RemoteSource, accessToken string) (gitsource.UserSource, error) {
|
2019-02-21 16:58:25 +00:00
|
|
|
var userSource gitsource.UserSource
|
|
|
|
var err error
|
|
|
|
switch rs.AuthType {
|
2019-07-31 13:17:54 +00:00
|
|
|
case cstypes.RemoteSourceAuthTypeOauth2:
|
2019-02-21 16:58:25 +00:00
|
|
|
userSource, err = GetOauth2Source(rs, accessToken)
|
2019-07-31 13:17:54 +00:00
|
|
|
case cstypes.RemoteSourceAuthTypePassword:
|
2019-02-21 16:58:25 +00:00
|
|
|
userSource, err = GetPasswordSource(rs, accessToken)
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("unknown remote source auth type")
|
|
|
|
}
|
|
|
|
|
|
|
|
return userSource, err
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:17:54 +00:00
|
|
|
func GetOauth2Source(rs *cstypes.RemoteSource, accessToken string) (gitsource.Oauth2Source, error) {
|
2019-02-21 16:58:25 +00:00
|
|
|
var oauth2Source gitsource.Oauth2Source
|
|
|
|
var err error
|
|
|
|
switch rs.Type {
|
2019-07-31 13:17:54 +00:00
|
|
|
case cstypes.RemoteSourceTypeGitea:
|
2019-05-09 12:14:13 +00:00
|
|
|
oauth2Source, err = newGitea(rs, accessToken)
|
2019-07-31 13:17:54 +00:00
|
|
|
case cstypes.RemoteSourceTypeGitlab:
|
2019-02-21 16:58:25 +00:00
|
|
|
oauth2Source, err = newGitlab(rs, accessToken)
|
2019-07-31 13:17:54 +00:00
|
|
|
case cstypes.RemoteSourceTypeGithub:
|
2019-05-15 21:46:21 +00:00
|
|
|
oauth2Source, err = newGithub(rs, accessToken)
|
2019-02-21 16:58:25 +00:00
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("remote source %s isn't a valid oauth2 source", rs.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return oauth2Source, err
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:17:54 +00:00
|
|
|
func GetPasswordSource(rs *cstypes.RemoteSource, accessToken string) (gitsource.PasswordSource, error) {
|
2019-02-21 16:58:25 +00:00
|
|
|
var passwordSource gitsource.PasswordSource
|
|
|
|
var err error
|
|
|
|
switch rs.Type {
|
2019-07-31 13:17:54 +00:00
|
|
|
case cstypes.RemoteSourceTypeGitea:
|
2019-02-21 16:58:25 +00:00
|
|
|
passwordSource, err = newGitea(rs, accessToken)
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("remote source %s isn't a valid oauth2 source", rs.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return passwordSource, err
|
|
|
|
}
|