Skip to content

Commit

Permalink
Merge pull request #73 from tombuildsstuff/project-features
Browse files Browse the repository at this point in the history
Project Features incl update/delete
  • Loading branch information
cvbarros authored Apr 20, 2020
2 parents d6ec381 + 66c21ad commit 9187e42
Show file tree
Hide file tree
Showing 8 changed files with 618 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ integration_tests/log_dir/

#OSX
.DS_Store

# vendor folders aren't needed in libraries
vendor/
2 changes: 1 addition & 1 deletion teamcity/build_feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (suite *SuiteBuildFeature) SetupTest() {
}

func (suite *SuiteBuildFeature) TearDownTest() {
// suite.BuildTypeContext.Teardown()
suite.BuildTypeContext.Teardown()
}

func (suite *SuiteBuildFeature) Service() *teamcity.BuildFeatureService {
Expand Down
146 changes: 146 additions & 0 deletions teamcity/project_feature.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package teamcity

import (
"fmt"
"net/http"

"github.com/dghubble/sling"
)

// The ProjectFeature interface represents the different types of features that can be added to a project.
type ProjectFeature interface {
ID() string
SetID(value string)

Type() string

ProjectID() string
SetProjectID(value string)

Properties() *Properties
}

type projectFeatureJSON struct {
ID string `json:"id,omitempty" xml:"id"`
Type string `json:"type,omitempty" xml:"type"`
Href string `json:"href,omitempty" xml:"href"`
Properties *Properties `json:"properties,omitempty"`
}

type projectFeatures struct {
Count int32 `json:"count,omitempty" xml:"count"`
Href string `json:"href,omitempty" xml:"href"`
Items []projectFeatureJSON `json:"projectFeature"`
}

// ProjectFeatureService provides operations for managing project features.
type ProjectFeatureService struct {
ProjectID string
restHelper *restHelper
}

func newProjectFeatureService(projectID string, c *http.Client, sling *sling.Sling) *ProjectFeatureService {
return &ProjectFeatureService{
ProjectID: projectID,
restHelper: newRestHelperWithSling(c, sling),
}
}

// Create creates a new ProjectFeature under the current project.
func (s *ProjectFeatureService) Create(feature ProjectFeature) (ProjectFeature, error) {
if feature == nil {
return nil, fmt.Errorf("feature is nil")
}
if feature.ProjectID() != s.ProjectID {
return nil, fmt.Errorf("given ProjectFeature for project %q to ProjectFeatureService for project %q.", feature.ProjectID(), s.ProjectID)
}

requestBody := &projectFeatureJSON{
Type: feature.Type(),
Properties: feature.Properties(),
}
createdProjectFeature := &projectFeatureJSON{}

url := fmt.Sprintf("projects/%s/projectFeatures", s.ProjectID)
if err := s.restHelper.post(url, &requestBody, createdProjectFeature, "projectFeature"); err != nil {
return nil, err
}

return s.parseProjectFeatureJSONResponse(*createdProjectFeature)
}

// Delete removes a single ProjectFeature for the current project by it's id.
func (s *ProjectFeatureService) Delete(id string) error {
url := fmt.Sprintf("projects/%s/projectFeatures/%s", s.ProjectID, id)
if err := s.restHelper.delete(url, "projectFeature"); err != nil {
return err
}

return nil
}

// Get all project features for the current project.
func (s *ProjectFeatureService) Get() ([]ProjectFeature, error) {
var out projectFeatures

url := fmt.Sprintf("projects/%s/projectFeatures", s.ProjectID)
if err := s.restHelper.get(url, &out, "projectFeature"); err != nil {
return nil, err
}

result := make([]ProjectFeature, len(out.Items))
for i, featureJSON := range out.Items {
feature, err := s.parseProjectFeatureJSONResponse(featureJSON)
if err != nil {
return result[:i], err
}

result[i] = feature
}

return result, nil
}

// GetByID returns a single ProjectFeature for the current project by it's id.
func (s *ProjectFeatureService) GetByID(id string) (ProjectFeature, error) {
var out projectFeatureJSON

url := fmt.Sprintf("projects/%s/projectFeatures/%s", s.ProjectID, id)
if err := s.restHelper.get(url, &out, "projectFeature"); err != nil {
return nil, err
}

return s.parseProjectFeatureJSONResponse(out)
}

// Update updated an existing a ProjectFeature under the current project.
func (s *ProjectFeatureService) Update(feature ProjectFeature) (ProjectFeature, error) {
if feature == nil {
return nil, fmt.Errorf("feature is nil")
}
if feature.ProjectID() != s.ProjectID {
return nil, fmt.Errorf("given ProjectFeature for project %q to ProjectFeatureService for project %q.", feature.ProjectID(), s.ProjectID)
}

requestBody := &projectFeatureJSON{
Type: feature.Type(),
Properties: feature.Properties(),
}
updatedProjectFeature := &projectFeatureJSON{}

url := fmt.Sprintf("projects/%s/projectFeatures/%s", s.ProjectID, feature.ID())
if err := s.restHelper.put(url, &requestBody, updatedProjectFeature, "projectFeature"); err != nil {
return nil, err
}

return s.parseProjectFeatureJSONResponse(*updatedProjectFeature)
}

func (s *ProjectFeatureService) parseProjectFeatureJSONResponse(feature projectFeatureJSON) (ProjectFeature, error) {
switch feature.Type {
case "versionedSettings":
return loadProjectFeatureVersionedSettings(s.ProjectID, feature)
default:
return nil, fmt.Errorf("Unknown project feature type %q", feature.Type)
}
}
Loading

0 comments on commit 9187e42

Please sign in to comment.