Jira Cloud: Add and Read Issue Comments
See more Jira Examples
Add a comment using an ADF document and retrieve all pages of comments. Comment bodies are structured rich text; the example displays their JSON instead of assuming every comment contains a single text node. Re-running the example creates another comment.
This platform example adds an ordinary Jira comment without a group/role visibility restriction. For a Service Management customer reply or internal note, use the Service Management comment API, where visibility is explicit.
The code uses an Atlassian account email and an API token without scopes with the site URL. For a scoped API token, keep Basic authentication and change baseUrl to https://api.atlassian.com/ex/jira/YOUR_CLOUD_ID. For OAuth2, use that gateway URL, omit Login/Password/BasicAuth, and set http.AuthToken to the access token. See API token authentication and OAuth2 authorization. The account must have permission for the operation; OAuth scopes do not grant additional Jira permissions.
Targets Jira Cloud. Jira Data Center has a separate REST API and may require different paths, authentication, or payloads.
Atlassian REST API documentation
Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Http
string ls_BaseUrl
oleobject loo_Url
oleobject loo_Resp
oleobject loo_Reply
string ls_IssueKey
oleobject loo_Request
oleobject loo_Body
long li_PageStart
long li_PageMore
oleobject loo_PageOffset
long li_PageCount
long li_PageIndex
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
destroy loo_Http
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Http.RequireSslCertVerify = 1
loo_Http.BasicAuth = 1
// Read these credentials from private application configuration.
loo_Http.Login = "YOUR_ATLASSIAN_EMAIL"
loo_Http.Password = "YOUR_API_TOKEN"
ls_BaseUrl = "https://your-domain.atlassian.net"
loo_Http.Accept = "application/json"
loo_Url = create oleobject
li_rc = loo_Url.ConnectToNewObject("Chilkat.StringBuilder")
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")
loo_Reply = create oleobject
li_rc = loo_Reply.ConnectToNewObject("Chilkat.JsonObject")
ls_IssueKey = "PROJECT-123"
loo_Http.SetUrlVar("issueKey",ls_IssueKey)
loo_Request = create oleobject
li_rc = loo_Request.ConnectToNewObject("Chilkat.JsonObject")
// Jira v3 rich text is an Atlassian Document Format (ADF) document.
loo_Request.UpdateString("body.type","doc")
loo_Request.UpdateInt("body.version",1)
loo_Request.UpdateString("body.content[0].type","paragraph")
loo_Request.UpdateString("body.content[0].content[0].type","text")
loo_Request.UpdateString("body.content[0].content[0].text","A comment added through the REST API.")
loo_Url.SetString(ls_BaseUrl)
loo_Url.Append("/rest/api/3/issue/{$issueKey}/comment")
li_Success = loo_Http.HttpJson("POST",loo_Url.GetAsString(),loo_Request,"application/json",loo_Resp)
if loo_Resp.StatusCode = 0 then
Write-Debug "No HTTP response was received. Check connectivity and TLS settings."
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Reply
destroy loo_Request
return
end if
if (li_Success <> 1) OR (loo_Resp.StatusCode < 200) OR (loo_Resp.StatusCode >= 300) then
Write-Debug "Request failed. HTTP " + string(loo_Resp.StatusCode)
if loo_Resp.StatusCode = 429 then
Write-Debug "Rate limited. Retry-After: " + loo_Resp.GetHeaderField("Retry-After")
end if
// Inspect this response locally; it can contain private Jira data.
Write-Debug loo_Resp.BodyStr
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Reply
destroy loo_Request
return
end if
li_Success = loo_Reply.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
Write-Debug "Expected a JSON object in the response."
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Reply
destroy loo_Request
return
end if
Write-Debug "Created comment ID: " + loo_Reply.StringOf("id")
loo_Body = create oleobject
li_rc = loo_Body.ConnectToNewObject("Chilkat.JsonObject")
li_PageStart = 0
li_PageMore = 1
loo_PageOffset = create oleobject
li_rc = loo_PageOffset.ConnectToNewObject("Chilkat.StringBuilder")
do while (li_PageMore = 1)
loo_PageOffset.Clear()
loo_PageOffset.AppendInt(li_PageStart)
loo_Http.SetUrlVar("offset",loo_PageOffset.GetAsString())
loo_Url.SetString(ls_BaseUrl)
loo_Url.Append("/rest/api/3/issue/{$issueKey}/comment?startAt={$offset}&maxResults=50")
li_Success = loo_Http.HttpNoBody("GET",loo_Url.GetAsString(),loo_Resp)
if loo_Resp.StatusCode = 0 then
Write-Debug "No HTTP response was received. Check connectivity and TLS settings."
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Reply
destroy loo_Request
destroy loo_Body
destroy loo_PageOffset
return
end if
if (li_Success <> 1) OR (loo_Resp.StatusCode < 200) OR (loo_Resp.StatusCode >= 300) then
Write-Debug "Request failed. HTTP " + string(loo_Resp.StatusCode)
if loo_Resp.StatusCode = 429 then
Write-Debug "Rate limited. Retry-After: " + loo_Resp.GetHeaderField("Retry-After")
end if
// Inspect this response locally; it can contain private Jira data.
Write-Debug loo_Resp.BodyStr
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Reply
destroy loo_Request
destroy loo_Body
destroy loo_PageOffset
return
end if
li_Success = loo_Reply.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
Write-Debug "Expected a JSON object in the response."
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Reply
destroy loo_Request
destroy loo_Body
destroy loo_PageOffset
return
end if
if loo_Reply.JsonTypeOf("comments") <> 4 then
Write-Debug "Expected an array: comments"
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Reply
destroy loo_Request
destroy loo_Body
destroy loo_PageOffset
return
end if
li_PageCount = loo_Reply.SizeOfArray("comments")
li_PageIndex = 0
do while (li_PageIndex < li_PageCount)
loo_Reply.I = li_PageIndex
Write-Debug "Comment " + loo_Reply.StringOf("comments[i].id") + " by " + loo_Reply.StringOf("comments[i].author.displayName")
loo_Body = loo_Reply.ObjectOf("comments[i].body")
if loo_Reply.LastMethodSuccess = 1 then
Write-Debug loo_Body.Emit()
destroy loo_Body
end if
li_PageIndex = li_PageIndex + 1
loop
li_PageStart = li_PageStart + li_PageCount
li_PageMore = (li_PageStart < loo_Reply.IntOf("total"))
if (li_PageMore = 1) AND (li_PageCount = 0) then
Write-Debug "Pagination made no progress; stopping."
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Reply
destroy loo_Request
destroy loo_Body
destroy loo_PageOffset
return
end if
loop
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Reply
destroy loo_Request
destroy loo_Body
destroy loo_PageOffset