Adding WebHook and GetPullRequestDiffRaw

This commit is contained in:
gfleury 2020-03-23 20:24:05 -03:00
parent 022f4bab90
commit cf6f9657ee
3 changed files with 137 additions and 31 deletions

View File

@ -349,36 +349,6 @@ type Webhook struct {
Configuration WebhookConfiguration `json:"configuration"`
}
// WebHookCallback contains payload to use while reading handling webhooks from bitbucket
type WebHookCallback struct {
Actor Actor `json:"actor"`
Repository Repository `json:"repository"`
Push struct {
Changes []Change `json:"changes"`
} `json:"push"`
}
// Actor contains the actor of reported changes from a webhook
type Actor struct {
Username string `json:"username"`
DisplayName string `json:"displayName"`
}
// Change contains changes reported by webhooks
type Change struct {
Created bool `json:"created"`
Closed bool `json:"closed"`
Old interface{} `json:"old"`
New struct {
Type string `json:"type"`
Name string `json:"name"`
Target struct {
Type string `json:"type"`
Hash string `json:"hash"`
} `json:"target"`
} `json:"new"`
}
// String converts global permission to its string representation
func (p PermissionGlobal) String() string {
return string(p)
@ -438,7 +408,7 @@ func GetRepositoryResponse(r *APIResponse) (Repository, error) {
// GetDiffResponse cast Diff into structure
func GetDiffResponse(r *APIResponse) (Diff, error) {
var m Diff
err := mapstructure.Decode(r.Values["values"], &m)
err := mapstructure.Decode(r.Values, &m)
return m, err
}

View File

@ -11438,6 +11438,85 @@ func (a *DefaultApiService) StreamDiff_39(projectKey, repositorySlug string, pat
return NewBitbucketAPIResponse(localVarHTTPResponse)
}
/* DefaultApiService
Streams a diff within a pull request. <p> If the specified file has been copied, moved or renamed, the <code>srcPath</code> must also be specified to produce the correct diff. <p> Note: This RESTful endpoint is currently <i>not paged</i>. The server will internally apply a hard cap to the streamed lines, and it is not possible to request subsequent pages if that cap is exceeded. <p> The authenticated user must have <strong>REPO_READ</strong> permission for the repository that this pull request targets to call this resource.
@param optional (nil or map[string]interface{}) with one or more of:
@param "contextLines" (int32) the number of context lines to include around added/removed lines in the diff
@param "diffType" (string) the type of diff being requested. When {@code withComments} is {@code true} this works as a hint to the system to attach the correct set of comments to the diff
@param "sinceId" (string) the since commit hash to stream a diff between two arbitrary hashes
@param "srcPath" (string) the previous path to the file, if the file has been copied, moved or renamed
@param "untilId" (string) the until commit hash to stream a diff between two arbitrary hashes
@param "whitespace" (string) optional whitespace flag which can be set to <code>ignore-all</code>
@param "withComments" (bool) <code>true</code> to embed comments in the diff (the default); otherwise, <code>false</code> to stream the diff without comments
@return */
func (a *DefaultApiService) GetPullRequestDiffRaw(projectKey, repositorySlug string, pullRequestID int, localVarOptionals map[string]interface{}) (*APIResponse, error) {
var (
localVarHTTPMethod = strings.ToUpper("Get")
localVarPostBody interface{}
localVarFileName string
localVarFileBytes []byte
)
// create path and map variables
localVarPath := a.client.cfg.BasePath + "/api/1.0/projects/{projectKey}/repos/{repositorySlug}/pull-requests/{pullRequestId}.diff"
localVarPath = strings.Replace(localVarPath, "{"+"projectKey"+"}", fmt.Sprintf("%v", projectKey), -1)
localVarPath = strings.Replace(localVarPath, "{"+"repositorySlug"+"}", fmt.Sprintf("%v", repositorySlug), -1)
localVarPath = strings.Replace(localVarPath, "{"+"pullRequestId"+"}", fmt.Sprintf("%v", pullRequestID), -1)
localVarHeaderParams := make(map[string]string)
localVarQueryParams := url.Values{}
localVarFormParams := url.Values{}
if err := typeCheckParameter(localVarOptionals["contextLines"], "int32", "contextLines"); err != nil {
return nil, err
}
if err := typeCheckParameter(localVarOptionals["whitespace"], "string", "whitespace"); err != nil {
return nil, err
}
if localVarTempParam, localVarOk := localVarOptionals["contextLines"].(int32); localVarOk {
localVarQueryParams.Add("contextLines", parameterToString(localVarTempParam, ""))
}
if localVarTempParam, localVarOk := localVarOptionals["whitespace"].(string); localVarOk {
localVarQueryParams.Add("whitespace", parameterToString(localVarTempParam, ""))
}
// to determine the Content-Type header
localVarHTTPContentTypes := []string{}
// set Content-Type header
localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes)
if localVarHTTPContentType != "" {
localVarHeaderParams["Content-Type"] = localVarHTTPContentType
}
// to determine the Accept header
localVarHTTPHeaderAccepts := []string{""}
// set Accept header
localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts)
if localVarHTTPHeaderAccept != "" {
localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
}
r, err := a.client.prepareRequest(a.client.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
if err != nil {
return nil, err
}
localVarHTTPResponse, err := a.client.callAPI(r)
if err != nil || localVarHTTPResponse == nil {
return NewAPIResponseWithError(localVarHTTPResponse, nil, err)
}
defer localVarHTTPResponse.Body.Close()
if localVarHTTPResponse.StatusCode >= 300 {
bodyBytes, _ := ioutil.ReadAll(localVarHTTPResponse.Body)
return NewAPIResponseWithError(localVarHTTPResponse, bodyBytes, reportError("Status: %v, Body: %s", localVarHTTPResponse.Status, bodyBytes))
}
return NewRawAPIResponse(localVarHTTPResponse)
}
/* DefaultApiService
Streams a diff within a pull request. <p> If the specified file has been copied, moved or renamed, the <code>srcPath</code> must also be specified to produce the correct diff. <p> Note: This RESTful endpoint is currently <i>not paged</i>. The server will internally apply a hard cap to the streamed lines, and it is not possible to request subsequent pages if that cap is exceeded. <p> The authenticated user must have <strong>REPO_READ</strong> permission for the repository that this pull request targets to call this resource.

57
webhook.go Normal file
View File

@ -0,0 +1,57 @@
package bitbucketv1
import (
"bytes"
"encoding/json"
"net/http"
)
// WebHookRepoPush contains payload to use while reading handling webhooks from bitbucket
type WebHookRepoPush struct {
Actor Actor `json:"actor"`
Repository Repository `json:"repository"`
Push struct {
Changes []Change `json:"changes"`
} `json:"push"`
}
// Actor contains the actor of reported changes from a webhook
type Actor struct {
Username string `json:"username"`
DisplayName string `json:"displayName"`
}
// Change contains changes reported by webhooks
type Change struct {
Created bool `json:"created"`
Closed bool `json:"closed"`
Old interface{} `json:"old"`
New struct {
Type string `json:"type"`
Name string `json:"name"`
Target struct {
Type string `json:"type"`
Hash string `json:"hash"`
} `json:"target"`
} `json:"new"`
}
func TriggerRepoPush(webHookURL string, webHook WebHookRepoPush) (*http.Response, error) {
payLoad, err := json.Marshal(webHook)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", webHookURL, bytes.NewBuffer(payLoad))
if err != nil {
return nil, err
}
req.Header.Add("X-Event-Key", "repo:push")
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
req.Header.Set("User-Agent", "Bitbucket version: 5.12.0, Post webhook plugin version: 1.6.3")
req.Header.Set("X-Bitbucket-Type", "server")
client := &http.Client{}
return client.Do(req)
}