{
  "$schema": "./.schemas/page.schema.json",
  "title": "Repository",
  "icon": "si:gitea",
  "sidebar": false,
  "params": {
    "owner": {
      "example": "harbor-labs",
      "pattern": "[A-Za-z0-9][A-Za-z0-9._-]{0,39}"
    },
    "repo": {
      "example": "tidewater-api",
      "pattern": "[A-Za-z0-9._-]{1,100}"
    }
  },
  "title_from": {
    "ref": "repo",
    "value": "repo.full_name"
  },
  "sources": {
    "repo": {
      "url": "http://gitea.local:3000",
      "secret": "gitea",
      "every": "2m",
      "requests": {
        "repo": "/api/v1/repos/{param.owner}/{param.repo}",
        "release": {
          "path": "/api/v1/repos/{owner.login}/{param.repo}/releases?draft=false&pre-release=false&limit=1",
          "each_row": {
            "from": "repo",
            "key": "id",
            "max": 1
          }
        }
      }
    },
    "prs": {
      "url": "http://gitea.local:3000",
      "secret": "gitea",
      "every": "2m",
      "requests": {
        "repo": "/api/v1/repos/{param.owner}/{param.repo}",
        "prs": "/api/v1/repos/{param.owner}/{param.repo}/pulls?state=open&sort=recentupdate&limit=7"
      },
      "derive": [
        {
          "name": "shown",
          "value": {
            "count": "prs"
          }
        }
      ]
    },
    "ci": {
      "url": "http://gitea.local:3000",
      "secret": "gitea",
      "every": "2m",
      "requests": {
        "repo": "/api/v1/repos/{param.owner}/{param.repo}",
        "ci": {
          "path": "/api/v1/repos/{param.owner}/{param.repo}/commits/{default_branch}/status?limit=50",
          "each_row": {
            "from": "repo",
            "key": "id",
            "max": 1,
            "pick": [
              "state",
              "total_count"
            ]
          }
        }
      }
    },
    "runs": {
      "url": "http://gitea.local:3000",
      "secret": "gitea",
      "every": "2m",
      "requests": {
        "repo": "/api/v1/repos/{param.owner}/{param.repo}",
        "runs": "/api/v1/repos/{param.owner}/{param.repo}/actions/runs?limit=10",
        "main": {
          "path": "/api/v1/repos/{param.owner}/{param.repo}/actions/runs?branch={default_branch}&limit=1",
          "each_row": {
            "from": "repo",
            "key": "id",
            "max": 1
          }
        }
      },
      "derive": [
        {
          "name": "failed_run_id",
          "value": {
            "first": "id",
            "of": "main[0].answer.workflow_runs",
            "where": {
              "all": [
                {
                  "path": "conclusion",
                  "equals": "failure"
                },
                {
                  "path": "head_branch",
                  "equals": {
                    "answer": "repo.default_branch"
                  }
                }
              ]
            }
          }
        },
        {
          "name": "failed_run_title",
          "value": {
            "first": "display_title",
            "of": "main[0].answer.workflow_runs",
            "where": {
              "path": "conclusion",
              "equals": "failure"
            }
          }
        },
        {
          "name": "failed_run_number",
          "value": {
            "first": "run_number",
            "of": "main[0].answer.workflow_runs",
            "where": {
              "path": "conclusion",
              "equals": "failure"
            }
          }
        },
        {
          "name": "run_rows",
          "value": {
            "each": "runs.workflow_runs",
            "limit": 10,
            "fields": {
              "title": "display_title",
              "url": "html_url",
              "word": {
                "if": {
                  "path": "status",
                  "equals": "completed"
                },
                "then": {
                  "path": "conclusion",
                  "values": {
                    "success": "Passed",
                    "failure": "Failed",
                    "cancelled": "Cancelled",
                    "skipped": "Skipped",
                    "neutral": "Neutral"
                  },
                  "default": "Finished"
                },
                "else": {
                  "path": "status",
                  "values": {
                    "in_progress": "Running",
                    "queued": "Queued",
                    "waiting": "Waiting",
                    "pending": "Pending",
                    "requested": "Requested",
                    "blocked": "Blocked"
                  },
                  "default": "Running"
                }
              },
              "at": {
                "if": {
                  "path": "created_at",
                  "exists": true
                },
                "then": {
                  "path": "created_at"
                },
                "else": {
                  "if": {
                    "value": {
                      "since": "started_at",
                      "default": 2000000000.0
                    },
                    "lt": 1000000000.0
                  },
                  "then": {
                    "path": "started_at"
                  }
                }
              },
              "branch": {
                "if": {
                  "any": [
                    {
                      "path": "head_branch",
                      "exists": false
                    },
                    {
                      "path": "head_branch",
                      "equals": ""
                    }
                  ]
                },
                "then": {
                  "path": "event",
                  "values": {
                    "pull_request": "Pull request",
                    "pull_request_target": "Pull request",
                    "schedule": "Scheduled",
                    "workflow_dispatch": "Run by hand"
                  },
                  "default": {
                    "template": "{event}"
                  }
                },
                "else": {
                  "template": "{head_branch}"
                }
              }
            }
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "ci",
      "type": "status",
      "placement": "page_info",
      "width": "full",
      "label": "CI",
      "state": "unknown",
      "source": {
        "ref": "ci",
        "map": {
          "label": {
            "template": "CI on {repo.default_branch}"
          },
          "state": {
            "if": {
              "path": "repo.empty",
              "equals": true
            },
            "then": {
              "template": "off"
            },
            "else": {
              "if": {
                "path": "ci[0].answer.total_count",
                "exists": false
              },
              "then": {
                "template": "unknown"
              },
              "else": {
                "if": {
                  "path": "ci[0].answer.total_count",
                  "equals": 0
                },
                "then": {
                  "template": "off"
                },
                "else": {
                  "path": "ci[0].answer.state",
                  "values": {
                    "success": "ok",
                    "failure": "critical",
                    "error": "critical",
                    "pending": "warning",
                    "warning": "warning",
                    "skipped": "off"
                  },
                  "default": "unknown"
                }
              }
            }
          },
          "detail": {
            "if": {
              "path": "repo.empty",
              "equals": true
            },
            "then": {
              "template": "Empty"
            },
            "else": {
              "if": {
                "path": "ci[0].answer.total_count",
                "exists": false
              },
              "then": {
                "template": "CI not read"
              },
              "else": {
                "if": {
                  "path": "ci[0].answer.total_count",
                  "equals": 0
                },
                "then": {
                  "template": "No CI"
                },
                "else": {
                  "path": "ci[0].answer.state",
                  "values": {
                    "success": "Passing",
                    "failure": "Failing",
                    "error": "Failing",
                    "pending": "Pending",
                    "warning": "Warning",
                    "skipped": "Skipped"
                  },
                  "default": "CI not read"
                }
              }
            }
          },
          "url": {
            "template": "{repo.html_url}/actions"
          }
        }
      }
    },
    {
      "id": "facts",
      "type": "facts",
      "label": "Repository",
      "items": [
        {
          "key": "Open PRs",
          "value": "—"
        },
        {
          "key": "Open issues",
          "value": "—"
        },
        {
          "key": "Latest release",
          "value": "—"
        },
        {
          "key": "Stars",
          "value": "—"
        },
        {
          "key": "Forks",
          "value": "—"
        },
        {
          "key": "Updated",
          "value": "—"
        }
      ],
      "source": {
        "ref": "repo",
        "map": {
          "items[0].value": "repo.open_pr_counter",
          "items[1].value": "repo.open_issues_count",
          "items[2].value": {
            "if": {
              "path": "release[0].answer[0].tag_name",
              "exists": true
            },
            "then": {
              "template": "{release[0].answer[0].tag_name} · {release[0].answer[0].published_at|ago}"
            },
            "else": {
              "if": {
                "path": "release[0].error",
                "exists": true
              },
              "then": {
                "template": "Not read"
              },
              "else": {
                "template": "None"
              }
            }
          },
          "items[3].value": "repo.stars_count",
          "items[4].value": "repo.forks_count",
          "items[5].value": {
            "template": "{repo.updated_at|ago}"
          }
        }
      }
    },
    {
      "id": "runs",
      "type": "list",
      "label": "Workflow runs",
      "max_items": 4,
      "items": [],
      "actions_placement": "corner",
      "actions": [
        {
          "label": "Re-run failed jobs",
          "icon": "play.circle",
          "method": "POST",
          "path": "/api/v1/repos/{param.owner}/{param.repo}/actions/runs/{answer.failed_run_id}/rerun-failed-jobs",
          "confirm": {
            "message": "Re-run the failed jobs of “{answer.failed_run_title}” (run {answer.failed_run_number})?"
          },
          "error": "message",
          "when": {
            "path": "failed_run_id",
            "exists": true
          }
        }
      ],
      "source": {
        "ref": "runs",
        "empty": {
          "text": "No workflow runs.",
          "when": {
            "value": {
              "count": "runs.workflow_runs",
              "default": 0
            },
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "run_rows",
            "fields": {
              "text": "title",
              "url": "url",
              "detail": {
                "if": {
                  "path": "at",
                  "exists": true
                },
                "then": {
                  "template": "{word} · {branch} · {at|ago}"
                },
                "else": {
                  "template": "{word} · {branch}"
                }
              }
            }
          }
        }
      }
    },
    {
      "id": "prs",
      "type": "list",
      "label": "Open pull requests",
      "max_items": 7,
      "items": [],
      "source": {
        "ref": "prs",
        "empty": {
          "text": "No open pull requests.",
          "when": {
            "value": {
              "count": "prs",
              "default": 0
            },
            "equals": 0
          }
        },
        "map": {
          "label": {
            "if": {
              "value": {
                "path": "repo.open_pr_counter",
                "default": 0
              },
              "gt": {
                "path": "shown"
              }
            },
            "then": {
              "template": "{shown} of {repo.open_pr_counter} open pull requests"
            },
            "else": {
              "template": "Open pull requests"
            }
          },
          "items": {
            "each": "prs",
            "limit": 7,
            "fields": {
              "text": "title",
              "url": "html_url",
              "detail": {
                "if": {
                  "path": "draft",
                  "equals": true
                },
                "then": {
                  "template": "Draft · {user.login}"
                },
                "else": {
                  "template": "{user.login} · {updated_at|ago}"
                }
              }
            }
          }
        }
      }
    }
  ]
}
