@@ -10,7 +10,6 @@ import (
1010 "encoding/json"
1111 "flag"
1212 "fmt"
13- "github.com/LF-Engineering/insights-datasource-shared/aws"
1413 "io"
1514 "math"
1615 "net/url"
@@ -25,8 +24,11 @@ import (
2524
2625 "github.com/LF-Engineering/insights-datasource-git/build"
2726 shared "github.com/LF-Engineering/insights-datasource-shared"
27+ "github.com/LF-Engineering/insights-datasource-shared/auth0"
28+ "github.com/LF-Engineering/insights-datasource-shared/aws"
2829 "github.com/LF-Engineering/insights-datasource-shared/cache"
2930 elastic "github.com/LF-Engineering/insights-datasource-shared/elastic"
31+ "github.com/LF-Engineering/insights-datasource-shared/http"
3032 logger "github.com/LF-Engineering/insights-datasource-shared/ingestjob"
3133 "github.com/LF-Engineering/insights-datasource-shared/report"
3234 "github.com/LF-Engineering/lfx-event-schema/service"
@@ -596,6 +598,7 @@ type DSGit struct {
596598 cacheProvider cache.Manager
597599 endpoint string
598600 reportProvider * report.Manager
601+ auth0Client * auth0.ClientProvider
599602}
600603
601604// PublisherPushEvents - this is a fake function to test publisher locally
@@ -2526,6 +2529,25 @@ func (j *DSGit) Sync(ctx *shared.Ctx) (err error) {
25262529 if err != nil {
25272530 return
25282531 }
2532+
2533+ sourceID := ""
2534+ if j .RepositorySource == "github" {
2535+ sourceID , err = j .getGithubRepoSourceId ()
2536+ if err != nil {
2537+ j .log .WithFields (logrus.Fields {"operation" : "sync" }).Error (fmt .Errorf ("getGithubRepoSourceId source id: %s, url: %s, source: %s.error: %+v" , j .SourceID , j .URL , j .RepositorySource , err ))
2538+ }
2539+ }
2540+
2541+ if j .RepositorySource == "gerrit" {
2542+ sourceID , err = j .getGerritRepoSourceId ()
2543+ if err != nil {
2544+ j .log .WithFields (logrus.Fields {"operation" : "sync" }).Error (fmt .Errorf ("getGerritRepoSourceId source id: %s, url: %s, source: %s.error: %+v" , j .SourceID , j .URL , j .RepositorySource , err ))
2545+ }
2546+ }
2547+
2548+ if sourceID != "" {
2549+ j .SourceID = sourceID
2550+ }
25292551 // Continue with operations that need git ops
25302552 nThreads := 0
25312553 locFinished := false
@@ -2797,7 +2819,9 @@ func main() {
27972819 git .log .WithFields (logrus.Fields {"operation" : "main" }).Errorf ("WriteLog Error : %+v" , err )
27982820 }
27992821 shared .FatalOnError (err )
2800-
2822+ if err = git .addAuth0Client (); err != nil {
2823+ git .log .WithFields (logrus.Fields {"operation" : "main" }).Errorf ("addAuth0Client Error : %+v" , err )
2824+ }
28012825 err = git .Sync (& ctx )
28022826 if err != nil {
28032827 git .log .WithFields (logrus.Fields {"operation" : "main" }).Errorf ("Error: %+v" , err )
@@ -3100,6 +3124,119 @@ func (j *DSGit) getHead(ctx *shared.Ctx) (string, error) {
31003124 return commitID , nil
31013125}
31023126
3127+ func (j * DSGit ) addAuth0Client () error {
3128+ esCacheClientProvider , err := elastic .NewClientProvider (& elastic.Params {
3129+ URL : os .Getenv ("ES_CACHE_URL" ),
3130+ })
3131+ if err != nil {
3132+ return err
3133+ }
3134+ httpClientProvider := http .NewClientProvider (time .Second * 50 , true )
3135+ authEnv := os .Getenv ("STAGE" )
3136+ if authEnv == "dev" {
3137+ authEnv = "test"
3138+ }
3139+ auth0Client , err := auth0 .NewAuth0Client (
3140+ authEnv ,
3141+ os .Getenv ("AUTH_GRANT_TYPE" ),
3142+ os .Getenv ("AUTH_CLIENT_ID" ),
3143+ os .Getenv ("AUTH_CLIENT_SECRET" ),
3144+ os .Getenv ("AUTH_AUDIENCE" ),
3145+ os .Getenv ("AUTH0_URL" ),
3146+ httpClientProvider ,
3147+ esCacheClientProvider ,
3148+ nil ,
3149+ build .AppName )
3150+ if err != nil {
3151+ return err
3152+ }
3153+ j .auth0Client = auth0Client
3154+ return nil
3155+ }
3156+
3157+ func (j * DSGit ) getGithubRepoSourceId () (string , error ) {
3158+ sourceID := ""
3159+ if j .auth0Client == nil {
3160+ return "" , nil
3161+ }
3162+
3163+ type response struct {
3164+ SourceID int64 `json:"sourceId"`
3165+ }
3166+
3167+ httpClientProvider := http .NewClientProvider (time .Second * 50 , true )
3168+ token , err := j .auth0Client .GetToken ()
3169+ if err != nil {
3170+ return sourceID , err
3171+ }
3172+ headers := make (map [string ]string )
3173+ headers ["Authorization" ] = fmt .Sprintf ("Bearer %s" , token )
3174+ orgRepoList := strings .Split (strings .TrimPrefix (j .URL , "https://github.com/" ), "/" )
3175+
3176+ URL := fmt .Sprintf ("%sproject/source/github/organization/%s/repository/%s" , os .Getenv ("INSIGHTS_SERVICE_URL_V2" ), orgRepoList [0 ], orgRepoList [1 ])
3177+ statusCode , res , err := httpClientProvider .Request (URL , "GET" , headers , nil , nil )
3178+ if err != nil {
3179+ return sourceID , err
3180+ }
3181+ if statusCode != 200 {
3182+ return sourceID , fmt .Errorf ("error getting source id status code: %d" , statusCode )
3183+ }
3184+ var repo response
3185+ if err = jsoniter .Unmarshal (res , & repo ); err != nil {
3186+ return sourceID , err
3187+ }
3188+ if repo .SourceID != 0 {
3189+ sourceID = strconv .FormatInt (repo .SourceID , 10 )
3190+ }
3191+ return sourceID , nil
3192+ }
3193+
3194+ func (j * DSGit ) getGerritRepoSourceId () (string , error ) {
3195+ sourceID := ""
3196+ const (
3197+ repoSeparatorGerrit = "/gerrit/"
3198+ repoSeparatorR = "/r/"
3199+ )
3200+ repoSeparatorList := []string {repoSeparatorR , repoSeparatorGerrit }
3201+ for _ , repoSeparator := range repoSeparatorList {
3202+ repoSlice := strings .Split (j .URL , repoSeparator )
3203+ if len (repoSlice ) > 1 {
3204+ if repoSeparator == repoSeparatorGerrit {
3205+ nRepoSlice := strings .Split (j .URL , repoSeparatorR )
3206+ if len (nRepoSlice ) > 1 {
3207+ sourceID = strings .TrimSpace (repoSlice [1 ])
3208+ break
3209+ }
3210+ }
3211+ sourceID = strings .TrimSpace (repoSlice [1 ])
3212+ break
3213+ }
3214+ }
3215+
3216+ // check if url already contains a partial
3217+ hasPartial := false
3218+ u , err := url .Parse (j .URL )
3219+ if err != nil {
3220+ return sourceID , err
3221+ }
3222+
3223+ for _ , p := range repoSeparatorList {
3224+ repoSlice := strings .Split (u .Path , p )
3225+ if len (repoSlice ) > 1 {
3226+ hasPartial = true
3227+ break
3228+ }
3229+ }
3230+
3231+ if ! hasPartial {
3232+ repoSlice := strings .Split (j .URL , strings .TrimSpace (fmt .Sprintf ("%s://%s" , u .Scheme , u .Host )))
3233+ if len (repoSlice ) > 1 {
3234+ sourceID = strings .TrimPrefix (strings .TrimSuffix (strings .TrimSpace (repoSlice [1 ]), "/" ), "/" )
3235+ }
3236+ }
3237+ return sourceID , nil
3238+ }
3239+
31033240// CommitCache single commit cache schema
31043241type CommitCache struct {
31053242 Timestamp string `json:"timestamp"`
0 commit comments