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

Add feature flags to perms syncer context#56492

Merged
pjlast merged 10 commits into
mainfrom
pjlast/add-feature-flags-to-context-gitlab
Sep 11, 2023
Merged

Add feature flags to perms syncer context#56492
pjlast merged 10 commits into
mainfrom
pjlast/add-feature-flags-to-context-gitlab

Conversation

@pjlast

@pjlast pjlast commented Sep 11, 2023

Copy link
Copy Markdown
Contributor

The FetchUserPermissions code for GitLab never had the feature flags added to the context, so the feature flag was not working as intended.

Loom:
https://www.loom.com/share/646c86923b7f4d70ad6e881b67794f6a

Test plan

Tests passing. Added GitLab integration tests to test feature flag.

@sourcegraph-bot

sourcegraph-bot commented Sep 11, 2023

Copy link
Copy Markdown
Contributor

Codenotify: Notifying subscribers in CODENOTIFY files for diff edd2096...c879393.

Notify File(s)
@unknwon cmd/repo-updater/internal/authz/BUILD.bazel
cmd/repo-updater/internal/authz/integration_test.go
cmd/repo-updater/internal/authz/perms_syncer.go
cmd/repo-updater/internal/authz/testdata/vcr/TestIntegration_GitLabPermissions/test_gitLabProjectVisibilityExperimental_feature_flag.yaml
internal/authz/providers/gitlab/authz.go
internal/authz/providers/gitlab/authz.go
internal/authz/providers/gitlab/oauth.go
internal/authz/providers/gitlab/oauth.go

@pjlast pjlast requested a review from a team September 11, 2023 09:29

@mrnugget mrnugget left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks! I think the test could be a bit smaller & cleaner

Comment thread CHANGELOG.md Outdated

### Fixed

- Fixed an issue where the "gitLabProjectVisibilityExperimental" feature flag would not be respected by the permissions syncer [#56492](https://github.com/sourcegraph/sourcegraph/pull/56492)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you also add what the non-respecting-of-feature-flag lead to and how the behaviour is different now?

Comment on lines +510 to +511
CreatedAt: timeutil.Now(),
Config: extsvc.NewUnencryptedConfig(`{"url": "https://gitlab.sgdev.org", "authorization": {"identityProvider": {"type": "oauth"}}, "token": "abc", "projectQuery": [ "projects?membership=true&archived=no" ],}`),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do you need all of this data? At least CreatedAt will be set by the Upsert further down, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The rest of the data is required for the integration test. I removed the CreatedAt, and then I factored out the test setup and made an assert function like you suggested. No reason to re-do the entire DB.

// https://gitlab.sgdev.org, then check if permissions are correctly granted for the test
// user "sourcegraph-vcr".
t.Run("user-centric", func(t *testing.T) {
t.Run("featureflag-enabled", func(t *testing.T) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's not super clear to me what the difference is between these two tests. Is it just the one FeatureFlags().CreateBool() line? If so, can you maybe create a helper like

assertUserPermissions := func(t *testing.T, wantIDs []int32) {
 /// ....
}

That does the whole reposSTore, provider setup etc. and then call it in each sub-test?

Or: why do we even need to duplicate it? Couldn't we just pull the setup with the external service etc. out? Do you need a fresh DB for both tests? Or could you also sync the permissions once, turn feature flag on, sync again, check that correct permissions are in DB?

@mrnugget mrnugget left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good, but I think the tests could be cleaner still.

// This integration tests performs a user-centric permissions syncing against
// https://gitlab.sgdev.org, then check if permissions are correctly granted for the test
// user "sourcegraph-vcr".
t.Run("test feature flag", func(t *testing.T) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
t.Run("test feature flag", func(t *testing.T) {
t.Run("test gitLabProjectVisibilityExperimental feature flag", func(t *testing.T) {

Comment on lines +632 to +641
t.Run("feature-flag disabled", func(t *testing.T) {
assertUserPermissions(t, []int32{2})
})

t.Run("feature-flag enabled", func(t *testing.T) {
_, err = testDB.FeatureFlags().CreateBool(ctx, "gitLabProjectVisibilityExperimental", true)
require.NoError(t, err, "feature flag creation failed")

assertUserPermissions(t, []int32{1, 2})
})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd personally remove the t.Run here. t.Run always suggests to me that these tests are independent and can be run independently. But in this case, if the 2nd test runs before the first one, the first one will fail, because "global" state has been modified.

So I'd personally do something like this:

Suggested change
t.Run("feature-flag disabled", func(t *testing.T) {
assertUserPermissions(t, []int32{2})
})
t.Run("feature-flag enabled", func(t *testing.T) {
_, err = testDB.FeatureFlags().CreateBool(ctx, "gitLabProjectVisibilityExperimental", true)
require.NoError(t, err, "feature flag creation failed")
assertUserPermissions(t, []int32{1, 2})
})
// Without the featuer flag, we should only get one repository back:
assertUserPermissions(t, []int32{2})
// But with the feature flag enabled, user should have access to both of them:
_, err = testDB.FeatureFlags().CreateBool(ctx, "gitLabProjectVisibilityExperimental", true)
require.NoError(t, err, "feature flag creation failed")
assertUserPermissions(t, []int32{1, 2})

That makes it clearer, IMHO, that the tests are dependent on the same state and that the order is important.

@pjlast pjlast merged commit a29b68f into main Sep 11, 2023
@pjlast pjlast deleted the pjlast/add-feature-flags-to-context-gitlab branch September 11, 2023 11:03
sourcegraph-release-bot pushed a commit that referenced this pull request Sep 11, 2023
keegancsmith pushed a commit that referenced this pull request Sep 11, 2023
Add feature flags to perms syncer context (#56492)

(cherry picked from commit a29b68f)

Co-authored-by: Petri-Johan Last <petri.last@sourcegraph.com>
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