Skip to content

Commit

Permalink
feat: support trigger by regex patterns (#1129)
Browse files Browse the repository at this point in the history
This change extends the folder settings on workspace settings by
allowing multiple fields separated by comma, the first path will be used
as the default workspace path for runs triggered from UI, the rest of
the paths are regex to match against the changed files in a push event
sent from VCS providers.

examples: /workspaces/test,modules/.*.tf
  • Loading branch information
stanleyz committed Jul 29, 2024
1 parent 8d47085 commit d3c4cec
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ public Flow getNextFlow(Job job) {
log.info("Checking import commands in YAML");
ImportComands importComands = finalFlow.getImportComands();
if (importComands != null) {
log.info("Import commands from {} branch {} folder {}", importComands.getRepository(), importComands.getBranch(), importComands.getFolder());
finalFlow.setCommands(importCommands(importComands.getRepository(), importComands.getBranch(), importComands.getFolder()));
log.info("Import commands from {} branch {} folder {}", importComands.getRepository(), importComands.getBranch(), importComands.getFolder().split(",")[0]);
finalFlow.setCommands(importCommands(importComands.getRepository(), importComands.getBranch(), importComands.getFolder().split(",")[0]));
}

return finalFlow;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public boolean execute(Job job, String stepId, Flow flow) {
}
executorContext.setTofu(iacType(job));
executorContext.setCommitId(job.getCommitId());
executorContext.setFolder(job.getWorkspace().getFolder());
executorContext.setFolder(job.getWorkspace().getFolder().split(",")[0]);
executorContext.setRefresh(job.isRefresh());
executorContext.setRefreshOnly(job.isRefreshOnly());
executorContext.setAgentUrl(getExecutorUrl(job));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ WorkspaceData getWorkspace(String organizationName, String workspaceName, Map<St
attributes.put("execution-mode", workspace.get().getExecutionMode());
attributes.put("global-remote-state", true);

if (workspace.get().getFolder() != null && (workspace.get().getVcs() != null || workspace.get().getSsh() != null) && !workspace.get().getFolder().equals("/")){
attributes.put("working-directory", workspace.get().getFolder());
if (workspace.get().getFolder() != null && (workspace.get().getVcs() != null || workspace.get().getSsh() != null) && !workspace.get().getFolder().split(",")[0].equals("/")){
attributes.put("working-directory", workspace.get().getFolder().split(",")[0]);
}

boolean isManageWorkspace = validateUserManageWorkspace(workspace.get().getOrganization(), currentUser);
Expand Down
25 changes: 14 additions & 11 deletions api/src/main/java/org/terrakube/api/plugin/vcs/WebhookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;

@AllArgsConstructor
@Slf4j
Expand Down Expand Up @@ -145,17 +144,21 @@ public String processWebhook(String webhookId, String jsonPayload,Map<String, St
}

private boolean checkFileChanges(List<String> files, String workspaceFolder){
if(files != null){
AtomicBoolean fileChanged = new AtomicBoolean(false);
files.forEach(file-> {
log.info("File: {} in {}: {}", file, workspaceFolder.substring(1), file.startsWith(workspaceFolder.substring(1)));
if(file.startsWith(workspaceFolder.substring(1)) && !fileChanged.get()){
fileChanged.set(true);
String[] triggeredPath = workspaceFolder.split(",");
for (String file: files){
if(file.startsWith(triggeredPath[0].substring(1))){
log.info("Changed file {} in set workspace path {}", file, triggeredPath[0]);
return true;
}
for (int i = 1; i< triggeredPath.length; i++){
if(file.matches(triggeredPath[i])){
log.info("Changed file {} matches set trigger pattern {}", file, triggeredPath[i]);
return true;
}
});
return fileChanged.get();
} else
return true;
}
}
log.info("Changed files {} doesn't match any of the trigger path pattern {}", files, triggeredPath);
return false;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ private void setupConsumerGroups(String jobId) {
public File getTerraformWorkingDir(TerraformJob terraformJob, File workingDirectory) throws IOException {
File terraformWorkingDir = workingDirectory;
try {
if (!terraformJob.getBranch().equals("remote-content") || (terraformJob.getFolder() != null && !terraformJob.getFolder().equals("/"))) {
terraformWorkingDir = new File(workingDirectory.getCanonicalPath() + terraformJob.getFolder());
if (!terraformJob.getBranch().equals("remote-content") || (terraformJob.getFolder() != null && !terraformJob.getFolder().split(",")[0].equals("/"))) {
terraformWorkingDir = new File(workingDirectory.getCanonicalPath() + terraformJob.getFolder().split(",")[0]);
}
} catch (IOException e) {
log.error(e.getMessage());
Expand Down

0 comments on commit d3c4cec

Please sign in to comment.