Skip to content

Commit

Permalink
[FIXED JENKINS-15472] Added parentId property
Browse files Browse the repository at this point in the history
Added a new job-configuration field for parent page ID#.
Plugin verifies that page ID exists and returns an error message if not.
  • Loading branch information
trevor-hill committed Aug 22, 2014
1 parent d7e9c4f commit 204a8d6
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
Expand Up @@ -70,14 +70,15 @@ public class ConfluencePublisher extends Notifier implements Saveable {

private final String spaceName;
private final String pageName;
private final long parentId;

private DescribableList<MarkupEditor, Descriptor<MarkupEditor>> editors = new DescribableList<MarkupEditor, Descriptor<MarkupEditor>>(
this);

@DataBoundConstructor
public ConfluencePublisher(String siteName, final boolean buildIfUnstable,
final String spaceName, final String pageName, final String labels, final boolean attachArchivedArtifacts,
final String fileSet, final List<MarkupEditor> editorList, final boolean replaceAttachments) throws IOException {
final String fileSet, final List<MarkupEditor> editorList, final boolean replaceAttachments, final long parentId) throws IOException {

if (siteName == null) {
List<ConfluenceSite> sites = getDescriptor().getSites();
Expand All @@ -90,6 +91,7 @@ public ConfluencePublisher(String siteName, final boolean buildIfUnstable,
this.siteName = siteName;
this.spaceName = spaceName;
this.pageName = pageName;
this.parentId = parentId;
this.labels = labels;
this.buildIfUnstable = buildIfUnstable;
this.attachArchivedArtifacts = attachArchivedArtifacts;
Expand Down Expand Up @@ -127,6 +129,13 @@ public String getPageName() {
return pageName;
}

/**
* @return the parentId
*/
public long getParentId() {
return parentId;
}

/**
* @return the labels
*/
Expand Down Expand Up @@ -330,6 +339,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen

String spaceName = this.spaceName;
String pageName = this.pageName;
long parentId = this.parentId;

try {
spaceName = build.getEnvironment(listener).expand(spaceName);
Expand All @@ -349,7 +359,14 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
log(listener, "Unable to locate page: " + spaceName + "/" + pageName + ". Attempting to create the page now...");

try {
pageData = this.createPage(confluence, spaceName, pageName);
// if we haven't specified a parent, assign the Space home page as the parent
if (parentId == 0L) {
RemoteSpace space = confluence.getSpace(spaceName);
if (space != null) parentId = space.getHomePage();
}

pageData = this.createPage(confluence, spaceName, pageName, parentId);

} catch (RemoteException exc2) {
log(listener, "Page could not be created! Aborting edits...");
e.printStackTrace(listener.getLogger());
Expand Down Expand Up @@ -427,15 +444,17 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
* @return The resulting Page
* @throws RemoteException
*/
private RemotePage createPage(ConfluenceSession confluence, String spaceName, String pageName)
private RemotePage createPage(ConfluenceSession confluence, String spaceName, String pageName, long parentId)
throws RemoteException {
RemotePage newPage = new RemotePage();
newPage.setTitle(pageName);
newPage.setSpace(spaceName);
newPage.setContent("");
newPage.setParentId(parentId);
return confluence.storePage(newPage);
}


private boolean performWikiReplacements(AbstractBuild<?, ?> build, Launcher launcher,
BuildListener listener, ConfluenceSession confluence,
jenkins.plugins.confluence.soap.v2.RemotePage pageDataV2, List<RemoteAttachment> remoteAttachments) throws IOException,
Expand Down Expand Up @@ -586,6 +605,40 @@ public boolean configure(StaplerRequest req, JSONObject formData) {
return true;
}

public FormValidation doParentIdCheck(@QueryParameter final String siteName,
@QueryParameter final String spaceName, @QueryParameter final String parentId) {
ConfluenceSite site = this.getSiteByName(siteName);

if (hudson.Util.fixEmptyAndTrim(spaceName) == null
|| hudson.Util.fixEmptyAndTrim(parentId) == null) {
return FormValidation.ok();
}

Long parentIdL;
try {
parentIdL = Long.valueOf(parentId);
} catch (NumberFormatException nfe) {
return FormValidation.error("The parent page id should be a numeric id.");
}

if (site == null) {
return FormValidation.error("Unknown site:" + siteName);
}

try {
ConfluenceSession confluence = site.createSession();
jenkins.plugins.confluence.soap.v2.RemotePage page = confluence.getPageV2(parentIdL);

if (page != null) {
return FormValidation.ok("OK: " + page.getTitle());
}

return FormValidation.error("Page not found");
} catch (RemoteException re) {
return FormValidation.warning("Page not found. Check that the page still exists. ");
}
}

public FormValidation doPageNameCheck(@QueryParameter final String siteName,
@QueryParameter final String spaceName, @QueryParameter final String pageName) {
ConfluenceSite site = this.getSiteByName(siteName);
Expand Down
Expand Up @@ -25,6 +25,11 @@
checkUrl="'descriptorByName/ConfluencePublisher/pageNameCheck?siteName='+Form.findMatchingInput(this,'siteName').value+'&amp;spaceName='+Form.findMatchingInput(this,'_.spaceName').value+'&amp;pageName='+escape(this.value)" />
</f:entry>

<f:entry title="Parent Page ID" field="parentId">
<f:textbox
checkUrl="'descriptorByName/ConfluencePublisher/parentIdCheck?siteName='+Form.findMatchingInput(this,'siteName').value+'&amp;spaceName='+Form.findMatchingInput(this,'_.spaceName').value+'&amp;parentId='+escape(this.value)" />
</f:entry>

<f:entry title="Page Labels" field="labels" help="${descriptor.getHelpFile('labels')}">
<f:textbox />
</f:entry>
Expand Down
@@ -0,0 +1,9 @@
<div>
(Optional) Enter the page ID of the parent wiki page. You can find this identifier by accessing the parent page in question,
and then selecting "Page Information" or "Page History" from the Confluence <em>Tools</em> menu. The pageId will
appear as a query parameter in the address bar. e.g. (<code>pageId=1234567</code>)
<p/>
If a parent is not specified, the space home will be used as the parent.
<p/>
This field is used for creating new pages only. If the page already exists, then parentId is discarded.
</div>

1 comment on commit 204a8d6

@sujit-kulkarni
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I dont find these changes as a part of plugin release. In jenkins I still dont see the options to specify the Parent page ID.

Please sign in to comment.