-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to GET all materials API
- Loading branch information
1 parent
7a325a3
commit 8d96eda
Showing
8 changed files
with
288 additions
and
13 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 |
---|---|---|
|
@@ -19,6 +19,7 @@ linters: | |
- forcetypeassert | ||
- dupl | ||
- goconst | ||
- maligned | ||
|
||
issues: | ||
exclude-rules: | ||
|
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
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,53 @@ | ||
{ | ||
"_links": { | ||
"doc": { | ||
"href": "https://api.gocd.org/#materials" | ||
}, | ||
"self": { | ||
"href": "https://ci.example.com/go/api/config/materials" | ||
} | ||
}, | ||
"_embedded": { | ||
"materials": [ | ||
{ | ||
"type": "svn", | ||
"fingerprint": "6b0cd6b9181434866c555f3c3bb780e950389a456764c876d804c848efbad554", | ||
"attributes": { | ||
"url": "https://github.com/gocd/gocd", | ||
"destination": "test", | ||
"filter": null, | ||
"invert_filter": false, | ||
"name": null, | ||
"auto_update": false, | ||
"username": "admin", | ||
"encrypted_password": "AES:lTFKwi5NvrlqmQ3LOQ5UQA==:Ebggz5N27w54NrhSXKIbng==", | ||
"check_externals": false | ||
} | ||
}, | ||
{ | ||
"type": "hg", | ||
"fingerprint": "f6b61bc6b33e524c2a94c5be4a4661e5f1d2b74ca089418de20de10b282e39e9", | ||
"attributes": { | ||
"url": "ssh://[email protected]/example/gocd-hg", | ||
"destination": "test", | ||
"filter": null, | ||
"invert_filter": false, | ||
"name": null, | ||
"auto_update": false, | ||
"branch": "" | ||
} | ||
}, | ||
{ | ||
"type": "dependency", | ||
"fingerprint": "21f7963a8eca6cced5b4f20b57b4e9cb8edaed29d683c83621a77dfb499c553d", | ||
"attributes": { | ||
"pipeline": "up42", | ||
"stage": "up42_stage", | ||
"name": "up42_material", | ||
"auto_update": true, | ||
"ignore_for_scheduling": false | ||
} | ||
} | ||
] | ||
} | ||
} |
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,36 @@ | ||
package gocd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/jinzhu/copier" | ||
) | ||
|
||
func (conf *client) GetMaterials() ([]Material, error) { | ||
newClient := &client{} | ||
if err := copier.CopyWithOption(newClient, conf, copier.Option{IgnoreEmpty: true, DeepCopy: true}); err != nil { | ||
return nil, err | ||
} | ||
|
||
var materials Materials | ||
resp, err := newClient.httpClient.R(). | ||
SetHeaders(map[string]string{ | ||
"Accept": HeaderVersionTwo, | ||
}). | ||
Get(MaterialEndpoint) | ||
if err != nil { | ||
return nil, fmt.Errorf("call made to get all available materials errored with: %w", err) | ||
} | ||
|
||
if resp.StatusCode() != http.StatusOK { | ||
return nil, APIErrorWithBody(resp.String(), resp.StatusCode()) | ||
} | ||
|
||
if err = json.Unmarshal(resp.Body(), &materials); err != nil { | ||
return nil, ResponseReadError(err.Error()) | ||
} | ||
|
||
return materials.Materials.Materials, 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,150 @@ | ||
package gocd_test | ||
|
||
import ( | ||
_ "embed" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/nikhilsbhat/gocd-sdk-go" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
//go:embed internal/fixtures/materials.json | ||
var attributesJSON string | ||
|
||
func Test_client_GetMaterials(t *testing.T) { | ||
correctArtifactHeader := map[string]string{"Accept": gocd.HeaderVersionTwo} | ||
t.Run("should be able to fetch all available materials present in GoCD successfully", func(t *testing.T) { | ||
server := mockServer([]byte(attributesJSON), http.StatusOK, | ||
correctArtifactHeader, false, nil) | ||
|
||
client := gocd.NewClient( | ||
server.URL, | ||
"admin", | ||
"admin", | ||
"info", | ||
nil, | ||
) | ||
|
||
expected := []gocd.Material{ | ||
{ | ||
Type: "svn", | ||
Fingerprint: "6b0cd6b9181434866c555f3c3bb780e950389a456764c876d804c848efbad554", | ||
Attributes: gocd.Attribute{ | ||
URL: "https://github.com/gocd/gocd", | ||
Username: "admin", | ||
EncryptedPassword: "AES:lTFKwi5NvrlqmQ3LOQ5UQA==:Ebggz5N27w54NrhSXKIbng==", | ||
Destination: "test", | ||
AutoUpdate: false, | ||
CheckExternals: false, | ||
UseTickets: false, | ||
IgnoreForScheduling: false, | ||
InvertFilter: false, | ||
}, | ||
}, | ||
{ | ||
Type: "hg", | ||
Fingerprint: "f6b61bc6b33e524c2a94c5be4a4661e5f1d2b74ca089418de20de10b282e39e9", | ||
Attributes: gocd.Attribute{ | ||
URL: "ssh://[email protected]/example/gocd-hg", | ||
Destination: "test", | ||
AutoUpdate: false, | ||
CheckExternals: false, | ||
UseTickets: false, | ||
IgnoreForScheduling: false, | ||
InvertFilter: false, | ||
}, | ||
}, | ||
{ | ||
Type: "dependency", | ||
Fingerprint: "21f7963a8eca6cced5b4f20b57b4e9cb8edaed29d683c83621a77dfb499c553d", | ||
Attributes: gocd.Attribute{ | ||
Pipeline: "up42", | ||
Stage: "up42_stage", | ||
Name: "up42_material", | ||
AutoUpdate: true, | ||
IgnoreForScheduling: false, | ||
}, | ||
}, | ||
} | ||
|
||
actual, err := client.GetMaterials() | ||
assert.NoError(t, err) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
t.Run("should error out while fetching all available materials present in GoCD due to wrong headers", func(t *testing.T) { | ||
server := mockServer([]byte(attributesJSON), http.StatusOK, | ||
map[string]string{"Accept": gocd.HeaderVersionThree}, false, nil) | ||
|
||
client := gocd.NewClient( | ||
server.URL, | ||
"admin", | ||
"admin", | ||
"info", | ||
nil, | ||
) | ||
|
||
var expected []gocd.Material | ||
|
||
actual, err := client.GetMaterials() | ||
assert.EqualError(t, err, "body: <html>\n<body>\n\t<h2>404 Not found</h2>\n</body>\n\n</html> httpcode: 404") | ||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
t.Run("should error out while fetching all available materials present in GoCD due to missing headers", func(t *testing.T) { | ||
server := mockServer([]byte(attributesJSON), http.StatusOK, | ||
nil, false, nil) | ||
client := gocd.NewClient( | ||
server.URL, | ||
"admin", | ||
"admin", | ||
"info", | ||
nil, | ||
) | ||
|
||
var expected []gocd.Material | ||
|
||
actual, err := client.GetMaterials() | ||
assert.EqualError(t, err, "body: <html>\n<body>\n\t<h2>404 Not found</h2>\n</body>\n\n</html> httpcode: 404") | ||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
t.Run("should error out while fetching all available materials from GoCD as server returned malformed response", func(t *testing.T) { | ||
server := mockServer([]byte("attributesJSON"), http.StatusOK, correctArtifactHeader, | ||
false, nil) | ||
client := gocd.NewClient( | ||
server.URL, | ||
"admin", | ||
"admin", | ||
"info", | ||
nil, | ||
) | ||
|
||
var expected []gocd.Material | ||
|
||
actual, err := client.GetMaterials() | ||
assert.EqualError(t, err, "reading response body errored with: invalid character 'a' looking for beginning of value") | ||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
t.Run("should error out while fetching all available materials present in GoCD as server is not reachable", func(t *testing.T) { | ||
client := gocd.NewClient( | ||
"http://localhost:8156/go", | ||
"admin", | ||
"admin", | ||
"info", | ||
nil, | ||
) | ||
|
||
client.SetRetryCount(1) | ||
client.SetRetryWaitTime(1) | ||
|
||
var expected []gocd.Material | ||
|
||
actual, err := client.GetMaterials() | ||
assert.EqualError(t, err, "call made to get all available materials errored with: "+ | ||
"Get \"http://localhost:8156/go/api/config/materials\": dial tcp [::1]:8156: connect: connection refused") | ||
assert.Equal(t, expected, actual) | ||
}) | ||
} |
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