func()

in pkg/gitprovider/bitbucket.go [57:111]


func (b *BitbucketClient) CreateWebHook(ctx context.Context, _, _, projectID, webHookSecret, webHookURL string, skipTLS bool) (*WebHook, error) {
	owner, repo, err := parseProjectID(projectID)
	if err != nil {
		return nil, err
	}

	r, err := b.client.PostRepositoriesWorkspaceRepoSlugHooksWithResponse(
		ctx,
		owner,
		repo,
		func(ctx context.Context, req *http.Request) error {
			var body []byte

			body, err = json.Marshal(map[string]interface{}{
				"description":            fmt.Sprintf("Automatically created %s", uuid.NewUUID()),
				"url":                    webHookURL,
				"active":                 true,
				"secret":                 webHookSecret,
				"skip_cert_verification": skipTLS,
				"history_enabled":        true,
				"events": []string{
					"pullrequest:created",
					"pullrequest:updated",
					"pullrequest:fulfilled",
					"pullrequest:comment_created",
					"pullrequest:comment_updated",
				},
			})
			if err != nil {
				return fmt.Errorf("failed to marshal Bitbucket web hook body: %w", err)
			}

			req.Body = io.NopCloser(bytes.NewReader(body))

			return nil
		},
	)

	if err != nil {
		return nil, fmt.Errorf("failed to create Bitbucket web hook: %w", err)
	}

	if !createObjectStatusOk(r.StatusCode()) {
		return nil, fmt.Errorf("failed to create Bitbucket web hook: %s %s", r.Status(), r.Body)
	}

	if r.JSON201 == nil || r.JSON201.Uuid == nil || r.JSON201.Url == nil {
		return nil, fmt.Errorf("failed to create Bitbucket web hook: invalid response %s", r.Body)
	}

	return &WebHook{
		ID:  *r.JSON201.Uuid,
		URL: *r.JSON201.Url,
	}, nil
}