Skip to content

Commit

Permalink
Enable force refresh by forceRefresh query parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
opeco17 committed Apr 7, 2024
1 parent c372fcf commit fd68d22
Show file tree
Hide file tree
Showing 26 changed files with 561 additions and 264 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,17 @@ public synchronized Environment findOne(String application, String profile, Stri

@Override
public synchronized Environment findOne(String application, String profile, String label, boolean includeOrigin) {
return findOne(application, profile, label, includeOrigin, false);
}

@Override
public synchronized Environment findOne(String application, String profile, String label, boolean includeOrigin,
boolean forceRefresh) {
NativeEnvironmentRepository delegate = new NativeEnvironmentRepository(getEnvironment(),
new NativeEnvironmentProperties(), this.observationRegistry);
Locations locations = getLocations(application, profile, label);
Locations locations = getLocations(application, profile, label, forceRefresh);
delegate.setSearchLocations(locations.getLocations());
Environment result = delegate.findOne(application, profile, "", includeOrigin);
Environment result = delegate.findOne(application, profile, "", includeOrigin, forceRefresh);
result.setVersion(locations.getVersion());
result.setLabel(label);
return this.cleaner.clean(result, getWorkingDirectory().toURI().toString(), getUri());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,25 @@ public Environment findOne(String application, String profile, String label) {

@Override
public Environment findOne(String application, String profile, String label, boolean includeOrigin) {
return findOne(application, profile, label, includeOrigin, false);
}

@Override
public Environment findOne(String application, String profile, String label, boolean includeOrigin,
boolean forceRefresh) {
Environment env = new Environment(application, new String[] { profile }, label, null, null);
if (this.environmentRepositories.size() == 1) {
Environment envRepo = this.environmentRepositories.get(0).findOne(application, profile, label,
includeOrigin);
includeOrigin, forceRefresh);
env.addAll(envRepo.getPropertySources());
env.setVersion(envRepo.getVersion());
env.setState(envRepo.getState());
}
else {
for (EnvironmentRepository repo : environmentRepositories) {
try {
env.addAll(repo.findOne(application, profile, label, includeOrigin).getPropertySources());
env.addAll(repo.findOne(application, profile, label, includeOrigin, forceRefresh)
.getPropertySources());
}
catch (Exception e) {
if (failOnError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,32 +106,36 @@ public void setAcceptEmpty(boolean acceptEmpty) {

@GetMapping(path = "/{name}/{profiles:(?!.*\\b\\.(?:ya?ml|properties|json)\\b).*}",
produces = MediaType.APPLICATION_JSON_VALUE)
public Environment defaultLabel(@PathVariable String name, @PathVariable String profiles) {
return getEnvironment(name, profiles, null, false);
public Environment defaultLabel(@PathVariable String name, @PathVariable String profiles,
@RequestParam(defaultValue = "false") boolean forceRefresh) {
return getEnvironment(name, profiles, null, false, forceRefresh);
}

@GetMapping(path = "/{name}/{profiles:(?!.*\\b\\.(?:ya?ml|properties|json)\\b).*}",
produces = EnvironmentMediaType.V2_JSON)
public Environment defaultLabelIncludeOrigin(@PathVariable String name, @PathVariable String profiles) {
return getEnvironment(name, profiles, null, true);
public Environment defaultLabelIncludeOrigin(@PathVariable String name, @PathVariable String profiles,
@RequestParam(defaultValue = "false") boolean forceRefresh) {
return getEnvironment(name, profiles, null, true, forceRefresh);
}

@GetMapping(path = "/{name}/{profiles}/{label:.*}", produces = MediaType.APPLICATION_JSON_VALUE)
public Environment labelled(@PathVariable String name, @PathVariable String profiles, @PathVariable String label) {
return getEnvironment(name, profiles, label, false);
public Environment labelled(@PathVariable String name, @PathVariable String profiles, @PathVariable String label,
@RequestParam(defaultValue = "false") boolean forceRefresh) {
return getEnvironment(name, profiles, label, false, forceRefresh);
}

@GetMapping(path = "/{name}/{profiles}/{label:.*}", produces = EnvironmentMediaType.V2_JSON)
public Environment labelledIncludeOrigin(@PathVariable String name, @PathVariable String profiles,
@PathVariable String label) {
return getEnvironment(name, profiles, label, true);
@PathVariable String label, @RequestParam(defaultValue = "false") boolean forceRefresh) {
return getEnvironment(name, profiles, label, true, forceRefresh);
}

public Environment getEnvironment(String name, String profiles, String label, boolean includeOrigin) {
public Environment getEnvironment(String name, String profiles, String label, boolean includeOrigin,
boolean forceRefresh) {
try {
name = normalize(name);
label = normalize(label);
Environment environment = this.repository.findOne(name, profiles, label, includeOrigin);
Environment environment = this.repository.findOne(name, profiles, label, includeOrigin, forceRefresh);
if (!this.acceptEmpty && (environment == null || environment.getPropertySources().isEmpty())) {
throw new EnvironmentNotFoundException("Profile Not found");
}
Expand All @@ -153,16 +157,17 @@ private String normalize(String part) {

@GetMapping("/{name}-{profiles}.properties")
public ResponseEntity<String> properties(@PathVariable String name, @PathVariable String profiles,
@RequestParam(defaultValue = "true") boolean resolvePlaceholders) throws IOException {
return labelledProperties(name, profiles, null, resolvePlaceholders);
@RequestParam(defaultValue = "true") boolean resolvePlaceholders,
@RequestParam(defaultValue = "false") boolean forceRefresh) throws IOException {
return labelledProperties(name, profiles, null, resolvePlaceholders, forceRefresh);
}

@GetMapping("/{label}/{name}-{profiles}.properties")
public ResponseEntity<String> labelledProperties(@PathVariable String name, @PathVariable String profiles,
@PathVariable String label, @RequestParam(defaultValue = "true") boolean resolvePlaceholders)
throws IOException {
@PathVariable String label, @RequestParam(defaultValue = "true") boolean resolvePlaceholders,
@RequestParam(defaultValue = "false") boolean forceRefresh) throws IOException {
validateProfiles(profiles);
Environment environment = labelled(name, profiles, label);
Environment environment = labelled(name, profiles, label, forceRefresh);
Map<String, Object> properties = convertToProperties(environment);
String propertiesString = getPropertiesString(properties);
if (resolvePlaceholders) {
Expand All @@ -173,16 +178,17 @@ public ResponseEntity<String> labelledProperties(@PathVariable String name, @Pat

@GetMapping("{name}-{profiles}.json")
public ResponseEntity<String> jsonProperties(@PathVariable String name, @PathVariable String profiles,
@RequestParam(defaultValue = "true") boolean resolvePlaceholders) throws Exception {
return labelledJsonProperties(name, profiles, null, resolvePlaceholders);
@RequestParam(defaultValue = "true") boolean resolvePlaceholders,
@RequestParam(defaultValue = "false") boolean forceRefresh) throws Exception {
return labelledJsonProperties(name, profiles, null, resolvePlaceholders, forceRefresh);
}

@GetMapping("/{label}/{name}-{profiles}.json")
public ResponseEntity<String> labelledJsonProperties(@PathVariable String name, @PathVariable String profiles,
@PathVariable String label, @RequestParam(defaultValue = "true") boolean resolvePlaceholders)
throws Exception {
@PathVariable String label, @RequestParam(defaultValue = "true") boolean resolvePlaceholders,
@RequestParam(defaultValue = "false") boolean forceRefresh) throws Exception {
validateProfiles(profiles);
Environment environment = labelled(name, profiles, label);
Environment environment = labelled(name, profiles, label, forceRefresh);
Map<String, Object> properties = convertToMap(environment);
String json = this.objectMapper.writeValueAsString(properties);
if (resolvePlaceholders) {
Expand All @@ -204,16 +210,17 @@ private String getPropertiesString(Map<String, Object> properties) {

@GetMapping({ "/{name}-{profiles}.yml", "/{name}-{profiles}.yaml" })
public ResponseEntity<String> yaml(@PathVariable String name, @PathVariable String profiles,
@RequestParam(defaultValue = "true") boolean resolvePlaceholders) throws Exception {
return labelledYaml(name, profiles, null, resolvePlaceholders);
@RequestParam(defaultValue = "true") boolean resolvePlaceholders,
@RequestParam(defaultValue = "false") boolean forceRefresh) throws Exception {
return labelledYaml(name, profiles, null, resolvePlaceholders, forceRefresh);
}

@GetMapping({ "/{label}/{name}-{profiles}.yml", "/{label}/{name}-{profiles}.yaml" })
public ResponseEntity<String> labelledYaml(@PathVariable String name, @PathVariable String profiles,
@PathVariable String label, @RequestParam(defaultValue = "true") boolean resolvePlaceholders)
throws Exception {
@PathVariable String label, @RequestParam(defaultValue = "true") boolean resolvePlaceholders,
@RequestParam(defaultValue = "false") boolean forceRefresh) throws Exception {
validateProfiles(profiles);
Environment environment = labelled(name, profiles, label);
Environment environment = labelled(name, profiles, label, forceRefresh);
Map<String, Object> result = convertToMap(environment);
if (this.stripDocument && result.size() == 1 && result.keySet().iterator().next().equals("document")) {
Object value = result.get("document");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ public Environment findOne(String name, String profiles, String label) {

@Override
public Environment findOne(String name, String profiles, String label, boolean includeOrigin) {
Environment environment = this.delegate.findOne(name, profiles, label, includeOrigin);
return findOne(name, profiles, label, includeOrigin, false);
}

@Override
public Environment findOne(String name, String profiles, String label, boolean includeOrigin,
boolean forceRefresh) {
Environment environment = this.delegate.findOne(name, profiles, label, includeOrigin, forceRefresh);
if (this.environmentEncryptors != null) {
for (EnvironmentEncryptor environmentEncryptor : environmentEncryptors) {
environment = environmentEncryptor.decrypt(environment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ default Environment findOne(String application, String profile, String label, bo
return findOne(application, profile, label);
}

default Environment findOne(String application, String profile, String label, boolean includeOrigin,
boolean forceRefresh) {
return findOne(application, profile, label, includeOrigin);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ public class JGitEnvironmentProperties extends AbstractScmAccessorProperties
*/
private int refreshRate = 0;

/**
* Allow forceRefresh query parameter to force refresh the git repository regardless
* of refreshRate if true.
*/
private boolean allowForceRefresh = false;

/**
* Valid SSH private key. Must be set if ignoreLocalSshSettings is true and Git URI is
* SSH format.
Expand Down Expand Up @@ -189,6 +195,14 @@ public void setRefreshRate(int refreshRate) {
this.refreshRate = refreshRate;
}

public boolean getAllowForceRefresh() {
return this.allowForceRefresh;
}

public void setAllowForceRefresh(boolean allowForceRefresh) {
this.allowForceRefresh = allowForceRefresh;
}

public String getPrivateKey() {
return this.privateKey;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
*/
private int timeout;

/**
* Allow forceRefresh query parameter to force refresh the git repository regardless
* of refreshRate if true.
*/
public boolean allowForceRefresh = false;

/**
* Time (in seconds) between refresh of the git repository.
*/
Expand Down Expand Up @@ -158,6 +164,7 @@ public JGitEnvironmentRepository(ConfigurableEnvironment environment, JGitEnviro
this.timeout = properties.getTimeout();
this.deleteUntrackedBranches = properties.isDeleteUntrackedBranches();
this.refreshRate = properties.getRefreshRate();
this.allowForceRefresh = properties.getAllowForceRefresh();
this.skipSslValidation = properties.isSkipSslValidation();
this.gitFactory = new JGitFactory(properties.isCloneSubmodules());
this.tryMasterBranch = properties.isTryMasterBranch();
Expand Down Expand Up @@ -196,6 +203,14 @@ public void setRefreshRate(int refreshRate) {
this.refreshRate = refreshRate;
}

public boolean getAllowForceRefresh() {
return this.allowForceRefresh;
}

public void setAllowForceRefresh(boolean allowForceRefresh) {
this.allowForceRefresh = allowForceRefresh;
}

public TransportConfigCallback getTransportConfigCallback() {
return this.transportConfigCallback;
}
Expand Down Expand Up @@ -254,19 +269,24 @@ public void setSkipSslValidation(boolean skipSslValidation) {

@Override
public synchronized Locations getLocations(String application, String profile, String label) {
return getLocations(application, profile, label, false);
}

@Override
public synchronized Locations getLocations(String application, String profile, String label, boolean forceRefresh) {
if (label == null) {
label = this.defaultLabel;
}
String version;
try {
version = refresh(label);
version = refresh(label, forceRefresh);
}
catch (Exception e) {
if (this.defaultLabel.equals(label) && JGitEnvironmentProperties.MAIN_LABEL.equals(this.defaultLabel)
&& tryMasterBranch) {
logger.info("Could not refresh default label " + label, e);
logger.info("Will try to refresh master label instead.");
version = refresh(JGitEnvironmentProperties.MASTER_LABEL);
version = refresh(JGitEnvironmentProperties.MASTER_LABEL, forceRefresh);
}
else {
throw e;
Expand All @@ -289,11 +309,11 @@ public synchronized void afterPropertiesSet() throws Exception {
* @param label label to refresh
* @return head id
*/
public String refresh(String label) {
public String refresh(String label, boolean forceRefresh) {
Git git = null;
try {
git = createGitClient();
if (shouldPull(git)) {
if (shouldPull(git, forceRefresh)) {
FetchResult fetchStatus = fetch(git, label);
if (this.deleteUntrackedBranches && fetchStatus != null) {
deleteUntrackedLocalBranches(fetchStatus.getTrackingRefUpdates(), git);
Expand Down Expand Up @@ -467,11 +487,10 @@ private Ref checkout(Git git, String label) throws GitAPIException {
return checkout.call();
}

protected boolean shouldPull(Git git) throws GitAPIException {
protected boolean shouldPull(Git git, boolean forceRefresh) throws GitAPIException {
boolean shouldPull;

if (this.refreshRate < 0 || (this.refreshRate > 0
&& System.currentTimeMillis() - this.lastRefresh < (this.refreshRate * 1000))) {
if (!(this.allowForceRefresh && forceRefresh) && (this.refreshRate < 0 || (this.refreshRate > 0
&& System.currentTimeMillis() - this.lastRefresh < (this.refreshRate * 1000)))) {
return false;
}

Expand Down
Loading

0 comments on commit fd68d22

Please sign in to comment.