haskell-github / github

The github API for Haskell
https://hackage.haskell.org/package/github
BSD 3-Clause "New" or "Revised" License
411 stars 190 forks source link

searchIssuesR leads to an error from GitHub when searching by multiple conditions #482

Closed pavelkucera closed 2 years ago

pavelkucera commented 2 years ago

Hi,

I was wondering if you could help me figure out an issue where calling searchIssuesR fails when I try to search by multiple conditions (repository / state of a PR). From the outside it looks like there is an issue in how q gets encoded when passed to GitHub, but I am guessing here.

Fails: searching for open PRs in a public repository

I am trying to replicate request such as:

curl "https://api.github.com/search/issues?q=is:pr+repo:haskell-github/github+is:pr+state:open"

which succeeds (modulo rate limiting)

With code:

{-# LANGUAGE OverloadedStrings #-}

module Main where

import qualified GitHub as GH

main = do
  result <- GH.github' (GH.searchIssuesR "repo:haskell-github/github+is:pr+state:open" (GH.FetchAtLeast 1))
  print result

which fails with a 422 response from GitHub:

{"message":"Validation Failed","errors":[{"message":"The listed users and repositories cannot be searched either because the resources do not exist or you do not have permission to view them.","resource":"Search","field":"q","code":"invalid"}],"documentation_url":"https://docs.github.com/v3/search/"}

Succeeds: searching for all issues in a public repository

Interestingly, searching for all issues in a public repository succeeds.

Curl:

curl "https://api.github.com/search/issues?q=is:pr+repo:haskell-github/github"

Code:

{-# LANGUAGE OverloadedStrings #-}

module Main where

import qualified GitHub as GH

main = do
  result <- GH.github' (GH.searchIssuesR "repo:haskell-github/github" (GH.FetchAtLeast 1))
  print result

This prints issues/PRs of the repository.

How to debug this further?

Since searching just by repository works, but it stops working when I add more conditions, it leads me to believe that q doesn't get encoded properly., but it is only a guess. And I'm not quite sure how to debug this further. Would you have any advice?

Using v0.28 of GitHub, and GHC 8.10.7.

Thanks for any help!

ghost commented 2 years ago

@pavelkucera Hello!

I think replacing + with plain space should fix the issue in your case as shown here (and it did fix it in my local testing session).

A working version:

{-# LANGUAGE OverloadedStrings #-}

module Main where

import qualified GitHub as GH

main = do
  result <- GH.github' (GH.searchIssuesR "repo:haskell-github/github is:pr state:open" (GH.FetchAtLeast 1))
  print result

I also believe the existing search examples could be improved further with query that includes various search tags as mentioned in this issue.

pavelkucera commented 2 years ago

@modotte Thank you! You're right, using a space works.