@@ -29,10 +29,6 @@ const (
2929 RawGitHubHost string = "raw.githubusercontent.com"
3030 GitLabHost string = "gitlab.com"
3131 BitbucketHost string = "bitbucket.org"
32-
33- GitHubToken string = "GITHUB_TOKEN"
34- GitLabToken string = "GITLAB_TOKEN"
35- BitbucketToken string = "BITBUCKET_TOKEN"
3632)
3733
3834type GitUrl struct {
@@ -46,7 +42,8 @@ type GitUrl struct {
4642 IsFile bool // defines if the URL points to a file in the repo
4743}
4844
49- // ParseGitUrl extracts information from a GitHub, GitLab, or Bitbucket url
45+ // ParseGitUrl extracts information from a support git url
46+ // Only supports git repositories hosted on GitHub, GitLab, and Bitbucket
5047func ParseGitUrl (fullUrl string ) (GitUrl , error ) {
5148 var g GitUrl
5249
@@ -83,7 +80,6 @@ func (g *GitUrl) parseGitHubUrl(url *url.URL) error {
8380
8481 g .Protocol = url .Scheme
8582 g .Host = url .Host
86- g .token = os .Getenv (GitHubToken )
8783
8884 if g .Host == RawGitHubHost {
8985 g .IsFile = true
@@ -130,7 +126,6 @@ func (g *GitUrl) parseGitLabUrl(url *url.URL) error {
130126 g .Protocol = url .Scheme
131127 g .Host = url .Host
132128 g .IsFile = false
133- g .token = os .Getenv (GitLabToken )
134129
135130 // GitLab urls contain a '-' separating the root of the repo
136131 // and the path to a file or directory
@@ -171,7 +166,6 @@ func (g *GitUrl) parseBitbucketUrl(url *url.URL) error {
171166 g .Protocol = url .Scheme
172167 g .Host = url .Host
173168 g .IsFile = false
174- g .token = os .Getenv (BitbucketToken )
175169
176170 splitUrl = strings .SplitN (url .Path [1 :], "/" , 5 )
177171 if len (splitUrl ) < 2 {
@@ -201,10 +195,31 @@ func (g *GitUrl) parseBitbucketUrl(url *url.URL) error {
201195 return err
202196}
203197
204- // ValidateToken makes a http get request to the repo with the GitUrl token
198+ // SetToken validates the token with a get request to the repo before setting the token
199+ // Defaults token to empty on failure.
200+ func (g * GitUrl ) SetToken (token string , httpTimeout * int ) error {
201+ err := g .validateToken (HTTPRequestParams {Token : token , Timeout : httpTimeout })
202+ if err != nil {
203+ g .token = ""
204+ return fmt .Errorf ("failed to set token. error: %v" , err )
205+ }
206+ g .token = token
207+ return nil
208+ }
209+
210+ // IsPublic checks if the GitUrl is public with a get request to the repo using an empty token
211+ // Returns true if the request succeeds
212+ func (g * GitUrl ) IsPublic (httpTimeout * int ) bool {
213+ err := g .validateToken (HTTPRequestParams {Token : "" , Timeout : httpTimeout })
214+ if err != nil {
215+ return false
216+ }
217+ return true
218+ }
219+
220+ // validateToken makes a http get request to the repo with the GitUrl token
205221// Returns an error if the get request fails
206- // If token is empty or invalid and validate succeeds, the repository is public
207- func (g * GitUrl ) ValidateToken (params HTTPRequestParams ) error {
222+ func (g * GitUrl ) validateToken (params HTTPRequestParams ) error {
208223 var apiUrl string
209224
210225 switch g .Host {
@@ -227,9 +242,17 @@ func (g *GitUrl) ValidateToken(params HTTPRequestParams) error {
227242 return nil
228243}
229244
230- // CloneGitRepo clones a git repo to a destination directory
231- // Only supports git repositories hosted on GitHub, GitLab, and Bitbucket
232- func CloneGitRepo (g GitUrl , destDir string , httpTimeout * int ) error {
245+ // IsGitProviderRepo checks if the url matches a repo from a supported git provider
246+ func IsGitProviderRepo (url string ) bool {
247+ if strings .Contains (url , RawGitHubHost ) || strings .Contains (url , GitHubHost ) ||
248+ strings .Contains (url , GitLabHost ) || strings .Contains (url , BitbucketHost ) {
249+ return true
250+ }
251+ return false
252+ }
253+
254+ // CloneGitRepo clones a git repo to a destination directory (either an absolute or relative path)
255+ func CloneGitRepo (g GitUrl , destDir string ) error {
233256 exist := CheckPathExists (destDir )
234257 if ! exist {
235258 return fmt .Errorf ("failed to clone repo, destination directory: '%s' does not exists" , destDir )
@@ -240,29 +263,13 @@ func CloneGitRepo(g GitUrl, destDir string, httpTimeout *int) error {
240263 host = GitHubHost
241264 }
242265
243- repoUrl := fmt .Sprintf ("%s://%s/%s/%s.git" , g .Protocol , host , g .Owner , g .Repo )
244-
245- params := HTTPRequestParams {
246- Timeout : httpTimeout ,
247- }
248-
249- // public repos will succeed even if token is invalid or empty
250- err := g .ValidateToken (params )
251-
252- if err != nil {
253- if g .token != "" {
254- params .Token = g .token
255- err := g .ValidateToken (params )
256- if err != nil {
257- return fmt .Errorf ("failed to validate git url with token, ensure that the url and token is correct. error: %v" , err )
258- } else {
259- repoUrl = fmt .Sprintf ("%s://token:%s@%s/%s/%s.git" , g .Protocol , g .token , host , g .Owner , g .Repo )
260- if g .Host == BitbucketHost {
261- repoUrl = fmt .Sprintf ("%s://x-token-auth:%s@%s/%s/%s.git" , g .Protocol , g .token , host , g .Owner , g .Repo )
262- }
263- }
264- } else {
265- return fmt .Errorf ("failed to validate git url without a token, ensure that a token is set if the repo is private. error: %v" , err )
266+ var repoUrl string
267+ if g .token == "" {
268+ repoUrl = fmt .Sprintf ("%s://%s/%s/%s.git" , g .Protocol , host , g .Owner , g .Repo )
269+ } else {
270+ repoUrl = fmt .Sprintf ("%s://token:%s@%s/%s/%s.git" , g .Protocol , g .token , host , g .Owner , g .Repo )
271+ if g .Host == BitbucketHost {
272+ repoUrl = fmt .Sprintf ("%s://x-token-auth:%s@%s/%s/%s.git" , g .Protocol , g .token , host , g .Owner , g .Repo )
266273 }
267274 }
268275
@@ -274,9 +281,13 @@ func CloneGitRepo(g GitUrl, destDir string, httpTimeout *int) error {
274281 c .Env = os .Environ ()
275282 c .Env = append (c .Env , "GIT_TERMINAL_PROMPT=0" , "GIT_ASKPASS=/bin/echo" )
276283
277- _ , err = c .CombinedOutput ()
284+ _ , err : = c .CombinedOutput ()
278285 if err != nil {
279- return fmt .Errorf ("failed to clone repo, ensure that the git url is correct. error: %v" , err )
286+ if g .token == "" {
287+ return fmt .Errorf ("failed to clone repo without a token, ensure that a token is set if the repo is private. error: %v" , err )
288+ } else {
289+ return fmt .Errorf ("failed to clone repo with token, ensure that the url and token is correct. error: %v" , err )
290+ }
280291 }
281292
282293 return nil
0 commit comments