Skip to content

Commit a5db151

Browse files
committed
improv code
1 parent 44241af commit a5db151

File tree

1 file changed

+84
-105
lines changed

1 file changed

+84
-105
lines changed

pkg/github/issues.go

Lines changed: 84 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,10 +1784,12 @@ func Label(getGQLClient GetGQLClientFn, t translations.TranslationHelperFunc) (m
17841784
mcp.Enum("create", "get", "update", "delete"),
17851785
),
17861786
mcp.WithString("owner",
1787-
mcp.Description("Repository owner (username or organization name) - required for all operations"),
1787+
mcp.Required(),
1788+
mcp.Description("Repository owner (username or organization name)"),
17881789
),
17891790
mcp.WithString("repo",
1790-
mcp.Description("Repository name - required for all operations"),
1791+
mcp.Required(),
1792+
mcp.Description("Repository name"),
17911793
),
17921794
mcp.WithString("name",
17931795
mcp.Description("Label name. REQUIRED for: create, update, delete operations. OPTIONAL for: get operation (if omitted, lists all repository labels; if provided, gets specific label details)."),
@@ -1803,17 +1805,24 @@ func Label(getGQLClient GetGQLClientFn, t translations.TranslationHelperFunc) (m
18031805
),
18041806
),
18051807
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
1808+
// Get and validate required parameters
18061809
method, err := RequiredParam[string](request, "method")
18071810
if err != nil {
18081811
return mcp.NewToolResultError(err.Error()), nil
18091812
}
1810-
1811-
// Normalize
18121813
method = strings.ToLower(method)
18131814

1814-
// Basic params used across methods
1815-
owner, _ := OptionalParam[string](request, "owner")
1816-
repo, _ := OptionalParam[string](request, "repo")
1815+
owner, err := RequiredParam[string](request, "owner")
1816+
if err != nil {
1817+
return mcp.NewToolResultError(err.Error()), nil
1818+
}
1819+
1820+
repo, err := RequiredParam[string](request, "repo")
1821+
if err != nil {
1822+
return mcp.NewToolResultError(err.Error()), nil
1823+
}
1824+
1825+
// Get optional parameters
18171826
name, _ := OptionalParam[string](request, "name")
18181827
newName, _ := OptionalParam[string](request, "new_name")
18191828
color, _ := OptionalParam[string](request, "color")
@@ -1824,42 +1833,67 @@ func Label(getGQLClient GetGQLClientFn, t translations.TranslationHelperFunc) (m
18241833
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
18251834
}
18261835

