go-github icon indicating copy to clipboard operation
go-github copied to clipboard

Add a positive/negative filter for labels to github.PullRequestListOptions

Open erzz opened this issue 3 years ago • 3 comments

Currently we can only filter with State pretty much. I would like to filter by the presence or absence of a label.

As a solid example that triggers this issue I would like to get all PR's that are closed but don't yet have the "released" label

I could currently pull 1000's of closed PR's for a repo and postprocess the labels for each one..... but usually its like 1-3 PR's not yet released of that list and it would be incredibly inefficient :)

erzz avatar Sep 09 '22 15:09 erzz

If you can figure out how to do what you want using curl, then we should be able to make that possible with this GitHub v3 API Go client repo. Please provide a working curl example.

gmlewis avatar Sep 09 '22 21:09 gmlewis

Any update on this?

I have a use case for finding open PRs with the label dependencies so I can show dependabot PRs and look to build approve them.

This is my query in Github for listing all PRs: https://github.com/pulls?q=is%3Aopen+is%3Apr+archived%3Afalse+label%3Adependencies+-reviewed-by%3A%40me+

emmaLP avatar Jul 04 '23 09:07 emmaLP

I just came up with this example, and it seems to work fine:

package main

import (
	"context"
	"log"
	"net/url"

	dbg "github.com/gmlewis/go-httpdebug/httpdebug"
	"github.com/google/go-github/v53/github"
	"golang.org/x/oauth2"
)

const SECRET = "<redacted>"

func main() {
	ts := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: SECRET},
	)
	tc := oauth2.NewClient(oauth2.NoContext, ts)
	tp := dbg.New(dbg.WithTransport(tc.Transport))
	client := github.NewClient(tp.Client())
	ctx := context.Background()

	q, err := url.QueryUnescape("is%3Aopen+is%3Apr+archived%3Afalse+label%3Adependencies+-reviewed-by%3A%40me+")
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("q=%v", q)
	opts := &github.SearchOptions{TextMatch: true}
	pulls, _, err := client.Search.Issues(ctx, q, opts)
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("Total: %v", pulls.GetTotal())
	log.Printf("Incomplete Results: %v", pulls.GetIncompleteResults())

	for i, pr := range pulls.Issues {
		log.Printf("\n\nPullRequest #%v:", i+1)
		log.Printf("  URL: %v", pr.GetHTMLURL())
	}
}

Truncated Output:

go run main.go
2023/07/04 12:55:39 q=is:open is:pr archived:false label:dependencies -reviewed-by:@me 
2023/07/04 12:55:39 curl -X GET \
  https://api.github.com/search/issues?q=is%3Aopen+is%3Apr+archived%3Afalse+label%3Adependencies+-reviewed-by%3A%40me+ \
  -H 'Accept: application/vnd.github.squirrel-girl-preview, application/vnd.github.v3.text-match+json' \
  -H 'User-Agent: go-github/v53.2.0' \
  -H 'X-Github-Api-Version: 2022-11-28'
2023/07/04 12:55:42 Total: 55966141
2023/07/04 12:55:42 Incomplete Results: false
2023/07/04 12:55:42 

PullRequest #1:
2023/07/04 12:55:42   URL: https://github.com/Pocket/fxa-webhook-proxy/pull/780
2023/07/04 12:55:42 
...

When you ask "Any update on this?" what kind of answer are you looking for? I have yet to see a curl example of using the GitHub v3 API to perform some query that you would like done using this Go client library.

gmlewis avatar Jul 04 '23 16:07 gmlewis