-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0b6c2ef
commit 97fb795
Showing
3 changed files
with
87 additions
and
29 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,26 @@ | ||
package check | ||
|
||
import ( | ||
"net/mail" | ||
"strings" | ||
) | ||
|
||
func IsEmailAddress(s string) bool { | ||
_, err := mail.ParseAddress(s) | ||
return err == nil | ||
} | ||
|
||
func IsGithubTeam(s string) bool { | ||
hasPrefix := strings.HasPrefix(s, "@") | ||
containsSlash := strings.Contains(s, "/") | ||
split := strings.SplitN(s, "/", 3) // 3 is enough to confirm that is invalid + will not overflow the buffer | ||
return hasPrefix && containsSlash && len(split) == 2 && len(split[1]) > 0 | ||
} | ||
|
||
func IsGithubUser(s string) bool { | ||
return !strings.Contains(s, "/") && strings.HasPrefix(s, "@") | ||
} | ||
|
||
func IsGithubGhostUser(s string) bool { | ||
return s == "@ghost" | ||
} |
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,52 @@ | ||
package check_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mszostok/codeowners-validator/internal/check" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestValidOwnerChecker(t *testing.T) { | ||
tests := map[string]struct { | ||
user string | ||
isValid bool | ||
}{ | ||
"Invalid Email": { | ||
user: `asda.comm`, | ||
isValid: false, | ||
}, | ||
"Valid Email": { | ||
user: `gmail@gmail.com`, | ||
isValid: true, | ||
}, | ||
"Invalid Team": { | ||
user: `@org/`, | ||
isValid: false, | ||
}, | ||
"Valid Team": { | ||
user: `@org/user`, | ||
isValid: true, | ||
}, | ||
"Invalid User": { | ||
user: `user`, | ||
isValid: false, | ||
}, | ||
"Valid User": { | ||
user: `@user`, | ||
isValid: true, | ||
}, | ||
} | ||
for tn, tc := range tests { | ||
t.Run(tn, func(t *testing.T) { | ||
// when | ||
result := isValidUser(tc.user) | ||
assert.Equal(t, tc.isValid, result) | ||
}) | ||
} | ||
} | ||
|
||
func isValidUser(user string) bool { | ||
return check.IsEmailAddress(user) || check.IsGithubUser(user) || check.IsGithubTeam(user) | ||
} |