Skip to content
Open
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
123 changes: 88 additions & 35 deletions commitserver/apiclient/commit.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 2 additions & 24 deletions commitserver/commit/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,6 @@ type hydratorMetadataFile struct {
References []v1alpha1.RevisionReference `json:"references,omitempty"`
}

// TODO: make this configurable via ConfigMap.
var manifestHydrationReadmeTemplate = `# Manifest Hydration

To hydrate the manifests in this repository, run the following commands:

` + "```shell" + `
git clone {{ .RepoURL }}
# cd into the cloned directory
git checkout {{ .DrySHA }}
{{ range $command := .Commands -}}
{{ $command }}
{{ end -}}` + "```" + `
{{ if .References -}}

## References

{{ range $ref := .References -}}
{{ if $ref.Commit -}}
* [{{ $ref.Commit.SHA | mustRegexFind "[0-9a-f]+" | trunc 7 }}]({{ $ref.Commit.RepoURL }}): {{ $ref.Commit.Subject }} ({{ $ref.Commit.Author }})
{{ end -}}
{{ end -}}
{{ end -}}`

// CommitHydratedManifests handles a commit request. It clones the repository, checks out the sync branch, checks out
// the target branch, clears the repository contents, writes the manifests to the repository, commits the changes, and
// pushes the changes. It returns the hydrated revision SHA and an error if one occurred.
Expand Down Expand Up @@ -120,6 +97,7 @@ func (s *Service) handleCommitRequest(logCtx *log.Entry, r *apiclient.CommitHydr
if r.Repo == nil {
return "", "", errors.New("repo is required")
}

if r.Repo.Repo == "" {
return "", "", errors.New("repo URL is required")
}
Expand Down Expand Up @@ -179,7 +157,7 @@ func (s *Service) handleCommitRequest(logCtx *log.Entry, r *apiclient.CommitHydr
}

logCtx.Debug("Writing manifests")
err = WriteForPaths(root, r.Repo.Repo, r.DrySha, r.DryCommitMetadata, r.Paths)
err = WriteForPaths(root, r.Repo.Repo, r.DrySha, r.DryCommitMetadata, r.Paths, r.ReadmeMessage)
if err != nil {
return "", "", fmt.Errorf("failed to write manifests: %w", err)
}
Expand Down
2 changes: 2 additions & 0 deletions commitserver/commit/commit.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ message CommitHydratedManifestsRequest {
repeated PathDetails paths = 6;
// DryCommitMetadata contains metadata about the DRY commit, such as the author and committer.
github.com.argoproj.argo_cd.v3.pkg.apis.application.v1alpha1.RevisionMetadata dryCommitMetadata = 7;
// ReadmeMessage is the message content for README template updates.
string readmeMessage = 8;
}

// PathDetails holds information about hydrated manifests to be written to a particular path in the hydrated manifests
Expand Down
8 changes: 4 additions & 4 deletions commitserver/commit/hydratorhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func init() {

// WriteForPaths writes the manifests, hydrator.metadata, and README.md files for each path in the provided paths. It
// also writes a root-level hydrator.metadata file containing the repo URL and dry SHA.
func WriteForPaths(root *os.Root, repoUrl, drySha string, dryCommitMetadata *appv1.RevisionMetadata, paths []*apiclient.PathDetails) error { //nolint:revive //FIXME(var-naming)
func WriteForPaths(root *os.Root, repoUrl, drySha string, dryCommitMetadata *appv1.RevisionMetadata, paths []*apiclient.PathDetails, rawReadmeTemplate string) error { //nolint:revive //FIXME(var-naming)
hydratorMetadata, err := hydrator.GetCommitMetadata(repoUrl, drySha, dryCommitMetadata)
if err != nil {
return fmt.Errorf("failed to retrieve hydrator metadata: %w", err)
Expand Down Expand Up @@ -83,7 +83,7 @@ func WriteForPaths(root *os.Root, repoUrl, drySha string, dryCommitMetadata *app
}

// Write README
err = writeReadme(root, hydratePath, hydratorMetadata)
err = writeReadme(root, hydratePath, hydratorMetadata, rawReadmeTemplate)
if err != nil {
return fmt.Errorf("failed to write readme: %w", err)
}
Expand Down Expand Up @@ -111,8 +111,8 @@ func writeMetadata(root *os.Root, dirPath string, metadata hydrator.HydratorComm
}

// writeReadme writes the readme to the README.md file.
func writeReadme(root *os.Root, dirPath string, metadata hydrator.HydratorCommitMetadata) error {
readmeTemplate, err := template.New("readme").Funcs(sprigFuncMap).Parse(manifestHydrationReadmeTemplate)
func writeReadme(root *os.Root, dirPath string, metadata hydrator.HydratorCommitMetadata, rawReadmeTemplate string) error {
readmeTemplate, err := template.New("readme").Funcs(sprigFuncMap).Parse(rawReadmeTemplate)
if err != nil {
return fmt.Errorf("failed to parse readme template: %w", err)
}
Expand Down
5 changes: 3 additions & 2 deletions commitserver/commit/hydratorhelper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/argoproj/argo-cd/v3/commitserver/apiclient"
appsv1 "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
"github.com/argoproj/argo-cd/v3/util/hydrator"
"github.com/argoproj/argo-cd/v3/util/settings"
)

// tempRoot creates a temporary directory and returns an os.Root object for it.
Expand Down Expand Up @@ -93,7 +94,7 @@ Argocd-reference-commit-sha: abc123
},
}

err := WriteForPaths(root, repoURL, drySha, metadata, paths)
err := WriteForPaths(root, repoURL, drySha, metadata, paths, settings.ManifestHydrationReadmeTemplate)
require.NoError(t, err)

// Check if the top-level hydrator.metadata exists and contains the repo URL and dry SHA
Expand Down Expand Up @@ -188,7 +189,7 @@ func TestWriteReadme(t *testing.T) {
},
}

err = writeReadme(root, "", metadata)
err = writeReadme(root, "", metadata, settings.ManifestHydrationReadmeTemplate)
require.NoError(t, err)

readmePath := filepath.Join(root.Name(), "README.md")
Expand Down
10 changes: 10 additions & 0 deletions controller/hydrator/hydrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ type Dependencies interface {

// GetHydratorCommitMessageTemplate gets the configured template for rendering commit messages.
GetHydratorCommitMessageTemplate() (string, error)

// GetHydratorReadmeMessageTemplate gets the configured template for rendering README messages.
GetHydratorReadmeMessageTemplate() (string, error)
}

// Hydrator is the main struct that implements the hydration logic. It uses the Dependencies interface to access the
Expand Down Expand Up @@ -425,6 +428,12 @@ func (h *Hydrator) hydrate(logCtx *log.Entry, apps []*appv1.Application, project
return targetRevision, "", errors, fmt.Errorf("failed to get hydrator commit templated message: %w", errMsg)
}

// get the readme message template
readmeTemplate, err := h.dependencies.GetHydratorReadmeMessageTemplate()
if err != nil {
return "", "", errors, fmt.Errorf("failed to get hydrated readme message template: %w", err)
}

manifestsRequest := commitclient.CommitHydratedManifestsRequest{
Repo: repo,
SyncBranch: syncBranch,
Expand All @@ -433,6 +442,7 @@ func (h *Hydrator) hydrate(logCtx *log.Entry, apps []*appv1.Application, project
CommitMessage: commitMessage,
Paths: paths,
DryCommitMetadata: revisionMetadata,
ReadmeMessage: readmeTemplate,
}

closer, commitService, err := h.commitClientset.NewCommitServerClient()
Expand Down
3 changes: 3 additions & 0 deletions controller/hydrator/hydrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,7 @@ func TestProcessHydrationQueueItem_SuccessfulHydration(t *testing.T) {
rc.On("GetRevisionMetadata", mock.Anything, mock.Anything).Return(nil, nil).Once()
d.On("GetWriteCredentials", mock.Anything, "https://example.com/repo", "test-project").Return(nil, nil).Once()
d.On("GetHydratorCommitMessageTemplate").Return("commit message", nil).Once()
d.On("GetHydratorReadmeMessageTemplate").Return("readme message", nil).Once()
cc.On("CommitHydratedManifests", mock.Anything, mock.Anything).Return(&commitclient.CommitHydratedManifestsResponse{HydratedSha: "def456"}, nil).Once()

h.ProcessHydrationQueueItem(hydrationKey)
Expand Down Expand Up @@ -801,6 +802,7 @@ func TestHydrator_hydrate_Success(t *testing.T) {
})
d.On("GetWriteCredentials", mock.Anything, readRepo.Repo, proj.Name).Return(writeRepo, nil)
d.On("GetHydratorCommitMessageTemplate").Return("commit message", nil)
d.On("GetHydratorReadmeMessageTemplate").Return("readme message", nil)
cc.On("CommitHydratedManifests", mock.Anything, mock.Anything).Return(&commitclient.CommitHydratedManifestsResponse{HydratedSha: "hydrated123"}, nil).Run(func(args mock.Arguments) {
r := args.Get(1).(*commitclient.CommitHydratedManifestsRequest)
assert.Equal(t, "commit message", r.CommitMessage)
Expand Down Expand Up @@ -1009,6 +1011,7 @@ func TestHydrator_hydrate_CommitHydratedManifestsError(t *testing.T) {
rc.On("GetRevisionMetadata", mock.Anything, mock.Anything).Return(&v1alpha1.RevisionMetadata{}, nil)
d.On("GetWriteCredentials", mock.Anything, mock.Anything, mock.Anything).Return(&v1alpha1.Repository{Repo: "https://example.com/repo"}, nil)
d.On("GetHydratorCommitMessageTemplate").Return("commit message", nil)
d.On("GetHydratorReadmeMessageTemplate").Return("readme message", nil)
cc.On("CommitHydratedManifests", mock.Anything, mock.Anything).Return(nil, errors.New("commit error"))
logCtx := log.NewEntry(log.StandardLogger())

Expand Down
53 changes: 53 additions & 0 deletions controller/hydrator/mocks/Dependencies.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading