feat: add GetBranchResponse

Parse a single Branch from an APIResponse
Useful in combination with the GetDefaultBranch API call
This commit is contained in:
mlosicki 2022-01-24 00:41:10 +01:00
parent dff2223ade
commit 4d3afe3c12
2 changed files with 56 additions and 0 deletions

View File

@ -554,6 +554,13 @@ func GetBranchesResponse(r *APIResponse) ([]Branch, error) {
return m, err return m, err
} }
// GetBrancheResponse cast Branch into structure
func GetBranchResponse(r *APIResponse) (Branch, error) {
var m Branch
err := mapstructure.Decode(r.Values, &m)
return m, err
}
// GetRepositoriesResponse cast Repositories into structure // GetRepositoriesResponse cast Repositories into structure
func GetRepositoriesResponse(r *APIResponse) ([]Repository, error) { func GetRepositoriesResponse(r *APIResponse) ([]Repository, error) {
var m []Repository var m []Repository

View File

@ -115,6 +115,55 @@ func TestGetBranchesResponse(t *testing.T) {
} }
} }
func TestGetBranchResponse(t *testing.T) {
type args struct {
r *APIResponse
}
tests := []struct {
name string
args args
want Branch
wantErr bool
}{
{
name: "Single branch",
args: args{
r: &APIResponse{
Values: map[string]interface{}{
"id": "refs/heads/main",
"displayId": "main",
"type": "BRANCH",
"latestCommit": "8d51122def5632836d1cb1026e879069e10a1e13",
"latestChangeset": "8d51122def5632836d1cb1026e879069e10a1e13",
"isDefault": true,
},
},
},
want: Branch{
ID: "refs/heads/main",
DisplayID: "main",
Type: "BRANCH",
LatestCommit: "8d51122def5632836d1cb1026e879069e10a1e13",
LatestChangeset: "8d51122def5632836d1cb1026e879069e10a1e13",
IsDefault: true,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetBranchResponse(tt.args.r)
if (err != nil) != tt.wantErr {
t.Errorf("GetBranchResponse() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetBranchResponse() = %v, want %v", got, tt.want)
}
})
}
}
func TestGetRepositoriesResponse(t *testing.T) { func TestGetRepositoriesResponse(t *testing.T) {
type args struct { type args struct {
r *APIResponse r *APIResponse