Skip to content

Commit

Permalink
Add app password into clone url for bitbucket clones (#425)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrie30 committed Jun 15, 2024
1 parent c83f1dd commit 1b94d6b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
### Deprecated
### Removed
### Fixed
- refs to deprecated io/ioutil (#415); thanks @testwill
- Refs to deprecated io/ioutil (#415); thanks @testwill
- Bitbucket clones without app password; thanks @SparklingAperioso
### Security

## [1.9.12] - 5/15/24
Expand Down
13 changes: 13 additions & 0 deletions scm/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scm

import (
"net/url"
"strings"

"os"

Expand Down Expand Up @@ -96,10 +97,22 @@ func (_ Bitbucket) filter(resp []bitbucket.Repository) (repoData []Repo, err err
} else if os.Getenv("GHORG_CLONE_PROTOCOL") == "https" && linkType == "https" {
r.URL = link.(string)
r.CloneURL = link.(string)
if os.Getenv("GHORG_BITBUCKET_OAUTH") != "" {
// TODO
} else {
r.CloneURL = insertAppPasswordCredentialsIntoURL(r.CloneURL)
}
cloneData = append(cloneData, r)
}
}
}

return cloneData, nil
}

func insertAppPasswordCredentialsIntoURL(url string) string {
credentials := ":" + os.Getenv("GHORG_BITBUCKET_APP_PASSWORD") + "@"
urlWithCredentials := strings.Replace(url, "@", credentials, 1)

return urlWithCredentials
}
26 changes: 26 additions & 0 deletions scm/bitbucket_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package scm

import (
"os"
"testing"
)

func TestInsertAppPasswordCredentialsIntoURL(t *testing.T) {
// Set environment variables for the test
os.Setenv("GHORG_BITBUCKET_USERNAME", "ghorg")
os.Setenv("GHORG_BITBUCKET_APP_PASSWORD", "testpassword")

// Define a test URL
testURL := "https://ghorg@bitbucket.org/foobar/testrepo.git"

// Call the function with the test URL
resultURL := insertAppPasswordCredentialsIntoURL(testURL)

// Define the expected result
expectedURL := "https://ghorg:testpassword@bitbucket.org/foobar/testrepo.git"

// Check if the result matches the expected result
if resultURL != expectedURL {
t.Errorf("Expected %s, but got %s", expectedURL, resultURL)
}
}

0 comments on commit 1b94d6b

Please sign in to comment.