fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

Select the types of activity you want to include in your feed.

Send index status to Sourcegraph once repo is indexed (#450)

+72 -1
+21 -1
cmd/zoekt-sourcegraph-indexserver/main.go
··· 587 587 }, 588 588 } 589 589 590 - return indexStateSuccess, gitIndex(c, args, s.Sourcegraph, s.logger) 590 + err = gitIndex(c, args, s.Sourcegraph, s.logger) 591 + if err != nil { 592 + return indexStateFail, err 593 + } 594 + 595 + status := []indexStatus{{RepoID: args.RepoID, Branches: args.Branches}} 596 + if err := s.Sourcegraph.UpdateIndexStatus(status); err != nil { 597 + branches := make([]string, len(args.Branches)) 598 + for i, b := range args.Branches { 599 + branches[i] = fmt.Sprintf("%s=%s", b.Name, b.Version) 600 + } 601 + 602 + s.logger.Error("failed to update index status", 603 + sglog.String("repo", args.Name), 604 + sglog.Uint32("id", args.RepoID), 605 + sglog.Strings("branches", branches), 606 + sglog.Error(err), 607 + ) 608 + } 609 + 610 + return indexStateSuccess, nil 591 611 } 592 612 593 613 func (s *Server) indexArgs(opts IndexOptions) *indexArgs {
+51
cmd/zoekt-sourcegraph-indexserver/sg.go
··· 79 79 // rank vectors. Paths are assumed to be ordered by each pairwise component of 80 80 // the resulting vector, higher ranks coming earlier 81 81 GetDocumentRanks(ctx context.Context, repoName string) (map[string][]float64, error) 82 + 83 + // UpdateIndexStatus sends a request to Sourcegraph to confirm that the 84 + // given repositories have been indexed. 85 + UpdateIndexStatus(repositories []indexStatus) error 82 86 } 83 87 84 88 func newSourcegraphClient(rootURL *url.URL, hostname string, batchSize int) *sourcegraphClient { ··· 445 449 return data.RepoIDs, nil 446 450 } 447 451 452 + type indexStatus struct { 453 + RepoID uint32 454 + Branches []zoekt.RepositoryBranch 455 + } 456 + 457 + // UpdateIndexStatus sends a request to Sourcegraph to confirm that the given 458 + // repositories have been indexed. 459 + func (s *sourcegraphClient) UpdateIndexStatus(repositories []indexStatus) error { 460 + type updateIndexStatusRequest struct { 461 + Repositories []indexStatus 462 + } 463 + 464 + body := &updateIndexStatusRequest{Repositories: repositories} 465 + payload, err := json.Marshal(body) 466 + if err != nil { 467 + return err 468 + } 469 + 470 + u := s.Root.ResolveReference(&url.URL{Path: "/.internal/search/index-status"}) 471 + req, err := retryablehttp.NewRequest(http.MethodPost, u.String(), bytes.NewReader(payload)) 472 + if err != nil { 473 + return err 474 + } 475 + req.Header.Set("Content-Type", "application/json; charset=utf8") 476 + 477 + resp, err := s.doRequest(req) 478 + if err != nil { 479 + return err 480 + } 481 + defer resp.Body.Close() 482 + 483 + if resp.StatusCode != http.StatusOK { 484 + return fmt.Errorf("failed to update index status: status %s", resp.Status) 485 + } 486 + 487 + return nil 488 + } 489 + 448 490 // doRequest executes the provided request after adding the appropriate headers 449 491 // for interacting with a Sourcegraph instance. 450 492 func (s *sourcegraphClient) doRequest(req *retryablehttp.Request) (*http.Response, error) { ··· 723 765 }) 724 766 } 725 767 768 + func (s sourcegraphFake) UpdateIndexStatus(repositories []indexStatus) error { 769 + // noop 770 + return nil 771 + } 772 + 726 773 // fakeID returns a deterministic ID based on name. Used for fakes and tests. 727 774 func fakeID(name string) uint32 { 728 775 // magic at the end is to ensure we get a positive number when casting. ··· 746 793 func (s sourcegraphNop) GetDocumentRanks(ctx context.Context, repoName string) (map[string][]float64, error) { 747 794 return nil, nil 748 795 } 796 + 797 + func (s sourcegraphNop) UpdateIndexStatus(repositories []indexStatus) error { 798 + return nil 799 + }