1+ using System ;
2+ using System . Collections . Generic ;
3+ using Newtonsoft . Json ;
4+ using Octopus . Ocl . Converters ;
5+
6+ namespace Tests . RealLifeScenario . Entities
7+ {
8+ /// <summary>
9+ /// Copied a subset from server, but it still has Id for serialization purposes, even if it doesn't have the IId interface.
10+ /// Excluded convenience methods.
11+ /// </summary>
12+ /// <summary>
13+ /// Represents a reference from a deployment-process (specifically an action or action-template) to a package.
14+ /// May be named or un-named.
15+ /// </summary>
16+ /// <history>
17+ /// Prior to Octopus 2018.8, deployment actions could have at most a single package-reference. This was
18+ /// captured as properties on the action (Octopus.Action.Package.PackageId, Octopus.Action.Package.FeedId, etc).
19+ /// In 2018.8, we introduced support for actions with multiple packages (initially script steps and kubernetes step).
20+ /// Storing collections of nested objects in the property-bag gets very messy, so package-references were moved into
21+ /// their own class
22+ /// and collection on the deployment actions.
23+ /// </history>
24+ public class PackageReference
25+ {
26+ /// <summary>
27+ /// Constructs a named package-reference.
28+ /// </summary>
29+ /// <param name="name">The package-reference name.</param>
30+ /// <param name="packageId">The package ID or a variable-expression</param>
31+ /// <param name="feedIdOrName">The feed ID or a variable-expression</param>
32+ /// <param name="acquisitionLocation">The location the package should be acquired</param>
33+ public PackageReference ( string ? name , string packageId , FeedIdOrName feedIdOrName , PackageAcquisitionLocation acquisitionLocation )
34+ : this ( name , packageId , feedIdOrName , acquisitionLocation . ToString ( ) )
35+ {
36+ }
37+
38+ /// <summary>
39+ /// Constructs a named package-reference.
40+ /// </summary>
41+ /// <param name="name">The package-reference name.</param>
42+ /// <param name="packageId">The package ID or a variable-expression</param>
43+ /// <param name="feedIdOrName">The feed ID or a variable-expression</param>
44+ /// <param name="acquisitionLocation">The location the package should be acquired.
45+ /// May be one <see cref="PackageAcquisitionLocation" /> or a variable-expression.</param>
46+ public PackageReference ( string ? name , string packageId , FeedIdOrName feedIdOrName , string acquisitionLocation )
47+ : this (
48+ null ,
49+ name ,
50+ packageId ,
51+ feedIdOrName ,
52+ acquisitionLocation )
53+ {
54+ }
55+
56+ /// <summary>
57+ /// For JSON deserialization only
58+ /// </summary>
59+ [ JsonConstructor ]
60+ public PackageReference ( string ? id ,
61+ string ? name ,
62+ string packageId ,
63+ FeedIdOrName feedIdOrName ,
64+ string acquisitionLocation )
65+ : this ( )
66+ {
67+ if ( ! string . IsNullOrEmpty ( id ) ) Id = id ;
68+
69+ PackageId = packageId ;
70+ FeedIdOrName = feedIdOrName ;
71+ AcquisitionLocation = acquisitionLocation ;
72+ Name = name ?? string . Empty ;
73+ }
74+
75+ /// <summary>
76+ /// Constructs a primary package (an un-named package reference)
77+ /// </summary>
78+ public PackageReference ( string packageId , FeedIdOrName feedIdOrName , PackageAcquisitionLocation acquisitionLocation )
79+ : this ( null , packageId , feedIdOrName , acquisitionLocation )
80+ {
81+ }
82+
83+ /// <summary>
84+ /// Constructs a primary package (an un-named package reference)
85+ /// </summary>
86+ public PackageReference ( string packageId , FeedIdOrName feedIdOrName , string acquisitionLocation )
87+ : this ( null , packageId , feedIdOrName , acquisitionLocation )
88+ {
89+ }
90+
91+ /// <summary>
92+ /// Constructs a primary package (an un-named package reference)
93+ /// </summary>
94+ public PackageReference ( string packageId , FeedIdOrName feedIdOrName )
95+ : this ( packageId , feedIdOrName , PackageAcquisitionLocation . Server )
96+ {
97+ }
98+
99+ /// <summary>
100+ /// In the scenario where we are migrating from 2.6 instances, we set the ID of the package-reference
101+ /// to the same ID as the deployment-action.
102+ /// This was the least-bad option that let the migrator do its thing when migrating project
103+ /// release-creation-strategy and versioning-strategy.
104+ /// </summary>
105+ public PackageReference ( string id ) : this ( )
106+ {
107+ if ( ! string . IsNullOrEmpty ( id ) ) Id = id ;
108+ }
109+
110+ public PackageReference ( )
111+ {
112+ Id = Guid . NewGuid ( ) . ToString ( ) ;
113+ Properties = new Dictionary < string , string > ( ) ;
114+ FeedIdOrName = "feeds-builtin" . ToFeedIdOrName ( ) ;
115+ }
116+
117+ public string Id { get ; }
118+
119+ /// <summary>
120+ /// An name for the package-reference.
121+ /// This may be empty.
122+ /// This is used to discriminate the package-references. Package ID isn't suitable because an action may potentially
123+ /// have multiple references to the same package ID (e.g. if you wanted to use different versions of the same package).
124+ /// Also, the package ID may be a variable-expression.
125+ /// </summary>
126+ public string Name { get ; set ; } = string . Empty ;
127+
128+ /// <summary>
129+ /// Package ID or a variable-expression
130+ /// </summary>
131+ public string PackageId { get ; set ; } = string . Empty ;
132+
133+ /// <summary>
134+ /// Feed ID, name or a variable-expression
135+ /// </summary>
136+ [ JsonProperty ( "FeedId" ) ] // This is named FeedId for backward-compatibility as we don't yet want to change the underlying database JSON/schema.
137+ [ OclName ( "feed" ) ]
138+ public FeedIdOrName FeedIdOrName { get ; set ; }
139+
140+ /// <summary>
141+ /// The package-acquisition location.
142+ /// One of <see cref="PackageAcquisitionLocation" /> or a variable-expression
143+ /// </summary>
144+ public string AcquisitionLocation { get ; set ; } = string . Empty ;
145+
146+ /// <summary>
147+ /// Specific version to use for this package. If not specified, package can be
148+ /// selected at release creation or runbook run time.
149+ /// </summary>
150+ public string ? Version { get ; set ; }
151+
152+ /// <summary>
153+ /// This reference identifier is populated when a step package step contains a package reference
154+ /// It allows us to correlate the reference within the step package inputs to this Server package reference
155+ /// </summary>
156+ [ JsonProperty ( DefaultValueHandling = DefaultValueHandling . Ignore ) ]
157+ public string ? StepPackageInputsReferenceId { get ; set ; }
158+
159+ /// <summary>
160+ /// This should never be null for new objects but there are a small number of customers with
161+ /// older databases that may have it as null.
162+ /// </summary>
163+ [ JsonProperty ( ObjectCreationHandling = ObjectCreationHandling . Reuse ) ]
164+ public IDictionary < string , string > ? Properties { get ; private set ; }
165+
166+ [ JsonIgnore ]
167+ public bool IsPrimaryPackage => Name == "" ;
168+ }
169+ }
0 commit comments