3535import io .harness .exception .ngexception .beans .templateservice .TemplateInputsErrorMetadataDTO ;
3636import io .harness .gitaware .dto .FetchRemoteEntityRequest ;
3737import io .harness .gitaware .dto .GetFileGitContextRequestParams ;
38+ import io .harness .gitaware .helper .GitAwareContextHelper ;
3839import io .harness .gitaware .helper .GitAwareEntityHelper ;
3940import io .harness .gitsync .beans .StoreType ;
4041import io .harness .logging .AutoLogContext ;
@@ -101,9 +102,10 @@ public TemplateEntityGetResponse getLinkedTemplateEntity(String accountId, Strin
101102 new NgAutoLogContextForMethod (projectId , orgId , accountId , "getLinkedTemplateEntity" , OVERRIDE_NESTS )) {
102103 log .info ("[TemplateService] Fetching Template {} from project {}, org {}, account {}" ,
103104 templateUniqueIdentifier .getTemplateIdentifier (), projectId , orgId , accountId );
104- TemplateEntity template = getLinkedTemplateEntityHelper (accountId , orgId , projectId ,
105- templateUniqueIdentifier .getTemplateIdentifier (), templateUniqueIdentifier .getVersionLabel (),
106- templateCacheMap , templateUniqueIdentifier .getVersionMaker (), loadFromCache );
105+ TemplateEntity template =
106+ getLinkedTemplateEntityHelper (accountId , orgId , projectId , templateUniqueIdentifier .getTemplateIdentifier (),
107+ templateUniqueIdentifier .getVersionLabel (), templateCacheMap , templateUniqueIdentifier .getVersionMaker (),
108+ loadFromCache , templateUniqueIdentifier .getGitBranch ());
107109 return new TemplateEntityGetResponse (template , NGTemplateDtoMapper .getEntityGitDetails (template ));
108110 } finally {
109111 log .debug ("[TemplateService] Finished fetching Template {} from project {}, org {}, account {} took {}ms " ,
@@ -123,17 +125,20 @@ public TemplateEntity getLinkedTemplateEntity(String accountId, String orgId, St
123125 return getLinkedTemplateEntityHelper (
124126 accountId , orgId , projectId , identifier , versionLabel , templateCacheMap , versionMarker , false );
125127 }
128+
126129 public TemplateEntity getLinkedTemplateEntityHelper (String accountId , String orgId , String projectId ,
127130 String identifier , String versionLabel , Map <String , TemplateEntity > templateCacheMap , String versionMarker ,
128- boolean loadFromCache ) {
129- IdentifierRef templateIdentifierRef = TemplateUtils .getIdentifierRef (accountId , orgId , projectId , identifier );
131+ boolean loadFromCache , String branch ) {
132+ IdentifierRef templateIdentifierRef =
133+ TemplateUtils .getIdentifierRef (accountId , orgId , projectId , identifier , branch );
130134 String templateUniqueIdentifier = generateUniqueTemplateIdentifier (templateIdentifierRef .getAccountIdentifier (),
131135 templateIdentifierRef .getOrgIdentifier (), templateIdentifierRef .getProjectIdentifier (),
132- templateIdentifierRef .getIdentifier (), versionMarker );
136+ templateIdentifierRef .getIdentifier (), versionMarker , branch );
133137 if (templateCacheMap .containsKey (templateUniqueIdentifier )) {
134138 return templateCacheMap .get (templateUniqueIdentifier );
135139 }
136140
141+ GitAwareContextHelper .updateGitEntityContextWithBranch (branch );
137142 Optional <TemplateEntity > templateEntity =
138143 templateServiceHelper .getTemplateOrThrowExceptionIfInvalid (templateIdentifierRef .getAccountIdentifier (),
139144 templateIdentifierRef .getOrgIdentifier (), templateIdentifierRef .getProjectIdentifier (),
@@ -148,14 +153,21 @@ public TemplateEntity getLinkedTemplateEntityHelper(String accountId, String org
148153 return template ;
149154 }
150155
156+ public TemplateEntity getLinkedTemplateEntityHelper (String accountId , String orgId , String projectId ,
157+ String identifier , String versionLabel , Map <String , TemplateEntity > templateCacheMap , String versionMarker ,
158+ boolean loadFromCache ) {
159+ return getLinkedTemplateEntityHelper (
160+ accountId , orgId , projectId , identifier , versionLabel , templateCacheMap , versionMarker , loadFromCache , null );
161+ }
162+
151163 // Checks if the current Json node is a Template node with fieldName as TEMPLATE and Non-null Value
152164 public boolean isTemplatePresent (String fieldName , JsonNode templateValue ) {
153165 return TEMPLATE .equals (fieldName ) && templateValue .isObject () && templateValue .get (TEMPLATE_REF ) != null ;
154166 }
155167
156168 // Generates a unique Template Identifier
157169 private String generateUniqueTemplateIdentifier (
158- String accountId , String orgId , String projectId , String templateIdentifier , String versionLabel ) {
170+ String accountId , String orgId , String projectId , String templateIdentifier , String versionLabel , String branch ) {
159171 List <String > fqnList = new LinkedList <>();
160172 fqnList .add (accountId );
161173 if (EmptyPredicate .isNotEmpty (orgId )) {
@@ -166,23 +178,8 @@ private String generateUniqueTemplateIdentifier(
166178 }
167179 fqnList .add (templateIdentifier );
168180 fqnList .add (versionLabel );
169-
170- return EntityReferenceHelper .createFQN (fqnList );
171- }
172-
173- private String generateUniqueTemplateIdentifier (IdentifierRef templateIdentifierRef , String versionLabel ) {
174- List <String > fqnList = new LinkedList <>();
175- fqnList .add (templateIdentifierRef .getAccountIdentifier ());
176- if (EmptyPredicate .isNotEmpty (templateIdentifierRef .getOrgIdentifier ())) {
177- fqnList .add (templateIdentifierRef .getOrgIdentifier ());
178- }
179- if (EmptyPredicate .isNotEmpty (templateIdentifierRef .getProjectIdentifier ())) {
180- fqnList .add (templateIdentifierRef .getProjectIdentifier ());
181- }
182- fqnList .add (templateIdentifierRef .getIdentifier ());
183- fqnList .add (versionLabel );
184- if (isNotEmpty (templateIdentifierRef .getBranch ())) {
185- fqnList .add (templateIdentifierRef .getBranch ());
181+ if (isNotEmpty (branch )) {
182+ fqnList .add (branch );
186183 }
187184
188185 return EntityReferenceHelper .createFQN (fqnList );
@@ -346,11 +343,12 @@ String getTemplateUniqueIdentifier(
346343 TemplateUniqueIdentifier templateUniqueIdentification = parseYamlAndGetTemplateIdentifierAndVersion (value );
347344
348345 IdentifierRef templateIdentifierRef =
349- TemplateUtils .getGitBranchAwareIdentifierRef (accountIdentifier , orgIdentifier , projectIdentifier ,
346+ TemplateUtils .getIdentifierRef (accountIdentifier , orgIdentifier , projectIdentifier ,
350347 templateUniqueIdentification .getTemplateIdentifier (), templateUniqueIdentification .getGitBranch ());
351348 String templateUniqueIdentifier = generateUniqueTemplateIdentifier (templateIdentifierRef .getAccountIdentifier (),
352349 templateIdentifierRef .getOrgIdentifier (), templateIdentifierRef .getProjectIdentifier (),
353- templateIdentifierRef .getIdentifier (), templateUniqueIdentification .getVersionMaker ());
350+ templateIdentifierRef .getIdentifier (), templateUniqueIdentification .getVersionMaker (),
351+ templateIdentifierRef .getBranch ());
354352 log .info ("Unique template identifier: {}" , templateUniqueIdentifier );
355353 return templateUniqueIdentifier ;
356354 }
@@ -362,9 +360,8 @@ Map<String, GetTemplateEntityRequest> prepareBatchGetTemplatesRequest(String acc
362360 for (Map .Entry <String , YamlNode > entry : templatesToGet .entrySet ()) {
363361 JsonNode yaml = entry .getValue ().getCurrJsonNode ();
364362 TemplateUniqueIdentifier templateUniqueIdentifier = parseYamlAndGetTemplateIdentifierAndVersion (yaml );
365- IdentifierRef templateIdentifierRef =
366- TemplateUtils .getGitBranchAwareIdentifierRef (accountIdentifier , orgIdentifier , projectIdentifier ,
367- templateUniqueIdentifier .getTemplateIdentifier (), templateUniqueIdentifier .getGitBranch ());
363+ IdentifierRef templateIdentifierRef = TemplateUtils .getIdentifierRef (accountIdentifier , orgIdentifier ,
364+ projectIdentifier , templateUniqueIdentifier .getTemplateIdentifier (), templateUniqueIdentifier .getGitBranch ());
368365
369366 Scope templateScope = Scope .builder ()
370367 .projectIdentifier (templateIdentifierRef .getProjectIdentifier ())
0 commit comments