Skip to content

Commit

Permalink
Fix state and ref parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
sjparkinson authored Feb 18, 2024
1 parent 9b5864e commit aaabfc6
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 27 deletions.
53 changes: 28 additions & 25 deletions pkg/cmd/deployment/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,24 @@ type listOptions struct {
}

type deployment struct {
ID int
Ref *struct {
Name string
}
Environment string
LatestStatus *struct {
State string
CreatedAt time.Time
}
CreatedAt time.Time
ID int
Ref *string
Environment string
Status *string
CreatedAt time.Time
}

type listDeploymentsQuery struct {
type deploymentsQuery struct {
Repository struct {
Deployments struct {
Nodes []struct {
DatabaseID int
Ref *struct {
DatabaseID int
Environment string
Ref *struct {
Name string
}
Environment string
LatestStatus *struct {
State string
State githubv4.DeploymentStatusState
CreatedAt time.Time
}
CreatedAt time.Time
Expand Down Expand Up @@ -146,8 +141,8 @@ func listRun(opts *listOptions) error {
tp.AddField(deployment.Environment, tableprinter.WithColor(cs.Bold))

status := "none"
if deployment.LatestStatus != nil {
status = strings.ToLower(deployment.LatestStatus.State)
if deployment.Status != nil {
status = *deployment.Status
}
switch status {
case "error", "failure":
Expand All @@ -158,14 +153,13 @@ func listRun(opts *listOptions) error {
tp.AddField(status, tableprinter.WithColor(cs.Blue))
case "success":
tp.AddField(status, tableprinter.WithColor(cs.Green))
case "inactive":
default:
tp.AddField(status, tableprinter.WithColor(cs.Gray))
}

ref := "none"
if deployment.Ref != nil {
ref = deployment.Ref.Name
ref = *deployment.Ref
}
tp.AddField(ref)

Expand Down Expand Up @@ -198,20 +192,29 @@ func listDeployments(client *api.Client, repo ghrepo.Interface, limit int, envir

pagination:
for {
var result listDeploymentsQuery
var result deploymentsQuery
err := client.Query(repo.RepoHost(), "RepositoryDeployments", &result, variables)
if err != nil {
return nil, err
}

for _, node := range result.Repository.Deployments.Nodes {
deployment := deployment{
ID: node.DatabaseID,
Ref: node.Ref,
Environment: node.Environment,
LatestStatus: node.LatestStatus,
CreatedAt: node.CreatedAt,
ID: node.DatabaseID,
Environment: node.Environment,
CreatedAt: node.CreatedAt,
}

if node.Ref != nil {
ref := node.Ref.Name
deployment.Ref = &ref
}

if node.LatestStatus != nil {
state := strings.ToLower(string(node.LatestStatus.State))
deployment.Status = &state
}

deployments = append(deployments, deployment)

if len(deployments) == limit {
Expand Down
102 changes: 100 additions & 2 deletions pkg/cmd/deployment/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func TestListRun(t *testing.T) {
"databaseId": 1,
"environment": "production",
"latestStatus": {
"state": "in_progress",
"state": "IN_PROGRESS",
"createdAt": "2020-10-11T20:38:03Z"
},
"ref": {
Expand All @@ -204,7 +204,7 @@ func TestListRun(t *testing.T) {
"databaseId": 2,
"environment": "test",
"latestStatus": {
"state": "success",
"state": "SUCCESS",
"createdAt": "2020-10-11T20:38:03Z"
},
"ref": {
Expand All @@ -229,6 +229,104 @@ func TestListRun(t *testing.T) {
2 test success test 2020-10-11T20:38:03Z
`),
},
{
name: "no status deployments",
tty: false,
opts: &listOptions{},
httpStubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.GraphQL(`query RepositoryDeployments\b`),
httpmock.StringResponse(`
{
"data": {
"repository": {
"deployments": {
"nodes": [
{
"databaseId": 1,
"environment": "production",
"latestStatus": null,
"ref": {
"name": "main"
},
"createdAt": "2020-10-11T20:38:03Z"
},
{
"databaseId": 2,
"environment": "test",
"latestStatus": {
"state": "SUCCESS",
"createdAt": "2020-10-11T20:38:03Z"
},
"ref": {
"name": "test"
},
"createdAt": "2020-10-11T20:38:03Z"
}
],
"pageInfo": {
"hasNextPage": false,
"endCursor": "Y3Vyc29yOnYyOpK5MjAxOS0xMC0xMVQwMTozODowMyswODowMM5f3HZq"
}
}
}
}
}`,
),
)
},
wantStdout: heredoc.Doc(`
1 production none main 2020-10-11T20:38:03Z
2 test success test 2020-10-11T20:38:03Z
`),
},
{
name: "no ref deployments",
tty: false,
opts: &listOptions{},
httpStubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.GraphQL(`query RepositoryDeployments\b`),
httpmock.StringResponse(`
{
"data": {
"repository": {
"deployments": {
"nodes": [
{
"databaseId": 1,
"environment": "production",
"latestStatus": null,
"ref": null,
"createdAt": "2020-10-11T20:38:03Z"
},
{
"databaseId": 2,
"environment": "test",
"latestStatus": {
"state": "SUCCESS",
"createdAt": "2020-10-11T20:38:03Z"
},
"ref": null,
"createdAt": "2020-10-11T20:38:03Z"
}
],
"pageInfo": {
"hasNextPage": false,
"endCursor": "Y3Vyc29yOnYyOpK5MjAxOS0xMC0xMVQwMTozODowMyswODowMM5f3HZq"
}
}
}
}
}`,
),
)
},
wantStdout: heredoc.Doc(`
1 production none none 2020-10-11T20:38:03Z
2 test success none 2020-10-11T20:38:03Z
`),
},
}

for _, tt := range tests {
Expand Down

0 comments on commit aaabfc6

Please sign in to comment.