This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update project metadata through REST API
This commit adds a new PATCH method on the "/project_metadata/{project}" route that patches the project metadata information. This is useful for automated project release processes. Closes gh-888 Closes gh-889
- Loading branch information
1 parent
290ce6e
commit f966492
Showing
5 changed files
with
174 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
sagan-common/src/main/java/sagan/projects/ProjectPatchingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package sagan.projects; | ||
|
||
import java.util.Objects; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.Function; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class ProjectPatchingService { | ||
|
||
public Project patch(Project newValue, Project valueToMutate) { | ||
return ObjectPatcher.patch(newValue, valueToMutate) | ||
.mutateIfDirty(Project::getRawOverview, Project::setRawOverview) | ||
.mutateIfDirty(Project::getRawBootConfig, Project::setRawBootConfig) | ||
.patchedValue(); | ||
} | ||
|
||
// could be reused in other projects | ||
static class ObjectPatcher<T> { | ||
private T newValue; | ||
|
||
private T valueToMutate; | ||
|
||
private ObjectPatcher(T newValue, T valueToMutate) { | ||
this.newValue = newValue; | ||
this.valueToMutate = valueToMutate; | ||
} | ||
|
||
static <T> ObjectPatcher<T> patch(T newValue, T valueToMutate) { | ||
return new ObjectPatcher<>(newValue, valueToMutate); | ||
} | ||
|
||
<V> ObjectPatcher<T> mutateIfDirty(Function<T, V> getter, BiConsumer<T, V> modifyIfDirty) { | ||
V newValue = getter.apply(this.newValue); | ||
V valueToMutate = getter.apply(this.valueToMutate); | ||
if (isDirty(newValue, valueToMutate)) { | ||
// mutates the old object | ||
modifyIfDirty.accept(this.valueToMutate, newValue); | ||
} | ||
return this; | ||
} | ||
|
||
// syntactic sugar since new value got mutated all the way | ||
T patchedValue() { | ||
return this.valueToMutate; | ||
} | ||
|
||
private boolean isDirty(Object newValue, Object oldValue) { | ||
return newValue != null && !Objects.equals(newValue, oldValue); | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters