github.com/cobalt77/jfrog-client-go@v0.14.5/artifactory/buildinfo/buildinfo.go (about)

     1  package buildinfo
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  func New() *BuildInfo {
     8  	return &BuildInfo{
     9  		Agent:      &Agent{},
    10  		BuildAgent: &Agent{Name: "GENERIC"},
    11  		Modules:    make([]Module, 0),
    12  		Vcs:        &Vcs{},
    13  	}
    14  }
    15  
    16  func (targetBuildInfo *BuildInfo) SetBuildAgentVersion(buildAgentVersion string) {
    17  	targetBuildInfo.BuildAgent.Version = buildAgentVersion
    18  }
    19  
    20  func (targetBuildInfo *BuildInfo) SetAgentName(agentName string) {
    21  	targetBuildInfo.Agent.Name = agentName
    22  }
    23  
    24  func (targetBuildInfo *BuildInfo) SetAgentVersion(agentVersion string) {
    25  	targetBuildInfo.Agent.Version = agentVersion
    26  }
    27  
    28  func (targetBuildInfo *BuildInfo) SetArtifactoryPluginVersion(artifactoryPluginVersion string) {
    29  	targetBuildInfo.ArtifactoryPluginVersion = artifactoryPluginVersion
    30  }
    31  
    32  // Append the modules of the received build info to this build info.
    33  // If the two build info instances contain modules with identical names, these modules are merged.
    34  // When merging the modules, the artifacts and dependencies remain unique according to their checksum.
    35  func (targetBuildInfo *BuildInfo) Append(buildInfo *BuildInfo) {
    36  	for _, newModule := range buildInfo.Modules {
    37  		exists := false
    38  		for i, _ := range targetBuildInfo.Modules {
    39  			if newModule.Id == targetBuildInfo.Modules[i].Id {
    40  				mergeModules(&newModule, &targetBuildInfo.Modules[i])
    41  				exists = true
    42  				break
    43  			}
    44  		}
    45  		if !exists {
    46  			targetBuildInfo.Modules = append(targetBuildInfo.Modules, newModule)
    47  		}
    48  	}
    49  }
    50  
    51  // Merge the first module into the second module.
    52  func mergeModules(merge *Module, into *Module) {
    53  	mergeArtifacts(&merge.Artifacts, &into.Artifacts)
    54  	mergeDependencies(&merge.Dependencies, &into.Dependencies)
    55  }
    56  
    57  func mergeArtifacts(mergeArtifacts *[]Artifact, intoArtifacts *[]Artifact) {
    58  	for _, mergeArtifact := range *mergeArtifacts {
    59  		exists := false
    60  		for _, artifact := range *intoArtifacts {
    61  			if mergeArtifact.Sha1 == artifact.Sha1 {
    62  				exists = true
    63  				break
    64  			}
    65  		}
    66  		if !exists {
    67  			*intoArtifacts = append(*intoArtifacts, mergeArtifact)
    68  		}
    69  	}
    70  }
    71  
    72  func mergeDependencies(mergeDependencies *[]Dependency, intoDependencies *[]Dependency) {
    73  	for _, mergeDependency := range *mergeDependencies {
    74  		exists := false
    75  		for _, dependency := range *intoDependencies {
    76  			if mergeDependency.Sha1 == dependency.Sha1 {
    77  				exists = true
    78  				break
    79  			}
    80  		}
    81  		if !exists {
    82  			*intoDependencies = append(*intoDependencies, mergeDependency)
    83  		}
    84  	}
    85  }
    86  
    87  type BuildInfo struct {
    88  	Name                     string   `json:"name,omitempty"`
    89  	Number                   string   `json:"number,omitempty"`
    90  	Agent                    *Agent   `json:"agent,omitempty"`
    91  	BuildAgent               *Agent   `json:"buildAgent,omitempty"`
    92  	Modules                  []Module `json:"modules,omitempty"`
    93  	Started                  string   `json:"started,omitempty"`
    94  	Properties               Env      `json:"properties,omitempty"`
    95  	ArtifactoryPrincipal     string   `json:"artifactoryPrincipal,omitempty"`
    96  	BuildUrl                 string   `json:"url,omitempty"`
    97  	Issues                   *Issues  `json:"issues,omitempty"`
    98  	ArtifactoryPluginVersion string   `json:"artifactoryPluginVersion,omitempty"`
    99  	*Vcs
   100  }
   101  
   102  type Agent struct {
   103  	Name    string `json:"name,omitempty"`
   104  	Version string `json:"version,omitempty"`
   105  }
   106  
   107  type Module struct {
   108  	Properties   interface{}  `json:"properties,omitempty"`
   109  	Id           string       `json:"id,omitempty"`
   110  	Artifacts    []Artifact   `json:"artifacts,omitempty"`
   111  	Dependencies []Dependency `json:"dependencies,omitempty"`
   112  }
   113  
   114  type Artifact struct {
   115  	Name string `json:"name,omitempty"`
   116  	Type string `json:"type,omitempty"`
   117  	Path string `json:"path,omitempty"`
   118  	*Checksum
   119  }
   120  
   121  type Dependency struct {
   122  	Id     string   `json:"id,omitempty"`
   123  	Type   string   `json:"type,omitempty"`
   124  	Scopes []string `json:"scopes,omitempty"`
   125  	*Checksum
   126  }
   127  
   128  type Issues struct {
   129  	Tracker                *Tracker        `json:"tracker,omitempty"`
   130  	AggregateBuildIssues   bool            `json:"aggregateBuildIssues,omitempty"`
   131  	AggregationBuildStatus string          `json:"aggregationBuildStatus,omitempty"`
   132  	AffectedIssues         []AffectedIssue `json:"affectedIssues,omitempty"`
   133  }
   134  
   135  type Tracker struct {
   136  	Name    string `json:"name,omitempty"`
   137  	Version string `json:"version,omitempty"`
   138  }
   139  
   140  type AffectedIssue struct {
   141  	Key        string `json:"key,omitempty"`
   142  	Url        string `json:"url,omitempty"`
   143  	Summary    string `json:"summary,omitempty"`
   144  	Aggregated bool   `json:"aggregated,omitempty"`
   145  }
   146  
   147  type Checksum struct {
   148  	Sha1 string `json:"sha1,omitempty"`
   149  	Md5  string `json:"md5,omitempty"`
   150  }
   151  
   152  type Env map[string]string
   153  
   154  type Vcs struct {
   155  	Url      string `json:"vcsUrl,omitempty"`
   156  	Revision string `json:"vcsRevision,omitempty"`
   157  }
   158  
   159  type Partials []*Partial
   160  
   161  type Partial struct {
   162  	Artifacts    []Artifact   `json:"Artifacts,omitempty"`
   163  	Dependencies []Dependency `json:"Dependencies,omitempty"`
   164  	Env          Env          `json:"Env,omitempty"`
   165  	Timestamp    int64        `json:"Timestamp,omitempty"`
   166  	ModuleId     string       `json:"ModuleId,omitempty"`
   167  	Issues       *Issues      `json:"Issues,omitempty"`
   168  	*Vcs
   169  }
   170  
   171  func (partials Partials) Len() int {
   172  	return len(partials)
   173  }
   174  
   175  func (partials Partials) Less(i, j int) bool {
   176  	return partials[i].Timestamp < partials[j].Timestamp
   177  }
   178  
   179  func (partials Partials) Swap(i, j int) {
   180  	partials[i], partials[j] = partials[j], partials[i]
   181  }
   182  
   183  type General struct {
   184  	Timestamp time.Time `json:"Timestamp,omitempty"`
   185  }