diff --git a/backend/plugins/gitextractor/impl/impl.go b/backend/plugins/gitextractor/impl/impl.go index 45c84ab6e17..df18a55a689 100644 --- a/backend/plugins/gitextractor/impl/impl.go +++ b/backend/plugins/gitextractor/impl/impl.go @@ -18,7 +18,9 @@ limitations under the License. package impl import ( + "fmt" "net/url" + "strings" "github.com/apache/incubator-devlake/core/dal" "github.com/apache/incubator-devlake/core/errors" @@ -26,6 +28,7 @@ import ( helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api" "github.com/apache/incubator-devlake/plugins/gitextractor/parser" "github.com/apache/incubator-devlake/plugins/gitextractor/tasks" + "github.com/apache/incubator-devlake/plugins/github/models" giturls "github.com/chainguard-dev/git-urls" ) @@ -68,9 +71,33 @@ func (p GitExtractor) PrepareTaskData(taskCtx plugin.TaskContext, options map[st return nil, err } - parsedURL, err := giturls.Parse(op.Url) + connectionHelper := helper.NewConnectionHelper( + taskCtx, + nil, + p.Name(), + ) + connection := &models.GithubConnection{} + err := connectionHelper.FirstById(connection, op.ConnectionId) if err != nil { - return nil, errors.BadInput.Wrap(err, "failed to parse git url") + return nil, errors.Default.Wrap(err, "unable to get github connection by the given connection ID") + } + + apiClient, err := helper.NewApiClient(taskCtx.GetContext(), connection.GetEndpoint(), nil, 0, connection.GetProxy(), taskCtx) + if err != nil { + return nil, err + } + + connection.PrepareApiClient(apiClient) + + newUrl, err := replaceAcessTokenInUrl(op.Url, connection.Token) + if err != nil { + return nil, err + } + op.Url = newUrl + + parsedURL, errParse := giturls.Parse(op.Url) + if errParse != nil { + return nil, errors.BadInput.Wrap(errParse, "failed to parse git url") } // append username to the git url @@ -126,3 +153,24 @@ func (p GitExtractor) RootPkgPath() string { func (p GitExtractor) TestConnection(id uint64) errors.Error { return nil } + +func replaceAcessTokenInUrl(gitURL, newCredential string) (string, errors.Error) { + atIndex := strings.Index(gitURL, "@") + if atIndex == -1 { + return "", errors.Default.New("Invalid Git URL") + } + + protocolIndex := strings.Index(gitURL, "://") + if protocolIndex == -1 { + return "", errors.Default.New("Invalid Git URL") + } + + // Extract the base URL (e.g., "https://git:") + baseURL := gitURL[:protocolIndex+7] + + repoURL := gitURL[atIndex+1:] + + modifiedURL := fmt.Sprintf("%s%s@%s", baseURL, newCredential, repoURL) + + return modifiedURL, nil +} diff --git a/backend/plugins/gitextractor/parser/taskdata.go b/backend/plugins/gitextractor/parser/taskdata.go index 1b22c98855e..57f5b4b4870 100644 --- a/backend/plugins/gitextractor/parser/taskdata.go +++ b/backend/plugins/gitextractor/parser/taskdata.go @@ -45,4 +45,5 @@ type GitExtractorOptions struct { SkipCommitStat *bool `json:"skipCommitStat" mapstructure:"skipCommitStat" comment:"skip all commit stat including added/deleted lines and commit files as well"` SkipCommitFiles *bool `json:"skipCommitFiles" mapstructure:"skipCommitFiles"` NoShallowClone bool `json:"noShallowClone" mapstructure:"noShallowClone"` + ConnectionId uint64 `json:"connectionId" mapstructure:"connectionId,omitempty"` } diff --git a/backend/plugins/github/api/blueprint_v200.go b/backend/plugins/github/api/blueprint_v200.go index 3016db8cb4a..20fd9b7dcd0 100644 --- a/backend/plugins/github/api/blueprint_v200.go +++ b/backend/plugins/github/api/blueprint_v200.go @@ -136,6 +136,7 @@ func makeDataSourcePipelinePlanV200( "fullName": githubRepo.FullName, "repoId": didgen.NewDomainIdGenerator(&models.GithubRepo{}).Generate(connection.ID, githubRepo.GithubId), "proxy": connection.Proxy, + "connectionId": githubRepo.ConnectionId, }, })