[bridge/github] exporter: Check bug import origin

Amine Hilaly created

[bridge/github] export only allowed bugs

Change summary

bridge/github/export.go | 36 +++++++++++++++++++++++++++++++++---
1 file changed, 33 insertions(+), 3 deletions(-)

Detailed changes

bridge/github/export.go 🔗

@@ -22,6 +22,9 @@ import (
 type githubExporter struct {
 	conf core.Configuration
 
+	// export only bugs taged with one of these origins
+	onlyOrigins []string
+
 	// cache identities clients
 	identityClient map[string]*githubv4.Client
 
@@ -41,8 +44,8 @@ type githubExporter struct {
 
 // Init .
 func (ge *githubExporter) Init(conf core.Configuration) error {
-	//TODO: initialize with multiple tokens
 	ge.conf = conf
+	//TODO: initialize with multiple tokens
 	ge.identityToken = make(map[string]string)
 	ge.identityClient = make(map[string]*githubv4.Client)
 	ge.cachedIDs = make(map[string]string)
@@ -50,18 +53,38 @@ func (ge *githubExporter) Init(conf core.Configuration) error {
 	return nil
 }
 
+func (ge *githubExporter) allowOrigin(origin string) bool {
+	if ge.onlyOrigins == nil {
+		return true
+	}
+
+	for _, o := range ge.onlyOrigins {
+		if origin == o {
+			return true
+		}
+	}
+
+	return false
+}
+
 func (ge *githubExporter) getIdentityClient(id string) (*githubv4.Client, error) {
 	client, ok := ge.identityClient[id]
 	if ok {
 		return client, nil
 	}
 
+	// get token
 	token, ok := ge.identityToken[id]
 	if !ok {
 		return nil, fmt.Errorf("unknown identity token")
 	}
 
-	return buildClient(token), nil
+	// create client
+	client = buildClient(token)
+	// cache client
+	ge.identityClient[id] = client
+
+	return client, nil
 }
 
 // ExportAll export all event made by the current user to Github
@@ -99,8 +122,8 @@ bugLoop:
 			continue
 		}
 
-		// if identity participated in a bug
 		for _, p := range snapshot.Participants {
+			// if we have a token for one of the participants
 			for userId := range ge.identityToken {
 				if p.Id() == userId {
 					// try to export the bug and it associated events
@@ -133,6 +156,13 @@ func (ge *githubExporter) exportBug(b *cache.BugCache, since time.Time) error {
 	createOp := snapshot.Operations[0].(*bug.CreateOperation)
 	author := createOp.GetAuthor()
 
+	// skip bug if origin is not allowed
+	origin, ok := createOp.GetMetadata(keyOrigin)
+	if ok && !ge.allowOrigin(origin) {
+		// TODO print a warn ?
+		return nil
+	}
+
 	// get github bug ID
 	githubID, ok := createOp.GetMetadata(keyGithubId)
 	if ok {