Skip to content

Commit fc85320

Browse files
authored
feat: [CDS-70703]: added support for static branch linking in missing flows (#48561)
* feat: [CDS-70703]: added support for static branch linking in missing flows * Update InputsValidator.java * Update TemplateMergeServiceHelper.java * Update TemplateMergeServiceImpl.java * feat: [CDS-70703]: added branch as parameter in template unique identifier * feat: [CDS-70703]: style fix * feat: [CDS-70703]: code format fix
1 parent 11972bf commit fc85320

File tree

3 files changed

+29
-32
lines changed

3 files changed

+29
-32
lines changed

template-service/service/src/main/java/io/harness/template/helpers/InputsValidator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public class InputsValidator {
5757
@Inject private TemplateMergeServiceHelper templateMergeServiceHelper;
5858
@Inject private NGTemplateFeatureFlagHelperService featureFlagHelperService;
5959
@Inject private NgManagerReconcileClient ngManagerReconcileClient;
60+
6061
public ValidateInputsResponseDTO validateInputsForTemplate(
6162
String accountId, String orgId, String projectId, TemplateEntityGetResponse templateEntityGetResponse) {
6263
TemplateEntity templateEntity = templateEntityGetResponse.getTemplateEntity();

template-service/service/src/main/java/io/harness/template/helpers/TemplateMergeServiceHelper.java

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import io.harness.exception.ngexception.beans.templateservice.TemplateInputsErrorMetadataDTO;
3636
import io.harness.gitaware.dto.FetchRemoteEntityRequest;
3737
import io.harness.gitaware.dto.GetFileGitContextRequestParams;
38+
import io.harness.gitaware.helper.GitAwareContextHelper;
3839
import io.harness.gitaware.helper.GitAwareEntityHelper;
3940
import io.harness.gitsync.beans.StoreType;
4041
import 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())

template-service/service/src/main/java/io/harness/template/utils/TemplateUtils.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,10 @@ public static YamlNode validateAndGetYamlNode(String yaml) {
168168

169169
public static IdentifierRef getIdentifierRef(
170170
String accountIdentifier, String orgIdentifier, String projectIdentifier, String identifier) {
171-
return IdentifierRefHelper.getIdentifierRefOrThrowException(
172-
identifier, accountIdentifier, orgIdentifier, projectIdentifier, TEMPLATE_FIELD_NAME);
171+
return getIdentifierRef(accountIdentifier, orgIdentifier, projectIdentifier, identifier, null);
173172
}
174173

175-
public static IdentifierRef getGitBranchAwareIdentifierRef(
174+
public static IdentifierRef getIdentifierRef(
176175
String accountIdentifier, String orgIdentifier, String projectIdentifier, String identifier, String gitBranch) {
177176
IdentifierRef identifierRef = IdentifierRefHelper.getIdentifierRefOrThrowException(
178177
identifier, accountIdentifier, orgIdentifier, projectIdentifier, TEMPLATE_FIELD_NAME);

0 commit comments

Comments
 (0)