github.com/argoproj/argo-cd/v3@v3.2.1/util/hydrator/hydrator.go (about)

     1  package hydrator
     2  
     3  import (
     4  	"strings"
     5  	"time"
     6  
     7  	log "github.com/sirupsen/logrus"
     8  
     9  	appv1 "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
    10  	"github.com/argoproj/argo-cd/v3/util/git"
    11  )
    12  
    13  // HydratorCommitMetadata defines the struct used by both Controller and commitServer
    14  // to define the templated commit message and the hydrated manifest
    15  type HydratorCommitMetadata struct {
    16  	RepoURL  string   `json:"repoURL,omitempty"`
    17  	DrySHA   string   `json:"drySha,omitempty"`
    18  	Commands []string `json:"commands,omitempty"`
    19  	Author   string   `json:"author,omitempty"`
    20  	Date     string   `json:"date,omitempty"`
    21  	// Subject is the subject line of the DRY commit message, i.e. `git show --format=%s`.
    22  	Subject string `json:"subject,omitempty"`
    23  	// Body is the body of the DRY commit message, excluding the subject line, i.e. `git show --format=%b`.
    24  	// Known Argocd- trailers with valid values are removed, but all other trailers are kept.
    25  	Body       string                    `json:"body,omitempty"`
    26  	References []appv1.RevisionReference `json:"references,omitempty"`
    27  }
    28  
    29  // GetCommitMetadata takes repo, drySha and commitMetadata and returns a HydratorCommitMetadata which is a
    30  // common contract controller and commitServer
    31  func GetCommitMetadata(repoUrl, drySha string, dryCommitMetadata *appv1.RevisionMetadata) (HydratorCommitMetadata, error) { //nolint:revive //FIXME(var-naming)
    32  	author := ""
    33  	message := ""
    34  	date := ""
    35  	var references []appv1.RevisionReference
    36  	if dryCommitMetadata != nil {
    37  		author = dryCommitMetadata.Author
    38  		message = dryCommitMetadata.Message
    39  		if dryCommitMetadata.Date != nil {
    40  			date = dryCommitMetadata.Date.Format(time.RFC3339)
    41  		}
    42  		references = dryCommitMetadata.References
    43  	}
    44  
    45  	subject, body, _ := strings.Cut(message, "\n\n")
    46  
    47  	_, bodyMinusTrailers := git.GetReferences(log.WithFields(log.Fields{"repo": repoUrl, "revision": drySha}), body)
    48  
    49  	hydratorCommitMetadata := HydratorCommitMetadata{
    50  		RepoURL:    repoUrl,
    51  		DrySHA:     drySha,
    52  		Author:     author,
    53  		Subject:    subject,
    54  		Body:       bodyMinusTrailers,
    55  		Date:       date,
    56  		References: references,
    57  	}
    58  
    59  	return hydratorCommitMetadata, nil
    60  }