1827-
switch method {
1828-
case "create":
1829-
// Validate required params for create
1830-
if owner == "" {
1831-
return mcp.NewToolResultError("owner is required for create"), nil
1832-
}
1833-
if repo == "" {
1834-
return mcp.NewToolResultError("repo is required for create"), nil
1836+
// Helper function to get repository ID
1837+
getRepositoryID := func() (githubv4.ID, error) {
1838+
var repoQuery struct {
1839+
Repository struct {
1840+
ID githubv4.ID
1841+
} `graphql:"repository(owner: $owner, name: $repo)"`
18351842
}
1836-
if name == "" {
1837-
return mcp.NewToolResultError("name is required for create"), nil
1843+
vars := map[string]any{
1844+
"owner": githubv4.String(owner),
1845+
"repo": githubv4.String(repo),
18381846
}
1839-
if color == "" {
1840-
return mcp.NewToolResultError("color is required for create"), nil
1847+
if err := client.Query(ctx, &repoQuery, vars); err != nil {
1848+
return "", err
18411849
}
1850+
return repoQuery.Repository.ID, nil
1851+
}
18421852

1843-
// Fetch repository node ID
1844-
var repoQuery struct {
1853+
// Helper function to get label by name
1854+
getLabelByName := func(labelName string) (githubv4.ID, error) {
1855+
var query struct {
18451856
Repository struct {
1846-
ID githubv4.ID
1857+
Label struct {
1858+
ID githubv4.ID
1859+
Name githubv4.String
1860+
} `graphql:"label(name: $name)"`
18471861
} `graphql:"repository(owner: $owner, name: $repo)"`
18481862
}
18491863
vars := map[string]any{
18501864
"owner": githubv4.String(owner),
18511865
"repo": githubv4.String(repo),
1866+
"name": githubv4.String(labelName),
18521867
}
1853-
if err := client.Query(ctx, &repoQuery, vars); err != nil {
1868+
if err := client.Query(ctx, &query, vars); err != nil {
1869+
return "", err
1870+
}
1871+
if query.Repository.Label.Name == "" {
1872+
return "", fmt.Errorf("label '%s' not found in %s/%s", labelName, owner, repo)
1873+
}
1874+
return query.Repository.Label.ID, nil
1875+
}
1876+
1877+
switch method {
1878+
case "create":
1879+
// Validate required params for create
1880+
if name == "" {
1881+
return mcp.NewToolResultError("name is required for create"), nil
1882+
}
1883+
if color == "" {
1884+
return mcp.NewToolResultError("color is required for create"), nil
1885+
}
1886+
1887+
// Get repository ID
1888+
repoID, err := getRepositoryID()
1889+
if err != nil {
18541890
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "Failed to find repository", err), nil
18551891
}
18561892

18571893
input := githubv4.CreateLabelInput{
1858-
RepositoryID: repoQuery.Repository.ID,
1894+
RepositoryID: repoID,
18591895
Name: githubv4.String(name),
1860-
}
1861-
if color != "" {
1862-
input.Color = githubv4.String(color)
1896+
Color: githubv4.String(color),
18631897
}
18641898
if description != "" {
18651899
d := githubv4.String(description)
@@ -1882,14 +1916,6 @@ func Label(getGQLClient GetGQLClientFn, t translations.TranslationHelperFunc) (m
18821916
return mcp.NewToolResultText(fmt.Sprintf("label %s created successfully", mutation.CreateLabel.Label.Name)), nil
18831917

18841918
case "get":
1885-
// Validate required params for get
1886-
if owner == "" {
1887-
return mcp.NewToolResultError("owner is required for get"), nil
1888-
}
1889-
if repo == "" {
1890-
return mcp.NewToolResultError("repo is required for get"), nil
1891-
}
1892-
18931919
if name != "" {
18941920
// Get specific label
18951921
var query struct {
@@ -1980,45 +2006,21 @@ func Label(getGQLClient GetGQLClientFn, t translations.TranslationHelperFunc) (m
19802006

19812007
case "update":
19822008
// Validate required params for update
1983-
if owner == "" {
1984-
return mcp.NewToolResultError("owner is required for update"), nil
1985-
}
1986-
if repo == "" {
1987-
return mcp.NewToolResultError("repo is required for update"), nil
1988-
}
19892009
if name == "" {
19902010
return mcp.NewToolResultError("name is required for update"), nil
19912011
}
19922012
if newName == "" && color == "" && description == "" {
1993-
return mcp.NewToolResultError("at least one of new_name, color or description must be provided for update"), nil
1994-
}
1995-
1996-
// Fetch the label to get its GQL ID
1997-
var query struct {
1998-
Repository struct {
1999-
Label struct {
2000-
ID githubv4.ID
2001-
Name githubv4.String
2002-
} `graphql:"label(name: $name)"`
2003-
} `graphql:"repository(owner: $owner, name: $repo)"`
2004-
}
2005-
2006-
vars := map[string]any{
2007-
"owner": githubv4.String(owner),
2008-
"repo": githubv4.String(repo),
2009-
"name": githubv4.String(name),
2010-
}
2011-
2012-
if err := client.Query(ctx, &query, vars); err != nil {
2013-
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "Failed to find label", err), nil
2013+
return mcp.NewToolResultError("at least one of name, color or description parameters must be provided for update"), nil
20142014
}
20152015

2016-
if query.Repository.Label.Name == "" {
2017-
return mcp.NewToolResultError(fmt.Sprintf("label '%s' not found in %s/%s", name, owner, repo)), nil
2016+
// Get the label ID
2017+
labelID, err := getLabelByName(name)
2018+
if err != nil {
2019+
return mcp.NewToolResultError(err.Error()), nil
20182020
}
20192021

20202022
input := githubv4.UpdateLabelInput{
2021-
ID: query.Repository.Label.ID,
2023+
ID: labelID,
20222024
}
20232025
if newName != "" {
20242026
n := githubv4.String(newName)
@@ -2050,41 +2052,18 @@ func Label(getGQLClient GetGQLClientFn, t translations.TranslationHelperFunc) (m
20502052

20512053
case "delete":
20522054
// Validate required params for delete
2053-
if owner == "" {
2054-
return mcp.NewToolResultError("owner is required for delete"), nil
2055-
}
2056-
if repo == "" {
2057-
return mcp.NewToolResultError("repo is required for delete"), nil
2058-
}
20592055
if name == "" {
20602056
return mcp.NewToolResultError("name is required for delete"), nil
20612057
}
20622058

2063-
var query struct {
2064-
Repository struct {
2065-
Label struct {
2066-
ID githubv4.ID
2067-
Name githubv4.String
2068-
} `graphql:"label(name: $name)"`
2069-
} `graphql:"repository(owner: $owner, name: $repo)"`
2070-
}
2071-
2072-
vars := map[string]any{
2073-
"owner": githubv4.String(owner),
2074-
"repo": githubv4.String(repo),
2075-
"name": githubv4.String(name),
2076-
}
2077-
2078-
if err := client.Query(ctx, &query, vars); err != nil {
2079-
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "Failed to find label", err), nil
2080-
}
2081-
2082-
if query.Repository.Label.Name == "" {
2083-
return mcp.NewToolResultError(fmt.Sprintf("label '%s' not found in %s/%s", name, owner, repo)), nil
2059+
// Get the label ID
2060+
labelID, err := getLabelByName(name)
2061+
if err != nil {
2062+
return mcp.NewToolResultError(err.Error()), nil
20842063
}
20852064

20862065
input := githubv4.DeleteLabelInput{
2087-
ID: query.Repository.Label.ID,
2066+
ID: labelID,
20882067
}
20892068

20902069
var mutation struct {
@@ -2098,10 +2077,10 @@ func Label(getGQLClient GetGQLClientFn, t translations.TranslationHelperFunc) (m
20982077
}
20992078

21002079
return mcp.NewToolResultText(fmt.Sprintf("label %s deleted successfully", name)), nil
2101-
}
21022080

2103-
// Should not reach here; ensure a return value for the compiler
2104-
return mcp.NewToolResultError("method did not return a result"), nil
2081+
default:
2082+
return mcp.NewToolResultError(fmt.Sprintf("unknown method: %s. Supported methods are: create, get, update, delete", method)), nil
2083+
}
21052084
}
21062085
}
21072086

@@ -2203,8 +2182,8 @@ func IssueLabel(getClient GetClientFn, t translations.TranslationHelperFunc) (mc
22032182
}
22042183

22052184
response := map[string]any{
2206-
"labels": issueLabels,
2207-
"totalCount": len(issueLabels),
2185+
"labels": issueLabels,
2186+
"totalCount": len(issueLabels),
22082187
}
22092188

22102189
out, err := json.Marshal(response)
@@ -2247,9 +2226,9 @@ func IssueLabel(getClient GetClientFn, t translations.TranslationHelperFunc) (mc
22472226
}
22482227

22492228
response := map[string]any{
2250-
"labels": issueLabels,
2251-
"totalCount": len(issueLabels),
2252-
"added": labels,
2229+
"labels": issueLabels,
2230+
"totalCount": len(issueLabels),
2231+
"added": labels,
22532232
}
22542233

22552234
out, err := json.Marshal(response)
@@ -2301,9 +2280,9 @@ func IssueLabel(getClient GetClientFn, t translations.TranslationHelperFunc) (mc
23012280
}
23022281

23032282
response := map[string]any{
2304-
"labels": issueLabels,
2305-
"totalCount": len(issueLabels),
2306-
"removed": labels,
2283+
"labels": issueLabels,
2284+
"totalCount": len(issueLabels),
2285+
"removed": labels,
23072286
}
23082287

23092288
out, err := json.Marshal(response)
@@ -2314,7 +2293,7 @@ func IssueLabel(getClient GetClientFn, t translations.TranslationHelperFunc) (mc
23142293
return mcp.NewToolResultText(string(out)), nil
23152294

23162295
default:
2317-
return mcp.NewToolResultError(fmt.Sprintf("unknown method: %s", method)), nil
2296+
return mcp.NewToolResultError(fmt.Sprintf("unknown method: %s. Supported methods are: list, add, remove", method)), nil
23182297
}
23192298
}
23202299
}

0 commit comments

Comments
 (0)