Skip to content

Commit

Permalink
refactor(component,jira): adopt new component interface
Browse files Browse the repository at this point in the history
  • Loading branch information
pinglin committed Dec 2, 2024
1 parent e2ae9d7 commit 886a3a1
Show file tree
Hide file tree
Showing 34 changed files with 2,633 additions and 2,546 deletions.
388 changes: 194 additions & 194 deletions pkg/component/application/jira/v0/README.mdx

Large diffs are not rendered by default.

68 changes: 15 additions & 53 deletions pkg/component/application/jira/v0/boards.go
Original file line number Diff line number Diff line change
@@ -1,90 +1,52 @@
package jira

import (
"context"
"fmt"

_ "embed"

"google.golang.org/protobuf/types/known/structpb"

"github.com/instill-ai/pipeline-backend/pkg/component/base"
"github.com/instill-ai/x/errmsg"
)

// Board is the Jira board object.
type Board struct {
ID int `json:"id"`
Name string `json:"name"`
Self string `json:"self"`
BoardType string `json:"type"`
}

type ListBoardsInput struct {
ProjectKeyOrID string `json:"project-key-or-id,omitempty" api:"projectKeyOrId"`
BoardType string `json:"board-type,omitempty" api:"type"`
Name string `json:"name,omitempty" api:"name"`
StartAt int `json:"start-at,omitempty" api:"startAt"`
MaxResults int `json:"max-results,omitempty" api:"maxResults"`
}
type ListBoardsResp struct {
Values []Board `json:"values"`
type listBoardsResp struct {
Boards []Board `json:"boards"`
StartAt int `json:"startAt"`
MaxResults int `json:"maxResults"`
Total int `json:"total"`
IsLast bool `json:"isLast"`
}

type ListBoardsOutput struct {
Boards []Board `json:"boards"`
StartAt int `json:"start-at"`
MaxResults int `json:"max-results"`
Total int `json:"total"`
IsLast bool `json:"is-last"`
}

func (jiraClient *Client) listBoardsTask(ctx context.Context, props *structpb.Struct) (*structpb.Struct, error) {
var opt ListBoardsInput
if err := base.ConvertFromStructpb(props, &opt); err != nil {
return nil, err
}

boards, err := jiraClient.listBoards(ctx, &opt)
if err != nil {
return nil, err
}
var output ListBoardsOutput
output.Boards = append(output.Boards, boards.Values...)
if output.Boards == nil {
output.Boards = []Board{}
}
output.StartAt = boards.StartAt
output.MaxResults = boards.MaxResults
output.IsLast = boards.IsLast
output.Total = boards.Total
return base.ConvertToStructpb(output)
}

func (jiraClient *Client) listBoards(_ context.Context, opt *ListBoardsInput) (*ListBoardsResp, error) {
func listBoards(c *jiraClient, opt *listBoardsInput) (*listBoardsResp, error) {
apiEndpoint := "rest/agile/1.0/board"

req := jiraClient.Client.R().SetResult(&ListBoardsResp{})
req := c.R().SetResult(&listBoardsResp{})
err := addQueryOptions(req, *opt)
if err != nil {
return nil, err
}
resp, err := req.Get(apiEndpoint)

if err != nil {
return nil, err
}
boards := resp.Result().(*ListBoardsResp)

boards := resp.Result().(*listBoardsResp)

return boards, err
}

type GetBoardResp struct {
type getBoardResp struct {
Location struct {
DisplayName string `json:"displayName"`
Name string `json:"name"`
DisplayName string `json:"displayName"`
Name string `json:"name"`

ProjectKey string `json:"projectKey"`
ProjectID int `json:"projectId"`
ProjectName string `json:"projectName"`
Expand All @@ -95,17 +57,17 @@ type GetBoardResp struct {
Board
}

func (jiraClient *Client) getBoard(_ context.Context, boardID int) (*GetBoardResp, error) {
func getBoard(c *jiraClient, boardID int) (*getBoardResp, error) {
apiEndpoint := fmt.Sprintf("rest/agile/1.0/board/%v", boardID)

req := jiraClient.Client.R().SetResult(&GetBoardResp{})
req := c.R().SetResult(&getBoardResp{})
resp, err := req.Get(apiEndpoint)
if err != nil {
return nil, fmt.Errorf(
err.Error(), errmsg.Message(err),
)
}
result := resp.Result().(*GetBoardResp)
result := resp.Result().(*getBoardResp)

return result, err
}
26 changes: 14 additions & 12 deletions pkg/component/application/jira/v0/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,27 @@ import (
"github.com/instill-ai/x/errmsg"
)

type Client struct {
const apiBaseURL = "https://api.atlassian.com"

type jiraClient struct {
*httpclient.Client
APIBaseURL string `json:"api-base-url"`
Domain string `json:"domain"`
CloudID string `json:"cloud-id"`
}

type CloudID struct {
type cloudID struct {
ID string `json:"cloudId"`
}

type AuthConfig struct {
type authConfig struct {
Email string `json:"email"`
Token string `json:"token"`
BaseURL string `json:"base-url"`
}

func newClient(_ context.Context, setup *structpb.Struct, logger *zap.Logger) (*Client, error) {
var authConfig AuthConfig
func newClient(_ context.Context, setup *structpb.Struct, logger *zap.Logger) (*jiraClient, error) {
var authConfig authConfig
if err := base.ConvertFromStructpb(setup, &authConfig); err != nil {
return nil, err
}
Expand All @@ -58,28 +60,28 @@ func newClient(_ context.Context, setup *structpb.Struct, logger *zap.Logger) (*
return nil, err
}

jiraClient := httpclient.New(
client := httpclient.New(
"Jira-Client",
baseURL,
httpclient.WithLogger(logger),
httpclient.WithEndUserError(new(errBody)),
)
jiraClient.
client.
SetHeader("Accept", "application/json").
SetHeader("Content-Type", "application/json").
SetBasicAuth(email, token)
client := &Client{
Client: jiraClient,

return &jiraClient{
Client: client,
APIBaseURL: apiBaseURL,
Domain: baseURL,
CloudID: cloudID,
}
return client, nil
}, nil
}

func getCloudID(baseURL string) (string, error) {
client := httpclient.New("Get-Domain-ID", baseURL, httpclient.WithEndUserError(new(errBody)))
resp := CloudID{}
resp := cloudID{}
req := client.R().SetResult(&resp)
// See https://developer.atlassian.com/cloud/jira/software/rest/intro/#base-url-differences
if _, err := req.Get("_edge/tenant_info"); err != nil {
Expand Down
Loading

0 comments on commit 886a3a1

Please sign in to comment.