{
  "$schema": "./.schemas/page.schema.json",
  "title": "Cycle",
  "icon": "square.stack.3d.up",
  "sidebar": false,
  "params": {
    "team": {
      "example": "APP",
      "pattern": "[A-Za-z0-9-]{1,64}"
    }
  },
  "title_from": {
    "ref": "cycle",
    "value": {
      "if": {
        "path": "gql.data.team.activeCycle.id",
        "exists": true
      },
      "then": {
        "template": "{gql.data.team.key} · Cycle {gql.data.team.activeCycle.number}"
      },
      "else": "gql.data.team.name"
    }
  },
  "sources": {
    "cycle": {
      "url": "https://api.linear.app",
      "secret": "linear",
      "auth": {
        "type": "header",
        "name": "Authorization"
      },
      "every": "2m",
      "requests": {
        "gql": {
          "path": "/graphql",
          "method": "POST",
          "body": {
            "query": "{ team(id: \"{param.team}\") { key name cyclesEnabled issueEstimationType activeCycle { id number startsAt endsAt scopeHistory completedScopeHistory issueCountHistory completedIssueCountHistory issues(first: 100) { pageInfo { hasNextPage } nodes { identifier title url priority state { name type position } assignee { displayName isMe } } } } } }"
          }
        }
      },
      "fail": {
        "when": {
          "path": "gql.errors",
          "exists": true
        },
        "message": "gql.errors[0].message"
      },
      "derive": [
        {
          "name": "estimates",
          "value": {
            "path": "gql.data.team.issueEstimationType",
            "default": "notUsed"
          }
        },
        {
          "name": "done",
          "value": {
            "if": {
              "path": "estimates",
              "equals": "notUsed"
            },
            "then": {
              "first": "$",
              "of": "gql.data.team.activeCycle.completedIssueCountHistory",
              "last": 1
            },
            "else": {
              "first": "$",
              "of": "gql.data.team.activeCycle.completedScopeHistory",
              "last": 1
            }
          }
        },
        {
          "name": "scope",
          "value": {
            "if": {
              "path": "estimates",
              "equals": "notUsed"
            },
            "then": {
              "first": "$",
              "of": "gql.data.team.activeCycle.issueCountHistory",
              "last": 1
            },
            "else": {
              "first": "$",
              "of": "gql.data.team.activeCycle.scopeHistory",
              "last": 1
            }
          }
        },
        {
          "name": "unit",
          "value": {
            "if": {
              "path": "estimates",
              "equals": "notUsed"
            },
            "then": {
              "template": "issues"
            },
            "else": {
              "template": "points"
            }
          }
        },
        {
          "name": "gone",
          "value": {
            "divide": [
              {
                "since": "gql.data.team.activeCycle.startsAt"
              },
              {
                "subtract": [
                  {
                    "since": "gql.data.team.activeCycle.startsAt"
                  },
                  {
                    "since": "gql.data.team.activeCycle.endsAt"
                  }
                ]
              }
            ]
          }
        },
        {
          "name": "elapsed",
          "value": {
            "if": {
              "path": "gone",
              "gt": 1
            },
            "then": {
              "add": [
                1,
                0
              ]
            },
            "else": {
              "if": {
                "path": "gone",
                "lt": 0
              },
              "then": {
                "add": [
                  0,
                  0
                ]
              },
              "else": "gone"
            }
          }
        },
        {
          "name": "left",
          "value": {
            "subtract": [
              "scope",
              "done"
            ]
          }
        },
        {
          "name": "states",
          "value": {
            "each": "gql.data.team.activeCycle.issues.nodes",
            "group": "state.name",
            "fields": {
              "key": "key",
              "count": "count"
            }
          }
        },
        {
          "name": "issue_rows",
          "value": {
            "each": "gql.data.team.activeCycle.issues.nodes",
            "lookup": {
              "from": "states",
              "by": "key",
              "on": "state.name",
              "as": "group"
            },
            "fields": {
              "title": "title",
              "url": "url",
              "who": {
                "if": {
                  "path": "assignee.isMe",
                  "equals": true
                },
                "then": {
                  "template": "You"
                },
                "else": {
                  "if": {
                    "path": "assignee.displayName",
                    "exists": true
                  },
                  "then": "assignee.displayName",
                  "else": {
                    "template": "Unassigned"
                  }
                }
              },
              "key": "identifier",
              "section": {
                "if": {
                  "path": "group.count",
                  "equals": 1
                },
                "then": {
                  "template": "Others"
                },
                "else": "state.name"
              },
              "order": {
                "if": {
                  "path": "group.count",
                  "equals": 1
                },
                "then": {
                  "add": [
                    9,
                    0
                  ]
                },
                "else": {
                  "path": "state.type",
                  "values": {
                    "started": 1,
                    "unstarted": 2,
                    "triage": 3,
                    "backlog": 4,
                    "completed": 5,
                    "canceled": 6,
                    "duplicate": 7
                  },
                  "default": 8
                }
              },
              "position": "state.position",
              "priority": {
                "path": "priority",
                "values": {
                  "1": 1,
                  "2": 2,
                  "3": 3,
                  "4": 4,
                  "0": 5
                },
                "default": 5
              }
            }
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "progress",
      "type": "pacing",
      "label": "Completed",
      "value": 0,
      "target": 1,
      "source": {
        "ref": "cycle",
        "when": {
          "all": [
            {
              "path": "gql.data.team.activeCycle.id",
              "exists": true
            },
            {
              "path": "scope",
              "gt": 0
            }
          ]
        },
        "map": {
          "value": "done",
          "target": "scope",
          "unit": "unit",
          "elapsed": "elapsed",
          "detail": {
            "template": "Ends {gql.data.team.activeCycle.endsAt|day} · {left} {unit} to go"
          }
        }
      }
    },
    {
      "id": "no-cycle",
      "type": "text",
      "body": "Cycles are off for this team.",
      "source": {
        "ref": "cycle",
        "when": {
          "path": "gql.data.team.cyclesEnabled",
          "equals": false
        }
      }
    },
    {
      "id": "between",
      "type": "text",
      "body": "No cycle is running for this team right now.",
      "source": {
        "ref": "cycle",
        "when": {
          "all": [
            {
              "path": "gql.data.team.cyclesEnabled",
              "equals": true
            },
            {
              "path": "gql.data.team.activeCycle.id",
              "exists": false
            }
          ]
        }
      }
    },
    {
      "id": "issues",
      "type": "list",
      "label": "Issues",
      "max_items": 15,
      "items": [],
      "source": {
        "ref": "cycle",
        "when": {
          "path": "gql.data.team.activeCycle.id",
          "exists": true
        },
        "empty": {
          "text": "No issues in this cycle yet.",
          "when": {
            "value": {
              "count": "issue_rows",
              "default": 0
            },
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "issue_rows",
            "sort": [
              "order",
              "position",
              "priority"
            ],
            "fields": {
              "text": "title",
              "url": "url",
              "detail": "who",
              "section": "section",
              "key": "key"
            }
          }
        }
      }
    },
    {
      "id": "more",
      "type": "text",
      "tone": "info",
      "body": "This cycle has more than 100 issues; Legato shows the first 100. Open [Linear](https://linear.app) for the rest.",
      "source": {
        "ref": "cycle",
        "when": {
          "value": {
            "path": "gql.data.team.activeCycle.issues.pageInfo.hasNextPage",
            "default": false
          },
          "equals": true
        }
      }
    }
  ]
}
