Skip to content

Commit

Permalink
Merge pull request #6 from mozilla-services/add-metrics-cli-app
Browse files Browse the repository at this point in the history
Add metrics CLI app πŸš€
  • Loading branch information
hackebrot authored Nov 23, 2023
2 parents d1a789b + be95695 commit 5d7df0e
Show file tree
Hide file tree
Showing 39 changed files with 1,757 additions and 157 deletions.
63 changes: 63 additions & 0 deletions metrics/cmd/deployments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cmd

import (
"context"
"fmt"

"github.com/mozilla-services/rapid-release-model/metrics/internal/factory"
"github.com/mozilla-services/rapid-release-model/metrics/internal/github"
"github.com/spf13/cobra"
)

type DeploymentsOptions struct {
Limit int
Environments *[]string
}

func newDeploymentsCmd(f *factory.Factory) *cobra.Command {
opts := new(DeploymentsOptions)

cmd := &cobra.Command{
Use: "deployments",
Short: "Retrieve data about GitHub Deployments",
Long: "Retrieve data about GitHub Deployments",
PreRunE: func(cmd *cobra.Command, args []string) error {
if opts.Limit < 1 {
return fmt.Errorf("Limit cannot be smaller than 1.")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return runDeployments(cmd.Root().Context(), f, opts)
},
}
cmd.Flags().IntVarP(&opts.Limit, "limit", "l", 10, "limit for how many Deployments to fetch")

opts.Environments = cmd.Flags().StringArray("env", nil, "multiple use for Deployment environments")

return cmd
}

func runDeployments(ctx context.Context, f *factory.Factory, opts *DeploymentsOptions) error {
repo, err := f.NewGitHubRepo()
if err != nil {
return err
}

gqlClient, err := f.NewGitHubGraphQLClient()
if err != nil {
return err
}

deployments, err := github.QueryDeployments(gqlClient, repo, opts.Limit, opts.Environments)
if err != nil {
return err
}

exporter, err := f.NewExporter()
if err != nil {
return err
}

return exporter.Export(deployments)
}
72 changes: 72 additions & 0 deletions metrics/cmd/deployments_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package cmd

import (
"path/filepath"
"testing"

"github.com/mozilla-services/rapid-release-model/metrics/internal/config"
"github.com/mozilla-services/rapid-release-model/metrics/internal/github"
"github.com/mozilla-services/rapid-release-model/metrics/internal/test"
"github.com/shurcooL/githubv4"
)

func TestDeployments(t *testing.T) {
repo := &github.Repo{Owner: "hackebrot", Name: "turtle"}

env := map[string]string{
config.EnvKey("GITHUB", "REPO_OWNER"): "",
config.EnvKey("GITHUB", "REPO_NAME"): "",
}

tempDir := t.TempDir()

tests := []test.TestCase{{
Name: "deployments__repo_owner__required",
Args: []string{"github", "-n", repo.Name, "deployments"},
ErrContains: "Repo.Owner and Repo.Name are required. Set env vars or pass flags",
Env: env,
}, {
Name: "deployments__repo_name__required",
Args: []string{"github", "-o", repo.Owner, "deployments"},
ErrContains: "Repo.Owner and Repo.Name are required. Set env vars or pass flags",
Env: env,
}, {
Name: "deployments__default",
Args: []string{"github", "-o", repo.Owner, "-n", repo.Name, "deployments"},
WantFixture: test.NewFixture("deployments", "want__default.json"),
Env: env,
}, {
Name: "deployments__limit",
Args: []string{"github", "-o", repo.Owner, "-n", repo.Name, "deployments", "-l", "2"},
WantFixture: test.NewFixture("deployments", "want__limit.json"),
Env: env,
}, {
Name: "deployments__json",
Args: []string{"github", "-o", repo.Owner, "-n", repo.Name, "deployments", "-e", "json"},
WantFixture: test.NewFixture("deployments", "want__default.json"),
Env: env,
}, {
Name: "deployments__csv",
Args: []string{"github", "-o", repo.Owner, "-n", repo.Name, "deployments", "-e", "csv"},
WantFixture: test.NewFixture("deployments", "want__default.csv"),
Env: env,
}, {
Name: "deployments__filename",
Args: []string{"github", "-o", repo.Owner, "-n", repo.Name, "deployments", "-f", filepath.Join(tempDir, "r.json")},
WantFixture: test.NewFixture("deployments", "want__default.json"),
WantFile: filepath.Join(tempDir, "r.json"),
Env: env,
}, {
Name: "deployments__env__single",
Args: []string{"github", "-o", repo.Owner, "-n", repo.Name, "deployments", "--env", "prod"},
WantVariables: map[string]interface{}{"environments": []githubv4.String{githubv4.String("prod")}},
Env: env,
}, {
Name: "deployments__env__multiple",
Args: []string{"github", "-o", repo.Owner, "-n", repo.Name, "deployments", "--env", "prod", "--env", "hello"},
WantVariables: map[string]interface{}{"environments": []githubv4.String{githubv4.String("prod"), githubv4.String("hello")}},
Env: env,
}}

test.RunTests(t, newRootCmd, tests)
}
52 changes: 52 additions & 0 deletions metrics/cmd/fixtures/deployments/query.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"Repository": {
"Name": "turtle",
"Owner": {
"Login": "hackebrot"
},
"Deployments": {
"PageInfo": {
"HasNextPage": true,
"EndCursor": "abc123"
},
"Nodes": [
{
"Description": "Deployment03",
"CreatedAt": "2022-05-02T20:25:05Z",
"UpdatedAt": "2022-05-02T20:25:05Z",
"OriginalEnvironment": "prod",
"LatestEnvironment": "prod",
"Task": "deploy",
"State": "ACTIVE",
"Commit": {
"AbbreviatedOid": "1abc111"
}
},
{
"Description": "Deployment03",
"CreatedAt": "2022-05-01T20:20:05Z",
"UpdatedAt": "2022-05-01T20:20:05Z",
"OriginalEnvironment": "stage",
"LatestEnvironment": "stage",
"Task": "deploy",
"State": "ACTIVE",
"Commit": {
"AbbreviatedOid": "1abc111"
}
},
{
"Description": "Deployment02",
"CreatedAt": "2022-04-01T20:25:05Z",
"UpdatedAt": "2022-04-01T20:25:05Z",
"OriginalEnvironment": "stage",
"LatestEnvironment": "stage",
"Task": "deploy",
"State": "INACTIVE",
"Commit": {
"AbbreviatedOid": "2abc111"
}
}
]
}
}
}
28 changes: 28 additions & 0 deletions metrics/cmd/fixtures/deployments/query_abc123.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"Repository": {
"Name": "turtle",
"Owner": {
"Login": "hackebrot"
},
"Deployments": {
"PageInfo": {
"HasNextPage": false,
"EndCursor": "abc456"
},
"Nodes": [
{
"Description": "Deployment01",
"CreatedAt": "2022-02-01T20:25:05Z",
"UpdatedAt": "2022-02-01T20:25:05Z",
"OriginalEnvironment": "hello",
"LatestEnvironment": "hello",
"Task": "deploy",
"State": "ACTIVE",
"Commit": {
"AbbreviatedOid": "3abc111"
}
}
]
}
}
}
5 changes: 5 additions & 0 deletions metrics/cmd/fixtures/deployments/want__default.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
description,createdAt,updatedAt,originalEnvironment,latestEnvironment,task,state,commitOid
Deployment03,2022-05-02T20:25:05Z,2022-05-02T20:25:05Z,prod,prod,deploy,ACTIVE,1abc111
Deployment03,2022-05-01T20:20:05Z,2022-05-01T20:20:05Z,stage,stage,deploy,ACTIVE,1abc111
Deployment02,2022-04-01T20:25:05Z,2022-04-01T20:25:05Z,stage,stage,deploy,INACTIVE,2abc111
Deployment01,2022-02-01T20:25:05Z,2022-02-01T20:25:05Z,hello,hello,deploy,ACTIVE,3abc111
50 changes: 50 additions & 0 deletions metrics/cmd/fixtures/deployments/want__default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[
{
"Description": "Deployment03",
"CreatedAt": "2022-05-02T20:25:05Z",
"UpdatedAt": "2022-05-02T20:25:05Z",
"OriginalEnvironment": "prod",
"LatestEnvironment": "prod",
"Task": "deploy",
"State": "ACTIVE",
"Commit": {
"AbbreviatedOid": "1abc111"
}
},
{
"Description": "Deployment03",
"CreatedAt": "2022-05-01T20:20:05Z",
"UpdatedAt": "2022-05-01T20:20:05Z",
"OriginalEnvironment": "stage",
"LatestEnvironment": "stage",
"Task": "deploy",
"State": "ACTIVE",
"Commit": {
"AbbreviatedOid": "1abc111"
}
},
{
"Description": "Deployment02",
"CreatedAt": "2022-04-01T20:25:05Z",
"UpdatedAt": "2022-04-01T20:25:05Z",
"OriginalEnvironment": "stage",
"LatestEnvironment": "stage",
"Task": "deploy",
"State": "INACTIVE",
"Commit": {
"AbbreviatedOid": "2abc111"
}
},
{
"Description": "Deployment01",
"CreatedAt": "2022-02-01T20:25:05Z",
"UpdatedAt": "2022-02-01T20:25:05Z",
"OriginalEnvironment": "hello",
"LatestEnvironment": "hello",
"Task": "deploy",
"State": "ACTIVE",
"Commit": {
"AbbreviatedOid": "3abc111"
}
}
]
26 changes: 26 additions & 0 deletions metrics/cmd/fixtures/deployments/want__limit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"Description": "Deployment03",
"CreatedAt": "2022-05-02T20:25:05Z",
"UpdatedAt": "2022-05-02T20:25:05Z",
"OriginalEnvironment": "prod",
"LatestEnvironment": "prod",
"Task": "deploy",
"State": "ACTIVE",
"Commit": {
"AbbreviatedOid": "1abc111"
}
},
{
"Description": "Deployment03",
"CreatedAt": "2022-05-01T20:20:05Z",
"UpdatedAt": "2022-05-01T20:20:05Z",
"OriginalEnvironment": "stage",
"LatestEnvironment": "stage",
"Task": "deploy",
"State": "ACTIVE",
"Commit": {
"AbbreviatedOid": "1abc111"
}
}
]
43 changes: 43 additions & 0 deletions metrics/cmd/fixtures/prs/query.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"Repository": {
"Name": "turtle",
"Owner": {
"Login": "hackebrot"
},
"PullRequests": {
"PageInfo": {
"HasNextPage": true,
"EndCursor": "abc"
},
"Nodes": [
{
"ID": "PULLREQUEST1",
"Number": 1,
"Title": "Set up CI/CD workflow πŸ“¦",
"CreatedAt": "2023-09-08T16:33:20Z",
"UpdatedAt": "2023-09-10T07:24:20Z",
"ClosedAt": "2023-09-10T07:24:17Z",
"MergedAt": "2023-09-10T07:24:16Z"
},
{
"ID": "PULLREQUEST2",
"Number": 2,
"Title": "Refactor test framework πŸ€–",
"CreatedAt": "2023-09-08T09:18:42Z",
"UpdatedAt": "2023-09-08T09:40:23Z",
"ClosedAt": "2023-09-08T09:40:20Z",
"MergedAt": "2023-09-08T09:40:19Z"
},
{
"ID": "PULLREQUEST3",
"Number": 3,
"Title": "Fetch deployment metrics πŸš€",
"CreatedAt": "2023-10-08T08:09:38Z",
"UpdatedAt": "2023-10-08T08:58:13Z",
"ClosedAt": "2023-11-08T08:58:10Z",
"MergedAt": "2023-11-08T08:58:10Z"
}
]
}
}
}
25 changes: 25 additions & 0 deletions metrics/cmd/fixtures/prs/query_abc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"Repository": {
"Name": "turtle",
"Owner": {
"Login": "hackebrot"
},
"PullRequests": {
"PageInfo": {
"HasNextPage": false,
"EndCursor": "hello"
},
"Nodes": [
{
"ID": "PULLREQUEST4",
"Number": 4,
"Title": "Updating Docker image πŸ“¦",
"CreatedAt": "2023-12-08T16:33:20Z",
"UpdatedAt": "2023-12-10T07:24:20Z",
"ClosedAt": "2023-12-10T07:24:17Z",
"MergedAt": "2023-12-10T07:24:16Z"
}
]
}
}
}
5 changes: 5 additions & 0 deletions metrics/cmd/fixtures/prs/want__default.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
id,number,title,createdAt,updatedAt,closedAt,mergedAt
PULLREQUEST1,1,Set up CI/CD workflow πŸ“¦,2023-09-08T16:33:20Z,2023-09-10T07:24:20Z,2023-09-10T07:24:17Z,2023-09-10T07:24:16Z
PULLREQUEST2,2,Refactor test framework πŸ€–,2023-09-08T09:18:42Z,2023-09-08T09:40:23Z,2023-09-08T09:40:20Z,2023-09-08T09:40:19Z
PULLREQUEST3,3,Fetch deployment metrics πŸš€,2023-10-08T08:09:38Z,2023-10-08T08:58:13Z,2023-11-08T08:58:10Z,2023-11-08T08:58:10Z
PULLREQUEST4,4,Updating Docker image πŸ“¦,2023-12-08T16:33:20Z,2023-12-10T07:24:20Z,2023-12-10T07:24:17Z,2023-12-10T07:24:16Z
Loading

0 comments on commit 5d7df0e

Please sign in to comment.