dmitri.shuralyov.com/service/change/...

gerritapi: Update for go-gerrit API changes.

Remove commented out code. It can be added when/if needed.
dmitshur committed 6 years ago commit 9fd50fad53a60a8f07cc81abed8d3a3dc4aa770b
Collapse all
gerritapi/gerritapi.go
@@ -5,40 +5,29 @@ import (
	"context"
	"fmt"
	"os"
	"sort"
	"strings"
	"time"
	"unicode"

	"dmitri.shuralyov.com/service/change"
	"github.com/andygrunwald/go-gerrit"
	"github.com/shurcooL/users"
)

// NewService creates a Gerrit-backed issues.Service using given Gerrit client.
// client must be non-nil.
func NewService(client *gerrit.Client) change.Service {
	s := service{
	return service{
		cl:     client,
		domain: client.BaseURL().Host,
		//users: users,
	}

	//s.currentUser, s.currentUserErr = s.users.GetAuthenticatedSpec(context.TODO())

	return s
}

type service struct {
	cl     *gerrit.Client
	domain string

	//users users.Service

	//currentUser    users.UserSpec
	//currentUserErr error
}

func (s service) List(ctx context.Context, rs string, opt change.ListOptions) ([]change.Change, error) {
	project := project(rs)
	var query string
@@ -71,11 +60,11 @@ func (s service) List(ctx context.Context, rs string, opt change.ListOptions) ([
		is = append(is, change.Change{
			ID:        uint64(chg.Number),
			State:     state(chg.Status),
			Title:     chg.Subject,
			Author:    s.gerritUser(chg.Owner),
			CreatedAt: time.Time(chg.Created),
			CreatedAt: chg.Created.Time,
			Replies:   len(chg.Messages),
		})
	}
	//sort.Sort(sort.Reverse(byID(is))) // For some reason, IDs don't completely line up with created times.
	sort.Slice(is, func(i, j int) bool {
@@ -102,11 +91,11 @@ func (s service) Get(ctx context.Context, _ string, id uint64) (change.Change, e
	return change.Change{
		ID:           id,
		State:        state(chg.Status),
		Title:        chg.Subject,
		Author:       s.gerritUser(chg.Owner),
		CreatedAt:    time.Time(chg.Created),
		CreatedAt:    chg.Created.Time,
		Replies:      len(chg.Messages),
		Commits:      len(chg.Revisions),
		ChangedFiles: 0, // TODO.
	}, nil
}
@@ -142,11 +131,11 @@ func (s service) ListCommits(ctx context.Context, _ string, id uint64) ([]change
		commits[r.Number-1] = change.Commit{
			SHA:     sha,
			Message: fmt.Sprintf("Patch Set %d", r.Number),
			// TODO: r.Uploader and r.Created describe the committer, not author.
			Author:     s.gerritUser(r.Uploader),
			AuthorTime: time.Time(r.Created),
			AuthorTime: r.Created.Time,
		}
	}
	return commits, nil
}

@@ -184,11 +173,11 @@ func (s service) GetDiff(ctx context.Context, _ string, id uint64, opt *change.G
		})
		if err != nil {
			return nil, err
		}
		var sortedFiles []string
		for file := range *files {
		for file := range files {
			sortedFiles = append(sortedFiles, file)
		}
		sort.Strings(sortedFiles)
		var diff string
		for _, file := range sortedFiles {
@@ -257,21 +246,21 @@ func (s service) ListTimeline(ctx context.Context, _ string, id uint64, opt *cha
	}
	var timeline []interface{}
	timeline = append(timeline, change.Comment{ // CL description.
		ID:        "0",
		User:      s.gerritUser(chg.Owner),
		CreatedAt: time.Time(chg.Created),
		CreatedAt: chg.Created.Time,
		Body:      "", // THINK: Include commit message or no?
		Editable:  false,
	})
	for idx, message := range chg.Messages {
		if strings.HasPrefix(message.Tag, "autogenerated:") {
			switch message.Tag[len("autogenerated:"):] {
			case "gerrit:merged":
				timeline = append(timeline, change.TimelineItem{
					Actor:     s.gerritUser(message.Author),
					CreatedAt: time.Time(message.Date),
					CreatedAt: message.Date.Time,
					Payload: change.MergedEvent{
						CommitID: message.Message[46:86], // TODO: Make safer.
						RefName:  chg.Branch,
					},
				})
@@ -292,11 +281,11 @@ func (s service) ListTimeline(ctx context.Context, _ string, id uint64, opt *cha
			state = change.ChangesRequested
		}
		var cs []change.InlineComment
		for file, comments := range *comments {
			for _, c := range comments {
				if time.Time(c.Updated).Equal(time.Time(message.Date)) {
				if c.Updated.Equal(message.Date.Time) {
					cs = append(cs, change.InlineComment{
						File: file,
						Line: c.Line,
						Body: c.Message,
					})
@@ -304,11 +293,11 @@ func (s service) ListTimeline(ctx context.Context, _ string, id uint64, opt *cha
			}
		}
		timeline = append(timeline, change.Review{
			ID:        fmt.Sprint(idx), // TODO: message.ID is not uint64; e.g., "bfba753d015916303152305cee7152ea7a112fe0".
			User:      s.gerritUser(message.Author),
			CreatedAt: time.Time(message.Date),
			CreatedAt: message.Date.Time,
			State:     state,
			Body:      body,
			Editable:  false,
			Comments:  cs,
		})