Skip to content

Commit

Permalink
refactor: pass api tokens to GetRepoImporterFromURL func
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoksr committed Jun 13, 2020
1 parent 5b6609b commit 4d72b53
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
6 changes: 3 additions & 3 deletions pkg/proji/storage/item/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
11 changes: 9 additions & 2 deletions pkg/proji/storage/item/class_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4d72b53

Please sign in to comment.