Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
[Backport 5.1] Enable "Test connection" for Perforce code hosts (#56792)
Browse files Browse the repository at this point in the history
Co-authored-by: Peter Guy <[email protected]>
Co-authored-by: Joe Chen <[email protected]>
  • Loading branch information
3 people authored Sep 20, 2023
1 parent 0d0cf48 commit e847ffa
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions cmd/frontend/graphqlbackend/external_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ var availabilityCheck = map[string]bool{
extsvc.KindBitbucketServer: true,
extsvc.KindBitbucketCloud: true,
extsvc.KindAzureDevOps: true,
extsvc.KindPerforce: true,
}

func externalServiceByID(ctx context.Context, db database.DB, gqlID graphql.ID) (*externalServiceResolver, error) {
Expand Down
4 changes: 4 additions & 0 deletions cmd/gitserver/server/vcs_syncer_perforce.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func (s *PerforceDepotSyncer) Type() string {
return "perforce"
}

func (s *PerforceDepotSyncer) CanConnect(ctx context.Context, host, username, password string) error {
return p4testWithTrust(ctx, host, username, password)
}

// IsCloneable checks to see if the Perforce remote URL is cloneable.
func (s *PerforceDepotSyncer) IsCloneable(ctx context.Context, remoteURL *vcs.URL) error {
username, password, host, path, err := decomposePerforceRemoteURL(remoteURL)
Expand Down
23 changes: 18 additions & 5 deletions internal/repos/perforce.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/sourcegraph/sourcegraph/internal/conf/reposource"
"github.com/sourcegraph/sourcegraph/internal/extsvc"
"github.com/sourcegraph/sourcegraph/internal/extsvc/perforce"
"github.com/sourcegraph/sourcegraph/internal/gitserver"
"github.com/sourcegraph/sourcegraph/internal/jsonc"
"github.com/sourcegraph/sourcegraph/internal/types"
"github.com/sourcegraph/sourcegraph/internal/vcs"
Expand Down Expand Up @@ -45,10 +46,19 @@ func newPerforceSource(svc *types.ExternalService, c *schema.PerforceConnection)
}, nil
}

// CheckConnection at this point assumes availability and relies on errors returned
// from the subsequent calls. This is going to be expanded as part of issue #44683
// to actually only return true if the source can serve requests.
// CheckConnection tests the code host connection to make sure it works.
// For Perforce, it uses the host (p4.port), username (p4.user) and password (p4.passwd)
// from the code host configuration.
func (s PerforceSource) CheckConnection(ctx context.Context) error {
// since CheckConnection is called from the frontend, we can't rely on the `p4` executable
// being available, so we need to make an RPC call to `gitserver`, where it is available.
// Use what is for us a "no-op" `p4` command that should always succeed.
gclient := gitserver.NewClient()
rc, _, err := gclient.P4Exec(ctx, s.config.P4Port, s.config.P4User, s.config.P4Passwd, "users")
if err != nil {
return errors.Wrap(err, "Unable to connect to the Perforce server")
}
rc.Close()
return nil
}

Expand Down Expand Up @@ -85,12 +95,15 @@ func (s PerforceSource) ListRepos(ctx context.Context, results chan SourceResult
// composePerforceCloneURL composes a clone URL for a Perforce depot based on
// given information. e.g.
// perforce://ssl:111.222.333.444:1666//Sourcegraph/
func composePerforceCloneURL(host, depot string) string {
func composePerforceCloneURL(host, depot, username, password string) string {
cloneURL := url.URL{
Scheme: "perforce",
Host: host,
Path: depot,
}
if username != "" && password != "" {
cloneURL.User = url.UserPassword(username, password)
}
return cloneURL.String()
}

Expand All @@ -101,7 +114,7 @@ func (s PerforceSource) makeRepo(depot string) *types.Repo {
name := strings.Trim(depot, "/")
urn := s.svc.URN()

cloneURL := composePerforceCloneURL(s.config.P4Port, depot)
cloneURL := composePerforceCloneURL(s.config.P4Port, depot, "", "")

return &types.Repo{
Name: reposource.PerforceRepoName(
Expand Down

0 comments on commit e847ffa

Please sign in to comment.