Bridges: move credential loading and client creation

Alexander Scharinger created

Gitlab and Jira bridge: move credential loading and client creation from
`Init` to `ImportAll` in order to harmonize the behaviour of the
different bridges.

Change summary

bridge/gitlab/import.go | 25 +++++++++-----------
bridge/jira/import.go   | 52 ++++++++++++++++++++++--------------------
2 files changed, 38 insertions(+), 39 deletions(-)

Detailed changes

bridge/gitlab/import.go 🔗

@@ -33,32 +33,29 @@ type gitlabImporter struct {
 
 func (gi *gitlabImporter) Init(_ context.Context, repo *cache.RepoCache, conf core.Configuration) error {
 	gi.conf = conf
+	return nil
+}
 
+// ImportAll iterate over all the configured repository issues (notes) and ensure the creation
+// of the missing issues / comments / label events / title changes ...
+func (gi *gitlabImporter) ImportAll(ctx context.Context, repo *cache.RepoCache, since time.Time) (<-chan core.ImportResult, error) {
 	creds, err := auth.List(repo,
 		auth.WithTarget(target),
 		auth.WithKind(auth.KindToken),
-		auth.WithMeta(auth.MetaKeyBaseURL, conf[confKeyGitlabBaseUrl]),
-		auth.WithMeta(auth.MetaKeyLogin, conf[confKeyDefaultLogin]),
+		auth.WithMeta(auth.MetaKeyBaseURL, gi.conf[confKeyGitlabBaseUrl]),
+		auth.WithMeta(auth.MetaKeyLogin, gi.conf[confKeyDefaultLogin]),
 	)
 	if err != nil {
-		return err
+		return nil, err
 	}
-
 	if len(creds) == 0 {
-		return ErrMissingIdentityToken
+		return nil, ErrMissingIdentityToken
 	}
-
-	gi.client, err = buildClient(conf[confKeyGitlabBaseUrl], creds[0].(*auth.Token))
+	gi.client, err = buildClient(gi.conf[confKeyGitlabBaseUrl], creds[0].(*auth.Token))
 	if err != nil {
-		return err
+		return nil, err
 	}
 
-	return nil
-}
-
-// ImportAll iterate over all the configured repository issues (notes) and ensure the creation
-// of the missing issues / comments / label events / title changes ...
-func (gi *gitlabImporter) ImportAll(ctx context.Context, repo *cache.RepoCache, since time.Time) (<-chan core.ImportResult, error) {
 	gi.iterator = iterator.NewIterator(ctx, gi.client, 10, gi.conf[confKeyProjectID], since)
 	out := make(chan core.ImportResult)
 	gi.out = out

bridge/jira/import.go 🔗

@@ -34,6 +34,12 @@ type jiraImporter struct {
 // Init .
 func (ji *jiraImporter) Init(ctx context.Context, repo *cache.RepoCache, conf core.Configuration) error {
 	ji.conf = conf
+	return nil
+}
+
+// ImportAll iterate over all the configured repository issues and ensure the
+// creation of the missing issues / timeline items / edits / label events ...
+func (ji *jiraImporter) ImportAll(ctx context.Context, repo *cache.RepoCache, since time.Time) (<-chan core.ImportResult, error) {
 
 	var cred auth.Credential
 
@@ -41,44 +47,40 @@ func (ji *jiraImporter) Init(ctx context.Context, repo *cache.RepoCache, conf co
 	creds, err := auth.List(repo,
 		auth.WithTarget(target),
 		auth.WithKind(auth.KindLoginPassword),
-		auth.WithMeta(auth.MetaKeyBaseURL, conf[confKeyBaseUrl]),
-		auth.WithMeta(auth.MetaKeyLogin, conf[confKeyDefaultLogin]),
-	)
-	if err != nil {
-		return err
-	}
-	if len(creds) > 0 {
-		cred = creds[0]
-		goto end
-	}
-
-	creds, err = auth.List(repo,
-		auth.WithTarget(target),
-		auth.WithKind(auth.KindLogin),
-		auth.WithMeta(auth.MetaKeyBaseURL, conf[confKeyBaseUrl]),
-		auth.WithMeta(auth.MetaKeyLogin, conf[confKeyDefaultLogin]),
+		auth.WithMeta(auth.MetaKeyBaseURL, ji.conf[confKeyBaseUrl]),
+		auth.WithMeta(auth.MetaKeyLogin, ji.conf[confKeyDefaultLogin]),
 	)
 	if err != nil {
-		return err
+		return nil, err
 	}
 	if len(creds) > 0 {
 		cred = creds[0]
+	} else {
+		creds, err = auth.List(repo,
+			auth.WithTarget(target),
+			auth.WithKind(auth.KindLogin),
+			auth.WithMeta(auth.MetaKeyBaseURL, ji.conf[confKeyBaseUrl]),
+			auth.WithMeta(auth.MetaKeyLogin, ji.conf[confKeyDefaultLogin]),
+		)
+		if err != nil {
+			return nil, err
+		}
+		if len(creds) > 0 {
+			cred = creds[0]
+		}
 	}
 
-end:
 	if cred == nil {
-		return fmt.Errorf("no credential for this bridge")
+		return nil, fmt.Errorf("no credential for this bridge")
 	}
 
 	// TODO(josh)[da52062]: Validate token and if it is expired then prompt for
 	// credentials and generate a new one
-	ji.client, err = buildClient(ctx, conf[confKeyBaseUrl], conf[confKeyCredentialType], cred)
-	return err
-}
+	ji.client, err = buildClient(ctx, ji.conf[confKeyBaseUrl], ji.conf[confKeyCredentialType], cred)
+	if err != nil {
+		return nil, err
+	}
 
-// ImportAll iterate over all the configured repository issues and ensure the
-// creation of the missing issues / timeline items / edits / label events ...
-func (ji *jiraImporter) ImportAll(ctx context.Context, repo *cache.RepoCache, since time.Time) (<-chan core.ImportResult, error) {
 	sinceStr := since.Format("2006-01-02 15:04")
 	project := ji.conf[confKeyProject]