Skip to content

Commit ec35b39

Browse files
authored
Add GitLab to the list of supported hosts (#249)
* Add GitLab to the list of supported hosts. * Perform an explicit version check.
1 parent 7eff52a commit ec35b39

File tree

3 files changed

+135
-3
lines changed

3 files changed

+135
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ All notable changes to `src-cli` are documented in this file.
1313

1414
### Added
1515

16+
- When used with Sourcegraph 3.18 or later, campaigns can now be created on GitLab. [#231](https://github.com/sourcegraph/src-cli/pull/231)
17+
1618
### Changed
1719

1820
### Fixed

cmd/src/actions_exec.go

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,15 @@ fragment repositoryFields on Repository {
448448
}
449449

450450
// Skip repos from unsupported code hosts but don't report them explicitly.
451-
if !includeUnsupported && strings.ToLower(repo.ExternalRepository.ServiceType) != "github" && strings.ToLower(repo.ExternalRepository.ServiceType) != "bitbucketserver" {
452-
unsupported = append(unsupported, repo.Name)
453-
continue
451+
if !includeUnsupported {
452+
ok, err := isCodeHostSupportedForCampaigns(repo.ExternalRepository.ServiceType)
453+
if err != nil {
454+
return nil, errors.Wrap(err, "failed code host check")
455+
}
456+
if !ok {
457+
unsupported = append(unsupported, repo.Name)
458+
continue
459+
}
454460
}
455461

456462
if repo.DefaultBranch == nil || repo.DefaultBranch.Name == "" {
@@ -517,3 +523,44 @@ func askForConfirmation(s string) (bool, error) {
517523

518524
return false, nil
519525
}
526+
527+
type minimumVersionDate struct {
528+
version string
529+
date string
530+
}
531+
532+
// codeHostCampaignVersions contains the minimum Sourcegraph version and build
533+
// date required for the given code host kind. If a code host is present with a
534+
// value of nil, this means that any Sourcegraph version will pass the check.
535+
var codeHostCampaignVersions = map[string]*minimumVersionDate{
536+
"github": nil,
537+
"bitbucketserver": nil,
538+
"gitlab": {
539+
version: "3.18.0",
540+
date: "2020-07-14",
541+
},
542+
}
543+
544+
func isCodeHostSupportedForCampaigns(kind string) (bool, error) {
545+
// TODO(LawnGnome): this is a temporary hack; I intend to improve our
546+
// testing story including mocking requests to Sourcegraph as part of
547+
// https://github.com/sourcegraph/sourcegraph/issues/12333
548+
return isCodeHostSupportedForCampaignsImpl(kind, getSourcegraphVersion)
549+
}
550+
551+
func isCodeHostSupportedForCampaignsImpl(kind string, getVersion func() (string, error)) (bool, error) {
552+
mvd, ok := codeHostCampaignVersions[strings.ToLower(kind)]
553+
if !ok {
554+
return false, nil
555+
}
556+
if mvd == nil {
557+
return true, nil
558+
}
559+
560+
ver, err := getVersion()
561+
if err != nil {
562+
return false, errors.Wrap(err, "getting Sourcegraph version")
563+
}
564+
565+
return sourcegraphVersionCheck(ver, fmt.Sprintf(">= %s", mvd.version), mvd.date)
566+
}

cmd/src/actions_exec_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/pkg/errors"
7+
)
8+
9+
func TestCodeHostSupported(t *testing.T) {
10+
t.Run("error from getSourcegraphVersion", func(t *testing.T) {
11+
want := errors.New("foo")
12+
if _, have := isCodeHostSupportedForCampaignsImpl("GITLAB", func() (string, error) {
13+
return "", want
14+
}); !errors.Is(have, want) {
15+
t.Errorf("unexpected error: have %+v; want %+v", have, want)
16+
}
17+
})
18+
19+
t.Run("error from sourcegraphVersionCheck", func(t *testing.T) {
20+
if _, err := isCodeHostSupportedForCampaignsImpl("GITLAB", func() (string, error) {
21+
return "x.y.z", nil
22+
}); err == nil {
23+
t.Error("unexpected nil error")
24+
}
25+
})
26+
27+
t.Run("no errors", func(t *testing.T) {
28+
for name, tc := range map[string]struct {
29+
want bool
30+
kind string
31+
version string
32+
}{
33+
"GitHub": {
34+
want: true,
35+
kind: "GITHUB",
36+
version: "1.2.3",
37+
},
38+
"BitBucket": {
39+
want: true,
40+
kind: "BITBUCKETSERVER",
41+
version: "1.2.3",
42+
},
43+
"GitLab with old semver version": {
44+
want: false,
45+
kind: "GITLAB",
46+
version: "1.2.3",
47+
},
48+
"GitLab with old dev version": {
49+
want: false,
50+
kind: "GITLAB",
51+
version: "68956_2019-07-21_c3a5992",
52+
},
53+
"GitLab with new semver version": {
54+
want: true,
55+
kind: "GITLAB",
56+
version: "3.18.0",
57+
},
58+
"GitLab with new dev version": {
59+
want: true,
60+
kind: "GITLAB",
61+
version: "68956_2020-07-21_c3a5992",
62+
},
63+
"unknown kind": {
64+
want: false,
65+
kind: "CODE HOSTS R US",
66+
version: "3.18.0",
67+
},
68+
} {
69+
t.Run(name, func(t *testing.T) {
70+
have, err := isCodeHostSupportedForCampaignsImpl(tc.kind, func() (string, error) {
71+
return tc.version, nil
72+
})
73+
if err != nil {
74+
t.Errorf("unexpected non-nil error: %+v", err)
75+
}
76+
77+
if have != tc.want {
78+
t.Errorf("unexpected support status: have %v; want %v", have, tc.want)
79+
}
80+
})
81+
}
82+
})
83+
}

0 commit comments

Comments
 (0)