Skip to content
This repository was archived by the owner on May 24, 2024. It is now read-only.

Commit 6bf9e18

Browse files
get github source id from onboarding api and generate gerrit source id (#84)
Signed-off-by: Ayman <[email protected]> Co-authored-by: Ayman <[email protected]>
1 parent ad26e05 commit 6bf9e18

File tree

4 files changed

+142
-2
lines changed

4 files changed

+142
-2
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ ENV GIT_SOURCE_ID='<GIT_SOURCE_ID>'
1212
ENV GIT_REPOSITORY_SOURCE='<GIT_REPOSITORY_SOURCE>'
1313
ENV LAST_SYNC='<LAST_SYNC>'
1414
ENV SPAN='<SPAN>'
15+
ENV INSIGHTS_SERVICE_URL_V2='<INSIGHTS_SERVICE_URL_V2>'
1516
RUN apk update && apk add git
1617
RUN apk add cloc
1718
RUN apk add --no-cache bash

cmd/git/git.go

Lines changed: 139 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
31043241
type CommitCache struct {
31053242
Timestamp string `json:"timestamp"`

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ require (
2323
github.com/dgraph-io/ristretto v0.1.0 // indirect
2424
github.com/dustin/go-humanize v1.0.0 // indirect
2525
github.com/elastic/go-elasticsearch/v8 v8.0.0-20201229214741-2366c2514674 // indirect
26+
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
2627
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
2728
github.com/google/uuid v1.3.0 // indirect
2829
github.com/jmespath/go-jmespath v0.4.0 // indirect

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4
5454
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
5555
github.com/elastic/go-elasticsearch/v8 v8.0.0-20201229214741-2366c2514674 h1:heH4w5l/KFP4Ry9Xp4+jbRx0Wn+TJD7+HlyoMJE4LvQ=
5656
github.com/elastic/go-elasticsearch/v8 v8.0.0-20201229214741-2366c2514674/go.mod h1:xe9a/L2aeOgFKKgrO3ibQTnMdpAeL0GC+5/HpGScSa4=
57+
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
5758
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
5859
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
5960
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=

0 commit comments

Comments
 (0)