175 lines
4.8 KiB
Diff
175 lines
4.8 KiB
Diff
diff --git a/modules/config/login.go b/modules/config/login.go
|
|
index 3b77fb9..94de9cd 100644
|
|
--- a/modules/config/login.go
|
|
+++ b/modules/config/login.go
|
|
@@ -13,6 +13,7 @@ import (
|
|
"net/http/cookiejar"
|
|
"net/url"
|
|
"os"
|
|
+ "strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
@@ -200,6 +201,63 @@ func UpdateLogin(login *Login) error {
|
|
return saveConfig()
|
|
}
|
|
|
|
+// CreateLoginFromEnvVars returns a login based on environment variables, or nil if no login can be created
|
|
+func CreateLoginFromEnvVars() (*Login, error) {
|
|
+ var token string
|
|
+
|
|
+ giteaToken := os.Getenv("GITEA_TOKEN")
|
|
+ githubToken := os.Getenv("GH_TOKEN")
|
|
+ giteaInstanceURL := os.Getenv("GITEA_INSTANCE_URL")
|
|
+ instanceInsecure := os.Getenv("GITEA_INSTANCE_INSECURE")
|
|
+ giteaInstanceSSHHost := os.Getenv("GITEA_INSTANCE_SSH_HOST")
|
|
+ insecure := false
|
|
+ if len(instanceInsecure) > 0 {
|
|
+ insecure, _ = strconv.ParseBool(instanceInsecure)
|
|
+ }
|
|
+
|
|
+ // if no tokens are set, or no instance url for gitea fail fast
|
|
+ if len(giteaInstanceURL) == 0 || (len(giteaToken) == 0 && len(githubToken) == 0) {
|
|
+ return nil, nil
|
|
+ }
|
|
+
|
|
+ token = giteaToken
|
|
+ if len(giteaToken) == 0 {
|
|
+ token = githubToken
|
|
+ }
|
|
+
|
|
+ login := &Login{
|
|
+ Name: "GITEA_LOGIN_VIA_ENV",
|
|
+ URL: giteaInstanceURL,
|
|
+ Token: token,
|
|
+ SSHHost: giteaInstanceSSHHost,
|
|
+ Insecure: insecure,
|
|
+ SSHKey: "",
|
|
+ SSHCertPrincipal: "",
|
|
+ SSHKeyFingerprint: "",
|
|
+ SSHAgent: false,
|
|
+ VersionCheck: true,
|
|
+ Created: time.Now().Unix(),
|
|
+ }
|
|
+
|
|
+ client := login.Client()
|
|
+ u, _, err := client.GetMyUserInfo()
|
|
+ if err != nil {
|
|
+ return nil, fmt.Errorf("failed to validate token: %s", err)
|
|
+ }
|
|
+
|
|
+ login.User = u.UserName
|
|
+
|
|
+ if login.SSHHost == "" {
|
|
+ parsedURL, err := url.Parse(giteaInstanceURL)
|
|
+ if err != nil {
|
|
+ return nil, err
|
|
+ }
|
|
+ login.SSHHost = parsedURL.Host
|
|
+ }
|
|
+
|
|
+ return login, nil
|
|
+}
|
|
+
|
|
// Client returns a client to operate Gitea API. You may provide additional modifiers
|
|
// for the client like gitea.SetBasicAuth() for customization
|
|
func (l *Login) Client(options ...gitea.ClientOption) *gitea.Client {
|
|
diff --git a/modules/context/context.go b/modules/context/context.go
|
|
index aec5592..636eeec 100644
|
|
--- a/modules/context/context.go
|
|
+++ b/modules/context/context.go
|
|
@@ -9,9 +9,7 @@ import (
|
|
"log"
|
|
"os"
|
|
"path"
|
|
- "strconv"
|
|
"strings"
|
|
- "time"
|
|
|
|
"code.gitea.io/tea/modules/config"
|
|
"code.gitea.io/tea/modules/git"
|
|
@@ -108,16 +106,6 @@ func InitCommand(cmd *cli.Command) *TeaContext {
|
|
c.RepoSlug = repoFlag
|
|
}
|
|
|
|
- // override config user with env variable
|
|
- envLogin := GetLoginByEnvVar()
|
|
- if envLogin != nil {
|
|
- _, err := utils.ValidateAuthenticationMethod(envLogin.URL, envLogin.Token, "", "", false, "", "")
|
|
- if err != nil {
|
|
- log.Fatal(err.Error())
|
|
- }
|
|
- c.Login = envLogin
|
|
- }
|
|
-
|
|
// override login from flag, or use default login if repo based detection failed
|
|
if len(loginFlag) != 0 {
|
|
c.Login = config.GetLoginByName(loginFlag)
|
|
@@ -196,10 +184,25 @@ func contextFromLocalRepo(repoPath, remoteValue string) (*git.TeaRepo, *config.L
|
|
return repo, nil, "", fmt.Errorf("Remote '%s' not found in this Git repository", remoteValue)
|
|
}
|
|
|
|
+ envLogin, err := config.CreateLoginFromEnvVars()
|
|
+ if err != nil {
|
|
+ log.Fatal(err.Error())
|
|
+ }
|
|
+
|
|
logins, err := config.GetLogins()
|
|
if err != nil {
|
|
return repo, nil, "", err
|
|
}
|
|
+
|
|
+ if envLogin != nil {
|
|
+ _, err := utils.ValidateAuthenticationMethod(envLogin.URL, envLogin.Token, "", "", false, "", "")
|
|
+ if err != nil {
|
|
+ log.Fatal(err.Error())
|
|
+ }
|
|
+
|
|
+ logins = append([]config.Login{*envLogin}, logins...)
|
|
+ }
|
|
+
|
|
for _, l := range logins {
|
|
sshHost := l.GetSSHHost()
|
|
for _, u := range remoteConfig.URLs {
|
|
@@ -223,40 +226,3 @@ func contextFromLocalRepo(repoPath, remoteValue string) (*git.TeaRepo, *config.L
|
|
|
|
return repo, nil, "", errNotAGiteaRepo
|
|
}
|
|
-
|
|
-// GetLoginByEnvVar returns a login based on environment variables, or nil if no login can be created
|
|
-func GetLoginByEnvVar() *config.Login {
|
|
- var token string
|
|
-
|
|
- giteaToken := os.Getenv("GITEA_TOKEN")
|
|
- githubToken := os.Getenv("GH_TOKEN")
|
|
- giteaInstanceURL := os.Getenv("GITEA_INSTANCE_URL")
|
|
- instanceInsecure := os.Getenv("GITEA_INSTANCE_INSECURE")
|
|
- insecure := false
|
|
- if len(instanceInsecure) > 0 {
|
|
- insecure, _ = strconv.ParseBool(instanceInsecure)
|
|
- }
|
|
-
|
|
- // if no tokens are set, or no instance url for gitea fail fast
|
|
- if len(giteaInstanceURL) == 0 || (len(giteaToken) == 0 && len(githubToken) == 0) {
|
|
- return nil
|
|
- }
|
|
-
|
|
- token = giteaToken
|
|
- if len(giteaToken) == 0 {
|
|
- token = githubToken
|
|
- }
|
|
-
|
|
- return &config.Login{
|
|
- Name: "GITEA_LOGIN_VIA_ENV",
|
|
- URL: giteaInstanceURL,
|
|
- Token: token,
|
|
- Insecure: insecure,
|
|
- SSHKey: "",
|
|
- SSHCertPrincipal: "",
|
|
- SSHKeyFingerprint: "",
|
|
- SSHAgent: false,
|
|
- Created: time.Now().Unix(),
|
|
- VersionCheck: false,
|
|
- }
|
|
-}
|