Skip to content

Commit 243d2e2

Browse files
committed
feat(cloning): support ssh cloning
Following a user's feedback, we now support SSH cloning with the dedicated option `ssh-cloning`.
1 parent 82f7b24 commit 243d2e2

File tree

6 files changed

+22
-9
lines changed

6 files changed

+22
-9
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ Here is an example of some lines of a `.jsonl` format output:
122122

123123
Note that by default, `src-fingerprint` will exclude forked repositories from the fingerprints computation. **For GitHub provider** archived repositories and public repositories will also be excluded by default. Use flags `--include-forked-repos`, `--include-archived-repos` or `include-public-repos` to change this behavior.
124124

125+
For all the following examples, we assume that the user is able to clone repositories using an HTTP URL with basic authentication. If for any reason this is not possible with the user's organization, `src-fingerprint` supports ssh cloning by using the dedicated option `--ssh-cloning`. Note though that this option is not the standard configuration of the tool but rather a workaround for this type of edge case. Especially, this option may bring some issues in the event of discrepancies in permissions between the token provided for API-based repos listing, and the SSH keys used to clone these repos.
126+
125127
### GitHub
126128

127129
1. Export all fingerprints from private repositories from a GitHub Org to the default path `./fingerprints.jsonl.gz` with logs:

cmd/src-fingerprint/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ func main() {
158158
Value: "-",
159159
Usage: "set cloning location for repositories",
160160
},
161+
&cli.BoolFlag{
162+
Name: "ssh-cloning",
163+
Value: false,
164+
Usage: "Use ssh to clone repositories. The standard behavior is not guaranteed with this option.",
165+
},
161166
&cli.StringFlag{
162167
Name: "after",
163168
Value: "",
@@ -243,6 +248,7 @@ func collectAction(c *cli.Context) error {
243248
BaseURL: c.String("provider-url"),
244249
RepositoryName: c.String("repo-name"),
245250
RespositoryIsPrivate: c.Bool("repo-is-private"),
251+
SSHCloning: c.Bool("ssh-cloning"),
246252
}
247253

248254
defer func() {

provider/bitbucket.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,10 @@ func (p *BitbucketProvider) Gather(user string) ([]GitRepository, error) {
177177
// CloneRepository clones a Github repository given the token. The token must have the `read_repository` rights.
178178
func (p *BitbucketProvider) CloneRepository(cloner cloner.Cloner,
179179
repository GitRepository) (string, error) {
180-
authURL := repository.GetHTTPUrl()
181-
// If token doesn't exist, don't try to basic auth
182-
if p.token != "" {
180+
authURL := repository.GetSSHUrl()
181+
// If token doesn't exist or if SSH cloning was specified, don't try to basic auth
182+
if p.token != "" && !p.options.SSHCloning {
183+
authURL = repository.GetHTTPUrl()
183184
authURL = strings.Replace(authURL,
184185
"https://", fmt.Sprintf("https://%s:%s@",
185186
p.transport.user, url.QueryEscape(p.token)), 1)

provider/github.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,10 @@ func (p *GitHubProvider) Gather(user string) ([]GitRepository, error) {
183183
// CloneRepository clones a Github repository given the token. The token must have the `read_repository` rights.
184184
func (p *GitHubProvider) CloneRepository(cloner cloner.Cloner,
185185
repository GitRepository) (string, error) {
186-
url := repository.GetHTTPUrl()
187-
// If token doesn't exist, don't try to basic auth
188-
if p.token != "" {
186+
url := repository.GetSSHUrl()
187+
// If token doesn't exist or if SSH cloning was specified, don't try to basic auth
188+
if p.token != "" && !p.options.SSHCloning {
189+
url = repository.GetHTTPUrl()
189190
url = strings.Replace(url, "https://", fmt.Sprintf("https://x-access-token:%s@", p.token), 1)
190191
}
191192

provider/gitlab.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,10 @@ func (p *GitLabProvider) collectFromGroup(repositories []GitRepository,
244244
func (p *GitLabProvider) CloneRepository(
245245
cloner cloner.Cloner,
246246
repository GitRepository) (string, error) {
247-
url := repository.GetHTTPUrl()
248-
// If token doesn't exist, don't try to basic auth
249-
if p.token != "" {
247+
url := repository.GetSSHUrl()
248+
// If token doesn't exist or if SSH cloning was specified, don't try to basic auth
249+
if p.token != "" && !p.options.SSHCloning {
250+
url = repository.GetHTTPUrl()
250251
url = strings.Replace(url, "https://", fmt.Sprintf("https://%s:%s@", p.token, p.token), 1)
251252
}
252253

provider/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ type Options struct {
4949
IncludePublicRepos bool
5050
// Repository private status to display in the output if the provider is 'repository'
5151
RespositoryIsPrivate bool
52+
// Use SSH to clone repositories.
53+
SSHCloning bool
5254
// BaseURL is the base URL of the API
5355
BaseURL string
5456
// Repository name to display in the output if the provider is 'repository'

0 commit comments

Comments
 (0)