Go-github: Repo.Username() + Repo.Name() helper methods

Created on 29 Oct 2017  路  8Comments  路  Source: google/go-github

When using go-github, I'm finding myself doing this a lot:

repoNameComponents := strings.Split(*userEvent.Repo.Name, "/")
username := repoNameComponents[0]
repoName := repoNameComponents[1]

it would be nice if there were helper methods like Repo.Username() and Repo.Name() to make this easier.

Or maybe return both in a single call:

username, reponame := Repo.Details()

I'd be willing to put together a PR if this is something that would be likely to get merged.

All 8 comments

*userEvent.Repo.Name

Could you please elaborate on that? What user event has a Repo field that has a Name field? Did you mean FullName?

I know there are some places where FullName field has the value "owner/repo" (e.g., https://godoc.org/github.com/google/go-github/github#Repository.FullName), but they usually also have Owner and Name fields, such that splitting FullName isn't neccessary.

I want to understand the problem better, so we can converge on a solution which is closer to optimal.

Are you sure that code is working as you expect? I'd expect *userEvent.Repo.Name to be just the repo name, not "org/repo".

PushEvent contains a Repo *PushEventRepository field, and PushEventRepository has Owner, Name, and FullName fields. I think you should be able to use Owner and Name directly, instead of splitting FullName.

Look at the example given at https://developer.github.com/v3/activity/events/types/#pushevent:

 "repository": {
    "id": 35129377,
    "name": "public-repo",
    "full_name": "baxterthehacker/public-repo",
    "owner": {
      "name": "baxterthehacker",
      "email": "[email protected]"
    },

I'll double check this and get back to you.

Are you sure that code is working as you expect? I'd expect *userEvent.Repo.Name to be just the repo name, not "org/repo".

I double-checked by retesting this similar code and adding some logging, and I'm seeing *userEvent.Repo.Name as "org/repo", and splitting on / is working (otherwise that code would panic if it was just the repo name)

PushEvent contains a Repo *PushEventRepository field, and PushEventRepository has Owner, Name, and FullName fields. I think you should be able to use Owner and Name directly, instead of splitting FullName.

In my case, the github.UserEvent has a non-empty Repo field, however when I call ParsePayload() on that event and get back the github.PushEvent, the resulting PushEvent has a nil .Repo field.

It corresponds to a recent PushEvent I grabbed off my own public event feed:

https://gist.github.com/tleyden/826a0bea5591a6082e85d3c905db20bf

Thanks, that's very helpful.

So you're looking to access the Repo field of Event type, not of the PushEvent payload.

The JSON content of the entire event you shared makes it very clear that there's very limited information available in that repo field:

"repo": {
  "id": 98492410,
  "name": "tleyden/keynuker",
  "url": "https://api.github.com/repos/tleyden/keynuker"
},

Thanks for providing the information. I'll take some time to think about this.

@gmlewis What do you think about this issue?

IMHO we can close this issue and if it happens again we reopen it.

Closing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gmlewis picture gmlewis  路  3Comments

gmlewis picture gmlewis  路  3Comments

gmlewis picture gmlewis  路  3Comments

propertone picture propertone  路  3Comments

smola picture smola  路  3Comments