Skip to content
This repository was archived by the owner on Aug 9, 2021. It is now read-only.

Commit 41af191

Browse files
authored
Merge ec99f20 into be917dd
2 parents be917dd + ec99f20 commit 41af191

6 files changed

Lines changed: 35 additions & 2 deletions

File tree

Gopkg.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262

6363
[[constraint]]
6464
name = "github.com/seborama/govcr"
65-
version = "2.4.1"
65+
# version = "2.4.1"
66+
revision = "a20ea7b5807f64c80cf6ed51de68063ad05164f4"
6667

6768
[[constraint]]
6869
name = "github.com/stretchr/testify"

example.capsule.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ scm_github_api_endpoint: ''
1818
# Specifies the access token to use when cloning from and committing to Github
1919
scm_github_access_token: ''
2020

21+
# Specifies the type of access token to be used. Can be `user` or `app`.
22+
scm_github_access_token_type: 'user'
2123

2224
scm_bitbucket_username: ''
2325
# Specifies the app password to use when cloning from and committing to Bitbucket

pkg/scm/factory_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func (suite *ScmTestSuite) TestCreate_Invalid() {
4646

4747
func (suite *ScmTestSuite) TestCreate_Github() {
4848
//setup
49+
suite.Config.EXPECT().SetDefault(gomock.Any(), gomock.Any()).MinTimes(1)
4950
suite.Config.EXPECT().GetString("scm_github_access_token").Return("placeholder")
5051
suite.Config.EXPECT().IsSet("scm_github_api_endpoint").Return(false)
5152
suite.Config.EXPECT().IsSet("scm_github_access_token").Return(true)

pkg/scm/scm_bitbucket_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ func (suite *ScmBitbucketTestSuite) TestScmBitbucket_CheckoutPushPayload() {
275275

276276
func (suite *ScmBitbucketTestSuite) TestScmBitbucket_CheckoutPushPayload_WithInvalidPayload() {
277277
//setup
278+
suite.Config.EXPECT().SetDefault(gomock.Any(), gomock.Any()).MinTimes(1)
278279
suite.Config.EXPECT().IsSet("scm_github_access_token").Return(true) //used by the init function
279280
suite.Config.EXPECT().IsSet("scm_github_api_endpoint").Return(false)
280281
suite.Config.EXPECT().IsSet("scm_git_parent_path").Return(false)

pkg/scm/scm_github.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type scmGithub struct {
2929
func (g *scmGithub) Init(pipelineData *pipeline.Data, myconfig config.Interface, client *http.Client) error {
3030
g.PipelineData = pipelineData
3131
g.Config = myconfig
32+
g.Config.SetDefault("scm_github_access_token_type", "user")
3233

3334
if !g.Config.IsSet("scm_github_access_token") {
3435
return errors.ScmAuthenticationFailed("Missing github access token")
@@ -141,7 +142,20 @@ func (g *scmGithub) CheckoutPushPayload(payload *Payload) error {
141142
return err
142143
}
143144

144-
authRemote, aerr := authGitRemote(g.PipelineData.GitHeadInfo.Repo.CloneUrl, g.Config.GetString("scm_github_access_token"), "")
145+
var gitRemoteUsername string
146+
var gitRemotePassword string
147+
148+
if g.Config.GetString("scm_github_access_token_type") == "app" {
149+
// see https://developer.github.com/apps/building-github-apps/authenticating-with-github-apps/
150+
gitRemoteUsername = "x-access-token"
151+
gitRemotePassword = g.Config.GetString("scm_github_access_token")
152+
} else {
153+
gitRemoteUsername = g.Config.GetString("scm_github_access_token")
154+
gitRemotePassword = ""
155+
}
156+
157+
158+
authRemote, aerr := authGitRemote(g.PipelineData.GitHeadInfo.Repo.CloneUrl, gitRemoteUsername, gitRemotePassword)
145159
if aerr != nil {
146160
return aerr
147161
}

pkg/scm/scm_github_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func TestScmGithub_Init_WithoutAccessToken(t *testing.T) {
6767
mockCtrl := gomock.NewController(t)
6868
defer mockCtrl.Finish()
6969
mockConfig := mock_config.NewMockInterface(mockCtrl)
70+
mockConfig.EXPECT().SetDefault(gomock.Any(), gomock.Any()).MinTimes(1)
7071
mockConfig.EXPECT().IsSet("scm_github_access_token").Return(false)
7172

7273
pipelineData := new(pipeline.Data)
@@ -87,6 +88,7 @@ func TestScmGithub_Init_WithGitParentPath(t *testing.T) {
8788
mockCtrl := gomock.NewController(t)
8889
defer mockCtrl.Finish()
8990
mockConfig := mock_config.NewMockInterface(mockCtrl)
91+
mockConfig.EXPECT().SetDefault(gomock.Any(), gomock.Any()).MinTimes(1)
9092
mockConfig.EXPECT().IsSet("scm_github_access_token").Return(true)
9193
mockConfig.EXPECT().IsSet("scm_github_api_endpoint").Return(false)
9294
pipelineData := new(pipeline.Data)
@@ -113,6 +115,7 @@ func TestScmGithub_Init_WithDefaults(t *testing.T) {
113115
mockCtrl := gomock.NewController(t)
114116
defer mockCtrl.Finish()
115117
mockConfig := mock_config.NewMockInterface(mockCtrl)
118+
mockConfig.EXPECT().SetDefault(gomock.Any(), gomock.Any()).MinTimes(1)
116119
mockConfig.EXPECT().IsSet("scm_github_access_token").Return(true)
117120
mockConfig.EXPECT().IsSet("scm_github_api_endpoint").Return(false)
118121
mockConfig.EXPECT().GetString("scm_github_access_token").Return("placeholder")
@@ -134,6 +137,7 @@ func TestScmGithub_RetrievePayload_PullRequest(t *testing.T) {
134137
mockCtrl := gomock.NewController(t)
135138
defer mockCtrl.Finish()
136139
mockConfig := mock_config.NewMockInterface(mockCtrl)
140+
mockConfig.EXPECT().SetDefault(gomock.Any(), gomock.Any()).MinTimes(1)
137141
mockConfig.EXPECT().IsSet("scm_github_access_token").Return(true)
138142
mockConfig.EXPECT().IsSet("scm_github_api_endpoint").Return(false)
139143
mockConfig.EXPECT().IsSet("scm_git_parent_path").Return(false)
@@ -160,6 +164,7 @@ func TestScmGithub_RetrievePayload_PullRequest_InvalidState(t *testing.T) {
160164
mockCtrl := gomock.NewController(t)
161165
defer mockCtrl.Finish()
162166
mockConfig := mock_config.NewMockInterface(mockCtrl)
167+
mockConfig.EXPECT().SetDefault(gomock.Any(), gomock.Any()).MinTimes(1)
163168
mockConfig.EXPECT().IsSet("scm_github_access_token").Return(true)
164169
mockConfig.EXPECT().IsSet("scm_github_api_endpoint").Return(false)
165170
mockConfig.EXPECT().IsSet("scm_git_parent_path").Return(false)
@@ -186,6 +191,7 @@ func TestScmGithub_RetrievePayload_Push(t *testing.T) {
186191
mockCtrl := gomock.NewController(t)
187192
defer mockCtrl.Finish()
188193
mockConfig := mock_config.NewMockInterface(mockCtrl)
194+
mockConfig.EXPECT().SetDefault(gomock.Any(), gomock.Any()).MinTimes(1)
189195
mockConfig.EXPECT().IsSet("scm_github_access_token").Return(true)
190196
mockConfig.EXPECT().IsSet("scm_github_api_endpoint").Return(false)
191197
mockConfig.EXPECT().IsSet("scm_git_parent_path").Return(false)
@@ -219,9 +225,11 @@ func TestScmGithub_CheckoutPushPayload(t *testing.T) {
219225
mockCtrl := gomock.NewController(t)
220226
defer mockCtrl.Finish()
221227
mockConfig := mock_config.NewMockInterface(mockCtrl)
228+
mockConfig.EXPECT().SetDefault(gomock.Any(), gomock.Any()).MinTimes(1)
222229
mockConfig.EXPECT().IsSet("scm_github_access_token").Return(true) //used by the init function
223230
mockConfig.EXPECT().IsSet("scm_github_api_endpoint").Return(false)
224231
mockConfig.EXPECT().GetString("scm_github_access_token").Return("") //set the Access Token to empty string before doing checkout
232+
mockConfig.EXPECT().GetString("scm_github_access_token_type").Return("user") //set the Access Token Type
225233
// (so that git doesnt fail on placeholder token)
226234
mockConfig.EXPECT().IsSet("scm_git_parent_path").Return(false)
227235
mockConfig.EXPECT().IsSet("scm_pull_request").Return(false)
@@ -252,6 +260,7 @@ func TestScmGithub_CheckoutPushPayload_WithInvalidPayload(t *testing.T) {
252260
mockCtrl := gomock.NewController(t)
253261
defer mockCtrl.Finish()
254262
mockConfig := mock_config.NewMockInterface(mockCtrl)
263+
mockConfig.EXPECT().SetDefault(gomock.Any(), gomock.Any()).MinTimes(1)
255264
mockConfig.EXPECT().IsSet("scm_github_access_token").Return(true) //used by the init function
256265
mockConfig.EXPECT().IsSet("scm_github_api_endpoint").Return(false)
257266
mockConfig.EXPECT().IsSet("scm_git_parent_path").Return(false)
@@ -276,6 +285,7 @@ func TestScmGithub_CheckoutPullRequestPayload(t *testing.T) {
276285
mockCtrl := gomock.NewController(t)
277286
defer mockCtrl.Finish()
278287
mockConfig := mock_config.NewMockInterface(mockCtrl)
288+
mockConfig.EXPECT().SetDefault(gomock.Any(), gomock.Any()).MinTimes(1)
279289
mockConfig.EXPECT().IsSet("scm_github_access_token").Return(true)
280290
mockConfig.EXPECT().IsSet("scm_github_api_endpoint").Return(false)
281291
mockConfig.EXPECT().GetString("scm_github_access_token").Return("")
@@ -346,6 +356,7 @@ func TestScmGithub_Cleanup_WithoutEnablingBranchCleanup(t *testing.T) {
346356
mockCtrl := gomock.NewController(t)
347357
defer mockCtrl.Finish()
348358
mockConfig := mock_config.NewMockInterface(mockCtrl)
359+
mockConfig.EXPECT().SetDefault(gomock.Any(), gomock.Any()).MinTimes(1)
349360
mockConfig.EXPECT().IsSet("scm_github_access_token").Return(true)
350361
mockConfig.EXPECT().IsSet("scm_github_api_endpoint").Return(false)
351362
mockConfig.EXPECT().IsSet("scm_git_parent_path").Return(false)
@@ -368,6 +379,7 @@ func TestScmGithub_Cleanup_WithDifferentOrgs(t *testing.T) {
368379
mockCtrl := gomock.NewController(t)
369380
defer mockCtrl.Finish()
370381
mockConfig := mock_config.NewMockInterface(mockCtrl)
382+
mockConfig.EXPECT().SetDefault(gomock.Any(), gomock.Any()).MinTimes(1)
371383
mockConfig.EXPECT().IsSet("scm_github_access_token").Return(true)
372384
mockConfig.EXPECT().IsSet("scm_github_api_endpoint").Return(false)
373385
mockConfig.EXPECT().IsSet("scm_git_parent_path").Return(false)
@@ -408,6 +420,7 @@ func TestScmGithub_Cleanup_WithHeadBranchMaster(t *testing.T) {
408420
mockCtrl := gomock.NewController(t)
409421
defer mockCtrl.Finish()
410422
mockConfig := mock_config.NewMockInterface(mockCtrl)
423+
mockConfig.EXPECT().SetDefault(gomock.Any(), gomock.Any()).MinTimes(1)
411424
mockConfig.EXPECT().IsSet("scm_github_access_token").Return(true)
412425
mockConfig.EXPECT().IsSet("scm_github_api_endpoint").Return(false)
413426
mockConfig.EXPECT().IsSet("scm_git_parent_path").Return(false)
@@ -488,6 +501,7 @@ func TestScmGithub_Notify(t *testing.T) {
488501
mockCtrl := gomock.NewController(t)
489502
defer mockCtrl.Finish()
490503
mockConfig := mock_config.NewMockInterface(mockCtrl)
504+
mockConfig.EXPECT().SetDefault(gomock.Any(), gomock.Any()).MinTimes(1)
491505
mockConfig.EXPECT().IsSet("scm_github_access_token").Return(true)
492506
mockConfig.EXPECT().IsSet("scm_github_api_endpoint").Return(false)
493507
mockConfig.EXPECT().IsSet("scm_git_parent_path").Return(false)

0 commit comments

Comments
 (0)