{
  "$schema": "./.schemas/page.schema.json",
  "title": "Repository",
  "icon": "chevron.left.forwardslash.chevron.right",
  "sidebar": false,
  "params": {
    "owner": {
      "example": "octocat",
      "pattern": "[A-Za-z0-9][A-Za-z0-9-]{0,38}"
    },
    "repo": {
      "example": "Hello-World",
      "pattern": "[A-Za-z0-9._-]{1,100}"
    }
  },
  "title_from": {
    "ref": "repo",
    "value": "repo.data.repository.nameWithOwner"
  },
  "sources": {
    "repo": {
      "url": "https://api.github.com",
      "secret": "github",
      "every": "2m",
      "headers": {
        "Accept": "application/vnd.github+json",
        "X-GitHub-Api-Version": "2022-11-28"
      },
      "requests": {
        "repo": {
          "path": "/graphql",
          "method": "POST",
          "body": {
            "query": "{ repository(owner: \"{param.owner}\", name: \"{param.repo}\") { nameWithOwner url stargazerCount forkCount pushedAt issues(states: OPEN) { totalCount } pullRequests(states: OPEN, first: 10, orderBy: {field: UPDATED_AT, direction: DESC}) { totalCount nodes { title url isDraft reviewDecision commits(last: 1) { nodes { commit { statusCheckRollup { state } } } } } } latestRelease { tagName publishedAt } defaultBranchRef { name target { ... on Commit { statusCheckRollup { state } } } } } }"
          }
        },
        "runs": {
          "path": "/repos/{param.owner}/{param.repo}/actions/runs?per_page=10"
        }
      },
      "fail": {
        "when": {
          "all": [
            {
              "path": "repo.errors",
              "exists": true
            },
            {
              "path": "repo.data.repository.nameWithOwner",
              "exists": false
            }
          ]
        },
        "message": "repo.errors[0].message"
      },
      "derive": [
        {
          "name": "default_branch",
          "value": "repo.data.repository.defaultBranchRef.name"
        },
        {
          "name": "newest_main",
          "value": {
            "first": "id",
            "of": "runs.workflow_runs",
            "where": {
              "path": "head_branch",
              "equals": {
                "answer": "default_branch"
              }
            }
          }
        },
        {
          "name": "failed_run_id",
          "value": {
            "first": "id",
            "of": "runs.workflow_runs",
            "where": {
              "all": [
                {
                  "path": "id",
                  "equals": {
                    "answer": "newest_main"
                  }
                },
                {
                  "path": "conclusion",
                  "in": [
                    "failure",
                    "timed_out"
                  ]
                }
              ]
            }
          }
        },
        {
          "name": "failed_run_name",
          "value": {
            "first": "name",
            "of": "runs.workflow_runs",
            "where": {
              "path": "id",
              "equals": {
                "answer": "failed_run_id"
              }
            }
          }
        },
        {
          "name": "failed_run_number",
          "value": {
            "first": "run_number",
            "of": "runs.workflow_runs",
            "where": {
              "path": "id",
              "equals": {
                "answer": "failed_run_id"
              }
            }
          }
        },
        {
          "name": "run_rows",
          "value": {
            "each": "runs.workflow_runs",
            "limit": 10,
            "fields": {
              "name": "name",
              "number": "run_number",
              "url": "html_url",
              "branch": "head_branch",
              "at": "created_at",
              "word": {
                "if": {
                  "path": "status",
                  "equals": "completed"
                },
                "then": {
                  "path": "conclusion",
                  "values": {
                    "success": "Passed",
                    "failure": "Failed",
                    "cancelled": "Cancelled",
                    "timed_out": "Timed out",
                    "skipped": "Skipped",
                    "action_required": "Needs approval",
                    "neutral": "Neutral",
                    "startup_failure": "Failed to start"
                  },
                  "default": "Finished"
                },
                "else": {
                  "path": "status",
                  "values": {
                    "in_progress": "Running",
                    "queued": "Queued",
                    "waiting": "Waiting",
                    "pending": "Pending",
                    "requested": "Requested"
                  },
                  "default": "Running"
                }
              }
            }
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "ci",
      "type": "status",
      "width": "full",
      "label": "CI",
      "state": "unknown",
      "actions_placement": "corner",
      "actions": [
        {
          "label": "Re-run failed jobs",
          "icon": "arrow.clockwise",
          "method": "POST",
          "path": "/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_name} #{answer.failed_run_number}?"
          },
          "error": "message",
          "when": {
            "path": "failed_run_id",
            "exists": true
          }
        }
      ],
      "source": {
        "ref": "repo",
        "map": {
          "label": {
            "template": "CI on {default_branch}"
          },
          "state": {
            "path": "repo.data.repository.defaultBranchRef.target.statusCheckRollup.state",
            "values": {
              "SUCCESS": "ok",
              "FAILURE": "critical",
              "ERROR": "critical",
              "PENDING": "warning",
              "EXPECTED": "warning"
            },
            "default": "off"
          },
          "detail": {
            "path": "repo.data.repository.defaultBranchRef.target.statusCheckRollup.state",
            "values": {
              "SUCCESS": "Passing",
              "FAILURE": "Failing",
              "ERROR": "Failing",
              "PENDING": "Running",
              "EXPECTED": "Waiting"
            },
            "default": "No CI"
          },
          "url": {
            "template": "{repo.data.repository.url}/actions"
          }
        }
      }
    },
    {
      "id": "facts",
      "type": "facts",
      "label": "Repository",
      "items": [
        {
          "key": "Stars",
          "value": "—"
        },
        {
          "key": "Open PRs",
          "value": "—"
        },
        {
          "key": "Open issues",
          "value": "—"
        },
        {
          "key": "Latest release",
          "value": "—"
        },
        {
          "key": "Forks",
          "value": "—"
        },
        {
          "key": "Last push",
          "value": "—"
        }
      ],
      "source": {
        "ref": "repo",
        "map": {
          "items[0].value": "repo.data.repository.stargazerCount",
          "items[1].value": "repo.data.repository.pullRequests.totalCount",
          "items[2].value": "repo.data.repository.issues.totalCount",
          "items[3].value": {
            "if": {
              "path": "repo.data.repository.latestRelease.tagName",
              "exists": true
            },
            "then": {
              "template": "{repo.data.repository.latestRelease.tagName} · {repo.data.repository.latestRelease.publishedAt|ago}"
            },
            "else": {
              "template": "None"
            },
            "default": "None"
          },
          "items[4].value": "repo.data.repository.forkCount",
          "items[5].value": {
            "template": "{repo.data.repository.pushedAt|ago}"
          }
        }
      }
    },
    {
      "id": "runs",
      "type": "list",
      "label": "Workflow runs",
      "max_items": 4,
      "items": [],
      "source": {
        "ref": "repo",
        "empty": {
          "text": "No workflow runs.",
          "when": {
            "value": {
              "count": "runs.workflow_runs",
              "default": 0
            },
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "run_rows",
            "fields": {
              "text": {
                "template": "{name} #{number}"
              },
              "url": "url",
              "detail": {
                "template": "{word} · {branch} · {at|ago}"
              }
            }
          }
        }
      }
    },
    {
      "id": "prs",
      "type": "list",
      "label": "Open pull requests",
      "max_items": 4,
      "items": [],
      "source": {
        "ref": "repo",
        "empty": {
          "text": "No open pull requests.",
          "when": {
            "value": {
              "count": "repo.data.repository.pullRequests.nodes",
              "default": 0
            },
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "repo.data.repository.pullRequests.nodes",
            "fields": {
              "text": "title",
              "url": "url",
              "detail": {
                "if": {
                  "path": "commits.nodes[0].commit.statusCheckRollup.state",
                  "in": [
                    "FAILURE",
                    "ERROR"
                  ]
                },
                "then": {
                  "template": "CI failing"
                },
                "else": {
                  "if": {
                    "path": "reviewDecision",
                    "equals": "CHANGES_REQUESTED"
                  },
                  "then": {
                    "template": "Changes requested"
                  },
                  "else": {
                    "if": {
                      "path": "isDraft",
                      "equals": true
                    },
                    "then": {
                      "template": "Draft"
                    },
                    "else": {
                      "if": {
                        "path": "reviewDecision",
                        "equals": "APPROVED"
                      },
                      "then": {
                        "template": "Approved"
                      },
                      "else": {
                        "template": "Open"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    {
      "id": "more",
      "type": "text",
      "tone": "info",
      "body": "",
      "source": {
        "ref": "repo",
        "when": {
          "any": [
            {
              "value": {
                "path": "runs.total_count",
                "default": 0
              },
              "gt": 10
            },
            {
              "value": {
                "path": "repo.data.repository.pullRequests.totalCount",
                "default": 0
              },
              "gt": 10
            }
          ]
        },
        "map": {
          "body": {
            "template": "This page shows the latest 10 runs and pull requests. GitHub has [every run](https://github.com/{param.owner}/{param.repo}/actions) and [every pull request](https://github.com/{param.owner}/{param.repo}/pulls)."
          }
        }
      }
    }
  ]
}
