Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kaantas/0a649008375cbea0817af720e7efbcc8 to your computer and use it in GitHub Desktop.
Save kaantas/0a649008375cbea0817af720e7efbcc8 to your computer and use it in GitHub Desktop.
import com.trendyol.deliveryconfigserver.model.TrendyolBitbucketResponse;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.web.client.RestTemplate;
import org.springframework.cloud.config.monitor.PropertyPathNotification;
import org.springframework.cloud.config.monitor.PropertyPathNotificationExtractor;
import org.springframework.beans.factory.annotation.*;
import org.springframework.core.*;
import org.springframework.util.*;
import java.util.*;
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CustomBitbucketPropertyPathNotificationExtractor implements PropertyPathNotificationExtractor {
@Value("${bitbucket.api.url}")
private String bitbucketApiUrl;
@Value("${spring.cloud.config.server.git.username}")
private String bitbucketUsername;
@Value("${spring.cloud.config.server.git.password}")
private String bitbucketPassword;
private RestTemplate restTemplate;
@Autowired
public void setRestTemplateBuilder(RestTemplateBuilder builder) {
restTemplate = builder.basicAuthentication(bitbucketUsername, bitbucketPassword).build();
}
@Override
public PropertyPathNotification extract(MultiValueMap<String, String> headers, Map<String, Object> payload) {
if ("pr:merged".equals(headers.getFirst("X-Event-Key")) && StringUtils.hasText(headers.getFirst("X-Request-Id"))) {
Map<String, Object> pr = (Map<String, Object>) payload.get("pullRequest");
Map<String, Object> fromRef = (Map<String, Object>) pr.get("fromRef");
String commitId = (String) fromRef.get("latestCommit");
String[] changedFiles = getChangedFiles(new String[]{commitId});
return new PropertyPathNotification(changedFiles);
}
return null;
}
private String[] getChangedFiles(String[] commitIds) {
Set<String> changedFiles = new HashSet<>();
for (String commitId : commitIds) {
TrendyolBitbucketResponse response = restTemplate.getForObject(bitbucketApiUrl, TrendyolBitbucketResponse.class, commitId);
for (TrendyolBitbucketResponse.Diff diff : response.getDiffs()) {
changedFiles.add(diff.getSource().getName());
changedFiles.add(diff.getDestination().getName());
}
}
return changedFiles.toArray(new String[changedFiles.size()]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment