{
  "$schema": "./.schemas/page.schema.json",
  "title": "GitHub",
  "icon": "chevron.left.forwardslash.chevron.right",
  "sources": {
    "gh": {
      "url": "https://api.github.com",
      "secret": "github",
      "every": "1m",
      "headers": {
        "Accept": "application/vnd.github+json",
        "X-GitHub-Api-Version": "2022-11-28"
      },
      "requests": {
        "work": {
          "path": "/graphql",
          "method": "POST",
          "body": {
            "query": "{ viewer { repositories(first: 40, orderBy: {field: PUSHED_AT, direction: DESC}, ownerAffiliations: [OWNER, ORGANIZATION_MEMBER, COLLABORATOR], isArchived: false) { totalCount nodes { name nameWithOwner pushedAt owner { login } defaultBranchRef { name target { ... on Commit { statusCheckRollup { state } } } } } } } reviews: search(query: \"is:pr is:open review-requested:@me archived:false sort:updated-desc\", type: ISSUE, first: 20) { issueCount nodes { ... on PullRequest { title url isDraft updatedAt repository { nameWithOwner } } } } mine: search(query: \"is:pr is:open author:@me archived:false sort:updated-desc\", type: ISSUE, first: 20) { issueCount nodes { ... on PullRequest { title url isDraft reviewDecision updatedAt repository { nameWithOwner } commits(last: 1) { nodes { commit { statusCheckRollup { state } } } } } } } }"
          }
        },
        "notes": {
          "path": "/notifications?per_page=50"
        }
      },
      "fail": {
        "when": {
          "all": [
            {
              "path": "work.errors",
              "exists": true
            },
            {
              "any": [
                {
                  "value": {
                    "count": "work.data.viewer.repositories.nodes",
                    "default": 0
                  },
                  "equals": 0
                },
                {
                  "path": "work.data.reviews.issueCount",
                  "exists": false
                },
                {
                  "path": "work.data.mine.issueCount",
                  "exists": false
                }
              ]
            }
          ]
        },
        "message": "work.errors[0].message"
      },
      "derive": [
        {
          "name": "repos",
          "value": {
            "each": "work.data.viewer.repositories.nodes",
            "fields": {
              "name": "name",
              "owner": "owner.login",
              "pushed": "pushedAt",
              "state": {
                "path": "defaultBranchRef.target.statusCheckRollup.state",
                "values": {
                  "SUCCESS": "ok",
                  "FAILURE": "critical",
                  "ERROR": "critical",
                  "PENDING": "warning",
                  "EXPECTED": "warning"
                },
                "default": "off"
              },
              "ci": {
                "path": "defaultBranchRef.target.statusCheckRollup.state",
                "values": {
                  "SUCCESS": "Passing",
                  "FAILURE": "Failing",
                  "ERROR": "Failing",
                  "PENDING": "Running",
                  "EXPECTED": "Waiting"
                },
                "default": "No CI"
              }
            }
          }
        },
        {
          "name": "unread",
          "value": {
            "each": "notes",
            "fields": {
              "title": "subject.title",
              "url": "repository.html_url",
              "repo": "repository.full_name",
              "why": {
                "path": "reason",
                "values": {
                  "review_requested": "Review requested",
                  "mention": "Mention",
                  "team_mention": "Team mention",
                  "assign": "Assigned",
                  "author": "Your thread",
                  "comment": "Comment",
                  "ci_activity": "Workflow",
                  "state_change": "State change",
                  "subscribed": "Watching",
                  "security_alert": "Security alert",
                  "approval_requested": "Approval requested",
                  "manual": "Subscribed",
                  "invitation": "Invitation",
                  "member_feature_requested": "Feature request"
                },
                "default": {
                  "template": "{reason|title}"
                }
              }
            }
          }
        }
      ]
    },
    "calendar": {
      "url": "https://api.github.com",
      "secret": "github",
      "every": "1h",
      "requests": {
        "cal": {
          "path": "/graphql",
          "method": "POST",
          "body": {
            "query": "{ viewer { contributionsCollection { contributionCalendar { totalContributions weeks { contributionDays { date contributionCount contributionLevel } } } } } }"
          }
        }
      },
      "fail": {
        "when": {
          "all": [
            {
              "path": "cal.errors",
              "exists": true
            },
            {
              "value": {
                "count": "cal.data.viewer.contributionsCollection.contributionCalendar.weeks",
                "default": 0
              },
              "equals": 0
            }
          ]
        },
        "message": "cal.errors[0].message"
      }
    }
  },
  "cards": [
    {
      "id": "work",
      "type": "tabs",
      "same_height": true,
      "tabs": [
        {
          "label": "Reviews",
          "cards": [
            {
              "id": "reviews",
              "type": "list",
              "max_items": 7,
              "items": [],
              "source": {
                "ref": "gh",
                "empty": {
                  "text": "No reviews waiting for you.",
                  "when": {
                    "value": {
                      "count": "work.data.reviews.nodes",
                      "default": 0
                    },
                    "equals": 0
                  }
                },
                "map": {
                  "items": {
                    "each": "work.data.reviews.nodes",
                    "fields": {
                      "text": "title",
                      "url": "url",
                      "detail": {
                        "if": {
                          "path": "isDraft",
                          "equals": true
                        },
                        "then": {
                          "template": "{repository.nameWithOwner} · Draft"
                        },
                        "else": {
                          "template": "{repository.nameWithOwner} · {updatedAt|ago}"
                        }
                      }
                    }
                  }
                }
              }
            }
          ]
        },
        {
          "label": "My PRs",
          "cards": [
            {
              "id": "mine",
              "type": "list",
              "max_items": 7,
              "items": [],
              "source": {
                "ref": "gh",
                "empty": {
                  "text": "No open pull requests.",
                  "when": {
                    "value": {
                      "count": "work.data.mine.nodes",
                      "default": 0
                    },
                    "equals": 0
                  }
                },
                "map": {
                  "items": {
                    "each": "work.data.mine.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": "{repository.nameWithOwner}"
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          ]
        },
        {
          "label": "Notifications",
          "cards": [
            {
              "id": "unread",
              "type": "list",
              "max_items": 7,
              "items": [],
              "source": {
                "ref": "gh",
                "empty": {
                  "text": "All caught up.",
                  "when": {
                    "value": {
                      "count": "unread",
                      "default": 0
                    },
                    "equals": 0
                  }
                },
                "map": {
                  "items": {
                    "each": "unread",
                    "fields": {
                      "text": "title",
                      "url": "url",
                      "detail": {
                        "template": "{repo} · {why}"
                      }
                    }
                  }
                }
              },
              "actions": [
                {
                  "label": "Mark all read",
                  "icon": "checkmark.circle",
                  "method": "PUT",
                  "path": "/notifications",
                  "body": {
                    "read": true
                  },
                  "when": {
                    "value": {
                      "count": "notes",
                      "default": 0
                    },
                    "gt": 0
                  }
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "id": "more",
      "type": "text",
      "tone": "info",
      "body": "Some lists are shortened here: Legato reads your 40 most recently pushed repositories, 20 pull requests of each kind and 50 notifications. Open [GitHub](https://github.com) for the rest.",
      "source": {
        "ref": "gh",
        "when": {
          "any": [
            {
              "value": {
                "path": "work.data.viewer.repositories.totalCount",
                "default": 0
              },
              "gt": 40
            },
            {
              "value": {
                "path": "work.data.reviews.issueCount",
                "default": 0
              },
              "gt": 20
            },
            {
              "value": {
                "path": "work.data.mine.issueCount",
                "default": 0
              },
              "gt": 20
            },
            {
              "value": {
                "count": "notes",
                "default": 0
              },
              "gt": 49
            }
          ]
        }
      }
    },
    {
      "id": "repos",
      "type": "fleet",
      "label": "Repositories",
      "max_items": 24,
      "items": [],
      "source": {
        "ref": "gh",
        "map": {
          "items": {
            "each": "repos",
            "fields": {
              "name": "name",
              "state": "state",
              "url": {
                "template": "legato://page/github-repo?owner={owner}&repo={name}"
              },
              "detail": {
                "template": "{ci} · pushed {pushed|ago}"
              }
            }
          }
        }
      }
    },
    {
      "id": "contributions",
      "type": "heatmap",
      "label": "Contributions",
      "items": [],
      "source": {
        "ref": "calendar",
        "map": {
          "detail": {
            "template": "{cal.data.viewer.contributionsCollection.contributionCalendar.totalContributions} in the past year"
          },
          "items": {
            "each": "cal.data.viewer.contributionsCollection.contributionCalendar.weeks",
            "flatten": "contributionDays",
            "last": 371,
            "fields": {
              "date": "date",
              "value": "contributionCount",
              "level": {
                "path": "contributionLevel",
                "values": {
                  "NONE": 0,
                  "FIRST_QUARTILE": 1,
                  "SECOND_QUARTILE": 2,
                  "THIRD_QUARTILE": 3,
                  "FOURTH_QUARTILE": 4
                }
              },
              "label": {
                "if": {
                  "path": "contributionCount",
                  "equals": 1
                },
                "then": {
                  "template": "1 contribution · {date|day}"
                },
                "else": {
                  "template": "{contributionCount} contributions · {date|day}"
                }
              }
            }
          }
        }
      }
    }
  ]
}
