github.com/redhat-appstudio/e2e-tests@v0.0.0-20240520140907-9709f6f59323/pkg/clients/github/webhook.go (about)

     1  package github
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/google/go-github/v44/github"
     8  )
     9  
    10  type Webhook struct {
    11  	github.Hook
    12  }
    13  
    14  func (g *Github) ListRepoWebhooks(repository string) ([]*github.Hook, error) {
    15  	hooks, _, err := g.client.Repositories.ListHooks(context.Background(), g.organization, repository, &github.ListOptions{})
    16  	if err != nil {
    17  		return nil, fmt.Errorf("error when listing webhooks: %v", err)
    18  	}
    19  	return hooks, err
    20  }
    21  
    22  func (g *Github) CreateWebhook(repository, url string) (int64, error) {
    23  	newWebhook := &github.Hook{
    24  		Events: []string{"push"},
    25  		Config: map[string]interface{}{
    26  			"content_type": "json",
    27  			"insecure_ssl": 0,
    28  			"url":          url,
    29  		},
    30  	}
    31  
    32  	hook, _, err := g.client.Repositories.CreateHook(context.Background(), g.organization, repository, newWebhook)
    33  	if err != nil {
    34  		return 0, fmt.Errorf("error when creating a webhook: %v", err)
    35  	}
    36  	return hook.GetID(), err
    37  }
    38  
    39  func (g *Github) DeleteWebhook(repository string, ID int64) error {
    40  	_, err := g.client.Repositories.DeleteHook(context.Background(), g.organization, repository, ID)
    41  	if err != nil {
    42  		return fmt.Errorf("error when deleting webhook: %v", err)
    43  	}
    44  	return nil
    45  }