Skip to content

Commit

Permalink
Add app password into clone url for bitbucket clones
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrie30 committed Jun 15, 2024
1 parent c83f1dd commit c783d4f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
15 changes: 15 additions & 0 deletions scm/bitbucket.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package scm

import (
"fmt"
"net/url"
"strings"

"os"

Expand Down Expand Up @@ -96,10 +98,23 @@ 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 {
password := os.Getenv("GHORG_BITBUCKET_APP_PASSWORD")
credentials := fmt.Sprintf(":%s@", 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 c783d4f

Please sign in to comment.