Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.0.0+

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 SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @iTmp1 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @sTmp1 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Assumes Chilkat is unlocked. See Global Unlock Sample.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
    EXEC sp_OASetProperty @http, 'BasicAuth', 1
    --  Read these credentials from private application configuration.
    EXEC sp_OASetProperty @http, 'Login', 'YOUR_ATLASSIAN_EMAIL'
    EXEC sp_OASetProperty @http, 'Password', 'YOUR_API_TOKEN'
    DECLARE @baseUrl nvarchar(4000)
    SELECT @baseUrl = 'https://your-domain.atlassian.net'
    EXEC sp_OASetProperty @http, 'Accept', 'application/json'
    DECLARE @url int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @url OUT

    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    DECLARE @reply int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @reply OUT

    DECLARE @issueKey nvarchar(4000)
    SELECT @issueKey = 'PROJECT-123'
    EXEC sp_OAMethod @http, 'SetUrlVar', @success OUT, 'issueKey', @issueKey
    DECLARE @request int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @request OUT

    --  Jira v3 rich text is an Atlassian Document Format (ADF) document.
    EXEC sp_OAMethod @request, 'UpdateString', @success OUT, 'body.type', 'doc'
    EXEC sp_OAMethod @request, 'UpdateInt', @success OUT, 'body.version', 1
    EXEC sp_OAMethod @request, 'UpdateString', @success OUT, 'body.content[0].type', 'paragraph'
    EXEC sp_OAMethod @request, 'UpdateString', @success OUT, 'body.content[0].content[0].type', 'text'
    EXEC sp_OAMethod @request, 'UpdateString', @success OUT, 'body.content[0].content[0].text', 'A comment added through the REST API.'
    EXEC sp_OAMethod @url, 'SetString', @success OUT, @baseUrl
    EXEC sp_OAMethod @url, 'Append', @success OUT, '/rest/api/3/issue/{$issueKey}/comment'
    EXEC sp_OAMethod @url, 'GetAsString', @sTmp0 OUT
    EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', @sTmp0, @request, 'application/json', @resp
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN

        PRINT 'No HTTP response was received. Check connectivity and TLS settings.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @url
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @reply
        EXEC @hr = sp_OADestroy @request
        RETURN
      END
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp1 OUT
    IF (@success <> 1) or (@iTmp0 < 200) or (@iTmp1 >= 300)
      BEGIN

        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        PRINT 'Request failed. HTTP ' + @iTmp0
        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        IF @iTmp0 = 429
          BEGIN

            EXEC sp_OAMethod @resp, 'GetHeaderField', @sTmp0 OUT, 'Retry-After'
            PRINT 'Rate limited. Retry-After: ' + @sTmp0
          END
        --  Inspect this response locally; it can contain private Jira data.
        EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @url
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @reply
        EXEC @hr = sp_OADestroy @request
        RETURN
      END
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    EXEC sp_OAMethod @reply, 'Load', @success OUT, @sTmp0
    IF @success <> 1
      BEGIN

        PRINT 'Expected a JSON object in the response.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @url
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @reply
        EXEC @hr = sp_OADestroy @request
        RETURN
      END

    EXEC sp_OAMethod @reply, 'StringOf', @sTmp0 OUT, 'id'
    PRINT 'Created comment ID: ' + @sTmp0
    DECLARE @body int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @body OUT

    DECLARE @pageStart int
    SELECT @pageStart = 0
    DECLARE @pageMore int
    SELECT @pageMore = 1
    DECLARE @pageOffset int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @pageOffset OUT

    WHILE (@pageMore = 1)
      BEGIN
        EXEC sp_OAMethod @pageOffset, 'Clear', NULL
        EXEC sp_OAMethod @pageOffset, 'AppendInt', @success OUT, @pageStart
        EXEC sp_OAMethod @pageOffset, 'GetAsString', @sTmp0 OUT
        EXEC sp_OAMethod @http, 'SetUrlVar', @success OUT, 'offset', @sTmp0
        EXEC sp_OAMethod @url, 'SetString', @success OUT, @baseUrl
        EXEC sp_OAMethod @url, 'Append', @success OUT, '/rest/api/3/issue/{$issueKey}/comment?startAt={$offset}&maxResults=50'
        EXEC sp_OAMethod @url, 'GetAsString', @sTmp0 OUT
        EXEC sp_OAMethod @http, 'HttpNoBody', @success OUT, 'GET', @sTmp0, @resp
        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        IF @iTmp0 = 0
          BEGIN

            PRINT 'No HTTP response was received. Check connectivity and TLS settings.'
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @url
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @reply
            EXEC @hr = sp_OADestroy @request
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @pageOffset
            RETURN
          END
        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp1 OUT
        IF (@success <> 1) or (@iTmp0 < 200) or (@iTmp1 >= 300)
          BEGIN

            EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
            PRINT 'Request failed. HTTP ' + @iTmp0
            EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
            IF @iTmp0 = 429
              BEGIN

                EXEC sp_OAMethod @resp, 'GetHeaderField', @sTmp0 OUT, 'Retry-After'
                PRINT 'Rate limited. Retry-After: ' + @sTmp0
              END
            --  Inspect this response locally; it can contain private Jira data.
            EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
            PRINT @sTmp0
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @url
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @reply
            EXEC @hr = sp_OADestroy @request
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @pageOffset
            RETURN
          END
        EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
        EXEC sp_OAMethod @reply, 'Load', @success OUT, @sTmp0
        IF @success <> 1
          BEGIN

            PRINT 'Expected a JSON object in the response.'
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @url
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @reply
            EXEC @hr = sp_OADestroy @request
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @pageOffset
            RETURN
          END
        EXEC sp_OAMethod @reply, 'JsonTypeOf', @iTmp0 OUT, 'comments'
        IF @iTmp0 <> 4
          BEGIN

            PRINT 'Expected an array: comments'
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @url
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @reply
            EXEC @hr = sp_OADestroy @request
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @pageOffset
            RETURN
          END
        DECLARE @pageCount int
        EXEC sp_OAMethod @reply, 'SizeOfArray', @pageCount OUT, 'comments'
        DECLARE @pageIndex int
        SELECT @pageIndex = 0
        WHILE (@pageIndex < @pageCount)
          BEGIN
            EXEC sp_OASetProperty @reply, 'I', @pageIndex

            EXEC sp_OAMethod @reply, 'StringOf', @sTmp0 OUT, 'comments[i].id'

            EXEC sp_OAMethod @reply, 'StringOf', @sTmp1 OUT, 'comments[i].author.displayName'
            PRINT 'Comment ' + @sTmp0 + ' by ' + @sTmp1
            EXEC sp_OAMethod @reply, 'ObjectOf', @body OUT, 'comments[i].body'
            EXEC sp_OAGetProperty @reply, 'LastMethodSuccess', @iTmp0 OUT
            IF @iTmp0 = 1
              BEGIN
                EXEC sp_OAMethod @body, 'Emit', @sTmp0 OUT
                PRINT @sTmp0
                EXEC @hr = sp_OADestroy @body

              END
            SELECT @pageIndex = @pageIndex + 1
          END
        SELECT @pageStart = @pageStart + @pageCount
        EXEC sp_OAMethod @reply, 'IntOf', @iTmp0 OUT, 'total'
        SELECT @pageMore = (@pageStart < @iTmp0)
        IF (@pageMore = 1) and (@pageCount = 0)
          BEGIN

            PRINT 'Pagination made no progress; stopping.'
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @url
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @reply
            EXEC @hr = sp_OADestroy @request
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @pageOffset
            RETURN
          END
      END

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @url
    EXEC @hr = sp_OADestroy @resp
    EXEC @hr = sp_OADestroy @reply
    EXEC @hr = sp_OADestroy @request
    EXEC @hr = sp_OADestroy @body
    EXEC @hr = sp_OADestroy @pageOffset


END
GO