From 87c2ebd429bd9652dca7912955889ab89516ffda Mon Sep 17 00:00:00 2001 From: Mangaal Date: Thu, 14 Mar 2024 20:23:22 +0530 Subject: [PATCH 1/5] update argocd app history command to print app history group by thier sources along with all the REVISIONS Signed-off-by: Mangaal --- cmd/argocd/commands/app.go | 41 ++++++++++++++++--- cmd/argocd/commands/app_test.go | 72 ++++++++++++++++++++++++++++++++- 2 files changed, 107 insertions(+), 6 deletions(-) diff --git a/cmd/argocd/commands/app.go b/cmd/argocd/commands/app.go index 25c02db5f291d..5d8e2d75d171c 100644 --- a/cmd/argocd/commands/app.go +++ b/cmd/argocd/commands/app.go @@ -2463,13 +2463,44 @@ func printApplicationHistoryIds(revHistory []argoappv1.RevisionHistory) { // Print a history table for an application. func printApplicationHistoryTable(revHistory []argoappv1.RevisionHistory) { w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) - _, _ = fmt.Fprintf(w, "ID\tDATE\tREVISION\n") + type history struct { + id int64 + date string + revision string + } + varHistory := map[string][]history{} for _, depInfo := range revHistory { - rev := depInfo.Source.TargetRevision - if len(depInfo.Revision) >= 7 { - rev = fmt.Sprintf("%s (%s)", rev, depInfo.Revision[0:7]) + if depInfo.Source.RepoURL != "" { + rev := depInfo.Source.TargetRevision + if len(depInfo.Revision) >= 7 { + rev = fmt.Sprintf("%s (%s)", rev, depInfo.Revision[0:7]) + } + varHistory[depInfo.Source.RepoURL] = append(varHistory[depInfo.Source.RepoURL], history{ + id: depInfo.ID, + date: depInfo.DeployedAt.String(), + revision: rev, + }) + } + if depInfo.Sources != nil { + for i, sourceInfo := range depInfo.Sources { + rev := sourceInfo.TargetRevision + if len(depInfo.Revisions) == len(depInfo.Sources) && len(depInfo.Revisions[i]) >= 7 { + rev = fmt.Sprintf("%s (%s)", rev, depInfo.Revisions[i][0:7]) + } + varHistory[sourceInfo.RepoURL] = append(varHistory[sourceInfo.RepoURL], history{ + id: depInfo.ID, + date: depInfo.DeployedAt.String(), + revision: rev, + }) + } + } + } + for source, historyEntries := range varHistory { + _, _ = fmt.Fprintf(w, "\nSOURCE\t%s\n", source) + _, _ = fmt.Fprintf(w, "ID\tDATE\tREVISION\n") + for _, history := range historyEntries { + _, _ = fmt.Fprintf(w, "%d\t%s\t%s\n", history.id, history.date, history.revision) } - _, _ = fmt.Fprintf(w, "%d\t%s\t%s\n", depInfo.ID, depInfo.DeployedAt, rev) } _ = w.Flush() } diff --git a/cmd/argocd/commands/app_test.go b/cmd/argocd/commands/app_test.go index 8079185dc569d..ab7bf2754992e 100644 --- a/cmd/argocd/commands/app_test.go +++ b/cmd/argocd/commands/app_test.go @@ -557,18 +557,21 @@ func TestPrintApplicationHistoryTable(t *testing.T) { ID: 1, Source: v1alpha1.ApplicationSource{ TargetRevision: "1", + RepoURL: "test", }, }, { ID: 2, Source: v1alpha1.ApplicationSource{ TargetRevision: "2", + RepoURL: "test", }, }, { ID: 3, Source: v1alpha1.ApplicationSource{ TargetRevision: "3", + RepoURL: "test", }, }, } @@ -578,7 +581,74 @@ func TestPrintApplicationHistoryTable(t *testing.T) { return nil }) - expectation := "ID DATE REVISION\n1 0001-01-01 00:00:00 +0000 UTC 1\n2 0001-01-01 00:00:00 +0000 UTC 2\n3 0001-01-01 00:00:00 +0000 UTC 3\n" + expectation := "\nSOURCE test\nID DATE REVISION\n1 0001-01-01 00:00:00 +0000 UTC 1\n2 0001-01-01 00:00:00 +0000 UTC 2\n3 0001-01-01 00:00:00 +0000 UTC 3\n" + + if output != expectation { + t.Fatalf("Incorrect print operation output %q, should be %q", output, expectation) + } +} + +func TestPrintApplicationHistoryTableWithMultipleSources(t *testing.T) { + histories := []v1alpha1.RevisionHistory{ + { + ID: 1, + Revisions: []string{ + "1a", + "1b", + }, + Sources: v1alpha1.ApplicationSources{ + v1alpha1.ApplicationSource{ + RepoURL: "test-1", + TargetRevision: "1a", + }, + v1alpha1.ApplicationSource{ + RepoURL: "test-2", + TargetRevision: "1b", + }, + }, + }, + { + ID: 2, + Revisions: []string{ + "2a", + "2b", + }, + Sources: v1alpha1.ApplicationSources{ + v1alpha1.ApplicationSource{ + RepoURL: "test-1", + TargetRevision: "2a", + }, + v1alpha1.ApplicationSource{ + RepoURL: "test-2", + TargetRevision: "2b", + }, + }, + }, + { + ID: 3, + Revisions: []string{ + "3a", + "3b", + }, + Sources: v1alpha1.ApplicationSources{ + v1alpha1.ApplicationSource{ + RepoURL: "test-1", + TargetRevision: "3a", + }, + v1alpha1.ApplicationSource{ + RepoURL: "test-2", + TargetRevision: "3b", + }, + }, + }, + } + + output, _ := captureOutput(func() error { + printApplicationHistoryTable(histories) + return nil + }) + + expectation := "\nSOURCE test-1\nID DATE REVISION\n1 0001-01-01 00:00:00 +0000 UTC 1a\n2 0001-01-01 00:00:00 +0000 UTC 2a\n3 0001-01-01 00:00:00 +0000 UTC 3a\n\nSOURCE test-2\nID DATE REVISION\n1 0001-01-01 00:00:00 +0000 UTC 1b\n2 0001-01-01 00:00:00 +0000 UTC 2b\n3 0001-01-01 00:00:00 +0000 UTC 3b\n" if output != expectation { t.Fatalf("Incorrect print operation output %q, should be %q", output, expectation) From 5ffcb7399e552a4185d86d0c81509a4129abc33f Mon Sep 17 00:00:00 2001 From: Mangaal Date: Fri, 15 Mar 2024 09:40:28 +0530 Subject: [PATCH 2/5] upadte unit test to ahve both Source and Sources and update function to overlooked source if sources is persent Signed-off-by: Mangaal --- cmd/argocd/commands/app.go | 22 ++++----- cmd/argocd/commands/app_test.go | 80 +++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 11 deletions(-) diff --git a/cmd/argocd/commands/app.go b/cmd/argocd/commands/app.go index 5d8e2d75d171c..36f833ed5b00d 100644 --- a/cmd/argocd/commands/app.go +++ b/cmd/argocd/commands/app.go @@ -2470,17 +2470,7 @@ func printApplicationHistoryTable(revHistory []argoappv1.RevisionHistory) { } varHistory := map[string][]history{} for _, depInfo := range revHistory { - if depInfo.Source.RepoURL != "" { - rev := depInfo.Source.TargetRevision - if len(depInfo.Revision) >= 7 { - rev = fmt.Sprintf("%s (%s)", rev, depInfo.Revision[0:7]) - } - varHistory[depInfo.Source.RepoURL] = append(varHistory[depInfo.Source.RepoURL], history{ - id: depInfo.ID, - date: depInfo.DeployedAt.String(), - revision: rev, - }) - } + if depInfo.Sources != nil { for i, sourceInfo := range depInfo.Sources { rev := sourceInfo.TargetRevision @@ -2493,6 +2483,16 @@ func printApplicationHistoryTable(revHistory []argoappv1.RevisionHistory) { revision: rev, }) } + } else { + rev := depInfo.Source.TargetRevision + if len(depInfo.Revision) >= 7 { + rev = fmt.Sprintf("%s (%s)", rev, depInfo.Revision[0:7]) + } + varHistory[depInfo.Source.RepoURL] = append(varHistory[depInfo.Source.RepoURL], history{ + id: depInfo.ID, + date: depInfo.DeployedAt.String(), + revision: rev, + }) } } for source, historyEntries := range varHistory { diff --git a/cmd/argocd/commands/app_test.go b/cmd/argocd/commands/app_test.go index ab7bf2754992e..248f7acf185fb 100644 --- a/cmd/argocd/commands/app_test.go +++ b/cmd/argocd/commands/app_test.go @@ -591,6 +591,86 @@ func TestPrintApplicationHistoryTable(t *testing.T) { func TestPrintApplicationHistoryTableWithMultipleSources(t *testing.T) { histories := []v1alpha1.RevisionHistory{ { + ID: 0, + Source: v1alpha1.ApplicationSource{ + TargetRevision: "0", + RepoURL: "test", + }, + }, + { + ID: 1, + Revisions: []string{ + "1a", + "1b", + }, + //added Source just for testing the fuction + Source: v1alpha1.ApplicationSource{ + TargetRevision: "-1", + RepoURL: "ignore", + }, + Sources: v1alpha1.ApplicationSources{ + v1alpha1.ApplicationSource{ + RepoURL: "test-1", + TargetRevision: "1a", + }, + v1alpha1.ApplicationSource{ + RepoURL: "test-2", + TargetRevision: "1b", + }, + }, + }, + { + ID: 2, + Revisions: []string{ + "2a", + "2b", + }, + Sources: v1alpha1.ApplicationSources{ + v1alpha1.ApplicationSource{ + RepoURL: "test-1", + TargetRevision: "2a", + }, + v1alpha1.ApplicationSource{ + RepoURL: "test-2", + TargetRevision: "2b", + }, + }, + }, + { + ID: 3, + Revisions: []string{ + "3a", + "3b", + }, + Sources: v1alpha1.ApplicationSources{ + v1alpha1.ApplicationSource{ + RepoURL: "test-1", + TargetRevision: "3a", + }, + v1alpha1.ApplicationSource{ + RepoURL: "test-2", + TargetRevision: "3b", + }, + }, + }, + } + + output, _ := captureOutput(func() error { + printApplicationHistoryTable(histories) + return nil + }) + + expectation := "\nSOURCE test\nID DATE REVISION\n0 0001-01-01 00:00:00 +0000 UTC 0\n\nSOURCE test-1\nID DATE REVISION\n1 0001-01-01 00:00:00 +0000 UTC 1a\n2 0001-01-01 00:00:00 +0000 UTC 2a\n3 0001-01-01 00:00:00 +0000 UTC 3a\n\nSOURCE test-2\nID DATE REVISION\n1 0001-01-01 00:00:00 +0000 UTC 1b\n2 0001-01-01 00:00:00 +0000 UTC 2b\n3 0001-01-01 00:00:00 +0000 UTC 3b\n" + + if output != expectation { + t.Fatalf("Incorrect print operation output %q, should be %q", output, expectation) + } +} + +func TestPrintApplicationHistoryTableForWhenBothSourcesAndSourceFiledsExist(t *testing.T) { + histories := []v1alpha1.RevisionHistory{ + { + ID: 1, Revisions: []string{ "1a", From f9e33af6e8ed0afe15c64154a83d28277ab88008 Mon Sep 17 00:00:00 2001 From: Mangaal Date: Fri, 15 Mar 2024 09:43:12 +0530 Subject: [PATCH 3/5] remove magic no 7 and introduc a variable MAX_ALLOWED_REVISIONS Signed-off-by: Mangaal --- cmd/argocd/commands/app.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cmd/argocd/commands/app.go b/cmd/argocd/commands/app.go index 36f833ed5b00d..4188e9d713c9d 100644 --- a/cmd/argocd/commands/app.go +++ b/cmd/argocd/commands/app.go @@ -2462,6 +2462,7 @@ func printApplicationHistoryIds(revHistory []argoappv1.RevisionHistory) { // Print a history table for an application. func printApplicationHistoryTable(revHistory []argoappv1.RevisionHistory) { + MAX_ALLOWED_REVISIONS := 7 w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) type history struct { id int64 @@ -2474,8 +2475,8 @@ func printApplicationHistoryTable(revHistory []argoappv1.RevisionHistory) { if depInfo.Sources != nil { for i, sourceInfo := range depInfo.Sources { rev := sourceInfo.TargetRevision - if len(depInfo.Revisions) == len(depInfo.Sources) && len(depInfo.Revisions[i]) >= 7 { - rev = fmt.Sprintf("%s (%s)", rev, depInfo.Revisions[i][0:7]) + if len(depInfo.Revisions) == len(depInfo.Sources) && len(depInfo.Revisions[i]) >= MAX_ALLOWED_REVISIONS { + rev = fmt.Sprintf("%s (%s)", rev, depInfo.Revisions[i][0:MAX_ALLOWED_REVISIONS]) } varHistory[sourceInfo.RepoURL] = append(varHistory[sourceInfo.RepoURL], history{ id: depInfo.ID, @@ -2485,8 +2486,8 @@ func printApplicationHistoryTable(revHistory []argoappv1.RevisionHistory) { } } else { rev := depInfo.Source.TargetRevision - if len(depInfo.Revision) >= 7 { - rev = fmt.Sprintf("%s (%s)", rev, depInfo.Revision[0:7]) + if len(depInfo.Revision) >= MAX_ALLOWED_REVISIONS { + rev = fmt.Sprintf("%s (%s)", rev, depInfo.Revision[0:MAX_ALLOWED_REVISIONS]) } varHistory[depInfo.Source.RepoURL] = append(varHistory[depInfo.Source.RepoURL], history{ id: depInfo.ID, From eb58e0eabfb53cb63c46611a74424323a970a245 Mon Sep 17 00:00:00 2001 From: Mangaal Date: Sun, 24 Mar 2024 11:03:39 +0530 Subject: [PATCH 4/5] remove extra unit test Signed-off-by: Mangaal --- cmd/argocd/commands/app_test.go | 79 --------------------------------- 1 file changed, 79 deletions(-) diff --git a/cmd/argocd/commands/app_test.go b/cmd/argocd/commands/app_test.go index 248f7acf185fb..742e56a458289 100644 --- a/cmd/argocd/commands/app_test.go +++ b/cmd/argocd/commands/app_test.go @@ -588,85 +588,6 @@ func TestPrintApplicationHistoryTable(t *testing.T) { } } -func TestPrintApplicationHistoryTableWithMultipleSources(t *testing.T) { - histories := []v1alpha1.RevisionHistory{ - { - ID: 0, - Source: v1alpha1.ApplicationSource{ - TargetRevision: "0", - RepoURL: "test", - }, - }, - { - ID: 1, - Revisions: []string{ - "1a", - "1b", - }, - //added Source just for testing the fuction - Source: v1alpha1.ApplicationSource{ - TargetRevision: "-1", - RepoURL: "ignore", - }, - Sources: v1alpha1.ApplicationSources{ - v1alpha1.ApplicationSource{ - RepoURL: "test-1", - TargetRevision: "1a", - }, - v1alpha1.ApplicationSource{ - RepoURL: "test-2", - TargetRevision: "1b", - }, - }, - }, - { - ID: 2, - Revisions: []string{ - "2a", - "2b", - }, - Sources: v1alpha1.ApplicationSources{ - v1alpha1.ApplicationSource{ - RepoURL: "test-1", - TargetRevision: "2a", - }, - v1alpha1.ApplicationSource{ - RepoURL: "test-2", - TargetRevision: "2b", - }, - }, - }, - { - ID: 3, - Revisions: []string{ - "3a", - "3b", - }, - Sources: v1alpha1.ApplicationSources{ - v1alpha1.ApplicationSource{ - RepoURL: "test-1", - TargetRevision: "3a", - }, - v1alpha1.ApplicationSource{ - RepoURL: "test-2", - TargetRevision: "3b", - }, - }, - }, - } - - output, _ := captureOutput(func() error { - printApplicationHistoryTable(histories) - return nil - }) - - expectation := "\nSOURCE test\nID DATE REVISION\n0 0001-01-01 00:00:00 +0000 UTC 0\n\nSOURCE test-1\nID DATE REVISION\n1 0001-01-01 00:00:00 +0000 UTC 1a\n2 0001-01-01 00:00:00 +0000 UTC 2a\n3 0001-01-01 00:00:00 +0000 UTC 3a\n\nSOURCE test-2\nID DATE REVISION\n1 0001-01-01 00:00:00 +0000 UTC 1b\n2 0001-01-01 00:00:00 +0000 UTC 2b\n3 0001-01-01 00:00:00 +0000 UTC 3b\n" - - if output != expectation { - t.Fatalf("Incorrect print operation output %q, should be %q", output, expectation) - } -} - func TestPrintApplicationHistoryTableForWhenBothSourcesAndSourceFiledsExist(t *testing.T) { histories := []v1alpha1.RevisionHistory{ { From 20bf81a4df3850a07fe1fcc639782bf4a9cd24c6 Mon Sep 17 00:00:00 2001 From: Mangaal Date: Sun, 24 Mar 2024 11:07:50 +0530 Subject: [PATCH 5/5] remove extra unit test TestPrintApplicationHistoryTableForWhenBothSourcesAndSourceFiledsExist() Signed-off-by: Mangaal --- cmd/argocd/commands/app_test.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/cmd/argocd/commands/app_test.go b/cmd/argocd/commands/app_test.go index 742e56a458289..23094e0ead02e 100644 --- a/cmd/argocd/commands/app_test.go +++ b/cmd/argocd/commands/app_test.go @@ -588,15 +588,26 @@ func TestPrintApplicationHistoryTable(t *testing.T) { } } -func TestPrintApplicationHistoryTableForWhenBothSourcesAndSourceFiledsExist(t *testing.T) { +func TestPrintApplicationHistoryTableWithMultipleSources(t *testing.T) { histories := []v1alpha1.RevisionHistory{ { - + ID: 0, + Source: v1alpha1.ApplicationSource{ + TargetRevision: "0", + RepoURL: "test", + }, + }, + { ID: 1, Revisions: []string{ "1a", "1b", }, + //added Source just for testing the fuction + Source: v1alpha1.ApplicationSource{ + TargetRevision: "-1", + RepoURL: "ignore", + }, Sources: v1alpha1.ApplicationSources{ v1alpha1.ApplicationSource{ RepoURL: "test-1", @@ -649,7 +660,7 @@ func TestPrintApplicationHistoryTableForWhenBothSourcesAndSourceFiledsExist(t *t return nil }) - expectation := "\nSOURCE test-1\nID DATE REVISION\n1 0001-01-01 00:00:00 +0000 UTC 1a\n2 0001-01-01 00:00:00 +0000 UTC 2a\n3 0001-01-01 00:00:00 +0000 UTC 3a\n\nSOURCE test-2\nID DATE REVISION\n1 0001-01-01 00:00:00 +0000 UTC 1b\n2 0001-01-01 00:00:00 +0000 UTC 2b\n3 0001-01-01 00:00:00 +0000 UTC 3b\n" + expectation := "\nSOURCE test\nID DATE REVISION\n0 0001-01-01 00:00:00 +0000 UTC 0\n\nSOURCE test-1\nID DATE REVISION\n1 0001-01-01 00:00:00 +0000 UTC 1a\n2 0001-01-01 00:00:00 +0000 UTC 2a\n3 0001-01-01 00:00:00 +0000 UTC 3a\n\nSOURCE test-2\nID DATE REVISION\n1 0001-01-01 00:00:00 +0000 UTC 1b\n2 0001-01-01 00:00:00 +0000 UTC 2b\n3 0001-01-01 00:00:00 +0000 UTC 3b\n" if output != expectation { t.Fatalf("Incorrect print operation output %q, should be %q", output, expectation)