Action

Post to Confluence

Posted by Alessandro Melandri, Last update about 2 months ago

UPDATES

about 2 months ago

Moved login informations (host, username and api token) to Drafts Credentials

show all updates...

about 2 months ago

Moved login informations (host, username and api token) to Drafts Credentials

8 months ago

Removed settings from the script code

This action allows you to publish a draft to Confluence Cloud.

Draft will be converted to HTML before publishing, so you’ll get a nicely formatted Confluence page.

Settings instructions:

On first run, the action will prompt for authentication infos and save them to Draft credentials

  • Atlassian_SpaceId: The space ID is NOT your space key. To get the space id, visit this URL while logged in: https://MYCOMPANY.atlassian.net/wiki/rest/api/space/SPACE_KEY
  • Atlassian_ParentPage: Parent page id for the new page

Steps

  • defineTemplateTag

    name
    Atlassian_SpaceId
    template
  • defineTemplateTag

    name
    Atlassian_ParentPage
    template
  • script

    // ----- Confluence API endpoint and authentication
    
    let credential = Credential.createWithHostUsernamePassword("ConfluenceCloud", "Confluence credentials");
    credential.addTextField("host", "Confluence Domain");
    credential.addTextField("username", "Confluence Username");
    credential.addPasswordField("password", "Confluence API Token");
    credential.authorize();
    
    // ------------------------------------------------------------------
    
    var spaceId    = draft.processTemplate("[[Atlassian_SpaceId]]");
    var parentPage = draft.processTemplate("[[Atlassian_ParentPage]]");
    
    // ------------------------------------------------------------------
    
    
    var apiUrl = "https://" + credential.getValue("host") + "/wiki/api/v2/pages";
    
    var authorizationToken = "Basic " + Base64.encode(credential.getValue("username") + ":" + credential.getValue("password"))
    
    
    // Get the note content from the current draft
    
    var noteTitle = draft.displayTitle
    var noteContent = draft.processTemplate("[[trimmed_body]]")
    
    var mmd = MultiMarkdown.create();
    mmd.format = "html";
    mmd.criticMarkup = true;
    var noteContentHtml = mmd.render(noteContent);
    
    var payload = {
      "spaceId": spaceId,
      "status": "current",
      "title": noteTitle,
      "parentId": parentPage,
      "body": {
        "representation": "storage",
        "value": noteContentHtml
      }
    };
    
    var http = HTTP.create();
    
    var response = http.request({
        "url": apiUrl,
        "method": "POST",
        "encoding": "json",
        "data": payload,
        "headers": {
            "Authorization": authorizationToken,
            "Content-Type": "application/json",
            "Accept": "application/json"
        }
    });
    
    // Process the response
    
    if (response.success) {
    	app.displaySuccessMessage("Note published to Confluence")
    } else {
    	app.displayErrorMessage(JSON.parse(response.responseText).errors[0].title)
    }
    

Options

  • After Success Default
    Notification None
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.