Skip to content

Commit

Permalink
showcommit command: add showcommit command and it's tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nam committed Nov 7, 2019
1 parent c02427f commit ac2ff53
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 114 deletions.
28 changes: 28 additions & 0 deletions apiserver/facades/client/modelgeneration/mocks/package_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 21 additions & 8 deletions apiserver/facades/client/modelgeneration/modelgeneration.go
Expand Up @@ -290,7 +290,8 @@ func (api *API) BranchInfo(
}

// ShowCommit will return details a commit given by its generationId
// An error is returned if no commit can be found corresponding to a branch.
// An error is returned if either no branch can be found corresponding to the generation id.
// Or the generation id given is below 1.
func (api *APIV3) ShowCommit(arg params.GenerationId) (params.GenerationCommitResult, error) {
result := params.GenerationCommitResult{}

Expand All @@ -312,13 +313,12 @@ func (api *APIV3) ShowCommit(arg params.GenerationId) (params.GenerationCommitRe
return result, nil
}

//TODO: convert them
_, err = api.oneBranchInfo(branch, true)
generationCommit, err := api.getGenerationCommit(branch)
if err != nil {
return generationCommitResultError(err)
}

//result.GenerationCommit. = generation.Applications
result.GenerationCommit = generationCommit

return result, nil
}
Expand Down Expand Up @@ -403,6 +403,23 @@ func (api *API) oneBranchInfo(branch Generation, detailed bool) (params.Generati
}, nil
}

func (api *APIV3) getGenerationCommit(branch Generation) (params.GenerationCommit, error) {

generation, err := api.oneBranchInfo(branch, true)
if err != nil {
return params.GenerationCommit{}, errors.Trace(err)
}
return params.GenerationCommit{
BranchName: branch.BranchName(),
Completed: branch.Completed(),
CompletedBy: branch.CompletedBy(),
GenerationId: branch.GenerationId(),
Created: branch.Created(),
CreatedBy: branch.CreatedBy(),
Applications: generation.Applications,
}, nil
}

// HasActiveBranch returns a true result if the input model has an "in-flight"
// branch matching the input name.
func (api *API) HasActiveBranch(arg params.BranchArg) (params.BoolResult, error) {
Expand All @@ -427,10 +444,6 @@ func (api *API) HasActiveBranch(arg params.BranchArg) (params.BoolResult, error)
return result, nil
}

func convertGenerationsToGenerationsCommit(generation Generation) params.GenerationCommit {
return params.GenerationCommit{}
}

func generationResultsError(err error) (params.GenerationResults, error) {
return params.GenerationResults{Error: common.ServerError(err)}, nil
}
Expand Down
103 changes: 2 additions & 101 deletions apiserver/facades/schema.json
Expand Up @@ -23096,25 +23096,6 @@
}
}
},
"ListCommits": {
"type": "object",
"properties": {
"Result": {
"$ref": "#/definitions/GenerationCommitResults"
}
}
},
"ShowCommit": {
"type": "object",
"properties": {
"Params": {
"$ref": "#/definitions/GenerationId"
},
"Result": {
"$ref": "#/definitions/GenerationCommitResult"
}
}
},
"TrackBranch": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -23280,7 +23261,8 @@
"required": [
"branch",
"created",
"created-by"
"created-by",
"applications"
]
},
"GenerationApplication": {
Expand Down Expand Up @@ -23321,87 +23303,6 @@
"config"
]
},
"GenerationCommit": {
"type": "object",
"properties": {
"applications": {
"type": "array",
"items": {
"$ref": "#/definitions/GenerationApplication"
}
},
"branch": {
"type": "string"
},
"completed": {
"type": "integer"
},
"completed-by": {
"type": "string"
},
"created": {
"type": "integer"
},
"created-by": {
"type": "string"
},
"generation-id": {
"type": "integer"
}
},
"additionalProperties": false,
"required": [
"branch",
"completed",
"completed-by",
"generation-id"
]
},
"GenerationCommitResult": {
"type": "object",
"properties": {
"error": {
"$ref": "#/definitions/Error"
},
"generation-commit": {
"$ref": "#/definitions/GenerationCommit"
}
},
"additionalProperties": false,
"required": [
"generation-commit"
]
},
"GenerationCommitResults": {
"type": "object",
"properties": {
"error": {
"$ref": "#/definitions/Error"
},
"generation-commit": {
"type": "array",
"items": {
"$ref": "#/definitions/GenerationCommit"
}
}
},
"additionalProperties": false,
"required": [
"generation-commit"
]
},
"GenerationId": {
"type": "object",
"properties": {
"generation-id": {
"type": "integer"
}
},
"additionalProperties": false,
"required": [
"generation-id"
]
},
"GenerationResults": {
"type": "object",
"properties": {
Expand Down
1 change: 1 addition & 0 deletions cmd/juju/commands/main.go
Expand Up @@ -373,6 +373,7 @@ func registerCommands(r commandRegistry, ctx *cmd.Context) {
r.Register(model.NewDiffCommand())
r.Register(model.NewAbortCommand())
r.Register(model.NewCommitsCommand())
r.Register(model.NewShowCommitCommand())
}

r.Register(newMigrateCommand())
Expand Down
8 changes: 8 additions & 0 deletions cmd/juju/model/export_test.go
Expand Up @@ -236,3 +236,11 @@ func NewListCommitsCommandForTest(api CommitsCommandAPI, store jujuclient.Client
cmd.SetClientStore(store)
return modelcmd.Wrap(cmd)
}

func NewShowCommitCommandForTest(api ShowCommitCommandAPI, store jujuclient.ClientStore) cmd.Command {
cmd := &ShowCommitCommand{
api: api,
}
cmd.SetClientStore(store)
return modelcmd.Wrap(cmd)
}
32 changes: 27 additions & 5 deletions cmd/juju/model/showcommit.go
Expand Up @@ -65,10 +65,7 @@ type ShowCommitCommand struct {
type ShowCommitCommandAPI interface {
Close() error

// ListCommitsBranch commits the branch with the input name to the model,
// effectively completing it and applying
// all branch changes across the model.
// The new generation ID of the model is returned.
// ShowCommit shows the branches which were committed
ShowCommit(func(time.Time) string) (model.GenerationCommit, error)
}

Expand Down Expand Up @@ -148,5 +145,30 @@ func (c *ShowCommitCommand) Run(ctx *cmd.Context) error {
if err != nil {
return err
}
return c.out.Write(ctx, cmt)
return errors.Trace(c.out.Write(ctx, c.getFormattedOutput(cmt)))
}

// Run implements the meaty part of the cmd.Command interface.
func (c *ShowCommitCommand) getFormattedOutput(gcm model.GenerationCommit) formattedShowCommit {
applications := map[string]formattedShowCommitApplications{gcm.BranchName: {gcm.Applications}}
commit := formattedShowCommit{
Branch: applications,
CommittedAt: gcm.Completed,
CommittedBy: gcm.CompletedBy,
Created: gcm.Created,
CreatedBy: gcm.CreatedBy,
}
return commit
}

type formattedShowCommit struct {
Branch map[string]formattedShowCommitApplications `json:"branch" yaml:"branch"`
CommittedAt string `json:"committed-at" yaml:"committed-at"`
CommittedBy string `json:"committed-by" yaml:"committed-by"`
Created string `json:"created" yaml:"created"`
CreatedBy string `json:"created-by" yaml:"created-by"`
}

type formattedShowCommitApplications struct {
Applications []model.GenerationApplication `json:"applications" yaml:"applications"`
}

0 comments on commit ac2ff53

Please sign in to comment.