All checks were successful
Golang test / go-test (push) Successful in 16s
Signed-off-by: Matheus Sampaio Queiroga <srherobrine20@gmail.com>
68 lines
2.3 KiB
Go
68 lines
2.3 KiB
Go
package gitea
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// EnvToken retrieves an authentication token from the environment variables.
|
|
// It checks for "GITEA_TOKEN", "GH_TOKEN", and "GITHUB_TOKEN" in that order,
|
|
// returning the value of the first one found. If none are set, it returns an empty string.
|
|
func EnvToken() string {
|
|
if giteaToken := os.Getenv("GITEA_TOKEN"); giteaToken != "" {
|
|
return giteaToken
|
|
} else if ghToken := os.Getenv("GH_TOKEN"); ghToken != "" {
|
|
return ghToken
|
|
} else if githubToken := os.Getenv("GITHUB_TOKEN"); githubToken != "" {
|
|
return githubToken
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// EnvApiHost returns the API host URL for Gitea or GitHub based on environment variables.
|
|
// It checks the following environment variables in order:
|
|
// 1. GITEA_API_URL: If set, returns its value.
|
|
// 2. GITHUB_API_URL: If set, returns its value.
|
|
// 3. GITHUB_SERVER_URL: If set to "https://github.com", returns "https://api.github.com".
|
|
// Otherwise, returns the value of GITHUB_SERVER_URL.
|
|
// If none of these are set, it returns an empty string.
|
|
func EnvApiHost() string {
|
|
// GITHUB_API_URL
|
|
if apiUrl := os.Getenv("GITEA_API_URL"); apiUrl != "" {
|
|
return apiUrl
|
|
} else if apiUrl = os.Getenv("GITHUB_API_URL"); apiUrl != "" {
|
|
return apiUrl
|
|
}
|
|
|
|
switch v := os.Getenv("GITHUB_SERVER_URL"); v {
|
|
case "https://github.com":
|
|
return "https://api.github.com"
|
|
default:
|
|
return v
|
|
}
|
|
}
|
|
|
|
// EnvRepository extracts the repository owner and name from environment variables.
|
|
// It first checks for the "GITHUB_REPOSITORY" environment variable, which is expected
|
|
// to be in the format "owner/repo". If "GITHUB_REPOSITORY_OWNER" is also set, it uses
|
|
// its value as the owner and extracts the repository name from "GITHUB_REPOSITORY".
|
|
// If only "GITHUB_REPOSITORY" is set and does not contain a '/', it returns an empty
|
|
// owner and the repository name. If "GITHUB_REPOSITORY" contains a '/', it splits the
|
|
// value into owner and repository name. If neither environment variable is set, it
|
|
// returns empty strings.
|
|
func EnvRepository() (owner, repo string) {
|
|
// GITHUB_REPOSITORY, GITHUB_REPOSITORY_OWNER
|
|
if v := os.Getenv("GITHUB_REPOSITORY"); v != "" {
|
|
if owner := os.Getenv("GITHUB_REPOSITORY_OWNER"); owner != "" {
|
|
v = v[len(owner)+1:]
|
|
return owner, v
|
|
}
|
|
if strings.Count(v, "/") == 0 {
|
|
return "", v
|
|
}
|
|
n2 := strings.SplitN(v, "/", 2)
|
|
return n2[0], n2[1]
|
|
}
|
|
return
|
|
}
|