| 1674 | } |
| 1675 | |
| 1676 | func RepoExists(client *Client, repo ghrepo.Interface) (bool, error) { |
| 1677 | path, err := safeurl.JoinPath("repos", repo.RepoOwner(), repo.RepoName()) |
| 1678 | if err != nil { |
| 1679 | return false, err |
| 1680 | } |
| 1681 | |
| 1682 | // A HEAD request has no body to decode, so Request is used rather than REST. Existence is |
| 1683 | // decided by the status alone. |
| 1684 | resp, err := client.Request(repo.RepoHost(), http.MethodHead, path.String(), nil) |
| 1685 | if err != nil { |
| 1686 | if httpErr, ok := errors.AsType[HTTPError](err); ok && httpErr.StatusCode == http.StatusNotFound { |
| 1687 | return false, nil |
| 1688 | } |
| 1689 | return false, err |
| 1690 | } |
| 1691 | |
| 1692 | defer resp.Body.Close() |
| 1693 | |
| 1694 | // Only 200 means the repository exists. Any other success status is unexpected here and is |
| 1695 | // reported as an error rather than being taken as existence. |
| 1696 | if resp.StatusCode != http.StatusOK { |
| 1697 | return false, UnexpectedStatusError(resp) |
| 1698 | } |
| 1699 | |
| 1700 | return true, nil |
| 1701 | } |
| 1702 | |
| 1703 | // RepoLicenses fetches available repository licenses. |
| 1704 | // It uses API v3 because licenses are not supported by GraphQL. |