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

gerritapi: Rename local change variables to chg.

This is to avoid naming conflicts for upcoming package rename from
changes to change.
dmitshur committed 6 years ago commit de318893dd9f71032b44a28364d85abdad4ba88b
Collapse all
gerritapi/gerritapi.go
@@ -62,22 +62,22 @@ func (s service) List(ctx context.Context, rs string, opt changes.ListOptions) (
	})
	if err != nil {
		return nil, err
	}
	var is []changes.Change
	for _, change := range *cs {
		if change.Status == "DRAFT" {
	for _, chg := range *cs {
		if chg.Status == "DRAFT" {
			continue
		}
		is = append(is, changes.Change{
			ID:    uint64(change.Number),
			State: state(change.Status),
			Title: change.Subject,
			ID:    uint64(chg.Number),
			State: state(chg.Status),
			Title: chg.Subject,
			//Labels: labels, // TODO.
			Author:    s.gerritUser(change.Owner),
			CreatedAt: time.Time(change.Created),
			Replies:   len(change.Messages),
			Author:    s.gerritUser(chg.Owner),
			CreatedAt: time.Time(chg.Created),
			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 {
		return is[i].CreatedAt.After(is[j].CreatedAt)
@@ -89,26 +89,26 @@ func (s service) Count(_ context.Context, repo string, opt changes.ListOptions)
	// TODO.
	return 0, nil
}

func (s service) Get(ctx context.Context, _ string, id uint64) (changes.Change, error) {
	change, _, err := s.cl.Changes.GetChange(fmt.Sprint(id), &gerrit.ChangeOptions{
	chg, _, err := s.cl.Changes.GetChange(fmt.Sprint(id), &gerrit.ChangeOptions{
		AdditionalFields: []string{"DETAILED_ACCOUNTS", "ALL_REVISIONS"},
	})
	if err != nil {
		return changes.Change{}, err
	}
	if change.Status == "DRAFT" {
	if chg.Status == "DRAFT" {
		return changes.Change{}, os.ErrNotExist
	}
	return changes.Change{
		ID:        id,
		State:     state(change.Status),
		Title:     change.Subject,
		Author:    s.gerritUser(change.Owner),
		CreatedAt: time.Time(change.Created),
		Commits:   len(change.Revisions),
		State:     state(chg.Status),
		Title:     chg.Subject,
		Author:    s.gerritUser(chg.Owner),
		CreatedAt: time.Time(chg.Created),
		Commits:   len(chg.Revisions),
	}, nil
}

func state(status string) changes.State {
	switch status {
@@ -124,22 +124,22 @@ func state(status string) changes.State {
		panic("unreachable")
	}
}

func (s service) ListCommits(ctx context.Context, _ string, id uint64) ([]changes.Commit, error) {
	change, _, err := s.cl.Changes.GetChange(fmt.Sprint(id), &gerrit.ChangeOptions{
	chg, _, err := s.cl.Changes.GetChange(fmt.Sprint(id), &gerrit.ChangeOptions{
		AdditionalFields: []string{"DETAILED_ACCOUNTS", "ALL_REVISIONS"},
		//AdditionalFields: []string{"ALL_REVISIONS", "ALL_COMMITS"}, // TODO: Consider using git committer/author instead...
	})
	if err != nil {
		return nil, err
	}
	if change.Status == "DRAFT" {
	if chg.Status == "DRAFT" {
		return nil, os.ErrNotExist
	}
	commits := make([]changes.Commit, len(change.Revisions))
	for sha, r := range change.Revisions {
	commits := make([]changes.Commit, len(chg.Revisions))
	for sha, r := range chg.Revisions {
		commits[r.Number-1] = changes.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),
@@ -156,20 +156,20 @@ func (s service) GetDiff(ctx context.Context, _ string, id uint64, opt *changes.
		if err != nil {
			return nil, err
		}
		return []byte(*diff), nil
	default:
		change, _, err := s.cl.Changes.GetChange(fmt.Sprint(id), &gerrit.ChangeOptions{
		chg, _, err := s.cl.Changes.GetChange(fmt.Sprint(id), &gerrit.ChangeOptions{
			AdditionalFields: []string{"ALL_REVISIONS"},
		})
		if err != nil {
			return nil, err
		}
		if change.Status == "DRAFT" {
		if chg.Status == "DRAFT" {
			return nil, os.ErrNotExist
		}
		r, ok := change.Revisions[opt.Commit]
		r, ok := chg.Revisions[opt.Commit]
		if !ok {
			return nil, os.ErrNotExist
		}
		var base string
		switch r.Number {
@@ -244,11 +244,11 @@ func (s service) GetDiff(ctx context.Context, _ string, id uint64, opt *changes.
}

func (s service) ListTimeline(ctx context.Context, _ string, id uint64, opt *changes.ListTimelineOptions) ([]interface{}, error) {
	// TODO: Pagination. Respect opt.Start and opt.Length, if given.

	change, _, err := s.cl.Changes.GetChangeDetail(fmt.Sprint(id), nil)
	chg, _, err := s.cl.Changes.GetChangeDetail(fmt.Sprint(id), nil)
	if err != nil {
		return nil, err
	}
	comments, _, err := s.cl.Changes.ListChangeComments(fmt.Sprint(id))
	if err != nil {
@@ -256,26 +256,26 @@ func (s service) ListTimeline(ctx context.Context, _ string, id uint64, opt *cha
	}
	var timeline []interface{}
	{
		timeline = append(timeline, changes.Comment{
			ID:        0,
			User:      s.gerritUser(change.Owner),
			CreatedAt: time.Time(change.Created),
			User:      s.gerritUser(chg.Owner),
			CreatedAt: time.Time(chg.Created),
			Body:      "", // THINK: Include commit message or no?
			Editable:  false,
		})
	}
	for idx, message := range change.Messages {
	for idx, message := range chg.Messages {
		if strings.HasPrefix(message.Tag, "autogenerated:") {
			switch message.Tag[len("autogenerated:"):] {
			case "gerrit:merged":
				timeline = append(timeline, changes.TimelineItem{
					Actor:     s.gerritUser(message.Author),
					CreatedAt: time.Time(message.Date),
					Payload: changes.MergedEvent{
						CommitID: message.Message[46:86], // TODO: Make safer.
						RefName:  change.Branch,
						RefName:  chg.Branch,
					},
				})
			}
			continue
		}