Skip to content

Commit 47f0ece

Browse files
committed
update go-github usage
1 parent b80806c commit 47f0ece

File tree

1 file changed

+23
-65
lines changed

1 file changed

+23
-65
lines changed

pkg/github/projects.go

Lines changed: 23 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -308,39 +308,25 @@ Note: item_id for add is the issue/PR ID; for update/delete it's the project ite
308308
}
309309

310310
func getProject(ctx context.Context, client *github.Client, owner string, ownerType string, projectNumber int) (*mcp.CallToolResult, error) {
311-
var url string
311+
var project *github.ProjectV2
312+
var resp *github.Response
313+
var err error
314+
312315
if ownerType == "org" {
313-
url = fmt.Sprintf("orgs/%s/projectsV2/%d", owner, projectNumber)
316+
project, resp, err = client.Projects.GetProjectForOrg(ctx, owner, projectNumber)
314317
} else {
315-
url = fmt.Sprintf("users/%s/projectsV2/%d", owner, projectNumber)
318+
project, resp, err = client.Projects.GetProjectForUser(ctx, owner, projectNumber)
316319
}
317320

318-
project := github.ProjectV2{}
319-
320-
httpRequest, err := client.NewRequest("GET", url, nil)
321-
if err != nil {
322-
return nil, fmt.Errorf("failed to create request: %w", err)
323-
}
324-
325-
resp, err := client.Do(ctx, httpRequest, &project)
326321
if err != nil {
327322
return ghErrors.NewGitHubAPIErrorResponse(ctx,
328323
"failed to get project",
329324
resp,
330325
err,
331326
), nil
332327
}
333-
defer func() { _ = resp.Body.Close() }()
334-
335-
if resp.StatusCode != http.StatusOK {
336-
body, err := io.ReadAll(resp.Body)
337-
if err != nil {
338-
return nil, fmt.Errorf("failed to read response body: %w", err)
339-
}
340-
return mcp.NewToolResultError(fmt.Sprintf("failed to get project: %s", string(body))), nil
341-
}
342328

343-
minimalProject := convertToMinimalProject(&project)
329+
minimalProject := convertToMinimalProject(project)
344330
r, err := json.Marshal(minimalProject)
345331
if err != nil {
346332
return nil, fmt.Errorf("failed to marshal response: %w", err)
@@ -350,15 +336,6 @@ func getProject(ctx context.Context, client *github.Client, owner string, ownerT
350336
}
351337

352338
func listProjects(ctx context.Context, client *github.Client, owner string, ownerType string, queryStr string, pagination paginationOptions) (*mcp.CallToolResult, error) {
353-
var url string
354-
if ownerType == "org" {
355-
url = fmt.Sprintf("orgs/%s/projectsV2", owner)
356-
} else {
357-
url = fmt.Sprintf("users/%s/projectsV2", owner)
358-
}
359-
projects := []github.ProjectV2{}
360-
minimalProjects := []MinimalProject{}
361-
362339
opts := &github.ListProjectsOptions{
363340
ListProjectsPaginationOptions: github.ListProjectsPaginationOptions{
364341
PerPage: pagination.PerPage,
@@ -368,36 +345,27 @@ func listProjects(ctx context.Context, client *github.Client, owner string, owne
368345
Query: queryStr,
369346
}
370347

371-
url, err := addOptions(url, opts)
372-
if err != nil {
373-
return nil, fmt.Errorf("failed to add options to request: %w", err)
374-
}
348+
var projects []*github.ProjectV2
349+
var resp *github.Response
350+
var err error
375351

376-
httpRequest, err := client.NewRequest("GET", url, nil)
377-
if err != nil {
378-
return nil, fmt.Errorf("failed to create request: %w", err)
352+
if ownerType == "org" {
353+
projects, resp, err = client.Projects.ListProjectsForOrg(ctx, owner, opts)
354+
} else {
355+
projects, resp, err = client.Projects.ListProjectsForUser(ctx, owner, opts)
379356
}
380357

381-
resp, err := client.Do(ctx, httpRequest, &projects)
382358
if err != nil {
383359
return ghErrors.NewGitHubAPIErrorResponse(ctx,
384360
"failed to list projects",
385361
resp,
386362
err,
387363
), nil
388364
}
389-
defer func() { _ = resp.Body.Close() }()
390365

366+
minimalProjects := []MinimalProject{}
391367
for _, project := range projects {
392-
minimalProjects = append(minimalProjects, *convertToMinimalProject(&project))
393-
}
394-
395-
if resp.StatusCode != http.StatusOK {
396-
body, err := io.ReadAll(resp.Body)
397-
if err != nil {
398-
return nil, fmt.Errorf("failed to read response body: %w", err)
399-
}
400-
return mcp.NewToolResultError(fmt.Sprintf("failed to list projects: %s", string(body))), nil
368+
minimalProjects = append(minimalProjects, *convertToMinimalProject(project))
401369
}
402370

403371
// Create response with pagination info
@@ -422,7 +390,7 @@ func getProjectField(ctx context.Context, client *github.Client, owner string, o
422390
url = fmt.Sprintf("users/%s/projectsV2/%d/fields/%d", owner, projectNumber, fieldID)
423391
}
424392

425-
projectField := projectV2Field{}
393+
var projectField github.ProjectV2Field
426394

427395
httpRequest, err := client.NewRequest("GET", url, nil)
428396
if err != nil {
@@ -446,6 +414,7 @@ func getProjectField(ctx context.Context, client *github.Client, owner string, o
446414
}
447415
return mcp.NewToolResultError(fmt.Sprintf("failed to get project field: %s", string(body))), nil
448416
}
417+
449418
r, err := json.Marshal(projectField)
450419
if err != nil {
451420
return nil, fmt.Errorf("failed to marshal response: %w", err)
@@ -461,7 +430,6 @@ func listProjectFields(ctx context.Context, client *github.Client, owner string,
461430
} else {
462431
url = fmt.Sprintf("users/%s/projectsV2/%d/fields", owner, projectNumber)
463432
}
464-
projectFields := []projectV2Field{}
465433

466434
type listProjectFieldsOptions struct {
467435
paginationOptions
@@ -481,6 +449,7 @@ func listProjectFields(ctx context.Context, client *github.Client, owner string,
481449
return nil, fmt.Errorf("failed to create request: %w", err)
482450
}
483451

452+
var projectFields []*github.ProjectV2Field
484453
resp, err := client.Do(ctx, httpRequest, &projectFields)
485454
if err != nil {
486455
return ghErrors.NewGitHubAPIErrorResponse(ctx,
@@ -756,22 +725,11 @@ type updateProjectItem struct {
756725
Value any `json:"value"`
757726
}
758727

759-
type projectV2Field struct {
760-
ID *int64 `json:"id,omitempty"` // The unique identifier for this field.
761-
NodeID string `json:"node_id,omitempty"` // The GraphQL node ID for this field.
762-
Name string `json:"name,omitempty"` // The display name of the field.
763-
DataType string `json:"data_type,omitempty"` // The data type of the field (e.g., "text", "number", "date", "single_select", "multi_select").
764-
URL string `json:"url,omitempty"` // The API URL for this field.
765-
Options []*any `json:"options,omitempty"` // Available options for single_select and multi_select fields.
766-
CreatedAt *github.Timestamp `json:"created_at,omitempty"` // The time when this field was created.
767-
UpdatedAt *github.Timestamp `json:"updated_at,omitempty"` // The time when this field was last updated.
768-
}
769-
770728
type projectV2ItemFieldValue struct {
771-
ID *int64 `json:"id,omitempty"` // The unique identifier for this field.
772-
Name string `json:"name,omitempty"` // The display name of the field.
773-
DataType string `json:"data_type,omitempty"` // The data type of the field (e.g., "text", "number", "date", "single_select", "multi_select").
774-
Value interface{} `json:"value,omitempty"` // The value of the field for a specific project item.
729+
ID *int64 `json:"id,omitempty"` // The unique identifier for this field.
730+
Name string `json:"name,omitempty"` // The display name of the field.
731+
DataType string `json:"data_type,omitempty"` // The data type of the field (e.g., "text", "number", "date", "single_select", "multi_select").
732+
Value any `json:"value,omitempty"` // The value of the field for a specific project item.
775733
}
776734

777735
type projectV2Item struct {

0 commit comments

Comments
 (0)