drone/service/hook/hook.go
ShobhitSingh11 87381af2ac
fix: [CI-11840]: Added env variable to control the event of inbound w… (#3508)
Have added the env var "DRONE_INCOMING_WEBHOOK_EVENTS" (of type list of
string) to control the event in case of incoming webhook. If no such env
variable is provided then it will fallback to default behaviour. Below
are the test cases


- Case 1: When no env variable "DRONE_INCOMING_WEBHOOK_EVENTS" is
present.


https://github.com/harness/gitness/assets/139750384/ad228d61-2608-4756-abaa-b3397eba1fb7

-  Case 2: When Deployment event is missing from env var.


https://github.com/harness/gitness/assets/139750384/0faf2afc-9e0b-42f1-9327-8dc674ed112d

- Case 3: When Deployment, pull_request events are missing from env var.


https://github.com/harness/gitness/assets/139750384/29f40107-4b7a-4eae-afa8-464d78662e8b

- Case 4: When Deployment and push events are only present in env var.


https://github.com/harness/gitness/assets/139750384/b6cdec79-20bb-4d56-8fd0-f72cb9e57a8e


Tested with few more combinations as well.
2024-05-07 16:35:06 +01:00

80 lines
2.1 KiB
Go

// Copyright 2019 Drone IO, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hook
import (
"context"
"time"
"github.com/drone/drone/core"
"github.com/drone/go-scm/scm"
)
// New returns a new HookService.
func New(client *scm.Client, addr string, renew core.Renewer, events []string) core.HookService {
return &service{client: client, addr: addr, renew: renew, events: events}
}
type service struct {
renew core.Renewer
client *scm.Client
addr string
events []string
}
func (s *service) Create(ctx context.Context, user *core.User, repo *core.Repository) error {
err := s.renew.Renew(ctx, user, false)
if err != nil {
return err
}
eventsMap := make(map[string]bool)
for _, event := range s.events {
eventsMap[event] = true
}
ctx = context.WithValue(ctx, scm.TokenKey{}, &scm.Token{
Token: user.Token,
Refresh: user.Refresh,
Expires: time.Unix(user.Expiry, 0),
})
hook := &scm.HookInput{
Name: "drone",
Target: s.addr + "/hook",
Secret: repo.Signer,
Events: scm.HookEvents{
Branch: eventsMap["branch"],
Deployment: eventsMap["deployment"],
PullRequest: eventsMap["pull_request"],
Push: eventsMap["push"],
Tag: eventsMap["tag"],
},
}
return replaceHook(ctx, s.client, repo.Slug, hook)
}
func (s *service) Delete(ctx context.Context, user *core.User, repo *core.Repository) error {
err := s.renew.Renew(ctx, user, false)
if err != nil {
return err
}
ctx = context.WithValue(ctx, scm.TokenKey{}, &scm.Token{
Token: user.Token,
Refresh: user.Refresh,
Expires: time.Unix(user.Expiry, 0),
})
return deleteHook(ctx, s.client, repo.Slug, s.addr)
}