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

gerritapi: pass context to Gerrit calls

Pull in a new version of go-gerrit that adds context.Context to method
calls as the first parameter, and propagate the existing context there.
dmitshur committed 2 years ago commit ce8900dcd6898d396808d962fd777dcf7be445e9
Collapse all
gerritapi/gerritapi.go
@@ -44,11 +44,11 @@ func (s service) List(ctx context.Context, repo string, opt change.ListOptions)
		// "status:closed" is equivalent to "(status:abandoned OR status:merged)".
		query = fmt.Sprintf("project:%s status:closed", project)
	case change.FilterAll:
		query = fmt.Sprintf("project:%s", project)
	}
	cs, resp, err := s.cl.Changes.QueryChanges(&gerrit.QueryChangeOptions{
	cs, resp, err := s.cl.Changes.QueryChanges(ctx, &gerrit.QueryChangeOptions{
		QueryOptions: gerrit.QueryOptions{
			Query: []string{query},
			Limit: 25,
		},
		ChangeOptions: gerrit.ChangeOptions{
@@ -95,11 +95,11 @@ func (s service) Count(_ context.Context, repo string, opt change.ListOptions) (
	return 0, nil
}

func (s service) Get(ctx context.Context, repo string, id uint64) (change.Change, error) {
	project := project(repo)
	chg, resp, err := s.cl.Changes.GetChange(fmt.Sprintf("%s~%d", project, id), &gerrit.ChangeOptions{
	chg, resp, err := s.cl.Changes.GetChange(ctx, fmt.Sprintf("%s~%d", project, id), &gerrit.ChangeOptions{
		AdditionalFields: []string{"DETAILED_ACCOUNTS", "MESSAGES", "ALL_REVISIONS"},
	})
	if err != nil {
		if resp != nil && resp.StatusCode == http.StatusNotFound {
			return change.Change{}, os.ErrNotExist
@@ -144,11 +144,11 @@ func changeState(status string) change.State {
	}
}

func (s service) ListCommits(ctx context.Context, repo string, id uint64) ([]change.Commit, error) {
	project := project(repo)
	chg, resp, err := s.cl.Changes.GetChange(fmt.Sprintf("%s~%d", project, id), &gerrit.ChangeOptions{
	chg, resp, err := s.cl.Changes.GetChange(ctx, fmt.Sprintf("%s~%d", project, 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 {
		if resp != nil && resp.StatusCode == http.StatusNotFound {
@@ -174,20 +174,20 @@ func (s service) ListCommits(ctx context.Context, repo string, id uint64) ([]cha

func (s service) GetDiff(ctx context.Context, repo string, id uint64, opt *change.GetDiffOptions) ([]byte, error) {
	project := project(repo)
	switch opt {
	case nil:
		diff, resp, err := s.cl.Changes.GetPatch(fmt.Sprintf("%s~%d", project, id), "current", nil)
		diff, resp, err := s.cl.Changes.GetPatch(ctx, fmt.Sprintf("%s~%d", project, id), "current", nil)
		if err != nil {
			if resp != nil && resp.StatusCode == http.StatusNotFound {
				return nil, os.ErrNotExist
			}
			return nil, err
		}
		return []byte(*diff), nil
	default:
		chg, resp, err := s.cl.Changes.GetChange(fmt.Sprintf("%s~%d", project, id), &gerrit.ChangeOptions{
		chg, resp, err := s.cl.Changes.GetChange(ctx, fmt.Sprintf("%s~%d", project, id), &gerrit.ChangeOptions{
			AdditionalFields: []string{"ALL_REVISIONS"},
		})
		if err != nil {
			if resp != nil && resp.StatusCode == http.StatusNotFound {
				return nil, os.ErrNotExist
@@ -206,11 +206,11 @@ func (s service) GetDiff(ctx context.Context, repo string, id uint64, opt *chang
		case 1:
			base = ""
		default:
			base = fmt.Sprint(r.Number - 1)
		}
		files, _, err := s.cl.Changes.ListFiles(fmt.Sprintf("%s~%d", project, id), opt.Commit, &gerrit.FilesOptions{
		files, _, err := s.cl.Changes.ListFiles(ctx, fmt.Sprintf("%s~%d", project, id), opt.Commit, &gerrit.FilesOptions{
			Base: base,
		})
		if err != nil {
			return nil, err
		}
@@ -219,11 +219,11 @@ func (s service) GetDiff(ctx context.Context, repo string, id uint64, opt *chang
			sortedFiles = append(sortedFiles, file)
		}
		sort.Strings(sortedFiles)
		var diff string
		for _, file := range sortedFiles {
			diffInfo, _, err := s.cl.Changes.GetDiff(fmt.Sprintf("%s~%d", project, id), opt.Commit, file, &gerrit.DiffOptions{
			diffInfo, _, err := s.cl.Changes.GetDiff(ctx, fmt.Sprintf("%s~%d", project, id), opt.Commit, file, &gerrit.DiffOptions{
				Base:    base,
				Context: "5",
			})
			if err != nil {
				return nil, err
@@ -275,24 +275,24 @@ func (s service) GetDiff(ctx context.Context, repo string, id uint64, opt *chang

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

	project := project(repo)
	chg, resp, err := s.cl.Changes.GetChangeDetail(fmt.Sprintf("%s~%d", project, id), &gerrit.ChangeOptions{
	chg, resp, err := s.cl.Changes.GetChangeDetail(ctx, fmt.Sprintf("%s~%d", project, id), &gerrit.ChangeOptions{
		AdditionalFields: []string{"ALL_REVISIONS"},
	})
	if err != nil {
		if resp != nil && resp.StatusCode == http.StatusNotFound {
			return nil, os.ErrNotExist
		}
		return nil, err
	}
	commit, _, err := s.cl.Changes.GetCommit(fmt.Sprintf("%s~%d", project, id), "current", nil)
	commit, _, err := s.cl.Changes.GetCommit(ctx, fmt.Sprintf("%s~%d", project, id), "current", nil)
	if err != nil {
		return nil, err
	}
	comments, _, err := s.cl.Changes.ListChangeComments(fmt.Sprintf("%s~%d", project, id))
	comments, _, err := s.cl.Changes.ListChangeComments(ctx, fmt.Sprintf("%s~%d", project, id))
	if err != nil {
		return nil, err
	}
	var timeline []interface{}
	timeline = append(timeline, change.Comment{ // CL description.
go.mod
@@ -1,5 +1,5 @@
module dmitri.shuralyov.com/service/change

go 1.19

require github.com/andygrunwald/go-gerrit v0.0.0-20220608063906-28cf26f84ecc
require github.com/andygrunwald/go-gerrit v0.0.0-20231116202240-40e4f30dae8f
go.sum
@@ -1,2 +1,2 @@
github.com/andygrunwald/go-gerrit v0.0.0-20220608063906-28cf26f84ecc h1:uUBFnrgkztFta0ztaovyFJWhjz8FX3pbXIgPL6D84Zc=
github.com/andygrunwald/go-gerrit v0.0.0-20220608063906-28cf26f84ecc/go.mod h1:aqcjwEnmLLSalFNYR0p2ttnEXOVVRctIzsUMHbEcruU=
github.com/andygrunwald/go-gerrit v0.0.0-20231116202240-40e4f30dae8f h1:BvVRbHp1pHrA+TSGUNBqfu+Pc0CICaD807Dq2zEOta8=
github.com/andygrunwald/go-gerrit v0.0.0-20231116202240-40e4f30dae8f/go.mod h1:SeP12EkHZxEVjuJ2HZET304NBtHGG2X6w2Gzd0QXAZw=