|
| 1 | +package phabricator |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/prometheus/client_golang/prometheus" |
| 8 | + "github.com/prometheus/client_golang/prometheus/promauto" |
| 9 | + "github.com/sourcegraph/log" |
| 10 | + |
| 11 | + "github.com/sourcegraph/sourcegraph/internal/actor" |
| 12 | + "github.com/sourcegraph/sourcegraph/internal/conf" |
| 13 | + "github.com/sourcegraph/sourcegraph/internal/database" |
| 14 | + "github.com/sourcegraph/sourcegraph/internal/extsvc" |
| 15 | + "github.com/sourcegraph/sourcegraph/internal/extsvc/phabricator" |
| 16 | + "github.com/sourcegraph/sourcegraph/internal/goroutine" |
| 17 | + "github.com/sourcegraph/sourcegraph/internal/httpcli" |
| 18 | + "github.com/sourcegraph/sourcegraph/internal/repos" |
| 19 | + "github.com/sourcegraph/sourcegraph/internal/types" |
| 20 | + "github.com/sourcegraph/sourcegraph/lib/errors" |
| 21 | + "github.com/sourcegraph/sourcegraph/schema" |
| 22 | +) |
| 23 | + |
| 24 | +const ( |
| 25 | + tagID = "id" |
| 26 | +) |
| 27 | + |
| 28 | +var ( |
| 29 | + phabricatorUpdateTime = promauto.NewGaugeVec(prometheus.GaugeOpts{ |
| 30 | + Name: "src_repoupdater_time_last_phabricator_sync", |
| 31 | + Help: "The last time a comprehensive Phabricator sync finished", |
| 32 | + }, []string{tagID}) |
| 33 | +) |
| 34 | + |
| 35 | +// NewRepositorySyncWorker runs the worker that syncs repositories from Phabricator to Sourcegraph. |
| 36 | +func NewRepositorySyncWorker(ctx context.Context, db database.DB, logger log.Logger, s repos.Store) goroutine.BackgroundRoutine { |
| 37 | + cf := httpcli.NewExternalClientFactory( |
| 38 | + httpcli.NewLoggingMiddleware(logger), |
| 39 | + ) |
| 40 | + |
| 41 | + return goroutine.NewPeriodicGoroutine( |
| 42 | + actor.WithInternalActor(ctx), |
| 43 | + goroutine.HandlerFunc(func(ctx context.Context) error { |
| 44 | + phabs, err := s.ExternalServiceStore().List(ctx, database.ExternalServicesListOptions{ |
| 45 | + Kinds: []string{extsvc.KindPhabricator}, |
| 46 | + }) |
| 47 | + if err != nil { |
| 48 | + return errors.Wrap(err, "unable to fetch Phabricator connections") |
| 49 | + } |
| 50 | + |
| 51 | + var errs error |
| 52 | + |
| 53 | + for _, phab := range phabs { |
| 54 | + src, err := repos.NewPhabricatorSource(ctx, logger, phab, cf) |
| 55 | + if err != nil { |
| 56 | + errs = errors.Append(errs, errors.Wrap(err, "failed to instantiate PhabricatorSource")) |
| 57 | + continue |
| 58 | + } |
| 59 | + |
| 60 | + repos, err := repos.ListAll(ctx, src) |
| 61 | + if err != nil { |
| 62 | + errs = errors.Append(errs, errors.Wrap(err, "error fetching Phabricator repos")) |
| 63 | + continue |
| 64 | + } |
| 65 | + |
| 66 | + err = updatePhabRepos(ctx, db, repos) |
| 67 | + if err != nil { |
| 68 | + errs = errors.Append(errs, errors.Wrap(err, "error updating Phabricator repos")) |
| 69 | + continue |
| 70 | + } |
| 71 | + |
| 72 | + cfg, err := phab.Configuration(ctx) |
| 73 | + if err != nil { |
| 74 | + errs = errors.Append(errs, errors.Wrap(err, "failed to parse Phabricator config")) |
| 75 | + continue |
| 76 | + } |
| 77 | + |
| 78 | + phabricatorUpdateTime.WithLabelValues( |
| 79 | + cfg.(*schema.PhabricatorConnection).Url, |
| 80 | + ).Set(float64(time.Now().Unix())) |
| 81 | + } |
| 82 | + |
| 83 | + return errs |
| 84 | + }), |
| 85 | + goroutine.WithName("repo-updater.phabricator-repository-syncer"), |
| 86 | + goroutine.WithDescription("periodically syncs repositories from Phabricator to Sourcegraph"), |
| 87 | + goroutine.WithIntervalFunc(func() time.Duration { |
| 88 | + return conf.RepoListUpdateInterval() |
| 89 | + }), |
| 90 | + ) |
| 91 | +} |
| 92 | + |
| 93 | +// updatePhabRepos ensures that all provided repositories exist in the phabricator_repos table. |
| 94 | +func updatePhabRepos(ctx context.Context, db database.DB, repos []*types.Repo) error { |
| 95 | + for _, r := range repos { |
| 96 | + repo := r.Metadata.(*phabricator.Repo) |
| 97 | + _, err := db.Phabricator().CreateOrUpdate(ctx, repo.Callsign, r.Name, r.ExternalRepo.ServiceID) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + } |
| 102 | + return nil |
| 103 | +} |
0 commit comments