{
  "$schema": "./.schemas/page.schema.json",
  "title": "Gitea",
  "icon": "si:gitea",
  "sources": {
    "server": {
      "url": "http://gitea.local:3000",
      "every": "1m",
      "requests": {
        "health": "/api/healthz"
      }
    },
    "version": {
      "url": "http://gitea.local:3000",
      "secret": "gitea",
      "every": "1h",
      "requests": {
        "version": "/api/v1/version"
      }
    },
    "work": {
      "url": "http://gitea.local:3000",
      "secret": "gitea",
      "every": "1m",
      "requests": {
        "paging": "/api/v1/settings/api",
        "reviews": "/api/v1/repos/issues/search?type=pulls&state=open&review_requested=true&limit=21",
        "issues": "/api/v1/repos/issues/search?type=issues&state=open&assigned=true&limit=21",
        "notes": "/api/v1/notifications?status-types=unread&limit=21"
      }
    },
    "mine": {
      "url": "http://gitea.local:3000",
      "secret": "gitea",
      "every": "2m",
      "requests": {
        "mine": "/api/v1/repos/issues/search?type=pulls&state=open&created=true&limit=11",
        "paging": "/api/v1/settings/api",
        "reviews": {
          "path": "/api/v1/repos/{repository.owner}/{repository.name}/pulls/{number}/reviews?limit=50",
          "each_row": {
            "from": "mine",
            "key": "id",
            "max": 10
          }
        },
        "pulls": {
          "path": "/api/v1/repos/{repository.owner}/{repository.name}/pulls/{number}",
          "each_row": {
            "from": "mine",
            "key": "id",
            "max": 10,
            "pick": [
              "id",
              "head",
              "base"
            ]
          }
        },
        "checks": {
          "path": "/api/v1/repos/{base.repo.owner.login}/{base.repo.name}/commits/{head.sha}/status?limit=50",
          "each_row": {
            "from": "pulls",
            "key": "id",
            "max": 10,
            "pick": [
              "state",
              "total_count"
            ]
          }
        }
      },
      "derive": [
        {
          "name": "paging_row",
          "value": {
            "each": "paging",
            "entries": true,
            "where": {
              "path": "key",
              "equals": "max_response_items"
            },
            "fields": {
              "state": {
                "template": "open"
              },
              "size": "value"
            }
          }
        },
        {
          "name": "sized",
          "value": {
            "each": "mine",
            "limit": 10,
            "lookup": {
              "from": "paging_row",
              "by": "state",
              "on": "state",
              "as": "pg"
            },
            "fields": {
              "id": "id",
              "title": "title",
              "html_url": "html_url",
              "pull_request": "pull_request",
              "repository": "repository",
              "page": {
                "path": "pg.size",
                "default": 50
              }
            }
          }
        },
        {
          "name": "reviewed",
          "value": {
            "each": "sized",
            "lookup": {
              "from": "reviews",
              "by": "key",
              "on": "id",
              "as": "rv"
            },
            "fields": {
              "id": "id",
              "title": "title",
              "html_url": "html_url",
              "draft": "pull_request.draft",
              "repo": "repository.full_name",
              "changes": {
                "count": "rv.answer",
                "where": {
                  "all": [
                    {
                      "path": "state",
                      "equals": "REQUEST_CHANGES"
                    },
                    {
                      "path": "dismissed",
                      "equals": false
                    }
                  ]
                }
              },
              "approved": {
                "count": "rv.answer",
                "where": {
                  "all": [
                    {
                      "path": "state",
                      "equals": "APPROVED"
                    },
                    {
                      "path": "dismissed",
                      "equals": false
                    },
                    {
                      "path": "stale",
                      "equals": false
                    }
                  ]
                }
              },
              "reviews": {
                "count": "rv.answer"
              },
              "page": "page"
            }
          }
        },
        {
          "name": "pulled",
          "value": {
            "each": "reviewed",
            "lookup": {
              "from": "pulls",
              "by": "key",
              "on": "id",
              "as": "pull"
            },
            "fields": {
              "title": "title",
              "html_url": "html_url",
              "draft": "draft",
              "repo": "repo",
              "changes": "changes",
              "approved": "approved",
              "reviews": "reviews",
              "page": "page",
              "pull_id": "pull.answer.id"
            }
          }
        },
        {
          "name": "rows",
          "value": {
            "each": "pulled",
            "lookup": {
              "from": "checks",
              "by": "key",
              "on": "pull_id",
              "as": "checks"
            },
            "fields": {
              "title": "title",
              "html_url": "html_url",
              "draft": "draft",
              "repo": "repo",
              "changes": "changes",
              "approved": "approved",
              "reviews": "reviews",
              "page": "page",
              "ci": {
                "path": "checks.answer.state",
                "values": {
                  "failure": "failing",
                  "error": "failing"
                },
                "default": "other"
              }
            }
          }
        }
      ]
    },
    "repos": {
      "url": "http://gitea.local:3000",
      "secret": "gitea",
      "every": "2m",
      "requests": {
        "me": "/api/v1/user",
        "paging": "/api/v1/settings/api",
        "repos": {
          "path": "/api/v1/repos/search?uid={uid}&archived=false&sort=updated&order=desc&limit=25",
          "with": {
            "uid": "me.id"
          }
        },
        "ci": {
          "path": "/api/v1/repos/{owner.login}/{name}/commits/{default_branch}/status?limit=50",
          "each_row": {
            "from": "repos.data",
            "key": "id",
            "max": 24,
            "pick": [
              "state",
              "total_count"
            ]
          }
        }
      },
      "fail": {
        "when": {
          "path": "repos.ok",
          "equals": false
        },
        "message": "repos.message"
      },
      "derive": [
        {
          "name": "rows",
          "value": {
            "each": "repos.data",
            "limit": 24,
            "lookup": {
              "from": "ci",
              "by": "key",
              "on": "id",
              "as": "ci"
            },
            "fields": {
              "name": "name",
              "owner": "owner.login",
              "updated": "updated_at",
              "state": {
                "if": {
                  "path": "empty",
                  "equals": true
                },
                "then": {
                  "template": "off"
                },
                "else": {
                  "if": {
                    "path": "ci.answer.total_count",
                    "exists": false
                  },
                  "then": {
                    "template": "unknown"
                  },
                  "else": {
                    "if": {
                      "path": "ci.answer.total_count",
                      "equals": 0
                    },
                    "then": {
                      "template": "off"
                    },
                    "else": {
                      "path": "ci.answer.state",
                      "values": {
                        "success": "ok",
                        "failure": "critical",
                        "error": "critical",
                        "pending": "warning",
                        "warning": "warning",
                        "skipped": "off"
                      },
                      "default": "unknown"
                    }
                  }
                }
              },
              "ci": {
                "if": {
                  "path": "empty",
                  "equals": true
                },
                "then": {
                  "template": "Empty"
                },
                "else": {
                  "if": {
                    "path": "ci.answer.total_count",
                    "exists": false
                  },
                  "then": {
                    "template": "CI not read"
                  },
                  "else": {
                    "if": {
                      "path": "ci.answer.total_count",
                      "equals": 0
                    },
                    "then": {
                      "template": "No CI"
                    },
                    "else": {
                      "path": "ci.answer.state",
                      "values": {
                        "success": "Passing",
                        "failure": "Failing",
                        "error": "Failing",
                        "pending": "Pending",
                        "warning": "Warning",
                        "skipped": "Skipped"
                      },
                      "default": "CI not read"
                    }
                  }
                }
              }
            }
          }
        },
        {
          "name": "shown",
          "value": {
            "count": "rows"
          }
        }
      ]
    },
    "calendar": {
      "url": "http://gitea.local:3000",
      "secret": "gitea",
      "every": "1h",
      "requests": {
        "me": "/api/v1/user",
        "heat": {
          "path": "/api/v1/users/{login}/heatmap",
          "with": {
            "login": "me.login"
          }
        }
      },
      "derive": [
        {
          "name": "days",
          "value": {
            "each": "heat",
            "by_day": {
              "at": "timestamp",
              "from": "{date.days_ago_365}",
              "through": "{date.today}"
            },
            "fields": {
              "date": "from",
              "value": {
                "sum": "contributions",
                "of": "rows"
              }
            }
          }
        },
        {
          "name": "cells",
          "value": {
            "each": "days",
            "fields": {
              "date": "date",
              "value": "value",
              "label": {
                "if": {
                  "path": "value",
                  "equals": 1
                },
                "then": {
                  "template": "1 contribution · {date|day}"
                },
                "else": {
                  "template": "{value} contributions · {date|day}"
                }
              }
            }
          }
        },
        {
          "name": "total",
          "value": {
            "sum": "value",
            "of": "days"
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "health",
      "type": "status",
      "placement": "page_info",
      "width": "full",
      "label": "Gitea",
      "state": "unknown",
      "source": {
        "ref": "server",
        "map": {
          "state": {
            "path": "health.status",
            "values": {
              "pass": "ok",
              "warn": "warning",
              "fail": "critical"
            },
            "default": "unknown"
          },
          "detail": {
            "path": "health.status",
            "values": {
              "pass": "Database and cache answering",
              "warn": "Gitea reports a warning",
              "fail": "Gitea reports a failed check"
            },
            "default": "Gitea didn't say"
          },
          "url": {
            "template": "http://gitea.local:3000"
          }
        },
        "failed": {
          "state": "critical",
          "detail": "Gitea isn't answering",
          "after": 3
        }
      }
    },
    {
      "id": "work",
      "type": "tabs",
      "same_height": true,
      "tabs": [
        {
          "label": "Reviews",
          "cards": [
            {
              "id": "reviews",
              "type": "list",
              "max_items": 7,
              "items": [],
              "source": {
                "ref": "work",
                "empty": {
                  "text": "No reviews waiting for you.",
                  "when": {
                    "value": {
                      "count": "reviews",
                      "default": 0
                    },
                    "equals": 0
                  }
                },
                "map": {
                  "items": {
                    "each": "reviews",
                    "limit": 20,
                    "fields": {
                      "text": "title",
                      "url": "html_url",
                      "detail": {
                        "if": {
                          "path": "pull_request.draft",
                          "equals": true
                        },
                        "then": {
                          "template": "{repository.full_name} · Draft"
                        },
                        "else": {
                          "template": "{repository.full_name} · {updated_at|ago}"
                        }
                      }
                    }
                  }
                }
              }
            }
          ]
        },
        {
          "label": "My PRs",
          "cards": [
            {
              "id": "mine",
              "type": "list",
              "max_items": 7,
              "items": [],
              "source": {
                "ref": "mine",
                "empty": {
                  "text": "No open pull requests.",
                  "when": {
                    "value": {
                      "count": "rows",
                      "default": 0
                    },
                    "equals": 0
                  }
                },
                "map": {
                  "items": {
                    "each": "rows",
                    "limit": 10,
                    "fields": {
                      "text": "title",
                      "url": "html_url",
                      "detail": {
                        "if": {
                          "path": "ci",
                          "equals": "failing"
                        },
                        "then": {
                          "template": "CI failing"
                        },
                        "else": {
                          "if": {
                            "path": "draft",
                            "equals": true
                          },
                          "then": {
                            "template": "Draft"
                          },
                          "else": {
                            "if": {
                              "any": [
                                {
                                  "value": {
                                    "path": "reviews",
                                    "default": 0
                                  },
                                  "gte": 50
                                },
                                {
                                  "value": {
                                    "path": "reviews",
                                    "default": 0
                                  },
                                  "gte": {
                                    "path": "page"
                                  }
                                }
                              ]
                            },
                            "then": {
                              "template": "{repo}"
                            },
                            "else": {
                              "if": {
                                "value": {
                                  "path": "changes",
                                  "default": 0
                                },
                                "gt": 0
                              },
                              "then": {
                                "template": "Changes requested"
                              },
                              "else": {
                                "if": {
                                  "value": {
                                    "path": "approved",
                                    "default": 0
                                  },
                                  "gt": 0
                                },
                                "then": {
                                  "template": "Approved"
                                },
                                "else": {
                                  "template": "{repo}"
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          ]
        },
        {
          "label": "Issues",
          "cards": [
            {
              "id": "issues",
              "type": "list",
              "max_items": 7,
              "items": [],
              "source": {
                "ref": "work",
                "empty": {
                  "text": "No issues assigned to you.",
                  "when": {
                    "value": {
                      "count": "issues",
                      "default": 0
                    },
                    "equals": 0
                  }
                },
                "map": {
                  "items": {
                    "each": "issues",
                    "limit": 20,
                    "fields": {
                      "text": "title",
                      "url": "html_url",
                      "detail": {
                        "template": "{repository.full_name} · {updated_at|ago}"
                      }
                    }
                  }
                }
              }
            }
          ]
        },
        {
          "label": "Notifications",
          "cards": [
            {
              "id": "unread",
              "type": "list",
              "max_items": 7,
              "items": [],
              "source": {
                "ref": "work",
                "empty": {
                  "text": "All caught up.",
                  "when": {
                    "value": {
                      "count": "notes",
                      "default": 0
                    },
                    "equals": 0
                  }
                },
                "map": {
                  "items": {
                    "each": "notes",
                    "limit": 20,
                    "fields": {
                      "text": "subject.title",
                      "url": "subject.html_url",
                      "detail": {
                        "if": {
                          "path": "subject.state",
                          "in": [
                            "closed",
                            "merged"
                          ]
                        },
                        "then": {
                          "template": "{repository.full_name} · {subject.state|title}"
                        },
                        "else": {
                          "path": "subject.type",
                          "values": {
                            "Issue": {
                              "template": "{repository.full_name} · Issue"
                            },
                            "Pull": {
                              "template": "{repository.full_name} · Pull request"
                            },
                            "Commit": {
                              "template": "{repository.full_name} · Commit"
                            },
                            "Repository": {
                              "template": "{repository.full_name} · Repository"
                            }
                          },
                          "default": {
                            "template": "{repository.full_name}"
                          }
                        }
                      }
                    }
                  }
                }
              },
              "actions": [
                {
                  "label": "Mark all read",
                  "icon": "checkmark.circle",
                  "method": "PUT",
                  "path": "/api/v1/notifications",
                  "error": "message",
                  "when": {
                    "value": {
                      "count": "notes",
                      "default": 0
                    },
                    "gt": 0
                  }
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "id": "more",
      "type": "text",
      "tone": "info",
      "body": "Legato reads up to 20 reviews, issues and notifications here, and Gitea may have more, at [http://gitea.local:3000](http://gitea.local:3000).",
      "source": {
        "ref": "work",
        "when": {
          "any": [
            {
              "any": [
                {
                  "value": {
                    "count": "reviews",
                    "default": 0
                  },
                  "gt": 20
                },
                {
                  "all": [
                    {
                      "path": "paging.max_response_items",
                      "exists": true
                    },
                    {
                      "value": {
                        "count": "reviews",
                        "default": 0
                      },
                      "gte": {
                        "path": "paging.max_response_items"
                      }
                    }
                  ]
                }
              ]
            },
            {
              "any": [
                {
                  "value": {
                    "count": "issues",
                    "default": 0
                  },
                  "gt": 20
                },
                {
                  "all": [
                    {
                      "path": "paging.max_response_items",
                      "exists": true
                    },
                    {
                      "value": {
                        "count": "issues",
                        "default": 0
                      },
                      "gte": {
                        "path": "paging.max_response_items"
                      }
                    }
                  ]
                }
              ]
            },
            {
              "any": [
                {
                  "value": {
                    "count": "notes",
                    "default": 0
                  },
                  "gt": 20
                },
                {
                  "all": [
                    {
                      "path": "paging.max_response_items",
                      "exists": true
                    },
                    {
                      "value": {
                        "count": "notes",
                        "default": 0
                      },
                      "gte": {
                        "path": "paging.max_response_items"
                      }
                    }
                  ]
                }
              ]
            }
          ]
        }
      }
    },
    {
      "id": "more_mine",
      "type": "text",
      "tone": "info",
      "body": "Legato reads up to 10 of your pull requests, and Gitea may have more, at [http://gitea.local:3000](http://gitea.local:3000).",
      "source": {
        "ref": "mine",
        "when": {
          "any": [
            {
              "value": {
                "count": "mine",
                "default": 0
              },
              "gt": 10
            },
            {
              "all": [
                {
                  "path": "paging.max_response_items",
                  "exists": true
                },
                {
                  "value": {
                    "count": "mine",
                    "default": 0
                  },
                  "gte": {
                    "path": "paging.max_response_items"
                  }
                }
              ]
            }
          ]
        }
      }
    },
    {
      "id": "repos",
      "type": "fleet",
      "label": "Repositories",
      "max_items": 24,
      "items": [],
      "source": {
        "ref": "repos",
        "map": {
          "label": {
            "if": {
              "any": [
                {
                  "value": {
                    "count": "repos.data",
                    "default": 0
                  },
                  "gt": 24
                },
                {
                  "all": [
                    {
                      "path": "paging.max_response_items",
                      "exists": true
                    },
                    {
                      "value": {
                        "count": "repos.data",
                        "default": 0
                      },
                      "gte": {
                        "path": "paging.max_response_items"
                      }
                    }
                  ]
                }
              ]
            },
            "then": {
              "template": "{shown} most recently updated"
            },
            "else": {
              "template": "Repositories"
            }
          },
          "items": {
            "each": "rows",
            "fields": {
              "name": "name",
              "state": "state",
              "url": {
                "template": "legato://page/gitea-repo?owner={owner}&repo={name}"
              },
              "group": "owner",
              "key": {
                "template": "{owner}/{name}"
              },
              "detail": {
                "template": "{owner}/{name} · {ci} · updated {updated|ago}"
              }
            }
          }
        }
      }
    },
    {
      "id": "contributions",
      "type": "heatmap",
      "label": "Contributions",
      "thresholds": [
        1,
        3,
        6,
        10
      ],
      "items": [],
      "source": {
        "ref": "calendar",
        "map": {
          "detail": {
            "template": "{total} in the past year"
          },
          "items": {
            "each": "cells",
            "fields": {
              "date": "date",
              "value": "value",
              "label": "label"
            }
          }
        }
      }
    },
    {
      "id": "gitea",
      "type": "facts",
      "label": "Gitea",
      "items": [
        {
          "key": "Version",
          "value": "—"
        }
      ],
      "source": {
        "ref": "version",
        "map": {
          "items[0].value": "version.version"
        }
      }
    }
  ]
}
