Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gitextractor): subtask Clone Git Repo ended unexpectedly #8136

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions backend/plugins/gitextractor/impl/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ 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"
"github.com/apache/incubator-devlake/core/plugin"
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"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions backend/plugins/gitextractor/parser/taskdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
1 change: 1 addition & 0 deletions backend/plugins/github/api/blueprint_v200.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
})

Expand Down