Skip to content

Commit

Permalink
github: also pull users email
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMure committed Oct 7, 2018
1 parent 03202fe commit 7cb7994
Show file tree
Hide file tree
Showing 20 changed files with 252 additions and 48 deletions.
9 changes: 7 additions & 2 deletions bridge/github/config.go
Expand Up @@ -29,7 +29,10 @@ func (*Github) Configure(repo repository.RepoCommon) (core.Configuration, error)
conf := make(core.Configuration)

fmt.Println()
fmt.Println("git-bug will generate an access token in your Github profile. Your credential are not stored and are only used to generate the token. The token is stored in the repository git config.")
fmt.Println("git-bug will now generate an access token in your Github profile. Your credential are not stored and are only used to generate the token. The token is stored in the repository git config.")
fmt.Println()
fmt.Println("The token will have the following scopes:")
fmt.Println(" - user:email: to be able to read public-only users email")
// fmt.Println("The token will have the \"repo\" permission, giving it read/write access to your repositories and issues. There is no narrower scope available, sorry :-|")
fmt.Println()

Expand Down Expand Up @@ -120,7 +123,9 @@ func requestTokenWith2FA(note, username, password, otpCode string) (*http.Respon
Note string `json:"note"`
Fingerprint string `json:"fingerprint"`
}{
// Scopes: []string{"repo"},
// user:email is requested to be able to read public emails
// - a private email will stay private, even with this token
Scopes: []string{"user:email"},
Note: note,
Fingerprint: randomFingerprint(),
}
Expand Down
31 changes: 29 additions & 2 deletions bridge/github/import.go
Expand Up @@ -565,9 +565,29 @@ func (gi *githubImporter) makePerson(actor *actor) bug.Person {
if actor == nil {
return gi.ghost
}
var name string
var email string

switch actor.Typename {
case "User":
if actor.User.Name != nil {
name = string(*(actor.User.Name))
}
email = string(actor.User.Email)
case "Organization":
if actor.Organization.Name != nil {
name = string(*(actor.Organization.Name))
}
if actor.Organization.Email != nil {
email = string(*(actor.Organization.Email))
}
case "Bot":
}

return bug.Person{
Name: string(actor.Login),
Name: name,
Email: email,
Login: string(actor.Login),
AvatarUrl: string(actor.AvatarUrl),
}
}
Expand All @@ -584,9 +604,16 @@ func (gi *githubImporter) fetchGhost() error {
return err
}

var name string
if q.User.Name != nil {
name = string(*q.User.Name)
}

gi.ghost = bug.Person{
Name: string(q.User.Login),
Name: name,
Login: string(q.User.Login),
AvatarUrl: string(q.User.AvatarUrl),
Email: string(q.User.Email),
}

return nil
Expand Down
16 changes: 15 additions & 1 deletion bridge/github/import_query.go
Expand Up @@ -10,8 +10,17 @@ type pageInfo struct {
}

type actor struct {
Typename githubv4.String `graphql:"__typename"`
Login githubv4.String
AvatarUrl githubv4.String
User struct {
Name *githubv4.String
Email githubv4.String
} `graphql:"... on User"`
Organization struct {
Name *githubv4.String
Email *githubv4.String
} `graphql:"... on Organization"`
}

