diff --git a/commands/commands_test.go b/commands/commands_test.go index 22b9695..d2cf8e5 100644 --- a/commands/commands_test.go +++ b/commands/commands_test.go @@ -40,7 +40,7 @@ func (v SourcesMock) RetrieveSha1ForProject(branch string) (string, error) { return "vb0f586a", nil } -func (v SourcesMock) Diff(deployedSha1 string, sha1ToDeploy string) (*services.Changes, error) { +func (v SourcesMock) Diff(deployedSha1 string, sha1ToDeploy string, options services.DiffOptions) (*services.Changes, error) { return &services.Changes{}, nil } diff --git a/commands/deploy.go b/commands/deploy.go index c93e76f..c46998e 100644 --- a/commands/deploy.go +++ b/commands/deploy.go @@ -26,7 +26,7 @@ var ( func init() { deployCmd.PersistentFlags().StringVar(&branch, "branch", "", "Change the branch from the default one.") - deployCmd.PersistentFlags().BoolVar(&force, "force", false, "Force the deployement, even if already up to date") + deployCmd.PersistentFlags().BoolVar(&force, "force", false, "Force the deployment, even if already up to date") } var deployCmd = &cobra.Command{ diff --git a/commands/diff.go b/commands/diff.go index 2753afc..4cba1dd 100644 --- a/commands/diff.go +++ b/commands/diff.go @@ -2,16 +2,21 @@ package commands import ( "fmt" + "io" "os" "errors" "github.com/blablacar/contactkey/context" - "github.com/olekukonko/tablewriter" + "github.com/blablacar/contactkey/services" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) +var ( + includeMerges = false +) + var diffCmd = &cobra.Command{ Use: "diff", Short: "Diff between what's currently deployed and what's going to be deployed", @@ -19,16 +24,21 @@ var diffCmd = &cobra.Command{ func init() { diffCmd.PersistentFlags().StringVar(&branch, "branch", "", "Change the branch from the default one.") + diffCmd.PersistentFlags().BoolVar(&includeMerges, "include-merges", false, "Include merges in the changelog message") } type Diff struct { - Env string - Service string - Context *context.Context - TableWriter *tablewriter.Table + Env string + Service string + Context *context.Context + Writer io.Writer } func (d Diff) execute() error { + options := services.DiffOptions{ + IncludeMerges: includeMerges, + } + // If the branch is null it will use the default one. sha1, err := d.Context.Vcs.RetrieveSha1ForProject(branch) if err != nil { @@ -58,17 +68,15 @@ func (d Diff) execute() error { } for _, uniqueVersion := range uniqueVersions { - changes, err := d.Context.Vcs.Diff(uniqueVersion, sha1) + changes, err := d.Context.Vcs.Diff(uniqueVersion, sha1, options) if err != nil { return errors.New(fmt.Sprintf("Failed to retrieve sha1: %q \n", err)) } log.Println(fmt.Sprintf("Diff between %q(deployed) and %q(branch) \n", uniqueVersion, sha1)) - d.TableWriter.SetHeader([]string{"Author", "sha1", "description"}) for _, change := range changes.Commits { - d.TableWriter.Append([]string{change.AuthorFullName, change.DisplayId, change.Title}) + fmt.Fprintf(d.Writer, "- %s (%s)\n", change.Title, change.DisplayId) } - d.TableWriter.Render() } return nil } @@ -77,5 +85,5 @@ func (d *Diff) fill(context *context.Context, service string, env string) { d.Env = env d.Service = service d.Context = context - d.TableWriter = tablewriter.NewWriter(os.Stdout) + d.Writer = os.Stdout } diff --git a/commands/diff_test.go b/commands/diff_test.go index 4236d61..5d8202a 100644 --- a/commands/diff_test.go +++ b/commands/diff_test.go @@ -8,7 +8,6 @@ import ( "github.com/blablacar/contactkey/context" "github.com/blablacar/contactkey/services" - "github.com/olekukonko/tablewriter" log "github.com/sirupsen/logrus" ) @@ -18,7 +17,7 @@ func (v *VcsMock) RetrieveSha1ForProject(branch string) (string, error) { return "abcde", nil } -func (v *VcsMock) Diff(deployedSha1 string, sha1ToDeploy string) (*services.Changes, error) { +func (v *VcsMock) Diff(deployedSha1 string, sha1ToDeploy string, options services.DiffOptions) (*services.Changes, error) { changes := new(services.Changes) commits := services.Commits{} commits.Title = "Title" @@ -45,9 +44,10 @@ func TestDiffExecute(t *testing.T) { }, } - cmd.TableWriter = tablewriter.NewWriter(out) + cmd.Writer = out cmd.execute() + if out.String() == "" { t.Errorf("Unexpected stdout : %q", out) } @@ -57,10 +57,6 @@ func TestDiffExecute(t *testing.T) { t.Error("Diff not found") } - if !strings.Contains(out.String(), "AuthorFullName") { - t.Error("AuthorFullName not found") - } - if !strings.Contains(out.String(), "DisplayId") { t.Error("DisplayId not found") } diff --git a/services/data/diff.json b/services/data/diff.json index 395832a..0fd1be3 100644 --- a/services/data/diff.json +++ b/services/data/diff.json @@ -20,12 +20,8 @@ } }, "authorTimestamp": 1500299005000, - "message": "Merge pull request #138 in repository/project from branch 1to master\n\n* commit '40acf891cee4bb64fe16c213e97333d83cb5f682':\n Some comments", + "message": "Some comments 1", "parents": [ - { - "id": "1c48ab0e9c1cc39c2bb41a1a90173376e267e26c", - "displayId": "1c48ab0e9c1" - }, { "id": "40acf891cee4bb64fe16c213e97333d83cb5f682", "displayId": "40acf891cee" @@ -56,7 +52,7 @@ } }, "authorTimestamp": 1491894222000, - "message": "Some comments", + "message": "Some comments 2", "parents": [ { "id": "1c48ab0e9c1cc39c2bb41a1a90173376e267e26c", @@ -84,12 +80,8 @@ } }, "authorTimestamp": 1499698695000, - "message": "Merge pull request #152 in repository/project from branch2 to master\n\n* commit 'd436e3c2b0385afc38bf6fb9b29567ef9b9f226b':\n Some comments 2", + "message": "Some comments 3", "parents": [ - { - "id": "81d5079fa77ac1e45853732b2c9374b2ac14af40", - "displayId": "81d5079fa77" - }, { "id": "d436e3c2b0385afc38bf6fb9b29567ef9b9f226b", "displayId": "d436e3c2b03" @@ -116,7 +108,7 @@ } }, "authorTimestamp": 1499698396000, - "message": "Some comments 2", + "message": "Some comments 4", "parents": [ { "id": "059ee06dc7d3cf305d6d69c3502cada0f2d4d6f8", diff --git a/services/sources.go b/services/sources.go index 3cd2520..2c41e5e 100644 --- a/services/sources.go +++ b/services/sources.go @@ -15,7 +15,11 @@ import ( type Sources interface { RetrieveSha1ForProject(branch string) (string, error) - Diff(deployedSha1 string, sha1ToDeploy string) (*Changes, error) + Diff(deployedSha1 string, sha1ToDeploy string, options DiffOptions) (*Changes, error) +} + +type DiffOptions struct { + IncludeMerges bool } type Stash struct { @@ -113,8 +117,14 @@ func (s Stash) RetrieveSha1ForProject(branch string) (string, error) { return stashResponse.Values[0].Id, nil } -func (s Stash) Diff(deployedSha1 string, sha1ToDeploy string) (*Changes, error) { +func (s Stash) Diff(deployedSha1 string, sha1ToDeploy string, options DiffOptions) (*Changes, error) { + merges := "include" + if !options.IncludeMerges { + merges = "exclude" + } + params := url.Values{} + params.Add("merges", merges) params.Add("since", deployedSha1) params.Add("until", sha1ToDeploy) stashResponse, err := s.getStashResponse(params) diff --git a/services/sources_test.go b/services/sources_test.go index 7486c6f..61465ac 100644 --- a/services/sources_test.go +++ b/services/sources_test.go @@ -20,7 +20,7 @@ func setupTestCase(t *testing.T) func(t *testing.T) { w.WriteHeader(http.StatusOK) w.Write(raw) - case "/rest/api/latest/projects/project/repos/repository/commits?since=b04ad09883d1858081702b8e2d80eb348ead9849&until=b0d5ca3e586d48cc6d3ad35f0e03dfc891e62752": + case "/rest/api/latest/projects/project/repos/repository/commits?merges=exclude&since=b04ad09883d1858081702b8e2d80eb348ead9849&until=b0d5ca3e586d48cc6d3ad35f0e03dfc891e62752": raw, err := ioutil.ReadFile("./data/diff.json") if err != nil { t.Error(err) @@ -75,7 +75,12 @@ func TestDiff(t *testing.T) { 7, } - changes, err := stash.Diff("b04ad09883d1858081702b8e2d80eb348ead9849", "b0d5ca3e586d48cc6d3ad35f0e03dfc891e62752") + changes, err := stash.Diff( + "b04ad09883d1858081702b8e2d80eb348ead9849", + "b0d5ca3e586d48cc6d3ad35f0e03dfc891e62752", + DiffOptions{IncludeMerges: false}, + ) + if err != nil { t.Errorf("Error shouldn't have been raised instead got %s", err.Error()) } @@ -94,7 +99,7 @@ func TestDiff(t *testing.T) { if changes.Commits[0].AuthorSlug != "slug" { t.Errorf("Error got %s", changes.Commits[0].AuthorSlug) } - if changes.Commits[0].Title != "Merge pull request #138 in repository/project from branch 1to master\n\n* commit '40acf891cee4bb64fe16c213e97333d83cb5f682':\n Some comments" { + if changes.Commits[0].Title != "Some comments 1" { t.Errorf("Error got %s", changes.Commits[0].Title) } @@ -108,7 +113,7 @@ func TestDiff(t *testing.T) { t.Errorf("Error got %s", changes.Commits[1].AuthorSlug) } - if changes.Commits[2].Title != "Merge pull request #152 in repository/project from branch2 to master\n\n* commit 'd436e3c2b0385afc38bf6fb9b29567ef9b9f226b':\n Some comments 2" { + if changes.Commits[2].Title != "Some comments 3" { t.Errorf("Error got %s", changes.Commits[1].Title) } }