Skip to content

Commit 1ecebe6

Browse files
authored
Disable team management from v7 on (#1257)
1 parent e7408e0 commit 1ecebe6

File tree

4 files changed

+99
-45
lines changed

4 files changed

+99
-45
lines changed

cmd/src/teams.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package main
22

33
import (
4+
"context"
45
"flag"
56
"fmt"
7+
"os"
8+
9+
"github.com/sourcegraph/src-cli/internal/api"
10+
"github.com/sourcegraph/src-cli/internal/features"
611
)
712

813
var teamsCommands commander
@@ -27,6 +32,9 @@ Use "src teams [command] -h" for more information about a command.
2732

2833
flagSet := flag.NewFlagSet("teams", flag.ExitOnError)
2934
handler := func(args []string) error {
35+
if err := checkTeamsAvailability(); err != nil {
36+
return err
37+
}
3038
teamsCommands.run(flagSet, "src teams", usage, args)
3139
return nil
3240
}
@@ -42,6 +50,27 @@ Use "src teams [command] -h" for more information about a command.
4250
})
4351
}
4452

53+
// checkTeamsAvailability verifies that the connected Sourcegraph instance
54+
// supports teams. Teams were removed in Sourcegraph 7.0.
55+
func checkTeamsAvailability() error {
56+
client := cfg.apiClient(api.NewFlags(flag.NewFlagSet("", flag.ContinueOnError)), os.Stderr)
57+
58+
version, err := api.GetSourcegraphVersion(context.Background(), client)
59+
if err != nil || version == "" {
60+
// If we can't determine the version, let the command proceed.
61+
return nil
62+
}
63+
64+
var ffs features.FeatureFlags
65+
if err := ffs.SetFromVersion(version, true); err != nil {
66+
return nil
67+
}
68+
if ffs.Sourcegraph70 {
69+
return fmt.Errorf("the 'src teams' commands are not available for Sourcegraph versions 7.0 and later (detected version: %s). Teams have been removed", version)
70+
}
71+
return nil
72+
}
73+
4574
const teamFragment = `
4675
fragment TeamFields on Team {
4776
id

internal/api/version.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package api
2+
3+
import "context"
4+
5+
// GetSourcegraphVersion queries the Sourcegraph instance for its product version.
6+
func GetSourcegraphVersion(ctx context.Context, client Client) (string, error) {
7+
var result struct {
8+
Site struct {
9+
ProductVersion string
10+
}
11+
}
12+
ok, err := client.NewQuery(`query { site { productVersion } }`).Do(ctx, &result)
13+
if err != nil || !ok {
14+
return "", err
15+
}
16+
return result.Site.ProductVersion, nil
17+
}

internal/batches/features.go

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,6 @@
11
package batches
22

3-
import (
4-
"fmt"
5-
"log"
3+
import "github.com/sourcegraph/src-cli/internal/features"
64

7-
"github.com/sourcegraph/sourcegraph/lib/api"
8-
"github.com/sourcegraph/sourcegraph/lib/errors"
9-
)
10-
11-
// FeatureFlags represent features that are only available on certain
12-
// Sourcegraph versions and we therefore have to detect at runtime.
13-
type FeatureFlags struct {
14-
Sourcegraph40 bool
15-
BinaryDiffs bool
16-
}
17-
18-
func (ff *FeatureFlags) SetFromVersion(version string, skipErrors bool) error {
19-
for _, feature := range []struct {
20-
flag *bool
21-
constraint string
22-
minDate string
23-
}{
24-
// NOTE: It's necessary to include a "-0" prerelease suffix on each constraint so that
25-
// prereleases of future versions are still considered to satisfy the constraint.
26-
//
27-
// For example, the version "3.35.1-rc.3" is not considered to satisfy the constraint
28-
// ">= 3.23.0". However, the same version IS considered to satisfy the constraint
29-
// "3.23.0-0". See
30-
// https://github.com/Masterminds/semver#working-with-prerelease-versions for more.
31-
// Example usage:
32-
// {&ff.FlagName, ">= 3.23.0-0", "2020-11-24"},
33-
{&ff.Sourcegraph40, ">= 4.0.0-0", "2022-08-24"},
34-
{&ff.BinaryDiffs, ">= 4.3.0-0", "2022-11-29"},
35-
} {
36-
value, err := api.CheckSourcegraphVersion(version, feature.constraint, feature.minDate)
37-
if err != nil {
38-
if skipErrors {
39-
log.Printf("failed to check version returned by Sourcegraph: %s. Assuming no feature flags.", version)
40-
} else {
41-
return errors.Wrap(err, fmt.Sprintf("failed to check version returned by Sourcegraph: %s", version))
42-
}
43-
}
44-
*feature.flag = value
45-
}
46-
47-
return nil
48-
}
5+
// FeatureFlags is an alias for features.FeatureFlags for backwards compatibility.
6+
type FeatureFlags = features.FeatureFlags

internal/features/features.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package features
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/sourcegraph/sourcegraph/lib/api"
8+
"github.com/sourcegraph/sourcegraph/lib/errors"
9+
)
10+
11+
// FeatureFlags represent features that are only available on certain
12+
// Sourcegraph versions and we therefore have to detect at runtime.
13+
type FeatureFlags struct {
14+
Sourcegraph40 bool
15+
BinaryDiffs bool
16+
Sourcegraph70 bool
17+
}
18+
19+
func (ff *FeatureFlags) SetFromVersion(version string, skipErrors bool) error {
20+
for _, feature := range []struct {
21+
flag *bool
22+
constraint string
23+
minDate string
24+
}{
25+
// NOTE: It's necessary to include a "-0" prerelease suffix on each constraint so that
26+
// prereleases of future versions are still considered to satisfy the constraint.
27+
//
28+
// For example, the version "3.35.1-rc.3" is not considered to satisfy the constraint
29+
// ">= 3.23.0". However, the same version IS considered to satisfy the constraint
30+
// "3.23.0-0". See
31+
// https://github.com/Masterminds/semver#working-with-prerelease-versions for more.
32+
// Example usage:
33+
// {&ff.FlagName, ">= 3.23.0-0", "2020-11-24"},
34+
{&ff.Sourcegraph40, ">= 4.0.0-0", "2022-08-24"},
35+
{&ff.BinaryDiffs, ">= 4.3.0-0", "2022-11-29"},
36+
{&ff.Sourcegraph70, ">= 7.0.0-0", "2026-02-25"},
37+
} {
38+
value, err := api.CheckSourcegraphVersion(version, feature.constraint, feature.minDate)
39+
if err != nil {
40+
if skipErrors {
41+
log.Printf("failed to check version returned by Sourcegraph: %s. Assuming no feature flags.", version)
42+
} else {
43+
return errors.Wrap(err, fmt.Sprintf("failed to check version returned by Sourcegraph: %s", version))
44+
}
45+
}
46+
*feature.flag = value
47+
}
48+
49+
return nil
50+
}

0 commit comments

Comments
 (0)