Skip to content

Commit

Permalink
Merge pull request #363 from LinuxSuRen/fix/gitlab-hook-ssl
Browse files Browse the repository at this point in the history
fix: the gitlab hook ssl verify setting incorrect
  • Loading branch information
jenkins-x-bot authored Jan 16, 2023
2 parents 88351fc + 6279447 commit 0475a2c
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 6 deletions.
8 changes: 2 additions & 6 deletions scm/driver/gitlab/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,7 @@ func (s *repositoryService) CreateHook(ctx context.Context, repo string, input *
if input.Secret != "" {
params.Set("token", input.Secret)
}
if input.SkipVerify {
params.Set("enable_ssl_verification", "true")
}
params.Set("enable_ssl_verification", strconv.FormatBool(input.SkipVerify))
hasStarEvents := false
for _, event := range input.NativeEvents {
if event == "*" {
Expand Down Expand Up @@ -394,9 +392,7 @@ func (s *repositoryService) UpdateHook(ctx context.Context, repo string, input *
if input.Secret != "" {
params.Set("token", input.Secret)
}
if input.SkipVerify {
params.Set("enable_ssl_verification", "true")
}
params.Set("enable_ssl_verification", strconv.FormatBool(input.SkipVerify))
hasStarEvents := false
for _, event := range input.NativeEvents {
if event == "*" {
Expand Down
82 changes: 82 additions & 0 deletions scm/driver/gitlab/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,49 @@ func TestRepositoryHookCreate(t *testing.T) {
t.Run("Rate", testRate(res))
}

func TestRepositoryHookCreateSkipVerify(t *testing.T) {
defer gock.Off()

gock.New("https://gitlab.com").
Post("/api/v4/projects/diaspora/diaspora/hooks").
MatchParam("enable_ssl_verification", "false").
MatchParam("token", "topsecret").
MatchParam("url", "https://ci.example.com/hook").
Reply(201).
Type("application/json").
SetHeaders(mockHeaders).
File("testdata/hook.json")

in := &scm.HookInput{
Name: "drone",
Target: "https://ci.example.com/hook",
Secret: "topsecret",
SkipVerify: false,
}

client := NewDefault()
got, res, err := client.Repositories.CreateHook(context.Background(), "diaspora/diaspora", in)
if err != nil {
t.Error(err)
return
}

want := new(scm.Hook)
raw, _ := os.ReadFile("testdata/hook.json.golden")
err = json.Unmarshal(raw, want)
if err != nil {
t.Error(err)
}

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}

t.Run("Request", testRequest(res))
t.Run("Rate", testRate(res))
}

func TestRepositoryHookUpdate(t *testing.T) {
defer gock.Off()

Expand Down Expand Up @@ -643,6 +686,45 @@ func TestRepositoryHookUpdate(t *testing.T) {
t.Run("Rate", testRate(res))
}

func TestRepositoryHookUpdateSkipVerify(t *testing.T) {
defer gock.Off()

gock.New("https://gitlab.com").
Put("/api/v4/projects/diaspora/diaspora/hooks/1").
Reply(200).
Type("application/json").
SetHeaders(mockHeaders).
File("testdata/hook_ssl_false.json")

in := &scm.HookInput{
Name: "1",
Target: "http://example.com/hook",
}

client := NewDefault()

got, res, err := client.Repositories.UpdateHook(context.Background(), "diaspora/diaspora", in)
if err != nil {
t.Error(err)
return
}

want := new(scm.Hook)
raw, _ := os.ReadFile("testdata/hook_ssl_false.json.golden")
err = json.Unmarshal(raw, want)
if err != nil {
t.Error(err)
}

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}

t.Run("Request", testRequest(res))
t.Run("Rate", testRate(res))
}

func TestRepositoryFindUserPermission(t *testing.T) {
defer gock.Off()

Expand Down
15 changes: 15 additions & 0 deletions scm/driver/gitlab/testdata/hook_ssl_false.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"id": 1,
"url": "http://example.com/hook",
"project_id": 3,
"push_events": true,
"issues_events": true,
"merge_requests_events": true,
"tag_push_events": true,
"note_events": true,
"job_events": true,
"pipeline_events": true,
"wiki_page_events": true,
"enable_ssl_verification": false,
"created_at": "2012-10-12T17:04:47Z"
}
14 changes: 14 additions & 0 deletions scm/driver/gitlab/testdata/hook_ssl_false.json.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"ID": "1",
"Name": "",
"Target": "http://example.com/hook",
"Events": [
"issues",
"tag",
"push",
"comment",
"merge"
],
"Active": true,
"SkipVerify": true
}

0 comments on commit 0475a2c

Please sign in to comment.