-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: start refactoring the report command
- Loading branch information
1 parent
8455003
commit 318ff26
Showing
3 changed files
with
141 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package report | ||
|
||
import ( | ||
"errors" | ||
"regexp" | ||
) | ||
|
||
var ( | ||
ErrInvalidDSN = errors.New("DeepSource | Error | Invalid DSN. Cross verify DEEPSOURCE_DSN value against the settings page of the repository") | ||
) | ||
|
||
type DSN struct { | ||
Protocol string | ||
Host string | ||
Token string | ||
} | ||
|
||
func NewDSN(raw string) (*DSN, error) { | ||
|
||
var dsnPattern = regexp.MustCompile(`^(https?)://([^:@]+)@([^:/]+)`) | ||
matches := dsnPattern.FindStringSubmatch(raw) | ||
if len(matches) != 4 { | ||
return nil, ErrInvalidDSN | ||
} | ||
return &DSN{ | ||
Protocol: matches[1], | ||
Token: matches[2], | ||
Host: matches[3], | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package report | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestNewDSN(t *testing.T) { | ||
type args struct { | ||
raw string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *DSN | ||
wantErr error | ||
}{ | ||
{ | ||
name: "valid DSN", | ||
args: args{ | ||
raw: "https://[email protected]", | ||
}, | ||
want: &DSN{ | ||
Token: "e1099ed7240c4045b5ab3fedebc7b5d7", | ||
Host: "app.deepsource.com", | ||
Protocol: "https", | ||
}, | ||
wantErr: nil, | ||
}, | ||
{ | ||
name: "invalid DSN no http", | ||
args: args{ | ||
raw: "no http", | ||
}, | ||
want: nil, | ||
wantErr: ErrInvalidDSN, | ||
}, | ||
{ | ||
name: "invalid DSN", | ||
args: args{ | ||
raw: "https://e1099ed7240c4045b5ab3fedebc7b5d7", | ||
}, | ||
want: nil, | ||
wantErr: ErrInvalidDSN, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := NewDSN(tt.args.raw) | ||
if err != tt.wantErr { | ||
t.Errorf("NewDSN() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("NewDSN() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters