Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #90 from blablacar/feat-simplify-changelog
Browse files Browse the repository at this point in the history
Simplify changelog
  • Loading branch information
nahk authored Sep 14, 2018
2 parents c4b2897 + f137055 commit 57aa2e6
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 37 deletions.
2 changes: 1 addition & 1 deletion commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion commands/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
28 changes: 18 additions & 10 deletions commands/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,43 @@ 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",
}

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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
10 changes: 3 additions & 7 deletions commands/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/blablacar/contactkey/context"
"github.com/blablacar/contactkey/services"
"github.com/olekukonko/tablewriter"
log "github.com/sirupsen/logrus"
)

Expand All @@ -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"
Expand All @@ -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)
}
Expand All @@ -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")
}
Expand Down
16 changes: 4 additions & 12 deletions services/data/diff.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -56,7 +52,7 @@
}
},
"authorTimestamp": 1491894222000,
"message": "Some comments",
"message": "Some comments 2",
"parents": [
{
"id": "1c48ab0e9c1cc39c2bb41a1a90173376e267e26c",
Expand Down Expand Up @@ -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"
Expand All @@ -116,7 +108,7 @@
}
},
"authorTimestamp": 1499698396000,
"message": "Some comments 2",
"message": "Some comments 4",
"parents": [
{
"id": "059ee06dc7d3cf305d6d69c3502cada0f2d4d6f8",
Expand Down
14 changes: 12 additions & 2 deletions services/sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 9 additions & 4 deletions services/sources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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())
}
Expand All @@ -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)
}

Expand All @@ -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)
}
}
Expand Down

0 comments on commit 57aa2e6

Please sign in to comment.