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

Jira Cloud: List Boards, Sprints, and Sprint Issues

See more Jira Examples

Find a board within a project by name, find a sprint on that board, and list its issues. Each list is paginated. Duplicate board or sprint names are treated as ambiguous; choose an ID explicitly if names are not unique.

Boards and sprints belong to the Jira Software REST API. A board's filter and the user's permissions affect what is visible. Sprints must be enabled for the selected board. Team-managed boards can have type simple even when sprints are enabled, so the discovery request does not restrict results to type=scrum. The final request uses the enhanced sprint-issue endpoint with token pagination, rather than the deprecated offset-based issue endpoint. OAuth integrations need the Jira Software scopes listed by these endpoints in addition to platform scopes.

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 Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    let http = CkoHttp()!
    http.requireSslCertVerify = true
    http.basicAuth = true
    //  Read these credentials from private application configuration.
    http.login = "YOUR_ATLASSIAN_EMAIL"
    http.password = "YOUR_API_TOKEN"
    var baseUrl: String? = "https://your-domain.atlassian.net"
    http.accept = "application/json"
    let url = CkoStringBuilder()!
    let resp = CkoHttpResponse()!
    let reply = CkoJsonObject()!
    var boardName: String? = "Example Board"
    var sprintName: String? = "Sprint 1"
    var boardId: String? = ""
    var sprintId: String? = ""
    var boardMatches: Int = 0
    var sprintMatches: Int = 0
    http.setUrlVar(name: "projectKey", value: "PROJECT")
    http.setUrlVar(name: "boardName", value: boardName)
    //  Team-managed boards can have type simple, including boards with sprints enabled.
    var boardsStart: Int = 0
    var boardsMore: Bool = true
    let boardsOffset = CkoStringBuilder()!
    while (boardsMore == true) {
        boardsOffset.clear()
        boardsOffset.appendInt(value: boardsStart)
        http.setUrlVar(name: "offset", value: boardsOffset.getAsString())
        url.setString(value: baseUrl)
        url.append(value: "/rest/agile/1.0/board?projectKeyOrId={$projectKey}&name={$boardName}&startAt={$offset}&maxResults=50")
        success = http.httpNoBody(verb: "GET", url: url.getAsString(), response: resp)
        if resp.statusCode.intValue == 0 {
            print("No HTTP response was received. Check connectivity and TLS settings.")
            return
        }

        if (success != true) || (resp.statusCode.intValue < 200) || (resp.statusCode.intValue >= 300) {
            print("Request failed. HTTP \(resp.statusCode.intValue)")
            if resp.statusCode.intValue == 429 {
                print("Rate limited. Retry-After: \(resp.getHeaderField(fieldName: "Retry-After")!)")
            }

            //  Inspect this response locally; it can contain private Jira data.
            print("\(resp.bodyStr!)")
            return
        }

        success = reply.load(json: resp.bodyStr)
        if success != true {
            print("Expected a JSON object in the response.")
            return
        }

        if reply.jsonType(of: "values").intValue != 4 {
            print("Expected an array: values")
            return
        }

        var boardsCount: Int = reply.size(ofArray: "values").intValue
        var boardsIndex: Int = 0
        while (boardsIndex < boardsCount) {
            reply.i = boardsIndex
            print("Board: \(reply.string(of: "values[i].name")!)  ID: \(reply.string(of: "values[i].id")!)")
            if reply.string(ofEquals: "values[i].name", value: boardName, caseSensitive: true) == true {
                boardId = reply.string(of: "values[i].id")
                boardMatches = boardMatches + 1
            }

            boardsIndex = boardsIndex + 1
        }

        boardsStart = boardsStart + boardsCount
        boardsMore = (reply.bool(of: "isLast") != true)
        if (boardsMore == true) && (boardsCount == 0) {
            print("Pagination made no progress; stopping.")
            return
        }

    }

    if boardMatches != 1 {
        print("Expected exactly one matching board in this project.")
        return
    }

    http.setUrlVar(name: "boardId", value: boardId)
    var sprintsStart: Int = 0
    var sprintsMore: Bool = true
    let sprintsOffset = CkoStringBuilder()!
    while (sprintsMore == true) {
        sprintsOffset.clear()
        sprintsOffset.appendInt(value: sprintsStart)
        http.setUrlVar(name: "offset", value: sprintsOffset.getAsString())
        url.setString(value: baseUrl)
        url.append(value: "/rest/agile/1.0/board/{$boardId}/sprint?startAt={$offset}&maxResults=50")
        success = http.httpNoBody(verb: "GET", url: url.getAsString(), response: resp)
        if resp.statusCode.intValue == 0 {
            print("No HTTP response was received. Check connectivity and TLS settings.")
            return
        }

        if (success != true) || (resp.statusCode.intValue < 200) || (resp.statusCode.intValue >= 300) {
            print("Request failed. HTTP \(resp.statusCode.intValue)")
            if resp.statusCode.intValue == 429 {
                print("Rate limited. Retry-After: \(resp.getHeaderField(fieldName: "Retry-After")!)")
            }

            //  Inspect this response locally; it can contain private Jira data.
            print("\(resp.bodyStr!)")
            return
        }

        success = reply.load(json: resp.bodyStr)
        if success != true {
            print("Expected a JSON object in the response.")
            return
        }

        if reply.jsonType(of: "values").intValue != 4 {
            print("Expected an array: values")
            return
        }

        var sprintsCount: Int = reply.size(ofArray: "values").intValue
        var sprintsIndex: Int = 0
        while (sprintsIndex < sprintsCount) {
            reply.i = sprintsIndex
            print("Sprint: \(reply.string(of: "values[i].name")!)  \(reply.string(of: "values[i].state")!)  ID: \(reply.string(of: "values[i].id")!)")
            if reply.string(ofEquals: "values[i].name", value: sprintName, caseSensitive: true) == true {
                sprintId = reply.string(of: "values[i].id")
                sprintMatches = sprintMatches + 1
            }

            sprintsIndex = sprintsIndex + 1
        }

        sprintsStart = sprintsStart + sprintsCount
        sprintsMore = (reply.bool(of: "isLast") != true)
        if (sprintsMore == true) && (sprintsCount == 0) {
            print("Pagination made no progress; stopping.")
            return
        }

    }

    if sprintMatches != 1 {
        print("Expected exactly one matching sprint.")
        return
    }

    http.setUrlVar(name: "sprintId", value: sprintId)
    var nextToken: String? = ""
    var previousToken: String? = ""
    var more: Bool = true
    while (more == true) {
        http.setUrlVar(name: "nextToken", value: nextToken)
        url.setString(value: baseUrl)
        url.append(value: "/rest/software/1.0/sprint/{$sprintId}/issue?maxResults=50&fields=summary,status")
        if nextToken != "" {
            url.append(value: "&nextPageToken={$nextToken}")
        }

        success = http.httpNoBody(verb: "GET", url: url.getAsString(), response: resp)
        if resp.statusCode.intValue == 0 {
            print("No HTTP response was received. Check connectivity and TLS settings.")
            return
        }

        if (success != true) || (resp.statusCode.intValue < 200) || (resp.statusCode.intValue >= 300) {
            print("Request failed. HTTP \(resp.statusCode.intValue)")
            if resp.statusCode.intValue == 429 {
                print("Rate limited. Retry-After: \(resp.getHeaderField(fieldName: "Retry-After")!)")
            }

            //  Inspect this response locally; it can contain private Jira data.
            print("\(resp.bodyStr!)")
            return
        }

        success = reply.load(json: resp.bodyStr)
        if success != true {
            print("Expected a JSON object in the response.")
            return
        }

        if reply.jsonType(of: "issues").intValue != 4 {
            print("Expected an array: issues")
            return
        }

        var i: Int = 0
        while (i < reply.size(ofArray: "issues").intValue) {
            reply.i = i
            print("\(reply.string(of: "issues[i].key")!)  \(reply.string(of: "issues[i].fields.summary")!)")
            i = i + 1
        }

        more = (reply.bool(of: "isLast") != true)
        if more == true {
            if (reply.jsonType(of: "nextPageToken").intValue != 1) || (reply.string(ofEquals: "nextPageToken", value: "", caseSensitive: true) == true) {
                print("Missing value: nextPageToken")
                return
            }

            nextToken = reply.string(of: "nextPageToken")
            if nextToken == previousToken {
                print("Repeated pagination token; stopping.")
                return
            }

            previousToken = nextToken
        }

    }


}