From 47e160ef650c1614fd90d29ef35fa6af19ffb416 Mon Sep 17 00:00:00 2001 From: Hugues Malphettes Date: Wed, 17 Apr 2019 10:01:47 +0800 Subject: [PATCH 01/22] Fix GetArchive Pass a writer to stream out the content of the archive. Support for multiple `path` by splitting the path parameter by comma. Add an optional parameter `apibasepath` that replaces the default base path `/api/1.0` as at least as seen in the wild, this base URL is different. --- default_api.go | 47 +++++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/default_api.go b/default_api.go index 87149e7..528da38 100644 --- a/default_api.go +++ b/default_api.go @@ -8,6 +8,7 @@ import ( "encoding/json" "fmt" "io" + "io/ioutil" "net/url" "strings" @@ -3267,16 +3268,20 @@ func (a *DefaultApiService) GetApplicationProperties() (*APIResponse, error) { /* DefaultApiService Streams an archive of the repository's contents at the requested commit. If no <code>at=</code> commit is requested, an archive of the default branch is streamed. <p> The <code>filename=</code> query parameter may be used to specify the exact filename to include in the <code>\"Content-Disposition\"</code> header. If an explicit filename is not provided, one will be automatically generated based on what is being archived. Its format depends on the <code>at=</code> value: <ul> <li>No <code>at=</code> commit: <code>&lt;slug&gt;-&lt;default-branch-name&gt;@&lt;commit&gt;.&lt;format&gt;</code>; e.g. example-master@43c2f8a0fe8.zip</li> <li><code>at=sha</code>: <code>&lt;slug&gt;-&lt;at&gt;.&lt;format&gt;</code>; e.g. example-09bcbb00100cfbb5310fb6834a1d5ce6cac253e9.tar.gz</li> <li><code>at=branchOrTag</code>: <code>&lt;slug&gt;-&lt;branchOrTag&gt;@&lt;commit&gt;.&lt;format&gt;</code>; e.g. example-feature@bbb225f16e1.tar <ul> <li>If the branch or tag is qualified (e.g. <code>refs/heads/master</code>, the short name (<code>master</code>) will be included in the filename</li> <li>If the branch or tag's <i>short name</i> includes slashes (e.g. <code>release/4.6</code>), they will be converted to hyphens in the filename (<code>release-4.5</code>)</li> </ul> </li> </ul> <p> Archives may be requested in the following formats by adding the <code>format=</code> query parameter: <ul> <li><code>zip</code>: A zip file using standard compression (Default)</li> <li><code>tar</code>: An uncompressed tarball</li> <li><code>tar.gz</code> or <code>tgz</code>: A GZip-compressed tarball</li> </ul> The contents of the archive may be filtered by using the <code>path=</code> query parameter to specify paths to include. <code>path=</code> may be specified multiple times to include multiple paths. <p> The <code>prefix=</code> query parameter may be used to define a directory (or multiple directories) where the archive's contents should be placed. If the prefix does not end with <code>/</code>, one will be added automatically. The prefix is <i>always</i> treated as a directory; it is not possible to use it to prepend characters to the entries in the archive. <p> Archives of public repositories may be streamed by any authenticated or anonymous user. Streaming archives for non-public repositories requires an <i>authenticated user</i> with at least <b>REPO_READ</b> permission. - + * @param ctx context.Context for authentication, logging, tracing, etc. @param optional (nil or map[string]interface{}) with one or more of: @param "at" (string) the commit to stream an archive of; if not supplied, an archive of the default branch is streamed @param "filename" (string) a filename to include the \"Content-Disposition\" header @param "format" (string) the format to stream the archive in; must be one of: zip, tar, tar.gz or tgz @param "path" (string) paths to include in the streamed archive; may be repeated to include multiple paths @param "prefix" (string) a prefix to apply to all entries in the streamed archive; if the supplied prefix does not end with a trailing <code>/</code>, one will be added automatically - @return + @param "apibasepath" (string) defaults to "/api/1.0" - in some installations, the value is "/archive/latest". Used as part of the URL endpoint. Not used as an actual parameter + + @param writer Where the archive's content is streamed out. + +@return the size of the archive written out */ -func (a *DefaultApiService) GetArchive(project, repository string, localVarOptionals map[string]interface{}) (*APIResponse, error) { +func (a *DefaultApiService) GetArchive(project, repository string, localVarOptionals map[string]interface{}, writer io.Writer) (int64, error) { var ( localVarHTTPMethod = strings.ToUpper("Get") localVarPostBody interface{} @@ -3285,7 +3290,16 @@ func (a *DefaultApiService) GetArchive(project, repository string, localVarOptio ) // create path and map variables - localVarPath := a.client.cfg.BasePath + "/api/1.0/projects/{projectKey}/repos/{repositorySlug}/archive" + if err := typeCheckParameter(localVarOptionals["apibasepath"], "string", "apibasepath"); err != nil { + return 0, err + } + apibasepath, _ := localVarOptionals["apibasepath"].(string) + if apibasepath == "" { + apibasepath = "/api/1.0" + } + + //localVarPath := a.client.cfg.BasePath + "/api/1.0/projects/{projectKey}/repos/{repositorySlug}/archive" + localVarPath := a.client.cfg.BasePath + apibasepath + "/projects/{projectKey}/repos/{repositorySlug}/archive" localVarPath = strings.Replace(localVarPath, "{"+"projectKey"+"}", fmt.Sprintf("%v", project), -1) localVarPath = strings.Replace(localVarPath, "{"+"repositorySlug"+"}", fmt.Sprintf("%v", repository), -1) @@ -3294,19 +3308,19 @@ func (a *DefaultApiService) GetArchive(project, repository string, localVarOptio localVarFormParams := url.Values{} if err := typeCheckParameter(localVarOptionals["at"], "string", "at"); err != nil { - return nil, err + return 0, err } if err := typeCheckParameter(localVarOptionals["filename"], "string", "filename"); err != nil { - return nil, err + return 0, err } if err := typeCheckParameter(localVarOptionals["format"], "string", "format"); err != nil { - return nil, err + return 0, err } if err := typeCheckParameter(localVarOptionals["path"], "string", "path"); err != nil { - return nil, err + return 0, err } if err := typeCheckParameter(localVarOptionals["prefix"], "string", "prefix"); err != nil { - return nil, err + return 0, err } if localVarTempParam, localVarOk := localVarOptionals["at"].(string); localVarOk { @@ -3319,7 +3333,9 @@ func (a *DefaultApiService) GetArchive(project, repository string, localVarOptio localVarQueryParams.Add("format", parameterToString(localVarTempParam, "")) } if localVarTempParam, localVarOk := localVarOptionals["path"].(string); localVarOk { - localVarQueryParams.Add("path", parameterToString(localVarTempParam, "")) + for _, path := range strings.Split(parameterToString(localVarTempParam, ""), ",") { + localVarQueryParams.Add("path", path) + } } if localVarTempParam, localVarOk := localVarOptionals["prefix"].(string); localVarOk { localVarQueryParams.Add("prefix", parameterToString(localVarTempParam, "")) @@ -3343,20 +3359,19 @@ func (a *DefaultApiService) GetArchive(project, repository string, localVarOptio } r, err := a.client.prepareRequest(a.client.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes) if err != nil { - return nil, err + return 0, err } localVarHTTPResponse, err := a.client.callAPI(r) if err != nil || localVarHTTPResponse == nil { - return NewAPIResponseWithError(localVarHTTPResponse, nil, err) + return 0, err } defer localVarHTTPResponse.Body.Close() if localVarHTTPResponse.StatusCode >= 300 { - bodyBytes, _ := io.ReadAll(localVarHTTPResponse.Body) - return NewAPIResponseWithError(localVarHTTPResponse, bodyBytes, reportError("Status: %v, Body: %s", localVarHTTPResponse.Status, bodyBytes)) + bodyBytes, _ := ioutil.ReadAll(localVarHTTPResponse.Body) + return 0, reportError("Status: %v, Body: %s", localVarHTTPResponse.Status, bodyBytes) } - - return NewRawAPIResponse(localVarHTTPResponse) + return io.Copy(writer, localVarHTTPResponse.Body) } /* From ce0f14930846d4fba7b88bc4dda5a75bf173d44e Mon Sep 17 00:00:00 2001 From: Hugues Malphettes Date: Sun, 26 May 2019 01:02:25 +0800 Subject: [PATCH 02/22] Fix Get Raw Content Add the missing variables, projectKey, repositorySlug. Buffer the http response body and use a response struct that does not assume that the raw content is valid JSON --- default_api.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/default_api.go b/default_api.go index 528da38..8f78ce3 100644 --- a/default_api.go +++ b/default_api.go @@ -4577,7 +4577,7 @@ func (a *DefaultApiService) GetContent_10(projectKey, repositorySlug string, loc /* DefaultApiService Retrieve the raw content for a file path at a specified revision. <p> The authenticated user must have <strong>REPO_READ</strong> permission for the specified repository to call this resource. - + * @param ctx context.Context for authentication, logging, tracing, etc. @param path the file path to retrieve content from @param optional (nil or map[string]interface{}) with one or more of: @param "at" (string) the commit ID or ref to retrieve the content for. @@ -4586,7 +4586,7 @@ func (a *DefaultApiService) GetContent_10(projectKey, repositorySlug string, loc @param "htmlEscape" (bool) (Optional) true if HTML should be escaped in the input markup, false otherwise. If not specified, {@link MarkupService} will use the value of the <code>markup.render.html.escape</code> property, which is <code>true</code> by default @return */ -func (a *DefaultApiService) GetContent_11(projectKey, repositorySlug, path string, localVarOptionals map[string]interface{}) (*APIResponse, error) { +func (a *DefaultApiService) GetRawContent(projectKey, repositorySlug, path string, localVarOptionals map[string]interface{}) (*APIResponse, error) { var ( localVarHTTPMethod = strings.ToUpper("Get") localVarPostBody interface{} @@ -4663,7 +4663,9 @@ func (a *DefaultApiService) GetContent_11(projectKey, repositorySlug, path strin return NewAPIResponseWithError(localVarHTTPResponse, bodyBytes, reportError("Status: %v, Body: %s", localVarHTTPResponse.Status, bodyBytes)) } - return NewRawAPIResponse(localVarHTTPResponse) + result := NewAPIResponse(localVarHTTPResponse) + result.Payload, err = ioutil.ReadAll(localVarHTTPResponse.Body) + return result, err } /* From 61677dd842949b3868e02ffd1eb5a246c3cd5282 Mon Sep 17 00:00:00 2001 From: Hugues Malphettes Date: Mon, 27 May 2019 09:31:25 +0800 Subject: [PATCH 03/22] Cleanup the debug --- default_api.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/default_api.go b/default_api.go index 8f78ce3..973edc3 100644 --- a/default_api.go +++ b/default_api.go @@ -4595,6 +4595,7 @@ func (a *DefaultApiService) GetRawContent(projectKey, repositorySlug, path strin ) // create path and map variables +<<<<<<< HEAD localVarPath := a.client.cfg.BasePath + "/api/1.0/projects/{projectKey}/repos/{repositorySlug}/raw/{path}" localVarPath = strings.Replace(localVarPath, "{"+"projectKey"+"}", fmt.Sprintf("%v", projectKey), -1) localVarPath = strings.Replace(localVarPath, "{"+"repositorySlug"+"}", fmt.Sprintf("%v", repositorySlug), -1) @@ -4602,6 +4603,12 @@ func (a *DefaultApiService) GetRawContent(projectKey, repositorySlug, path strin localVarPath = strings.Replace(localVarPath, "{"+"projectKey"+"}", fmt.Sprintf("%v", projectKey), -1) localVarPath = strings.Replace(localVarPath, "{"+"repositorySlug"+"}", fmt.Sprintf("%v", repositorySlug), -1) +======= + localVarPath := a.client.cfg.BasePath + "/" + projectKey + "/repos/" + repositorySlug + "/raw/" + path + localVarPath = strings.Replace(localVarPath, "/rest/", "/projects/", 1) + // https://bitbucket.global.standardchartered.com/projects/CISO/repos/planes/raw/enactments/tes_mattermost.yaml?at=refs%2Fheads%2Fstate%2Fdev + // https://bitbucket.global.standardchartered.com/projects/CISO/repos/planes/raw/enactments/tes_mattermost.yaml?at=refs%2Fheads%2Fstate%2Fdev +>>>>>>> 2cd48ac (Cleanup the debug) localVarHeaderParams := make(map[string]string) localVarQueryParams := url.Values{} localVarFormParams := url.Values{} From 11a5256ee7939f597f75f758c2c4d8b7233376e3 Mon Sep 17 00:00:00 2001 From: Hugues Malphettes Date: Wed, 10 Jul 2019 16:43:02 +0800 Subject: [PATCH 04/22] Add support for SetRepositoryPermissionGroups Support for https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-rest.html#idm8297426496: SetRepositoryPermissionGroups(projectKey, repositorySlug, permission, groupNames []string --- default_api.go | 72 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 64 insertions(+), 8 deletions(-) diff --git a/default_api.go b/default_api.go index 973edc3..1061309 100644 --- a/default_api.go +++ b/default_api.go @@ -4595,7 +4595,6 @@ func (a *DefaultApiService) GetRawContent(projectKey, repositorySlug, path strin ) // create path and map variables -<<<<<<< HEAD localVarPath := a.client.cfg.BasePath + "/api/1.0/projects/{projectKey}/repos/{repositorySlug}/raw/{path}" localVarPath = strings.Replace(localVarPath, "{"+"projectKey"+"}", fmt.Sprintf("%v", projectKey), -1) localVarPath = strings.Replace(localVarPath, "{"+"repositorySlug"+"}", fmt.Sprintf("%v", repositorySlug), -1) @@ -4603,12 +4602,6 @@ func (a *DefaultApiService) GetRawContent(projectKey, repositorySlug, path strin localVarPath = strings.Replace(localVarPath, "{"+"projectKey"+"}", fmt.Sprintf("%v", projectKey), -1) localVarPath = strings.Replace(localVarPath, "{"+"repositorySlug"+"}", fmt.Sprintf("%v", repositorySlug), -1) -======= - localVarPath := a.client.cfg.BasePath + "/" + projectKey + "/repos/" + repositorySlug + "/raw/" + path - localVarPath = strings.Replace(localVarPath, "/rest/", "/projects/", 1) - // https://bitbucket.global.standardchartered.com/projects/CISO/repos/planes/raw/enactments/tes_mattermost.yaml?at=refs%2Fheads%2Fstate%2Fdev - // https://bitbucket.global.standardchartered.com/projects/CISO/repos/planes/raw/enactments/tes_mattermost.yaml?at=refs%2Fheads%2Fstate%2Fdev ->>>>>>> 2cd48ac (Cleanup the debug) localVarHeaderParams := make(map[string]string) localVarQueryParams := url.Values{} localVarFormParams := url.Values{} @@ -5260,10 +5253,73 @@ func (a *DefaultApiService) GetGroupsWithoutAnyPermission_14(projectKey string, return NewBitbucketAPIResponse(localVarHTTPResponse) } +// AddRepositoryPermissionGroup https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-rest.html#idm8297426496 +func (a *DefaultApiService) SetRepositoryPermissionGroups(projectKey, repositorySlug, permission, groupNames []string, localVarHTTPContentTypes []string) (*APIResponse, error) { + var ( + localVarHTTPMethod = strings.ToUpper("Put") + localVarFileName string + localVarFileBytes []byte + localVarPostBody interface{} + ) + + // create path and map variables + localVarPath := a.client.cfg.BasePath + "/api/1.0/projects/{projectKey}/repos/{repositorySlug}/permissions/groups" + localVarPath = strings.Replace(localVarPath, "{"+"projectKey"+"}", fmt.Sprintf("%v", projectKey), -1) + localVarPath = strings.Replace(localVarPath, "{"+"repositorySlug"+"}", fmt.Sprintf("%v", repositorySlug), -1) + + localVarHeaderParams := make(map[string]string) + localVarQueryParams := url.Values{ + "name": groupNames, + "permission": []string{permission}, + } + localVarFormParams := url.Values{} + + // 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 { + if localVarHTTPResponse != nil { + return NewBitbucketAPIResponse(localVarHTTPResponse) + } + return nil, err + } + if localVarHTTPResponse == nil { + return nil, nil + } + defer localVarHTTPResponse.Body.Close() + if localVarHTTPResponse.StatusCode >= 300 { + bodyBytes, _ := ioutil.ReadAll(localVarHTTPResponse.Body) + return NewAPIResponseWithError(localVarHTTPResponse, reportError("Status: %v, Body: %s", localVarHTTPResponse.Status, bodyBytes)) + } + if localVarHTTPResponse.StatusCode == 204 { + return nil, nil + } + + return NewBitbucketAPIResponse(localVarHTTPResponse) +} + /* DefaultApiService Retrieve a page of groups that have no granted permissions for the specified repository. <p> The authenticated user must have <strong>REPO_ADMIN</strong> permission for the specified repository or a higher project or global permission to call this resource. - + * @param ctx context.Context for authentication, logging, tracing, etc. @param optional (nil or map[string]interface{}) with one or more of: @param "filter" (string) if specified only group names containing the supplied string will be returned @return From 02adb517626abe150ee4846afafa43c61b4b5391 Mon Sep 17 00:00:00 2001 From: Hugues Malphettes Date: Wed, 10 Jul 2019 16:44:24 +0800 Subject: [PATCH 05/22] Add SetRepositoryPermissionGroup(projectKey, repositorySlug, permission string, groupNames []string https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-rest.html#idm8297426496 --- default_api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/default_api.go b/default_api.go index 1061309..f0bdfa0 100644 --- a/default_api.go +++ b/default_api.go @@ -5254,7 +5254,7 @@ func (a *DefaultApiService) GetGroupsWithoutAnyPermission_14(projectKey string, } // AddRepositoryPermissionGroup https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-rest.html#idm8297426496 -func (a *DefaultApiService) SetRepositoryPermissionGroups(projectKey, repositorySlug, permission, groupNames []string, localVarHTTPContentTypes []string) (*APIResponse, error) { +func (a *DefaultApiService) SetRepositoryPermissionGroups(projectKey, repositorySlug, permission string, groupNames []string, localVarHTTPContentTypes []string) (*APIResponse, error) { var ( localVarHTTPMethod = strings.ToUpper("Put") localVarFileName string From 7c4d13674c1bf2111b3083aab04cd8e811d1286d Mon Sep 17 00:00:00 2001 From: Hugues Malphettes Date: Mon, 19 Aug 2019 19:42:35 +0800 Subject: [PATCH 06/22] Cleanup setpermissions --- default_api.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/default_api.go b/default_api.go index f0bdfa0..7ea6589 100644 --- a/default_api.go +++ b/default_api.go @@ -4593,14 +4593,11 @@ func (a *DefaultApiService) GetRawContent(projectKey, repositorySlug, path strin localVarFileName string localVarFileBytes []byte ) - // create path and map variables localVarPath := a.client.cfg.BasePath + "/api/1.0/projects/{projectKey}/repos/{repositorySlug}/raw/{path}" localVarPath = strings.Replace(localVarPath, "{"+"projectKey"+"}", fmt.Sprintf("%v", projectKey), -1) localVarPath = strings.Replace(localVarPath, "{"+"repositorySlug"+"}", fmt.Sprintf("%v", repositorySlug), -1) localVarPath = strings.Replace(localVarPath, "{"+"path"+"}", fmt.Sprintf("%v", path), -1) - localVarPath = strings.Replace(localVarPath, "{"+"projectKey"+"}", fmt.Sprintf("%v", projectKey), -1) - localVarPath = strings.Replace(localVarPath, "{"+"repositorySlug"+"}", fmt.Sprintf("%v", repositorySlug), -1) localVarHeaderParams := make(map[string]string) localVarQueryParams := url.Values{} From 792d95fe6ddd813915bd552723c02208bacbec48 Mon Sep 17 00:00:00 2001 From: Hugues Malphettes Date: Tue, 3 Sep 2019 21:08:26 +0800 Subject: [PATCH 07/22] Add the AccessTokenResponse https://docs.atlassian.com/bitbucket-server/rest/5.5.2/bitbucket-access-tokens-rest.html --- api_response.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/api_response.go b/api_response.go index d5242cb..d0972fe 100644 --- a/api_response.go +++ b/api_response.go @@ -538,6 +538,23 @@ func (p PermissionRepository) String() string { return string(p) } +type AccessTokenResponse struct { + ID string `json:"id"` + CreatedDate int64 `json:"createdDate"` + LastAuthenticated int64 `json:"lastAuthenticated"` + Name string `json:"name"` + Permissions []string `json:"permissions"` + User User `json:"user"` + Token string `json:"token"` +} + +// GetAccessTokenResponse cast AccessTokenResponse into structure +func GetAccessTokenResponse(r *APIResponse) (AccessTokenResponse, error) { + var m AccessTokenResponse + err := mapstructure.Decode(r.Values, &m) + return m, err +} + func (k *SSHKey) String() string { parts := make([]string, 1, 2) parts[0] = strings.TrimSpace(k.Text) From 3b3b6a8f5f95555c637d5b7f66e1039c48ac0492 Mon Sep 17 00:00:00 2001 From: Hugues Malphettes Date: Tue, 3 Sep 2019 21:11:49 +0800 Subject: [PATCH 08/22] Add the CreateAccessToken api https://docs.atlassian.com/bitbucket-server/rest/5.5.2/bitbucket-access-tokens-rest.html --- default_api.go | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/default_api.go b/default_api.go index 7ea6589..7e9fe35 100644 --- a/default_api.go +++ b/default_api.go @@ -10982,6 +10982,78 @@ func (a *DefaultApiService) SetPermissionForUsers_33(projectKey string, localVar return NewAPIResponse(localVarHTTPResponse), nil } +/* + DefaultApiService + + @param optional (nil or map[string]interface{}) with one or more of: + @param "name" (string) the names of the users + @param "permission" (string) the permission to grant + @return +*/ +func (a *DefaultApiService) CreateAccessToken(userSlug, tokenName string, repoAdmin, projectRead bool) (*APIResponse, error) { + var ( + localVarHTTPMethod = strings.ToUpper("Put") + localVarPostBody interface{} + localVarFileName string + localVarFileBytes []byte + ) + + // create path and map variables + // /rest/access-tokens/1.0/users/{userSlug} + localVarPath := a.client.cfg.BasePath + "/access-tokens/1.0/users/" + userSlug + + localVarHeaderParams := make(map[string]string) + localVarQueryParams := url.Values{} + localVarFormParams := url.Values{} + + // 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 + } + + permissions := make([]string, 0) + if repoAdmin { + permissions = append(permissions, "REPO_ADMIN") + } + if projectRead { + permissions = append(permissions, "PROJECT_READ") + } + localVarPostBody = map[string]interface{}{ + "name": tokenName, + "permissions": permissions, + } + + 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 NewBitbucketAPIResponse(localVarHTTPResponse) + } + defer localVarHTTPResponse.Body.Close() + if localVarHTTPResponse.StatusCode >= 300 { + bodyBytes, _ := ioutil.ReadAll(localVarHTTPResponse.Body) + return NewAPIResponseWithError(localVarHTTPResponse, reportError("Status: %v, Body: %s", localVarHTTPResponse.Status, bodyBytes)) + } + + return NewBitbucketAPIResponse(localVarHTTPResponse) +} + /* DefaultApiService From 9ae52f75c6c4af402c83a67ef0ef2c7153d7d99a Mon Sep 17 00:00:00 2001 From: Hugues Malphettes Date: Sat, 28 Sep 2019 15:34:58 +0800 Subject: [PATCH 09/22] Improve error message when forking a repository --- default_api.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/default_api.go b/default_api.go index 7e9fe35..2bf08ff 100644 --- a/default_api.go +++ b/default_api.go @@ -3056,8 +3056,8 @@ func (a *DefaultApiService) ForkRepository(projectKey, repositorySlug string, } defer localVarHTTPResponse.Body.Close() if localVarHTTPResponse.StatusCode >= 300 { - bodyBytes, _ := io.ReadAll(localVarHTTPResponse.Body) - return NewAPIResponseWithError(localVarHTTPResponse, bodyBytes, reportError("Status: %v, Body: %s", localVarHTTPResponse.Status, bodyBytes)) + bodyBytes, _ := ioutil.ReadAll(localVarHTTPResponse.Body) + return NewAPIResponseWithError(localVarHTTPResponse, reportError("Status: %s, Url: %s, Body: %s", localVarHTTPResponse.Status, localVarPath, bodyBytes)) } return NewBitbucketAPIResponse(localVarHTTPResponse) From 67fa18c768699a304c6c17fce3a82f01f774878c Mon Sep 17 00:00:00 2001 From: Hugues Malphettes Date: Sat, 28 Sep 2019 22:53:54 +0800 Subject: [PATCH 10/22] Add repo ssh key get and post --- default_api.go | 173 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) diff --git a/default_api.go b/default_api.go index 2bf08ff..eb74e75 100644 --- a/default_api.go +++ b/default_api.go @@ -8249,6 +8249,179 @@ func (a *DefaultApiService) GetSSHKeys(user string) (*APIResponse, error) { return NewBitbucketAPIResponse(localVarHTTPResponse) } +//https://docs.atlassian.com/bitbucket-server/rest/6.2.0/bitbucket-ssh-rest.html#idp17 +type KeysResponse struct { + Size int `json:"size"` + Limit int `json:"limit"` + IsLastPage bool `json:"isLastPage"` + Values []struct { + Key struct { + ID int `json:"id"` + Text string `json:"text"` + Label string `json:"label"` + } `json:"key"` + Project struct { + Key string `json:"key"` + ID int `json:"id"` + Name string `json:"name"` + Description string `json:"description"` + Public bool `json:"public"` + Type string `json:"type"` + Links struct { + Self []struct { + Href string `json:"href"` + } `json:"self"` + } `json:"links"` + } `json:"project"` + Permission string `json:"permission"` + } `json:"values"` + Start int `json:"start"` +} + +/*GetSSHRepoKeys retrieve ssh keys per repo, params + +https://docs.atlassian.com/bitbucket-server/rest/6.2.0/bitbucket-ssh-rest.html#idp17 + +/REST/KEYS/1.0//PROJECTS/{PROJECTKEY}/REPOS/{REPOSITORYSLUG}/SSH?FILTER&EFFECTIVE&PERMISSION + +*/ +func (a *DefaultApiService) GetSSHRepoKeys(projectKey, repositorySlug, sshKeyFilter string, repoKeysOnly, writeKeysOnly bool) (*KeysResponse, error) { + var ( + localVarHTTPMethod = strings.ToUpper("Get") + localVarPostBody interface{} + localVarFileName string + localVarFileBytes []byte + ) + + // create path and map variables + localVarPath := a.client.cfg.BasePath + "/keys/1.0/projects/{projectKey}/repos/{repositorySlug}/ssh" + localVarPath = strings.Replace(localVarPath, "{"+"projectKey"+"}", fmt.Sprintf("%v", projectKey), -1) + localVarPath = strings.Replace(localVarPath, "{"+"repositorySlug"+"}", fmt.Sprintf("%v", repositorySlug), -1) + + localVarHeaderParams := make(map[string]string) + localVarQueryParams := url.Values{} + localVarFormParams := url.Values{} + + if writeKeysOnly { + localVarQueryParams.Set("permission", "REPO_WRITE") + } + if repoKeysOnly { + localVarQueryParams.Add("effective", "true") + } + if sshKeyFilter != "" { + localVarQueryParams.Add("filter", sshKeyFilter) + } + + // 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 { + return nil, err + } + defer localVarHTTPResponse.Body.Close() + bodyBytes, _ := ioutil.ReadAll(localVarHTTPResponse.Body) + + if localVarHTTPResponse.StatusCode >= 300 { + return nil, errors.New(fmt.Sprintf("Error reading %s: %s", localVarPath, localVarHTTPResponse.Status)) + } + typedResp := KeysResponse{} + err = json.Unmarshal(bodyBytes, &typedResp) + return &typedResp, err +} + + +/*CreateSSHKey create ssh key for repository, params user and text +/rest/keys/1.0/projects/{projectKey}/repos/{repositorySlug}/ssh +{ + "key": { + "text": "ssh-rsa AAAAB3... me@127.0.0.1" + }, + "permission": "REPO_WRITE" +} +https://docs.atlassian.com/bitbucket-server/rest/6.2.0/bitbucket-ssh-rest.html#idp16*/ +func (a *DefaultApiService) CreateRepoSSHKey(projectKey, repositorySlug, sshPubKey string, isWrite bool) (*APIResponse, error) { + var ( + localVarHTTPMethod = strings.ToUpper("Post") + localVarPostBody interface{} + localVarFileName string + localVarFileBytes []byte + ) + + // create path and map variables + localVarPath := a.client.cfg.BasePath + "/keys/1.0/projects/{projectKey}/repos/{repositorySlug}/ssh" + localVarPath = strings.Replace(localVarPath, "{"+"projectKey"+"}", fmt.Sprintf("%v", projectKey), -1) + localVarPath = strings.Replace(localVarPath, "{"+"repositorySlug"+"}", fmt.Sprintf("%v", repositorySlug), -1) + + localVarHeaderParams := make(map[string]string) + localVarQueryParams := url.Values{} + localVarFormParams := url.Values{} + + permissions := "REPO_READ" + if isWrite { + permissions = "REPO_WRITE" + } + localVarPostBody = map[string]interface{}{ + "key": map[string]interface{}{ + "text": sshPubKey, + }, + "permissions": permissions, + } + + // to determine the Content-Type header + localVarHTTPContentTypes := []string{"application/json"} + + // 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 NewBitbucketAPIResponse(localVarHTTPResponse) + } + defer localVarHTTPResponse.Body.Close() + if localVarHTTPResponse.StatusCode >= 300 { + bodyBytes, _ := ioutil.ReadAll(localVarHTTPResponse.Body) + return NewAPIResponseWithError(localVarHTTPResponse, reportError("Status: %v, Body: %s", localVarHTTPResponse.Status, bodyBytes)) + } + + return NewBitbucketAPIResponse(localVarHTTPResponse) +} + /*CreateSSHKey create ssh key for user, params user and text */ func (a *DefaultApiService) CreateSSHKey(localVarOptionals map[string]interface{}) (*APIResponse, error) { var ( From 1a4b3950cbc099373301b82ef5a8b50b6f62ef23 Mon Sep 17 00:00:00 2001 From: Hugues Malphettes Date: Mon, 30 Sep 2019 07:00:58 +0800 Subject: [PATCH 11/22] Missing imports for the repo ssh keys --- default_api.go | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/default_api.go b/default_api.go index eb74e75..16457fe 100644 --- a/default_api.go +++ b/default_api.go @@ -6,6 +6,7 @@ package bitbucketv1 import ( "encoding/json" + "errors" "fmt" "io" "io/ioutil" @@ -8249,7 +8250,7 @@ func (a *DefaultApiService) GetSSHKeys(user string) (*APIResponse, error) { return NewBitbucketAPIResponse(localVarHTTPResponse) } -//https://docs.atlassian.com/bitbucket-server/rest/6.2.0/bitbucket-ssh-rest.html#idp17 +// https://docs.atlassian.com/bitbucket-server/rest/6.2.0/bitbucket-ssh-rest.html#idp17 type KeysResponse struct { Size int `json:"size"` Limit int `json:"limit"` @@ -8278,12 +8279,12 @@ type KeysResponse struct { Start int `json:"start"` } -/*GetSSHRepoKeys retrieve ssh keys per repo, params +/* +GetSSHRepoKeys retrieve ssh keys per repo, params https://docs.atlassian.com/bitbucket-server/rest/6.2.0/bitbucket-ssh-rest.html#idp17 /REST/KEYS/1.0//PROJECTS/{PROJECTKEY}/REPOS/{REPOSITORYSLUG}/SSH?FILTER&EFFECTIVE&PERMISSION - */ func (a *DefaultApiService) GetSSHRepoKeys(projectKey, repositorySlug, sshKeyFilter string, repoKeysOnly, writeKeysOnly bool) (*KeysResponse, error) { var ( @@ -8349,16 +8350,19 @@ func (a *DefaultApiService) GetSSHRepoKeys(projectKey, repositorySlug, sshKeyFil return &typedResp, err } - -/*CreateSSHKey create ssh key for repository, params user and text +/* +CreateSSHKey create ssh key for repository, params user and text /rest/keys/1.0/projects/{projectKey}/repos/{repositorySlug}/ssh -{ - "key": { - "text": "ssh-rsa AAAAB3... me@127.0.0.1" - }, - "permission": "REPO_WRITE" -} -https://docs.atlassian.com/bitbucket-server/rest/6.2.0/bitbucket-ssh-rest.html#idp16*/ + + { + "key": { + "text": "ssh-rsa AAAAB3... me@127.0.0.1" + }, + "permission": "REPO_WRITE" + } + +https://docs.atlassian.com/bitbucket-server/rest/6.2.0/bitbucket-ssh-rest.html#idp16 +*/ func (a *DefaultApiService) CreateRepoSSHKey(projectKey, repositorySlug, sshPubKey string, isWrite bool) (*APIResponse, error) { var ( localVarHTTPMethod = strings.ToUpper("Post") @@ -8381,7 +8385,7 @@ func (a *DefaultApiService) CreateRepoSSHKey(projectKey, repositorySlug, sshPubK permissions = "REPO_WRITE" } localVarPostBody = map[string]interface{}{ - "key": map[string]interface{}{ + "key": map[string]interface{}{ "text": sshPubKey, }, "permissions": permissions, From c32dc03fcbfa26c275906233717c0bcd6dedbd0b Mon Sep 17 00:00:00 2001 From: Hugues Malphettes Date: Mon, 30 Sep 2019 18:10:39 +0800 Subject: [PATCH 12/22] Fix setting REPO_WRITE permission on a repo ssh key --- default_api.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/default_api.go b/default_api.go index 16457fe..ed018b3 100644 --- a/default_api.go +++ b/default_api.go @@ -8380,15 +8380,15 @@ func (a *DefaultApiService) CreateRepoSSHKey(projectKey, repositorySlug, sshPubK localVarQueryParams := url.Values{} localVarFormParams := url.Values{} - permissions := "REPO_READ" + permission := "REPO_READ" if isWrite { - permissions = "REPO_WRITE" + permission = "REPO_WRITE" } localVarPostBody = map[string]interface{}{ "key": map[string]interface{}{ "text": sshPubKey, }, - "permissions": permissions, + "permission": permission, } // to determine the Content-Type header From 0216903e7a072be52523b8be554a3fab29e02c22 Mon Sep 17 00:00:00 2001 From: Hugues Malphettes Date: Sat, 12 Oct 2019 09:14:26 +0800 Subject: [PATCH 13/22] Fix the StreamFiles api --- default_api.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/default_api.go b/default_api.go index ed018b3..a78a418 100644 --- a/default_api.go +++ b/default_api.go @@ -12617,9 +12617,6 @@ func (a *DefaultApiService) StreamFiles(projectKey, repositorySlug string, local localVarQueryParams := url.Values{} localVarFormParams := url.Values{} - if err := typeCheckParameter(localVarOptionals["at"], "string", "at"); err != nil { - return nil, err - } if err := typeCheckParameter(localVarOptionals["limit"], "int", "limit"); err != nil { return nil, err } From 5fe08c4852ae641ca792d422bd6b95153ae2b19b Mon Sep 17 00:00:00 2001 From: Hugues Malphettes Date: Wed, 11 Dec 2019 11:07:38 +0800 Subject: [PATCH 14/22] Add a GetHeadCommit API Given a repo and a branch, resolve the head commit sha. --- default_api.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/default_api.go b/default_api.go index a78a418..62b554c 100644 --- a/default_api.go +++ b/default_api.go @@ -14683,3 +14683,66 @@ func (a *DefaultApiService) SearchCode(query SearchQuery) (*APIResponse, error) return NewBitbucketAPIResponse(localVarHTTPResponse) } + +// "http:///stash/rest/api/latest/projects//repos//commits?at=refs/heads/deploy&start=0&limit=0" +func (a *DefaultApiService) GetHeadCommit(project, repository, branch string, localVarOptionals map[string]interface{}) (*APIResponse, error) { + var ( + localVarHTTPMethod = strings.ToUpper("Get") + localVarPostBody interface{} + localVarFileName string + localVarFileBytes []byte + ) + // create path and map variables + if err := typeCheckParameter(localVarOptionals["apibasepath"], "string", "apibasepath"); err != nil { + return nil, err + } + apibasepath, _ := localVarOptionals["apibasepath"].(string) + if apibasepath == "" { + apibasepath = "/api/1.0" // or "apibasepath": "/api/latest" + } + + localVarPath := a.client.cfg.BasePath + apibasepath + "/projects/{projectKey}/repos/{repositorySlug}/commits?at=refs/heads/{branch}&start=0&limit=1" + localVarPath = strings.Replace(localVarPath, "{"+"projectKey"+"}", fmt.Sprintf("%v", project), -1) + localVarPath = strings.Replace(localVarPath, "{"+"repositorySlug"+"}", fmt.Sprintf("%v", repository), -1) + localVarPath = strings.Replace(localVarPath, "{"+"branch"+"}", fmt.Sprintf("%v", branch), -1) + + fmt.Println("localVarPath", localVarPath) + + localVarHeaderParams := make(map[string]string) + localVarQueryParams := url.Values{} + localVarFormParams := url.Values{} + + // 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 NewBitbucketAPIResponse(localVarHTTPResponse) + } + defer localVarHTTPResponse.Body.Close() + if localVarHTTPResponse.StatusCode >= 300 { + bodyBytes, _ := ioutil.ReadAll(localVarHTTPResponse.Body) + return NewAPIResponseWithError(localVarHTTPResponse, reportError("Status: %v, Body: %s", localVarHTTPResponse.Status, bodyBytes)) + } + + return NewBitbucketAPIResponse(localVarHTTPResponse) +} From abeaf149e91423a15a625cdadab59cfdcd548ed3 Mon Sep 17 00:00:00 2001 From: Hugues Malphettes Date: Wed, 11 Dec 2019 11:14:21 +0800 Subject: [PATCH 15/22] GetHeadCommit on a branch --- default_api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/default_api.go b/default_api.go index 62b554c..4e184b3 100644 --- a/default_api.go +++ b/default_api.go @@ -14701,7 +14701,7 @@ func (a *DefaultApiService) GetHeadCommit(project, repository, branch string, lo apibasepath = "/api/1.0" // or "apibasepath": "/api/latest" } - localVarPath := a.client.cfg.BasePath + apibasepath + "/projects/{projectKey}/repos/{repositorySlug}/commits?at=refs/heads/{branch}&start=0&limit=1" + localVarPath := a.client.cfg.BasePath + apibasepath + "/projects/{projectKey}/repos/{repositorySlug}/commits?until=refs/heads/{branch}&start=0&limit=1" localVarPath = strings.Replace(localVarPath, "{"+"projectKey"+"}", fmt.Sprintf("%v", project), -1) localVarPath = strings.Replace(localVarPath, "{"+"repositorySlug"+"}", fmt.Sprintf("%v", repository), -1) localVarPath = strings.Replace(localVarPath, "{"+"branch"+"}", fmt.Sprintf("%v", branch), -1) From aaa21ab88cd1b7c22487e2857c207d9f093518d4 Mon Sep 17 00:00:00 2001 From: Hugues Malphettes Date: Fri, 13 Dec 2019 14:53:33 +0800 Subject: [PATCH 16/22] Cleanup Copy this 3 times on the blackboard for your retention: fmt.Println shall not be committed fmt.Println shall not be committed fmt.Println shall not be committed ... --- default_api.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/default_api.go b/default_api.go index 4e184b3..6784974 100644 --- a/default_api.go +++ b/default_api.go @@ -14706,8 +14706,6 @@ func (a *DefaultApiService) GetHeadCommit(project, repository, branch string, lo localVarPath = strings.Replace(localVarPath, "{"+"repositorySlug"+"}", fmt.Sprintf("%v", repository), -1) localVarPath = strings.Replace(localVarPath, "{"+"branch"+"}", fmt.Sprintf("%v", branch), -1) - fmt.Println("localVarPath", localVarPath) - localVarHeaderParams := make(map[string]string) localVarQueryParams := url.Values{} localVarFormParams := url.Values{} From 0f2067afb7ed6e07d507520bc489882aaf4a9566 Mon Sep 17 00:00:00 2001 From: Hugues Malphettes Date: Sat, 1 Feb 2020 10:31:36 +0800 Subject: [PATCH 17/22] Fix CreateSSHKey --- default_api.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/default_api.go b/default_api.go index 6784974..b87343a 100644 --- a/default_api.go +++ b/default_api.go @@ -8453,7 +8453,9 @@ func (a *DefaultApiService) CreateSSHKey(localVarOptionals map[string]interface{ localVarQueryParams.Add("user", parameterToString(localVarTempParam, "")) } if localVarTempParam, localVarOk := localVarOptionals["text"].(string); localVarOk { - localVarFormParams.Add("text", parameterToString(localVarTempParam, "")) + localVarPostBody = map[string]interface{}{ + "text": localVarTempParam, + } } // to determine the Content-Type header From e0ef906417ff835232ed3a8ea6ba2bdfc9e86887 Mon Sep 17 00:00:00 2001 From: George Fleury Date: Wed, 30 Aug 2023 13:56:55 +0200 Subject: [PATCH 18/22] Fixes --- default_api.go | 22 +++++++++++----------- default_api_test.go | 9 +++++---- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/default_api.go b/default_api.go index b87343a..d1c9a5f 100644 --- a/default_api.go +++ b/default_api.go @@ -3057,8 +3057,8 @@ func (a *DefaultApiService) ForkRepository(projectKey, repositorySlug string, } defer localVarHTTPResponse.Body.Close() if localVarHTTPResponse.StatusCode >= 300 { - bodyBytes, _ := ioutil.ReadAll(localVarHTTPResponse.Body) - return NewAPIResponseWithError(localVarHTTPResponse, reportError("Status: %s, Url: %s, Body: %s", localVarHTTPResponse.Status, localVarPath, bodyBytes)) + bodyBytes, _ := io.ReadAll(localVarHTTPResponse.Body) + return NewAPIResponseWithError(localVarHTTPResponse, bodyBytes, reportError("Status: %s, Url: %s, Body: %s", localVarHTTPResponse.Status, localVarPath, bodyBytes)) } return NewBitbucketAPIResponse(localVarHTTPResponse) @@ -5304,8 +5304,8 @@ func (a *DefaultApiService) SetRepositoryPermissionGroups(projectKey, repository } defer localVarHTTPResponse.Body.Close() if localVarHTTPResponse.StatusCode >= 300 { - bodyBytes, _ := ioutil.ReadAll(localVarHTTPResponse.Body) - return NewAPIResponseWithError(localVarHTTPResponse, reportError("Status: %v, Body: %s", localVarHTTPResponse.Status, bodyBytes)) + bodyBytes, _ := io.ReadAll(localVarHTTPResponse.Body) + return NewAPIResponseWithError(localVarHTTPResponse, bodyBytes, reportError("Status: %v, Body: %s", localVarHTTPResponse.Status, bodyBytes)) } if localVarHTTPResponse.StatusCode == 204 { return nil, nil @@ -8419,8 +8419,8 @@ func (a *DefaultApiService) CreateRepoSSHKey(projectKey, repositorySlug, sshPubK } defer localVarHTTPResponse.Body.Close() if localVarHTTPResponse.StatusCode >= 300 { - bodyBytes, _ := ioutil.ReadAll(localVarHTTPResponse.Body) - return NewAPIResponseWithError(localVarHTTPResponse, reportError("Status: %v, Body: %s", localVarHTTPResponse.Status, bodyBytes)) + bodyBytes, _ := io.ReadAll(localVarHTTPResponse.Body) + return NewAPIResponseWithError(localVarHTTPResponse, bodyBytes, reportError("Status: %v, Body: %s", localVarHTTPResponse.Status, bodyBytes)) } return NewBitbucketAPIResponse(localVarHTTPResponse) @@ -11226,8 +11226,8 @@ func (a *DefaultApiService) CreateAccessToken(userSlug, tokenName string, repoAd } defer localVarHTTPResponse.Body.Close() if localVarHTTPResponse.StatusCode >= 300 { - bodyBytes, _ := ioutil.ReadAll(localVarHTTPResponse.Body) - return NewAPIResponseWithError(localVarHTTPResponse, reportError("Status: %v, Body: %s", localVarHTTPResponse.Status, bodyBytes)) + bodyBytes, _ := io.ReadAll(localVarHTTPResponse.Body) + return NewAPIResponseWithError(localVarHTTPResponse, bodyBytes, reportError("Status: %v, Body: %s", localVarHTTPResponse.Status, bodyBytes)) } return NewBitbucketAPIResponse(localVarHTTPResponse) @@ -14686,7 +14686,6 @@ func (a *DefaultApiService) SearchCode(query SearchQuery) (*APIResponse, error) return NewBitbucketAPIResponse(localVarHTTPResponse) } -// "http:///stash/rest/api/latest/projects//repos//commits?at=refs/heads/deploy&start=0&limit=0" func (a *DefaultApiService) GetHeadCommit(project, repository, branch string, localVarOptionals map[string]interface{}) (*APIResponse, error) { var ( localVarHTTPMethod = strings.ToUpper("Get") @@ -14694,6 +14693,7 @@ func (a *DefaultApiService) GetHeadCommit(project, repository, branch string, lo localVarFileName string localVarFileBytes []byte ) + // create path and map variables if err := typeCheckParameter(localVarOptionals["apibasepath"], "string", "apibasepath"); err != nil { return nil, err @@ -14740,8 +14740,8 @@ func (a *DefaultApiService) GetHeadCommit(project, repository, branch string, lo } defer localVarHTTPResponse.Body.Close() if localVarHTTPResponse.StatusCode >= 300 { - bodyBytes, _ := ioutil.ReadAll(localVarHTTPResponse.Body) - return NewAPIResponseWithError(localVarHTTPResponse, reportError("Status: %v, Body: %s", localVarHTTPResponse.Status, bodyBytes)) + bodyBytes, _ := io.ReadAll(localVarHTTPResponse.Body) + return NewAPIResponseWithError(localVarHTTPResponse, bodyBytes, reportError("Status: %v, Body: %s", localVarHTTPResponse.Status, bodyBytes)) } return NewBitbucketAPIResponse(localVarHTTPResponse) diff --git a/default_api_test.go b/default_api_test.go index 72f045e..9350e0a 100644 --- a/default_api_test.go +++ b/default_api_test.go @@ -2282,6 +2282,7 @@ func TestDefaultApiService_GetArchive(t *testing.T) { project string repository string localVarOptionals map[string]interface{} + writer io.Writer } tests := []struct { name string @@ -2300,7 +2301,7 @@ func TestDefaultApiService_GetArchive(t *testing.T) { a := &DefaultApiService{ client: tt.fields.client, } - got, err := a.GetArchive(tt.args.project, tt.args.repository, tt.args.localVarOptionals) + got, err := a.GetArchive(tt.args.project, tt.args.repository, tt.args.localVarOptionals, tt.args.writer) if (err != nil) != tt.wantErr { t.Errorf("DefaultApiService.GetArchive() error = %v, wantErr %v", err, tt.wantErr) return @@ -2963,13 +2964,13 @@ func TestDefaultApiService_GetContent_11(t *testing.T) { a := &DefaultApiService{ client: tt.fields.client, } - got, err := a.GetContent_11(tt.args.projectKey, tt.args.repositorySlug, tt.args.path, tt.args.localVarOptionals) + got, err := a.GetRawContent(tt.args.projectKey, tt.args.repositorySlug, tt.args.path, tt.args.localVarOptionals) if (err != nil) != tt.wantErr { - t.Errorf("DefaultApiService.GetContent_11() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("DefaultApiService.GetRawContent() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(got, tt.want) { - t.Errorf("DefaultApiService.GetContent_11() = %v, want %v", got, tt.want) + t.Errorf("DefaultApiService.GetRawContent() = %v, want %v", got, tt.want) } }) } From 3d427fc798a4332a8707ec551cad08abd0641742 Mon Sep 17 00:00:00 2001 From: George Fleury Date: Wed, 30 Aug 2023 13:57:45 +0200 Subject: [PATCH 19/22] Fixes --- default_api.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/default_api.go b/default_api.go index d1c9a5f..34a7321 100644 --- a/default_api.go +++ b/default_api.go @@ -9,7 +9,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net/url" "strings" @@ -3369,7 +3368,7 @@ func (a *DefaultApiService) GetArchive(project, repository string, localVarOptio } defer localVarHTTPResponse.Body.Close() if localVarHTTPResponse.StatusCode >= 300 { - bodyBytes, _ := ioutil.ReadAll(localVarHTTPResponse.Body) + bodyBytes, _ := io.ReadAll(localVarHTTPResponse.Body) return 0, reportError("Status: %v, Body: %s", localVarHTTPResponse.Status, bodyBytes) } return io.Copy(writer, localVarHTTPResponse.Body) @@ -4662,7 +4661,7 @@ func (a *DefaultApiService) GetRawContent(projectKey, repositorySlug, path strin } result := NewAPIResponse(localVarHTTPResponse) - result.Payload, err = ioutil.ReadAll(localVarHTTPResponse.Body) + result.Payload, err = io.ReadAll(localVarHTTPResponse.Body) return result, err } @@ -8340,7 +8339,7 @@ func (a *DefaultApiService) GetSSHRepoKeys(projectKey, repositorySlug, sshKeyFil return nil, err } defer localVarHTTPResponse.Body.Close() - bodyBytes, _ := ioutil.ReadAll(localVarHTTPResponse.Body) + bodyBytes, _ := io.ReadAll(localVarHTTPResponse.Body) if localVarHTTPResponse.StatusCode >= 300 { return nil, errors.New(fmt.Sprintf("Error reading %s: %s", localVarPath, localVarHTTPResponse.Status)) From 86c9000ec5956e35195da6cb195f1af3dd202890 Mon Sep 17 00:00:00 2001 From: George Fleury Date: Wed, 30 Aug 2023 13:59:49 +0200 Subject: [PATCH 20/22] Fix tests --- default_api_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/default_api_test.go b/default_api_test.go index 9350e0a..2dc13e7 100644 --- a/default_api_test.go +++ b/default_api_test.go @@ -2288,10 +2288,10 @@ func TestDefaultApiService_GetArchive(t *testing.T) { name string fields fields args args - want *APIResponse + want int64 wantErr, integrationTest bool }{ - {"networkErrorContextExceeded", fields{client: generateConfigFake()}, args{}, &APIResponse{Message: "Get https://stash.domain.com/rest/api/1.0/projects//repos//archive: context canceled"}, true, false}, + {"networkErrorContextExceeded", fields{client: generateConfigFake()}, args{}, 0, true, false}, } for _, tt := range tests { if tt.integrationTest != runIntegrationTests { From 32bc346e3b3c7a23b58c52bc11f90cd0cdb4ac5f Mon Sep 17 00:00:00 2001 From: George Fleury Date: Wed, 30 Aug 2023 14:08:15 +0200 Subject: [PATCH 21/22] Clean up --- default_api.go | 94 ++++++++++++--------------------------------- default_api_test.go | 4 +- 2 files changed, 26 insertions(+), 72 deletions(-) diff --git a/default_api.go b/default_api.go index 34a7321..c4eeaa2 100644 --- a/default_api.go +++ b/default_api.go @@ -6,7 +6,6 @@ package bitbucketv1 import ( "encoding/json" - "errors" "fmt" "io" "net/url" @@ -3281,7 +3280,7 @@ func (a *DefaultApiService) GetApplicationProperties() (*APIResponse, error) { @return the size of the archive written out */ -func (a *DefaultApiService) GetArchive(project, repository string, localVarOptionals map[string]interface{}, writer io.Writer) (int64, error) { +func (a *DefaultApiService) GetArchive(project, repository string, localVarOptionals map[string]interface{}, writer io.Writer) (*APIResponse, error) { var ( localVarHTTPMethod = strings.ToUpper("Get") localVarPostBody interface{} @@ -3291,7 +3290,7 @@ func (a *DefaultApiService) GetArchive(project, repository string, localVarOptio // create path and map variables if err := typeCheckParameter(localVarOptionals["apibasepath"], "string", "apibasepath"); err != nil { - return 0, err + return nil, err } apibasepath, _ := localVarOptionals["apibasepath"].(string) if apibasepath == "" { @@ -3308,19 +3307,19 @@ func (a *DefaultApiService) GetArchive(project, repository string, localVarOptio localVarFormParams := url.Values{} if err := typeCheckParameter(localVarOptionals["at"], "string", "at"); err != nil { - return 0, err + return nil, err } if err := typeCheckParameter(localVarOptionals["filename"], "string", "filename"); err != nil { - return 0, err + return nil, err } if err := typeCheckParameter(localVarOptionals["format"], "string", "format"); err != nil { - return 0, err + return nil, err } if err := typeCheckParameter(localVarOptionals["path"], "string", "path"); err != nil { - return 0, err + return nil, err } if err := typeCheckParameter(localVarOptionals["prefix"], "string", "prefix"); err != nil { - return 0, err + return nil, err } if localVarTempParam, localVarOk := localVarOptionals["at"].(string); localVarOk { @@ -3359,19 +3358,25 @@ func (a *DefaultApiService) GetArchive(project, repository string, localVarOptio } r, err := a.client.prepareRequest(a.client.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes) if err != nil { - return 0, err + return nil, err } localVarHTTPResponse, err := a.client.callAPI(r) if err != nil || localVarHTTPResponse == nil { - return 0, err + return NewAPIResponseWithError(localVarHTTPResponse, nil, err) } defer localVarHTTPResponse.Body.Close() if localVarHTTPResponse.StatusCode >= 300 { bodyBytes, _ := io.ReadAll(localVarHTTPResponse.Body) - return 0, reportError("Status: %v, Body: %s", localVarHTTPResponse.Status, bodyBytes) + return NewAPIResponseWithError(localVarHTTPResponse, bodyBytes, reportError("Status: %v, Body: %s", localVarHTTPResponse.Status, bodyBytes)) } - return io.Copy(writer, localVarHTTPResponse.Body) + + _, err = io.Copy(writer, localVarHTTPResponse.Body) + if err != nil { + return NewAPIResponseWithError(localVarHTTPResponse, nil, err) + } + + return NewRawAPIResponse(localVarHTTPResponse) } /* @@ -8249,43 +8254,7 @@ func (a *DefaultApiService) GetSSHKeys(user string) (*APIResponse, error) { return NewBitbucketAPIResponse(localVarHTTPResponse) } -// https://docs.atlassian.com/bitbucket-server/rest/6.2.0/bitbucket-ssh-rest.html#idp17 -type KeysResponse struct { - Size int `json:"size"` - Limit int `json:"limit"` - IsLastPage bool `json:"isLastPage"` - Values []struct { - Key struct { - ID int `json:"id"` - Text string `json:"text"` - Label string `json:"label"` - } `json:"key"` - Project struct { - Key string `json:"key"` - ID int `json:"id"` - Name string `json:"name"` - Description string `json:"description"` - Public bool `json:"public"` - Type string `json:"type"` - Links struct { - Self []struct { - Href string `json:"href"` - } `json:"self"` - } `json:"links"` - } `json:"project"` - Permission string `json:"permission"` - } `json:"values"` - Start int `json:"start"` -} - -/* -GetSSHRepoKeys retrieve ssh keys per repo, params - -https://docs.atlassian.com/bitbucket-server/rest/6.2.0/bitbucket-ssh-rest.html#idp17 - -/REST/KEYS/1.0//PROJECTS/{PROJECTKEY}/REPOS/{REPOSITORYSLUG}/SSH?FILTER&EFFECTIVE&PERMISSION -*/ -func (a *DefaultApiService) GetSSHRepoKeys(projectKey, repositorySlug, sshKeyFilter string, repoKeysOnly, writeKeysOnly bool) (*KeysResponse, error) { +func (a *DefaultApiService) GetSSHRepoKeys(projectKey, repositorySlug, sshKeyFilter string, repoKeysOnly, writeKeysOnly bool) (*APIResponse, error) { var ( localVarHTTPMethod = strings.ToUpper("Get") localVarPostBody interface{} @@ -8335,33 +8304,18 @@ func (a *DefaultApiService) GetSSHRepoKeys(projectKey, repositorySlug, sshKeyFil } localVarHTTPResponse, err := a.client.callAPI(r) - if err != nil { - return nil, err + if err != nil || localVarHTTPResponse == nil { + return NewAPIResponseWithError(localVarHTTPResponse, nil, err) } defer localVarHTTPResponse.Body.Close() - bodyBytes, _ := io.ReadAll(localVarHTTPResponse.Body) - if localVarHTTPResponse.StatusCode >= 300 { - return nil, errors.New(fmt.Sprintf("Error reading %s: %s", localVarPath, localVarHTTPResponse.Status)) + bodyBytes, _ := io.ReadAll(localVarHTTPResponse.Body) + return NewAPIResponseWithError(localVarHTTPResponse, bodyBytes, reportError("Status: %v, Body: %s", localVarHTTPResponse.Status, bodyBytes)) } - typedResp := KeysResponse{} - err = json.Unmarshal(bodyBytes, &typedResp) - return &typedResp, err + + return NewBitbucketAPIResponse(localVarHTTPResponse) } -/* -CreateSSHKey create ssh key for repository, params user and text -/rest/keys/1.0/projects/{projectKey}/repos/{repositorySlug}/ssh - - { - "key": { - "text": "ssh-rsa AAAAB3... me@127.0.0.1" - }, - "permission": "REPO_WRITE" - } - -https://docs.atlassian.com/bitbucket-server/rest/6.2.0/bitbucket-ssh-rest.html#idp16 -*/ func (a *DefaultApiService) CreateRepoSSHKey(projectKey, repositorySlug, sshPubKey string, isWrite bool) (*APIResponse, error) { var ( localVarHTTPMethod = strings.ToUpper("Post") diff --git a/default_api_test.go b/default_api_test.go index 2dc13e7..9350e0a 100644 --- a/default_api_test.go +++ b/default_api_test.go @@ -2288,10 +2288,10 @@ func TestDefaultApiService_GetArchive(t *testing.T) { name string fields fields args args - want int64 + want *APIResponse wantErr, integrationTest bool }{ - {"networkErrorContextExceeded", fields{client: generateConfigFake()}, args{}, 0, true, false}, + {"networkErrorContextExceeded", fields{client: generateConfigFake()}, args{}, &APIResponse{Message: "Get https://stash.domain.com/rest/api/1.0/projects//repos//archive: context canceled"}, true, false}, } for _, tt := range tests { if tt.integrationTest != runIntegrationTests { From 8c5c25aeccb04b1587be625f5163a3ed2f908538 Mon Sep 17 00:00:00 2001 From: George Fleury Date: Wed, 30 Aug 2023 14:09:03 +0200 Subject: [PATCH 22/22] Clean up --- default_api.go | 1 - 1 file changed, 1 deletion(-) diff --git a/default_api.go b/default_api.go index c4eeaa2..e664649 100644 --- a/default_api.go +++ b/default_api.go @@ -5255,7 +5255,6 @@ func (a *DefaultApiService) GetGroupsWithoutAnyPermission_14(projectKey string, return NewBitbucketAPIResponse(localVarHTTPResponse) } -// AddRepositoryPermissionGroup https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-rest.html#idm8297426496 func (a *DefaultApiService) SetRepositoryPermissionGroups(projectKey, repositorySlug, permission string, groupNames []string, localVarHTTPContentTypes []string) (*APIResponse, error) { var ( localVarHTTPMethod = strings.ToUpper("Put")