From 4d72b53c44dc7ab3160695738fa772d6b6d913e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niko=20K=C3=B6ser?= Date: Sat, 13 Jun 2020 02:42:13 +0200 Subject: [PATCH] refactor: pass api tokens to GetRepoImporterFromURL func --- pkg/proji/storage/item/class.go | 6 +++--- pkg/proji/storage/item/class_test.go | 11 +++++++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/pkg/proji/storage/item/class.go b/pkg/proji/storage/item/class.go index b34881b9..952427c8 100644 --- a/pkg/proji/storage/item/class.go +++ b/pkg/proji/storage/item/class.go @@ -423,15 +423,15 @@ func pickLabel(className string) string { */ // GetRepoImporterFromURL returns the most suiting importer based on the code hosting platform. -func GetRepoImporterFromURL(URL *url.URL) (repo.Importer, error) { +func GetRepoImporterFromURL(URL *url.URL, auth *config.APIAuthentication) (repo.Importer, error) { var importer repo.Importer var err error switch URL.Hostname() { case "github.com": - importer, err = github.New(URL) + importer, err = github.New(URL, auth.GHToken) case "gitlab.com": - importer, err = gitlab.New(URL) + importer, err = gitlab.New(URL, auth.GLToken) default: return nil, fmt.Errorf("platform not supported yet") } diff --git a/pkg/proji/storage/item/class_test.go b/pkg/proji/storage/item/class_test.go index 19b26513..69aa3cdd 100644 --- a/pkg/proji/storage/item/class_test.go +++ b/pkg/proji/storage/item/class_test.go @@ -7,6 +7,8 @@ import ( "path/filepath" "testing" + "github.com/nikoksr/proji/pkg/config" + "github.com/nikoksr/proji/pkg/proji/repo/github" "github.com/nikoksr/proji/pkg/proji/repo" @@ -30,6 +32,11 @@ var badURLs = []*url.URL{ {Scheme: "https", Host: "google.com", Path: ""}, } +var apiAuth = config.APIAuthentication{ + GHToken: os.Getenv("PROJI_AUTH_GH_TOKEN"), + GLToken: os.Getenv("PROJI_AUTH_GL_TOKEN"), +} + func TestNewClass(t *testing.T) { classExp := &Class{ Name: "test", @@ -268,7 +275,7 @@ func TestClass_ImportRepoStructure(t *testing.T) { for _, test := range tests { c := NewClass("", "", false) - importer, err := GetRepoImporterFromURL(test.URL) + importer, err := GetRepoImporterFromURL(test.URL, &apiAuth) if err != nil { t.Errorf("failed getting repo importer for URL %s", test.URL.String()) } @@ -442,7 +449,7 @@ func TestGetRepoImporterFromURL(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := GetRepoImporterFromURL(tt.args.URL) + got, err := GetRepoImporterFromURL(tt.args.URL, &apiAuth) assert.Equal(t, err != nil, tt.wantErr, "GetRepoImporterFromURL() error = %v, wantErr %v", err, tt.wantErr) if got != nil && tt.want != nil {