Skip to content

Commit

Permalink
feat: generating new github access token to every gitextractor task
Browse files Browse the repository at this point in the history
Signed-off-by: Caio Queiroz <[email protected]>
  • Loading branch information
caioq committed Oct 9, 2024
1 parent 0b8c8ea commit 512c748
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
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

0 comments on commit 512c748

Please sign in to comment.