Skip to content

Commit

Permalink
Merge pull request #127 from alainsahli/pr/aloren/feature/message-to-…
Browse files Browse the repository at this point in the history
…page-history
  • Loading branch information
cstettler committed Feb 25, 2019
2 parents 0a47b03 + 6e85771 commit 552f506
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public static void main(String[] args) throws Exception {
String password = mandatoryArgument("password", args);
String spaceKey = mandatoryArgument("spaceKey", args);
String ancestorId = mandatoryArgument("ancestorId", args);
String versionMessage = optionalArgument("versionMessage", args).orElse(null);

PublishingStrategy publishingStrategy = PublishingStrategy.valueOf(optionalArgument("strategy", args).orElse(APPEND_TO_ANCESTOR.name()));

Expand All @@ -70,7 +71,8 @@ public static void main(String[] args) throws Exception {
ConfluencePublisherMetadata confluencePublisherMetadata = asciidocConfluenceConverter.convert(asciidocPagesStructureProvider, pageTitlePostProcessor, buildFolder);

ConfluenceRestClient confluenceClient = new ConfluenceRestClient(rootConfluenceUrl, username, password);
ConfluencePublisher confluencePublisher = new ConfluencePublisher(confluencePublisherMetadata, publishingStrategy, confluenceClient, new SystemOutLoggingConfluencePublisherListener());
ConfluencePublisher confluencePublisher = new ConfluencePublisher(confluencePublisherMetadata, publishingStrategy,
confluenceClient, new SystemOutLoggingConfluencePublisherListener(), versionMessage);
confluencePublisher.publish();
} finally {
deleteDirectory(buildFolder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,20 @@ public class ConfluencePublisher {
private final PublishingStrategy publishingStrategy;
private final ConfluenceClient confluenceClient;
private final ConfluencePublisherListener confluencePublisherListener;
private final String versionMessage;

public ConfluencePublisher(ConfluencePublisherMetadata metadata, PublishingStrategy publishingStrategy, ConfluenceClient confluenceClient) {
this(metadata, publishingStrategy, confluenceClient, new NoOpConfluencePublisherListener());
this(metadata, publishingStrategy, confluenceClient, new NoOpConfluencePublisherListener(), null);
}

public ConfluencePublisher(ConfluencePublisherMetadata metadata, PublishingStrategy publishingStrategy, ConfluenceClient confluenceClient, ConfluencePublisherListener confluencePublisherListener) {
public ConfluencePublisher(ConfluencePublisherMetadata metadata, PublishingStrategy publishingStrategy,
ConfluenceClient confluenceClient, ConfluencePublisherListener confluencePublisherListener,
String versionMessage) {
this.metadata = metadata;
this.publishingStrategy = publishingStrategy;
this.confluenceClient = confluenceClient;
this.confluencePublisherListener = confluencePublisherListener;
this.versionMessage = versionMessage;
}

public void publish() {
Expand Down Expand Up @@ -153,7 +157,7 @@ private String addOrUpdatePageUnderAncestor(String spaceKey, String ancestorId,
updatePage(contentId, ancestorId, page);
} catch (NotFoundException e) {
String content = fileContent(page.getContentFilePath(), UTF_8);
contentId = this.confluenceClient.addPageUnderAncestor(spaceKey, ancestorId, page.getTitle(), content);
contentId = this.confluenceClient.addPageUnderAncestor(spaceKey, ancestorId, page.getTitle(), content, this.versionMessage);
this.confluenceClient.setPropertyByKey(contentId, CONTENT_HASH_PROPERTY_KEY, contentHash(content));
this.confluencePublisherListener.pageAdded(new ConfluencePage(contentId, page.getTitle(), content, INITIAL_PAGE_VERSION));
}
Expand All @@ -170,7 +174,7 @@ private void updatePage(String contentId, String ancestorId, ConfluencePageMetad
if (notSameContentHash(existingContentHash, newContentHash) || !existingPage.getTitle().equals(page.getTitle())) {
this.confluenceClient.deletePropertyByKey(contentId, CONTENT_HASH_PROPERTY_KEY);
int newPageVersion = existingPage.getVersion() + 1;
this.confluenceClient.updatePage(contentId, ancestorId, page.getTitle(), content, newPageVersion);
this.confluenceClient.updatePage(contentId, ancestorId, page.getTitle(), content, newPageVersion, this.versionMessage);
this.confluenceClient.setPropertyByKey(contentId, CONTENT_HASH_PROPERTY_KEY, newContentHash);
this.confluencePublisherListener.pageUpdated(existingPage, new ConfluencePage(contentId, page.getTitle(), content, newPageVersion));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
*/
public interface ConfluenceClient {

String addPageUnderAncestor(String spaceKey, String ancestorId, String title, String content);
String addPageUnderAncestor(String spaceKey, String ancestorId, String title, String content, String versionMessage);

void updatePage(String contentId, String ancestorId, String title, String content, int newVersion);
void updatePage(String contentId, String ancestorId, String title, String content, int newVersion, String versionMessage);

void deletePage(String contentId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ private void configureObjectMapper() {
}

@Override
public String addPageUnderAncestor(String spaceKey, String ancestorId, String title, String content) {
HttpPost addPageUnderSpaceRequest = this.httpRequestFactory.addPageUnderAncestorRequest(spaceKey, ancestorId, title, content);
public String addPageUnderAncestor(String spaceKey, String ancestorId, String title, String content, String versionMessage) {
HttpPost addPageUnderSpaceRequest = this.httpRequestFactory.addPageUnderAncestorRequest(spaceKey, ancestorId, title, content, versionMessage);

return sendRequestAndFailIfNot20x(addPageUnderSpaceRequest, (response) -> {
String contentId = extractIdFromJsonNode(parseJsonResponse(response));
Expand All @@ -87,8 +87,8 @@ public String addPageUnderAncestor(String spaceKey, String ancestorId, String ti
}

@Override
public void updatePage(String contentId, String ancestorId, String title, String content, int newVersion) {
HttpPut updatePageRequest = this.httpRequestFactory.updatePageRequest(contentId, ancestorId, title, content, newVersion);
public void updatePage(String contentId, String ancestorId, String title, String content, int newVersion, String versionMessage) {
HttpPut updatePageRequest = this.httpRequestFactory.updatePageRequest(contentId, ancestorId, title, content, newVersion, versionMessage);
sendRequestAndFailIfNot20x(updatePageRequest);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class HttpRequestFactory {

private final static Header APPLICATION_JSON_UTF8_HEADER = new BasicHeader("Content-Type", "application/json;charset=utf-8");
private static final String REST_API_CONTEXT = "/rest/api";
private static final int INITAL_VERSION = 1;
private final String rootConfluenceUrl;
private final String confluenceRestApiEndpoint;

Expand All @@ -69,7 +70,7 @@ class HttpRequestFactory {
this.confluenceRestApiEndpoint = rootConfluenceUrl + REST_API_CONTEXT;
}

HttpPost addPageUnderAncestorRequest(String spaceKey, String ancestorId, String title, String content) {
HttpPost addPageUnderAncestorRequest(String spaceKey, String ancestorId, String title, String content, String versionMessage) {
assertMandatoryParameter(isNotBlank(spaceKey), "spaceKey");
assertMandatoryParameter(isNotBlank(ancestorId), "ancestorId");
assertMandatoryParameter(isNotBlank(title), "title");
Expand All @@ -79,12 +80,14 @@ HttpPost addPageUnderAncestorRequest(String spaceKey, String ancestorId, String
.ancestorId(ancestorId)
.title(title)
.content(content)
.version(INITAL_VERSION)
.versionMessage(versionMessage)
.build();

return addPageHttpPost(this.confluenceRestApiEndpoint, pagePayload);
}

HttpPut updatePageRequest(String contentId, String ancestorId, String title, String content, int newVersion) {
HttpPut updatePageRequest(String contentId, String ancestorId, String title, String content, int newVersion, String versionMessage) {
assertMandatoryParameter(isNotBlank(contentId), "contentId");
assertMandatoryParameter(isNotBlank(title), "title");

Expand All @@ -93,6 +96,7 @@ HttpPut updatePageRequest(String contentId, String ancestorId, String title, Str
.title(title)
.content(content)
.version(newVersion)
.versionMessage(versionMessage)
.build();

HttpPut updatePageRequest = new HttpPut(this.confluenceRestApiEndpoint + "/content/" + contentId);
Expand Down Expand Up @@ -324,6 +328,7 @@ static class PagePayloadBuilder {
private String spaceKey;
private String ancestorId;
private Integer version;
private String versionMessage;

public PagePayloadBuilder title(String title) {
this.title = title;
Expand Down Expand Up @@ -355,6 +360,12 @@ public PagePayloadBuilder version(Integer version) {
return this;
}

public PagePayloadBuilder versionMessage(String versionMessage) {
this.versionMessage = versionMessage;

return this;
}

private PagePayload build() {
Storage storage = new Storage();
storage.setValue(this.content);
Expand All @@ -381,6 +392,9 @@ private PagePayload build() {
if (this.version != null) {
Version versionContainer = new Version();
versionContainer.setNumber(this.version);
if (this.versionMessage != null) {
versionContainer.setMessage(this.versionMessage);
}
pagePayload.setVersion(versionContainer);
}

Expand All @@ -390,7 +404,6 @@ private PagePayload build() {
static PagePayloadBuilder pagePayloadBuilder() {
return new PagePayloadBuilder();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
public class Version {

private int number;
private String message;

@RuntimeUse
public int getNumber() {
Expand All @@ -34,4 +35,11 @@ public void setNumber(int number) {
this.number = number;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
Loading

0 comments on commit 552f506

Please sign in to comment.