Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Update command argocd app history to support multiple sources #17530

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions cmd/argocd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2508,14 +2508,46 @@ 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)
_, _ = 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.Sources != nil {
for i, sourceInfo := range depInfo.Sources {
rev := sourceInfo.TargetRevision
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,
date: depInfo.DeployedAt.String(),
revision: rev,
})
}
} else {
rev := depInfo.Source.TargetRevision
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,
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()
}
Expand Down
84 changes: 83 additions & 1 deletion cmd/argocd/commands/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
}
Expand All @@ -578,7 +581,86 @@ 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also add a test where the application has both source and sources, but the history command only displays history of sources.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ishitasequeira , I have updated the PR with the changes you have suggested.

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)
Expand Down
Loading