Skip to content

Commit 098afc4

Browse files
authored
azuredevops: Add support for SSH cloning (sourcegraph#58655)
In line with other code hosts, adding SSH clone support here via gitURLType. ## Test plan Verified locally by configuring it and seeing a repo clone successfully via ssh.
1 parent 1300b73 commit 098afc4

File tree

8 files changed

+28
-6
lines changed

8 files changed

+28
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ All notable changes to Sourcegraph are documented in this file.
6060
### Added
6161

6262
- Added the ability to use Workload Identity, Managed Identity and Environmental credentials when using the Azure OpenAI completions and embeddings providers [#58289](https://github.com/sourcegraph/sourcegraph/pull/58289)
63+
- Added support for cloning via SSH from Azure DevOps. [#58655](https://github.com/sourcegraph/sourcegraph/pull/58655)
6364

6465
### Fixed
6566

cmd/gitserver/internal/cloneurl/clone_url.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,11 @@ func awsCodeCloneURL(logger log.Logger, repo *awscodecommit.Repository, cfg *sch
129129
}
130130

131131
func azureDevOpsCloneURL(logger log.Logger, repo *azuredevops.Repository, cfg *schema.AzureDevOpsConnection) string {
132-
u, err := url.Parse(repo.CloneURL)
132+
if cfg.GitURLType == "ssh" {
133+
return repo.SSHURL
134+
}
135+
136+
u, err := url.Parse(repo.RemoteURL)
133137
if err != nil {
134138
logger.Warn("Error adding authentication to Azure DevOps repo remote URL.", log.String("url", cfg.Url), log.Error(err))
135139
return cfg.Url

cmd/gitserver/internal/cloneurl/clone_url_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ func TestAzureDevOpsCloneURL(t *testing.T) {
6363
}
6464

6565
repo := &azuredevops.Repository{
66-
ID: "test-project",
67-
CloneURL: "https://[email protected]/sgtestazure/sgtestazure/_git/sgtestazure",
66+
ID: "test-project",
67+
RemoteURL: "https://[email protected]/sgtestazure/sgtestazure/_git/sgtestazure",
6868
}
6969

7070
got := azureDevOpsCloneURL(logtest.Scoped(t), repo, &cfg)

internal/batches/sources/azuredevops_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var (
2424
testProjectName = "testproject"
2525
testOrgName = "testorg"
2626
testPRID = "42"
27-
testRepository = azuredevops.Repository{ID: "testrepoid", Name: testRepoName, Project: azuredevops.Project{ID: "testprojectid", Name: testProjectName}, APIURL: fmt.Sprintf("https://dev.azure.com/%s/%s/_git/%s", testOrgName, testProjectName, testRepoName), CloneURL: fmt.Sprintf("https://dev.azure.com/%s/%s/_git/%s", testOrgName, testProjectName, testRepoName)}
27+
testRepository = azuredevops.Repository{ID: "testrepoid", Name: testRepoName, Project: azuredevops.Project{ID: "testprojectid", Name: testProjectName}, APIURL: fmt.Sprintf("https://dev.azure.com/%s/%s/_git/%s", testOrgName, testProjectName, testRepoName), RemoteURL: fmt.Sprintf("https://dev.azure.com/%s/%s/_git/%s", testOrgName, testProjectName, testRepoName)}
2828
testCommonPullRequestArgs = azuredevops.PullRequestCommonArgs{Org: testOrgName, Project: testProjectName, RepoNameOrID: testRepoName, PullRequestID: testPRID}
2929
testOrgProjectRepoArgs = azuredevops.OrgProjectRepoArgs{Org: testOrgName, Project: testProjectName, RepoNameOrID: testRepoName}
3030
)

internal/extsvc/azuredevops/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ type PullRequestStatusState string
226226
type Repository struct {
227227
ID string `json:"id"`
228228
Name string `json:"name"`
229-
CloneURL string `json:"remoteURL"`
229+
RemoteURL string `json:"remoteURL"`
230230
APIURL string `json:"url"`
231231
SSHURL string `json:"sshUrl"`
232232
WebURL string `json:"webUrl"`

internal/repos/azuredevops.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ func (s *AzureDevOpsSource) makeRepo(p azuredevops.Repository) (*types.Repo, err
158158
return nil, err
159159
}
160160

161+
cloneURL := p.RemoteURL
162+
if s.config.GitURLType == "ssh" {
163+
cloneURL = p.SSHURL
164+
}
165+
161166
name := path.Join(fullURL.Host, fullURL.Path)
162167
return &types.Repo{
163168
Name: api.RepoName(name),
@@ -171,7 +176,7 @@ func (s *AzureDevOpsSource) makeRepo(p azuredevops.Repository) (*types.Repo, err
171176
Sources: map[string]*types.SourceInfo{
172177
urn: {
173178
ID: urn,
174-
CloneURL: p.CloneURL,
179+
CloneURL: cloneURL,
175180
},
176181
},
177182
Metadata: p,

schema/azuredevops.schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
"format": "uri",
2020
"examples": ["https://dev.azure.com"]
2121
},
22+
"gitURLType": {
23+
"description": "The type of Git URLs to use for cloning and fetching Git repositories.\n\nIf \"http\", Sourcegraph will access repositories using Git URLs of the form http(s)://dev.azure.com/myrepo.git.\n\nIf \"ssh\", Sourcegraph will access repositories using Git URLs of the form [email protected]:v3/myrepo. See the documentation for how to provide SSH private keys and known_hosts: https://docs.sourcegraph.com/admin/repo/auth#repositories-that-need-http-s-or-ssh-authentication.",
24+
"type": "string",
25+
"enum": ["http", "ssh"],
26+
"default": "http"
27+
},
2228
"enforcePermissions": {
2329
"description": "A flag to enforce Azure DevOps repository access permissions",
2430
"type": "boolean",

schema/schema.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)