-
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 (#238)
- Loading branch information
1 parent
8455003
commit b4ee0fc
Showing
8 changed files
with
171 additions
and
107 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
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,27 @@ | ||
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) { | ||
dsnPattern := regexp.MustCompile(`^(https?)://([^:@]+)@([^:/]+(?:\:\d+)?)`) | ||
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,70 @@ | ||
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: "valid DSN with port", | ||
args: args{ | ||
raw: "http://f59a44307@localhost:8081", | ||
}, | ||
want: &DSN{ | ||
Token: "f59a44307", | ||
Host: "localhost:8081", | ||
Protocol: "http", | ||
}, | ||
}, | ||
{ | ||
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
Oops, something went wrong.