type actorEvent struct {
Expand Down Expand Up @@ -152,5 +161,10 @@ type commentEditQuery struct {
}

type userQuery struct {
User actor `graphql:"user(login: $login)"`
User struct {
Login githubv4.String
AvatarUrl githubv4.String
Name *githubv4.String
Email githubv4.String
} `graphql:"user(login: $login)"`
}
31 changes: 26 additions & 5 deletions bug/person.go
Expand Up @@ -12,6 +12,7 @@ import (
type Person struct {
Name string `json:"name"`
Email string `json:"email"`
Login string `json:"login"`
AvatarUrl string `json:"avatar_url"`
}

Expand All @@ -38,12 +39,15 @@ func GetUser(repo repository.Repo) (Person, error) {

// Match tell is the Person match the given query string
func (p Person) Match(query string) bool {
return strings.Contains(strings.ToLower(p.Name), strings.ToLower(query))
query = strings.ToLower(query)

return strings.Contains(strings.ToLower(p.Name), query) ||
strings.Contains(strings.ToLower(p.Login), query)
}

func (p Person) Validate() error {
if text.Empty(p.Name) {
return fmt.Errorf("name is not set")
if text.Empty(p.Name) && text.Empty(p.Login) {
return fmt.Errorf("either name or login should be set")
}

if strings.Contains(p.Name, "\n") {
Expand All @@ -54,6 +58,14 @@ func (p Person) Validate() error {
return fmt.Errorf("name is not fully printable")
}

if strings.Contains(p.Login, "\n") {
return fmt.Errorf("login should be a single line")
}

if !text.Safe(p.Login) {
return fmt.Errorf("login is not fully printable")
}

if strings.Contains(p.Email, "\n") {
return fmt.Errorf("email should be a single line")
}
Expand All @@ -69,6 +81,15 @@ func (p Person) Validate() error {
return nil
}

func (p Person) String() string {
return fmt.Sprintf("%s <%s>", p.Name, p.Email)
func (p Person) DisplayName() string {
switch {
case p.Name == "" && p.Login != "":
return p.Login
case p.Name != "" && p.Login == "":
return p.Name
case p.Name != "" && p.Login != "":
return fmt.Sprintf("%s (%s)", p.Name, p.Login)
}

panic("invalid person data")
}
2 changes: 1 addition & 1 deletion commands/ls.go
Expand Up @@ -59,7 +59,7 @@ func runLsBug(cmd *cobra.Command, args []string) error {

// truncate + pad if needed
titleFmt := fmt.Sprintf("%-50.50s", snapshot.Title)
authorFmt := fmt.Sprintf("%-15.15s", author.Name)
authorFmt := fmt.Sprintf("%-15.15s", author.DisplayName())

fmt.Printf("%s %s\t%s\t%s\t%s\n",
colors.Cyan(b.HumanId()),
Expand Down
4 changes: 2 additions & 2 deletions commands/show.go
Expand Up @@ -39,7 +39,7 @@ func runShowBug(cmd *cobra.Command, args []string) error {
)

fmt.Printf("%s opened this issue %s\n\n",
colors.Magenta(firstComment.Author.Name),
colors.Magenta(firstComment.Author.DisplayName()),
firstComment.FormatTimeRel(),
)

Expand All @@ -59,7 +59,7 @@ func runShowBug(cmd *cobra.Command, args []string) error {
fmt.Printf("%s#%d %s <%s>\n\n",
indent,
i,
comment.Author.Name,
comment.Author.DisplayName(),
comment.Author.Email,
)

Expand Down
11 changes: 11 additions & 0 deletions graphql/gqlgen.yml
Expand Up @@ -15,6 +15,15 @@ models:
model: github.com/MichaelMure/git-bug/bug.Comment
Person:
model: github.com/MichaelMure/git-bug/bug.Person
fields:
name:
resolver: true
email:
resolver: true
login:
resolver: true
avatarUrl:
resolver: true
Label:
model: github.com/MichaelMure/git-bug/bug.Label
Hash:
Expand All @@ -27,6 +36,8 @@ models:
model: github.com/MichaelMure/git-bug/bug.SetTitleOperation
AddCommentOperation:
model: github.com/MichaelMure/git-bug/bug.AddCommentOperation
EditCommentOperation:
model: github.com/MichaelMure/git-bug/bug.EditCommentOperation
SetStatusOperation:
model: github.com/MichaelMure/git-bug/bug.SetStatusOperation
LabelChangeOperation:
Expand Down

0 comments on commit 7cb7994

Please sign in to comment.