Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

repo exclusion: switch to dynamic rule builder#58405

Merged
mrnugget merged 1 commit intomainfrom
mrn/refactor-repo-exclusion-to-rules
Nov 20, 2023
Merged

repo exclusion: switch to dynamic rule builder#58405
mrnugget merged 1 commit intomainfrom
mrn/refactor-repo-exclusion-to-rules

Conversation

@mrnugget
Copy link
Contributor

@mrnugget mrnugget commented Nov 17, 2023

This is a follow-up to https://github.com/sourcegraph/sourcegraph/pull/58377 and changes how exclude works by AND'ing all conditions in a single rule together.

It does this mostly by refactoring the code and turning things upside down (inside out?):

  • instead of each []exclude entry in a code host connection config being turned into a separate condition, one of which had to be true
  • each entry is turned into a *rule that can have multiple conditions and all of them have to be true for a repo to be excluded.

Best explained with an example.

Old code:

var eb excludeBuilder
for _, r := range c.Exclude {
  eb.Exact(r.Name)
  eb.Exact(r.Id)
}
exclude, err := eb.Build()
if err != nil {
  return nil, err
}

// then:

exclude(r.Id) || exclude(r.Name) || exclude(r)

New code:

var ex repoExcluder
for _, r := range c.Exclude {
  ex.AddRule().
    Exact(r.Name).
    Exact(r.Id)
}
if err := ex.RuleErrors(); err != nil {
  return nil, err
}

// then:

ex.ShouldExclude(r.Id) || ex.ShouldExclude(r.Name) || ex.ShouldExclude(r)

Comparison not totally fair because stil need to add error handling for invalid patterns.

Downsides

Currently it's a lot more function calls per exclusion-check... we could probably amortize the cost but I'm not sure yet what the nicest way is.

Test plan

  • unit tests, extended

@cla-bot cla-bot bot added the cla-signed label Nov 17, 2023
@mrnugget mrnugget force-pushed the mrn/refactor-repo-exclusion-to-rules branch 2 times, most recently from e5e224d to 4316606 Compare November 20, 2023 13:08
Comment on lines 153 to 179
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wonder if it would be nice to encapsulate the excluder in a separate package, and then implement this special excludeFn in the github source file, I stumbled a bit over the fact that there's a github specific implementation in this otherwise quite generic file. Not needed for this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I keep thinking about this. I haven't solved the puzzle yet. I have an idea for all the "rules" to compile down to this, but I still need to play around with the ideas a bit.

@mrnugget mrnugget force-pushed the mrn/refactor-repo-exclusion-to-rules branch from 4316606 to e1754f8 Compare November 20, 2023 13:29
@mrnugget mrnugget marked this pull request as ready for review November 20, 2023 13:29
@mrnugget mrnugget requested a review from pjlast November 20, 2023 13:29
@mrnugget mrnugget force-pushed the mrn/refactor-repo-exclusion-to-rules branch from e1754f8 to 1f90a29 Compare November 20, 2023 13:50
@sourcegraph-bot
Copy link
Contributor

Codenotify: Notifying subscribers in CODENOTIFY files for diff 000a43e...1f90a29.

Notify File(s)
@eseliger internal/repos/awscodecommit.go
internal/repos/azuredevops.go
internal/repos/bitbucketcloud.go
internal/repos/bitbucketserver.go
internal/repos/exclude.go
internal/repos/exclude_test.go
internal/repos/github.go
internal/repos/gitlab.go
internal/repos/gitolite.go
internal/repos/other.go
internal/repos/sources_test.go

@mrnugget mrnugget merged commit a614957 into main Nov 20, 2023
@mrnugget mrnugget deleted the mrn/refactor-repo-exclusion-to-rules branch November 20, 2023 14:49
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants