Go-github: [Question] Edit an issue I just searched for

Created on 14 Jul 2019  Â·  8Comments  Â·  Source: google/go-github

I do not understand how am I supposed to edit an issue after I search for it via SearchService (client.Search.Issues(...)).

I get []Issues but then I can't really client.Issues.Edit(). Am I supposed to write my own edit function for that? E.g: ~splitting~ parsing issue.RepositoryURL and using it in the client.Issues.Edit()?


Is the issues.GetRepository() (added 2 years ago) accessor an ancient thing from GitHub API 1,2 times? I don't see how I can really use it.

All 8 comments

After you have an Issue, you can then edit it with the IssuesService.Edit call.

All you need is the owner, repo, and issue number, and then you build up an IssueRequest struct that specifies all the changes you would like to make, then finally make the call.

You don't need anything else other than the IssuesService.Edit API call if you already know the owner, repo, and issue number. For example, this issue could be edited (if you had permission) with owner=google, repo=go-github, and number=1230. Fill in the IssueRequest struct with your changes, then call IssuesService.Edit.

Please let me know if you have any other questions.

Wow, thank you for super-fast reply.

My search query actually looks like is:open is:pr org:my-org archived:false, thus I get results from all repositories within the organization my-org, but I do not know repository of a particular issue.

I read the docs of the IssueService.Edit() and currently I am parsing issue.RepositoryURL to get repository name and I don't like it. Any other hints? Or its just how it was meant - the issue editing.


Whats the accessor issues.GetRepository() (added 2 years ago) for?

All the accessors make it easier to get a value of a field (or its zero value if it is not set in the struct).
This simply makes your code a lot easier to write so that you don't have to keep checking for nil all over the place.

OK, so you are getting a response from your search query, but you are saying that you can't find the repo name or the issue number?

So going back to your first question, it looks like you are calling client.Search.Issues which returns []Issue. Looking at the struct for an issue, we have:

type Issue struct {
...
    Number           *int              `json:"number,omitempty"`
...
    Repository       *Repository       `json:"repository,omitempty"`
...
}

so you can say:

issueNumber := issue.GetNumber()

to get the issue number.

Now, let's look at the Repository struct:

// Repository represents a GitHub repository.
type Repository struct {
...
    Owner            *User            `json:"owner,omitempty"`
    Name             *string          `json:"name,omitempty"`
...
}

Owner is the organization, and Name is the repository name. So you could say:

owner := issue.Repository.GetOwner().GetName()  // I had to look up the User struct too.

and

repo := issue.Repository.GetName()

and now you have the 3 pieces of information that you need in order to call IssuesService.Edit.

Does that make sense, or am I missing something?

I think I was not clear enough, after I execute a search, the results.Issues[x] does not have *Repository struct populated it's just nil.

Let me demonstrate via the following piece:

package main

import (
    "context"
    "github.com/google/go-github/github"
    "golang.org/x/oauth2"
    "os"
)

func main() {
    ctx := context.Background()
    ts := oauth2.StaticTokenSource(
        &oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
    )
    tc := oauth2.NewClient(ctx, ts)

    client := github.NewClient(tc)

    results, _, _ := client.Search.Issues(ctx, "is:open is:issue org:google author:kyslik archived:false", nil)
    println(results.Issues[0].Repository.GetOwner().GetName())
    println(results.Issues[0].GetNumber())
}

When you run the above sample you will see what I mean, *Repository struct is not populated.

➜ export GITHUB_TOKEN=token
➜ go run main.go

1230

Oh! Now I understand the problem... sorry I missed it before. I'll get back to you after some experiments later tonight after work.

I was able to duplicate the issue of the Repository field not being populated.

So now I'm going back to the GitHub Developer API docs to see what they say (which I probably should have done first):
https://developer.github.com/v3/search/#search

The only examples I see that actually populate the Repository field are:

So it looks like you will need to manually parse the returned repo URLs if you want that information, unfortunately. Sorry.

Thank you very much for the care you put in to answering me.


Yes I've already read the v3 docs thats why I was like - hmmm getRepository() just returns nil I guess its from v2 or v1... and needed clarification.

One thing that can be very confusing in this repo is that we re-use structs like Repository all over the place, and what fields get populated totally depend on the GitHub API endpoint that fills in the data. So this is a rather common occurrence to not get back all the information you wanted even though it looks like it should be there from the Go struct. That's also why we have pointers to fields and omitempty everywhere. 😄

Was this page helpful?
0 / 5 - 0 ratings