Skip to content

Commit

Permalink
✨ Mapped the new issue.Metadata methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
ctreminiom committed Oct 10, 2024
1 parent 97e55f7 commit 87f1568
Show file tree
Hide file tree
Showing 4 changed files with 572 additions and 1 deletion.
61 changes: 60 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ instance.Auth.SetBasicAuth("YOUR_CLIENT_MAIL", "YOUR_APP_ACCESS_TOKEN")

## ☕Cookbooks

For detailed examples and usage of the go-atlassian library, please refer to our Cookbook. This section provides step-by-step guides and code samples for common tasks and scenarios.
For detailed examples and usage of the go-atlassian library, please refer to our [**Cookbook**](https://docs.go-atlassian.io/cookbooks). This section provides step-by-step guides and code samples for common tasks and scenarios.

Check warning

Code scanning / Markdownlint (reported by Codacy)

Expected: 80; Actual: 227 Warning

Expected: 80; Actual: 227

-------------------------
## 🌍 Services
Expand Down Expand Up @@ -161,6 +161,65 @@ for _, transition := range issue.Transitions {

The rest of the service functions work much the same way; they are concise and behave as you would expect. The [documentation](https://docs.go-atlassian.io/) contains several examples on how to use each service function.


Check warning

Code scanning / Markdownlint (reported by Codacy)

Expected: 1; Actual: 2 Warning

Expected: 1; Actual: 2
## 📪Call a RAW API Endpoint

Check warning

Code scanning / Markdownlint (reported by Codacy)

Expected: 1; Actual: 0; Below Warning

Expected: 1; Actual: 0; Below
If you need to interact with an Atlassian API endpoint that hasn't been implemented in the `go-atlassian` library yet, you can make a custom API request using the built-in `Client.Call` method to execute raw HTTP requests.

Check warning

Code scanning / Markdownlint (reported by Codacy)

Expected: 80; Actual: 222 Warning

Expected: 80; Actual: 222

> Please raise an issue in order to implement the endpoint
```go
package main

import (
"context"
"fmt"
"github.com/ctreminiom/go-atlassian/jira/v3"
"log"
"net/http"
"os"
)

type IssueTypeMetadata struct {
IssueTypes []struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
} `json:"issueTypes"`
}

func main() {

var (
host = os.Getenv("SITE")
mail = os.Getenv("MAIL")
token = os.Getenv("TOKEN")
)

atlassian, err := v3.New(nil, host)
if err != nil {
log.Fatal(err)
}

atlassian.Auth.SetBasicAuth(mail, token)

// Define the RAW endpoint
apiEndpoint := "rest/api/3/issue/createmeta/KP/issuetypes"

request, err := atlassian.NewRequest(context.Background(), http.MethodGet, apiEndpoint, "", nil)

Check warning

Code scanning / Markdownlint (reported by Codacy)

Expected: 80; Actual: 102 Warning

Expected: 80; Actual: 102
if err != nil {
log.Fatal(err)
}

customResponseStruct := new(IssueTypeMetadata)
response, err := atlassian.Call(request, &customResponseStruct)
if err != nil {
log.Fatal(err)
}

fmt.Println(response.Status)
}
```

-------------------------
## ✍️ Contributions

Expand Down
107 changes: 107 additions & 0 deletions jira/internal/metadata_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"net/url"
"strconv"

"github.com/tidwall/gjson"

Expand Down Expand Up @@ -33,6 +34,8 @@ type MetadataService struct {

// Get edit issue metadata returns the edit screen fields for an issue that are visible to and editable by the user.
//
// Deprecated. Please use Issue.Metadata.FetchIssueMappings() and Issue.Metadata.FetchFieldMappings() instead.
//
// Use the information to populate the requests in Edit issue.
//
// GET /rest/api/{2-3}/issue/{issueKeyOrID}/editmeta
Expand All @@ -44,6 +47,8 @@ func (m *MetadataService) Get(ctx context.Context, issueKeyOrID string, override

// Create returns details of projects, issue types within projects, and, when requested,
//
// Deprecated. Please use Issue.Metadata.FetchIssueMappings() and Issue.Metadata.FetchFieldMappings() instead.
//
// the create screen fields for each issue type for the user.
//
// GET /rest/api/{2-3}/issue/createmeta
Expand All @@ -53,11 +58,113 @@ func (m *MetadataService) Create(ctx context.Context, opts *model.IssueMetadataC
return m.internalClient.Create(ctx, opts)
}

// FetchIssueMappings returns a page of issue type metadata for a specified project.
//
// Use the information to populate the requests in Create issue and Create issues.
//
// This operation can be accessed anonymously.
//
// GET /rest/api/{2-3}/issue/createmeta/{projectIdOrKey}/issuetypes
//
// Parameters:
// - ctx: The context for the request.
// - projectKeyOrID: The key or ID of the project.
// - startAt: The starting index of the returned issues.
// - maxResults: The maximum number of results to return.
//
// Returns:
// - A gjson.Result containing the issue type metadata.
// - A pointer to the response scheme.
// - An error if the retrieval fails.
//
// https://docs.go-atlassian.io/jira-software-cloud/issues/metadata#get-create-metadata-issue-types-for-a-project
func (m *MetadataService) FetchIssueMappings(ctx context.Context, projectKeyOrID string, startAt, maxResults int) (gjson.Result, *model.ResponseScheme, error) {
return m.internalClient.FetchIssueMappings(ctx, projectKeyOrID, startAt, maxResults)
}

// FetchFieldMappings returns a page of field metadata for a specified project and issue type.
//
// Use the information to populate the requests in Create issue and Create issues.
//
// This operation can be accessed anonymously.
//
// GET /rest/api/{2-3}/issue/createmeta/{projectIdOrKey}/fields/{issueTypeId}
//
// Parameters:
// - ctx: The context for the request.
// - projectKeyOrID: The key or ID of the project.
// - issueTypeID: The ID of the issue type whose metadata is to be retrieved.
// - startAt: The starting index of the returned fields.
// - maxResults: The maximum number of results to return.
//
// Returns:
// - A gjson.Result containing the field metadata.
// - A pointer to the response scheme.
// - An error if the retrieval fails.
//
// https://docs.go-atlassian.io/jira-software-cloud/issues/metadata#get-create-field-metadata-for-a-project-and-issue-type-id
func (m *MetadataService) FetchFieldMappings(ctx context.Context, projectKeyOrID, issueTypeID string, startAt, maxResults int) (gjson.Result, *model.ResponseScheme, error) {
return m.internalClient.FetchFieldMappings(ctx, projectKeyOrID, issueTypeID, startAt, maxResults)
}

type internalMetadataImpl struct {
c service.Connector
version string
}

func (i *internalMetadataImpl) FetchIssueMappings(ctx context.Context, projectKeyOrID string, startAt, maxResults int) (gjson.Result, *model.ResponseScheme, error) {

if projectKeyOrID == "" {
return gjson.Result{}, nil, model.ErrNoProjectIDOrKey
}

params := url.Values{}
params.Add("startAt", strconv.Itoa(startAt))
params.Add("maxResults", strconv.Itoa(maxResults))

endpoint := fmt.Sprintf("rest/api/%v/issue/createmeta/%v/issuetypes?%v", i.version, projectKeyOrID, params.Encode())

request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil)
if err != nil {
return gjson.Result{}, nil, err
}

response, err := i.c.Call(request, nil)
if err != nil {
return gjson.Result{}, response, err
}

return gjson.ParseBytes(response.Bytes.Bytes()), response, nil
}

func (i *internalMetadataImpl) FetchFieldMappings(ctx context.Context, projectKeyOrID, issueTypeID string, startAt, maxResults int) (gjson.Result, *model.ResponseScheme, error) {

if projectKeyOrID == "" {
return gjson.Result{}, nil, model.ErrNoProjectIDOrKey
}

if issueTypeID == "" {
return gjson.Result{}, nil, model.ErrNoIssueTypeID
}

params := url.Values{}
params.Add("startAt", strconv.Itoa(startAt))
params.Add("maxResults", strconv.Itoa(maxResults))
endpoint := fmt.Sprintf("rest/api/%v/issue/createmeta/%v/issuetypes/%v?%v", i.version, projectKeyOrID, issueTypeID, params.Encode())

request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil)
if err != nil {
return gjson.Result{}, nil, err
}

response, err := i.c.Call(request, nil)
if err != nil {
return gjson.Result{}, response, err
}

return gjson.ParseBytes(response.Bytes.Bytes()), response, nil
}

func (i *internalMetadataImpl) Get(ctx context.Context, issueKeyOrID string, overrideScreenSecurity, overrideEditableFlag bool) (gjson.Result, *model.ResponseScheme, error) {

if issueKeyOrID == "" {
Expand Down
Loading

0 comments on commit 87f1568

Please sign in to comment.