8000 feat(vcs): update github vcs provider to use github enterprise · ovh/cds@5c5a6d9 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 5c5a6d9

Browse files
NicPattersonfsamin
authored andcommitted
feat(vcs): update github vcs provider to use github enterprise
1 parent 9c80069 commit 5c5a6d9

File tree

6 files changed

+50
-21
lines changed

6 files changed

+50
-21
lines changed

engine/vcs/github/client_rate_limit.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package github
22

33
import (
44
"encoding/json"
5+
"net/http"
6+
"strings"
57
"time"
68

79
"github.com/ovh/cds/sdk"
@@ -25,6 +27,10 @@ func (g *githubClient) RateLimit() error {
2527
log.Warning("githubClient.RateLimit> Error %s", err)
2628
return err
2729
}
30+
// If the GitHub instance does not have Rate Limitting enabled you will see a 404.
31+
if status == http.StatusNotFound && strings.Contains(string(body), "Rate limiting is not enabled.") {
32+
return nil
33+
}
2834
if status >= 400 {
2935
return sdk.NewError(sdk.ErrUnknownError, errorAPI(body))
3036
}

engine/vcs/github/github.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package github
22

33
import (
4+
"fmt"
45
"github.com/ovh/cds/engine/api/cache"
56
"github.com/ovh/cds/sdk"
67
)
78

89
// githubClient is a github.com wrapper for CDS vcs. interface
910
type githubClient struct {
11+
GitHubURL string
12+
GitHubAPIURL string
1013
ClientID string
1114
OAuthToken string
1215
DisableStatus bool
@@ -24,6 +27,8 @@ type githubConsumer struct {
2427
ClientID string `json:"client-id"`
2528
ClientSecret string `json:"-"`
2629
Cache cache.Store
30+
GitHubURL string
31+
GitHubAPIURL string
2732
uiURL string
2833
apiURL string
2934
proxyURL string
@@ -34,10 +39,29 @@ type githubConsumer struct {
3439
}
3540

3641
//New creates a new GithubConsumer
37-
func New(ClientID, ClientSecret string, apiURL, uiURL, proxyURL, username, token string, store cache.Store, disableStatus, disableStatusDetail bool) sdk.VCSServer {
42+
func New(ClientID, ClientSecret, githubURL, githubAPIURL, apiURL, uiURL, proxyURL, username, token string, store cache.Store, disableStatus, disableStatusDetail bool) sdk.VCSServer {
43+
//Github const
44+
const (
45+
URL = "https://github.com"
46+
APIURL = "https://api.github.com"
47+
)
48+
// if the github GitHubURL is passed as an empty string default it to public GitHub
49+
if githubURL == "" {
50+
githubURL = URL
51+
}
52+
// if the githubAPIURL is empty first check if githubURL was passed in, if not set to default
53+
if githubAPIURL == "" {
54+
if githubURL == "" {
55+
githubAPIURL = APIURL
56+
} else {
57+
githubAPIURL = fmt.Sprintf("%s/api/v3", githubURL)
58+
}
59+
}
3860
return &githubConsumer{
3961
ClientID: ClientID,
4062
ClientSecret: ClientSecret,
63+
GitHubURL: githubURL,
64+
GitHubAPIURL: githubAPIURL,
4165
Cache: store,
4266
apiURL: apiURL,
4367
uiURL: uiURL,

engine/vcs/github/http.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var (
3131
func (g *githubConsumer) postForm(path string, data url.Values, headers map[string][]string) (int, []byte, error) {
3232
body := strings.NewReader(data.Encode())
3333

34-
req, err := http.NewRequest(http.MethodPost, URL+path, body)
34+
req, err := http.NewRequest(http.MethodPost, g.GitHubURL+path, body)
3535
if err != nil {
3636
return 0, nil, err
3737
}
@@ -122,8 +122,8 @@ func (c *githubClient) post(path string, bodyType string, body io.Reader, opts *
122122
if opts == nil {
123123
opts = new(postOptions)
124124
}
125-
if !opts.skipDefaultBaseURL && !strings.HasPrefix(path, APIURL) {
126-
path = APIURL + path
125+
if !opts.skipDefaultBaseURL && !strings.HasPrefix(path, c.GitHubAPIURL) {
126+
path = c.GitHubAPIURL + path
127127
}
128128

129129
req, err := http.NewRequest(http.MethodPost, path, body)
@@ -149,8 +149,8 @@ func (c *githubClient) patch(path string, opts *postOptions) (*http.Response, er
149149
if opts == nil {
150150
opts = new(postOptions)
151151
}
152-
if !opts.skipDefaultBaseURL && !strings.HasPrefix(path, APIURL) {
153-
path = APIURL + path
152+
if !opts.skipDefaultBaseURL && !strings.HasPrefix(path, c.GitHubAPIURL) {
153+
path = c.GitHubAPIURL + path
154154
}
155155

156156
req, err := http.NewRequest(http.MethodPatch, path, nil)
@@ -175,8 +175,8 @@ func (c *githubClient) put(path string, bodyType string, body io.Reader, opts *p
175175
if opts == nil {
176176
opts = new(postOptions)
177177
}
178-
if !opts.skipDefaultBaseURL && !strings.HasPrefix(path, APIURL) {
179-
path = APIURL + path
178+
if !opts.skipDefaultBaseURL && !strings.HasPrefix(path, c.GitHubAPIURL) {
179+
path = c.GitHubAPIURL + path
180180
}
181181

182182
req, err := http.NewRequest(http.MethodPut, path, body)
@@ -203,8 +203,8 @@ func (c *githubClient) get(path string, opts ...getArgFunc) (int, []byte, http.H
203203
return 0, nil, nil, ErrorRateLimit
204204
}
205205

206-
if !strings.HasPrefix(path, APIURL) {
207-
path = APIURL + path
206+
if !strings.HasPrefix(path, c.GitHubAPIURL) {
207+
path = c.GitHubAPIURL + path
208208
}
209209

210210
callURL, err := url.ParseRequestURI(path)
@@ -229,7 +229,7 @@ func (c *githubClient) get(path string, opts ...getArgFunc) (int, []byte, http.H
229229
}
230230
}
231231

232-
log.Debug("Github API>> Request URL %s", req.URL.String())
232+
log.Debug("Github API>> Request GitHubURL %s", req.URL.String())
233233

234234
res, err := httpClient.Do(req)
235235
if err != nil {
@@ -275,8 +275,8 @@ func (c *githubClient) delete(path string) error {
275275
return ErrorRateLimit
276276
}
277277

278-
if !strings.HasPrefix(path, APIURL) {
279-
path = APIURL + path
278+
if !strings.HasPrefix(path, c.GitHubAPIURL) {
279+
path = c.GitHubAPIURL + path
280280
}
281281

282282
req, err := http.NewRequest(http.MethodDelete, path, nil)

engine/vcs/github/oauth_consumer.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,12 @@ import (
1111
"github.com/ovh/cds/sdk/log"
1212
)
1313

14-
//Github const
15-
const (
16-
URL = "https://github.com"
17-
APIURL = "https://api.github.com"
18-
)
19-
2014
//Github const
2115
var (
2216
requestedScope = []string{"user:email", "repo", "admin:repo_hook"} //https://developer.github.com/v3/oauth/#scopes
2317
)
2418

25-
//AuthorizeRedirect returns the request token, the Authorize URL
19+
//AuthorizeRedirect returns the request token, the Authorize GitHubURL
2620
//doc: https://developer.github.com/v3/oauth/#web-application-flow
2721
func (g *githubConsumer) AuthorizeRedirect(ctx context.Context) (string, string, error) {
2822
// GET https://github.com/login/oauth/authorize
@@ -38,7 +32,7 @@ func (g *githubConsumer) AuthorizeRedirect(ctx context.Context) (string, string,
3832
val.Add("scope", strings.Join(requestedScope, " "))
3933
val.Add("state", requestToken)
4034

41-
authorizeURL := fmt.Sprintf("%s/login/oauth/authorize?%s", URL, val.Encode())
35+
authorizeURL := fmt.Sprintf("%s/login/oauth/authorize?%s", g.GitHubURL, val.Encode())
4236

4337
return requestToken, authorizeURL, nil
4438
}
@@ -93,6 +87,8 @@ func (g *githubConsumer) GetAuthorizedClient(ctx context.Context, accessToken, a
9387
c = &githubClient{
9488
ClientID: g.ClientID,
9589
OAuthToken: accessToken,
90+
GitHubURL: g.GitHubURL,
91+
GitHubAPIURL: g.GitHubAPIURL,
9692
Cache: g.Cache,
9793
uiURL: g.uiURL,
9894
DisableStatus: g.disableStatus,

engine/vcs/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type ServerConfiguration struct {
5555
type GithubServerConfiguration struct {
5656
ClientID string `toml:"clientId" json:"-" comment:"#######\n CDS <-> Github. Documentation on https://ovh.github.io/cds/hosting/repositories-manager/github/ \n#######\n Github OAuth Application Client ID"`
5757
ClientSecret string `toml:"clientSecret" json:"-" comment:"Github OAuth Application Client Secret"`
58+
APIURL string `toml:"apiUrl" json:"-" comment:"The URL for the GitHub API."`
5859
Status struct {
5960
Disable bool `toml:"disable" default:"false" commented:"true" comment:"Set to true if you don't want CDS to push statuses on the VCS server" json:"disable"`
6061
ShowDetail bool `toml:"showDetail" default:"false" commented:"true" comment:"Set to true if you don't want CDS to push CDS URL in statuses on the VCS server" json:"show_detail"`

engine/vcs/vcs.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ func (s *Service) getConsumer(name string) (sdk.VCSServer, error) {
7878
return github.New(
7979
serverCfg.Github.ClientID,
8080
serverCfg.Github.ClientSecret,
81+
serverCfg.URL,
82+
serverCfg.Github.APIURL,
8183
s.Cfg.API.HTTP.URL,
8284
s.Cfg.UI.HTTP.URL,
8385
serverCfg.Github.ProxyWebhook,

0 commit comments

Comments
 (0)
0