# Exporting issues

> Export issues from the Tidal Control API with the issues_paged query. Findings, gaps, incidents and action plans linked to controls and assets, with a script.

Source: https://tidalcontrol.com/docs/api/issues

Issues are exported using the `issues_paged` query. Issues represent findings, gaps, incidents, and action plans linked to controls and assets.

## Basic query

```graphql
query ExportIssues($first: Int, $after: String, $filter: IssueFilter) {
  issues_paged(first: $first, after: $after, filter: $filter) {
    edges {
      node {
        id
        sequenceId
        name
        description
        priority
        issueType
        notBefore
        expires
        closedDate
        reviewedDate
        createdDate
        controls {
          id
          sequenceId
          name
        }
        assets {
          id
          name
        }
        assignments {
          assignmentType
          user {
            name
            email
          }
        }
        attributes {
          key
          value
        }
      }
    }
    pageInfo {
      hasNextPage
      endCursor
    }
  }
}
```

Variables to fetch the first page:

```json
{ "first": 50, "after": null, "filter": {} }
```

## Key fields

- `sequenceId`: the human-readable number shown in the UI (e.g. I-23)
- `priority`: `LOW`, `MEDIUM`, or `HIGH`
- `issueType`: `GENERIC`, `AUDIT_FINDING`, `CONTROL_GAP`, `CONTROL_DEFICIENCY`, `ACTION_PLAN`, `IMPROVEMENT_OPPORTUNITY`, or `INCIDENT`
- `closedDate`: set when the issue is closed; `null` if still open
- `reviewedDate`: set when the issue has been reviewed

## Complete export script (Python)

This script authenticates, pages through every issue record, and writes the result to a CSV file:

```python

GRAPHQL_URL = "https://portal.tidalcontrol.com/graphql"

QUERY = """
query ExportIssues($first: Int, $after: String, $filter: IssueFilter) {
  issues_paged(first: $first, after: $after, filter: $filter) {
    edges {
      node {
        id
        sequenceId
        name
        description
        priority
        issueType
        notBefore
        expires
        closedDate
        reviewedDate
        createdDate
        controls { id sequenceId name }
        assets { id name }
        assignments { assignmentType user { name email } }
      }
    }
    pageInfo { hasNextPage endCursor }
  }
}
"""

def graphql(query, variables, access_token):
    r = requests.post(
        GRAPHQL_URL,
        json={"query": query, "variables": variables},
        headers={"Authorization": f"Bearer {access_token}"},
    )
    r.raise_for_status()
    result = r.json()
    if "errors" in result:
        raise RuntimeError(result["errors"])
    return result["data"]

def export_issues(access_token, closed=None, priority=None, issue_type=None):
    issues = []
    cursor = None
    issue_filter = {}
    if closed is not None:
        issue_filter["closed"] = closed
    if priority:
        issue_filter["priority"] = priority
    if issue_type:
        issue_filter["type"] = issue_type

    while True:
        data = graphql(
            QUERY,
            {"first": 50, "after": cursor, "filter": issue_filter},
            access_token,
        )
        page = data["issues_paged"]
        issues.extend(edge["node"] for edge in page["edges"])

        if not page["pageInfo"]["hasNextPage"]:
            break
        cursor = page["pageInfo"]["endCursor"]

    return issues

def to_csv(issues, output_path):
    if not issues:
        print("No issues found.")
        return

    with open(output_path, "w", newline="", encoding="utf-8") as f:
        writer = csv.writer(f)
        writer.writerow([
            "ID", "Sequence ID", "Name", "Description",
            "Priority", "Type", "Not Before", "Expires",
            "Created Date", "Closed Date", "Reviewed Date",
            "Linked Controls", "Linked Assets", "Owners",
        ])
        for i in issues:
            owners = [
                a["user"]["email"]
                for a in i["assignments"]
                if a["assignmentType"] == "OWNER"
            ]
            writer.writerow([
                i["id"],
                i["sequenceId"],
                i["name"],
                i.get("description", ""),
                i.get("priority", ""),
                i.get("issueType", ""),
                i.get("notBefore", ""),
                i.get("expires", ""),
                i.get("createdDate", ""),
                i.get("closedDate", "") or "",
                i.get("reviewedDate", "") or "",
                ", ".join(c["name"] for c in i["controls"]),
                ", ".join(a["name"] for a in i["assets"]),
                ", ".join(owners),
            ])

    print(f"Exported {len(issues)} issues to {output_path}")

if __name__ == "__main__":
    ACCESS_TOKEN = "eyJhbGci..."

    # Export all open high-priority issues
    issues = export_issues(ACCESS_TOKEN, closed=False, priority=["HIGH"])
    to_csv(issues, "issues_export.csv")
```

## Filtering examples

Pass a `filter` argument to narrow the export. These are the combinations asked for most often:

**Only open issues:**

```json
{ "filter": { "closed": false } }
```

**High-priority open issues:**

```json
{ "filter": { "closed": false, "priority": ["HIGH"] } }
```

**Audit findings only:**

```json
{ "filter": { "type": ["AUDIT_FINDING"] } }
```

**Issues linked to a specific control:**

```json
{ "filter": { "controls": ["control-uuid-here"] } }
```

**Overdue issues:**

```json
{ "filter": { "status": ["OVERDUE"] } }
```

## Count only

To check how many issues exist before exporting the full set:

```graphql
query {
  issues_count(filter: { closed: false })
}
```
