dmitri.shuralyov.com/service/change/githubapi

Infer the current user from client.
dmitshur committed 7 years ago commit 1b2f9264f3d6e4b4792f672884d31fb824f09d43
Showing partial commit. Full Commit
Collapse all
githubapi/githubapi.go
@@ -13,43 +13,45 @@ import (
	"github.com/shurcooL/githubql"
	"github.com/shurcooL/issues"
	"github.com/shurcooL/notifications"
	"github.com/shurcooL/reactions"
	"github.com/shurcooL/users"
	ghusers "github.com/shurcooL/users/githubapi"
)

// NewService creates a GitHub-backed changes.Service using given GitHub clients.
// It uses notifications service, if not nil. At this time it infers the current user
// from the client (its authentication info), and cannot be used to serve multiple users.
func NewService(clientV3 *github.Client, clientV4 *githubql.Client, notifications notifications.ExternalService, users users.Service) changes.Service {
	s := service{
func NewService(clientV3 *github.Client, clientV4 *githubql.Client, notifications notifications.ExternalService) (changes.Service, error) {
	users, err := ghusers.NewService(clientV3)
	if err != nil {
		return nil, err
	}
	currentUser, err := users.GetAuthenticated(context.Background())
	if err != nil {
		return nil, err
	}
	return service{
		clV3:          clientV3,
		clV4:          clientV4,
		notifications: notifications,
		users:         users,
	}

	s.currentUser, s.currentUserErr = s.users.GetAuthenticated(context.TODO())

	return s
		currentUser:   currentUser,
	}, nil
}

type service struct {
	clV3 *github.Client   // GitHub REST API v3 client.
	clV4 *githubql.Client // GitHub GraphQL API v4 client.

	// notifications may be nil if there's no notifications service.
	notifications notifications.ExternalService

	users users.Service

	currentUser    users.User
	currentUserErr error
	currentUser users.User
}

// We use 0 as a special ID for the comment that is the issue description. This comment is edited differently.
const issueDescriptionCommentID uint64 = 0
// We use 0 as a special ID for the comment that is the PR description. This comment is edited differently.
const prDescriptionCommentID uint64 = 0

func (s service) List(ctx context.Context, rs string, opt changes.ListOptions) ([]changes.Change, error) {
	repo, err := ghRepoSpec(rs)
	if err != nil {
		// TODO: Map to 400 Bad Request HTTP error.
@@ -310,11 +312,11 @@ func (s service) ListComments(ctx context.Context, rs string, id uint64, opt *ch
			By: ghActor(*pr.Editor),
			At: pr.LastEditedAt.Time,
		}
	}
	comments = append(comments, issues.Comment{
		ID:        issueDescriptionCommentID,
		ID:        prDescriptionCommentID,
		User:      ghActor(pr.Author),
		CreatedAt: pr.PublishedAt.Time,
		Edited:    edited,
		Body:      string(pr.Body),
		Reactions: reactions,