-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow passing of commit hash on the cli (#278)
Signed-off-by: Benji Visser <benji@093b.org>
- Loading branch information
Showing
5 changed files
with
134 additions
and
1 deletion.
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,16 @@ | ||
package types | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
type CommitHash string | ||
|
||
func (c CommitHash) IsValid() error { | ||
re := regexp.MustCompile(`^[a-fA-F0-9]{40}$`) | ||
if !re.MatchString(string(c)) { | ||
return fmt.Errorf("invalid SHA1 hash format for commit hash '%s'", string(c)) | ||
} | ||
return 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,27 @@ | ||
package types | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestCommitHash_IsValid(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
hash CommitHash | ||
wantErr bool | ||
}{ | ||
{"Valid SHA1", "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", false}, | ||
{"Invalid SHA1 - Short", "a94a8fe5cc", true}, | ||
{"Invalid SHA1 - Long", "a94a8fe5ccb19ba61c4c0873d391e9879", true}, | ||
{"Invalid SHA1 - Special Characters", "a94a8fe5cc#19ba61c4c0873d391e9$", true}, | ||
{"Invalid SHA1 - Empty", "", true}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := tt.hash.IsValid(); (err != nil) != tt.wantErr { | ||
t.Errorf("CommitHash.IsValid() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
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,16 @@ | ||
package types | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
type ProjectName string | ||
|
||
func (p ProjectName) IsValid() error { | ||
re := regexp.MustCompile(`^(gitlab|github|azure)//([a-zA-Z0-9\-_]+/[a-zA-Z0-9\-_]+(/[a-zA-Z0-9\-_]+)?)$`) | ||
if ok := re.MatchString(string(p)); !ok { | ||
return fmt.Errorf("invalid project name. Accepted formats: 'gitlab//<owner>/<repo>', 'github//<owner>/<repo>', 'azure//<owner>/<project>/<repo>'") | ||
} | ||
return 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,46 @@ | ||
package types | ||
|
||
import "testing" | ||
|
||
func TestIsValidProjectName(t *testing.T) { | ||
tests := []struct { | ||
projectName string | ||
wantErr bool | ||
}{ | ||
{ | ||
projectName: "gitlab//noqcks/test", | ||
wantErr: false, | ||
}, | ||
{ | ||
projectName: "github//noqcks/test", | ||
wantErr: false, | ||
}, | ||
{ | ||
projectName: "azure//noqcks/test", | ||
wantErr: false, | ||
}, | ||
{ | ||
projectName: "azure//noqcks/test/test", | ||
wantErr: false, | ||
}, | ||
{ | ||
projectName: "azure//noqcks/test/test/test", | ||
wantErr: true, | ||
}, | ||
{ | ||
projectName: "azure//noqcks", | ||
wantErr: true, | ||
}, | ||
{ | ||
projectName: "test//test", | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
err := ProjectName(test.projectName).IsValid() | ||
if test.wantErr && err == nil { | ||
t.Errorf("Expected error for '%s', but got nil", test.projectName) | ||
} | ||
} | ||
} |