Skip to content

Commit 6baf170

Browse files
committed
Take a string and parse
1 parent ee9abaa commit 6baf170

File tree

2 files changed

+70
-38
lines changed

2 files changed

+70
-38
lines changed

pkg/github/actions.go

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func ActionsRead(getClient GetClientFn, t translations.TranslationHelperFunc) (t
188188
return mcp.NewToolResultError(fmt.Sprintf("unknown action: %s", actionTypeStr)), nil
189189
}
190190

191-
resourceIDInt, err := RequiredInt(request, "resource_id")
191+
resourceID, err := RequiredParam[string](request, "resource_id")
192192
if err != nil {
193193
return mcp.NewToolResultError(err.Error()), nil
194194
}
@@ -203,23 +203,36 @@ func ActionsRead(getClient GetClientFn, t translations.TranslationHelperFunc) (t
203203
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
204204
}
205205

206+
var resourceIDInt int64
207+
var parseErr error
208+
switch resourceType {
209+
case actionsActionTypeGetWorkflow, actionsActionTypeListWorkflowRuns:
210+
// Do nothing, we accept both a string workflow ID or filename
211+
default:
212+
// For other actions, resource ID must be an integer
213+
resourceIDInt, parseErr = strconv.ParseInt(resourceID, 10, 64)
214+
if parseErr != nil {
215+
return mcp.NewToolResultError(fmt.Sprintf("invalid resource_id, must be an integer for action %s: %v", actionTypeStr, parseErr)), nil
216+
}
217+
}
218+
206219
switch resourceType {
207220
case actionsActionTypeGetWorkflow:
208-
return getWorkflow(ctx, client, request, owner, repo, int64(resourceIDInt))
221+
return getWorkflow(ctx, client, request, owner, repo, resourceID)
209222
case actionsActionTypeGetWorkflowRun:
210-
return getWorkflowRun(ctx, client, request, owner, repo, int64(resourceIDInt))
223+
return getWorkflowRun(ctx, client, request, owner, repo, resourceIDInt)
211224
case actionsActionTypeListWorkflowRuns:
212-
return listWorkflowRuns(ctx, client, request, owner, repo, int64(resourceIDInt), pagination)
225+
return listWorkflowRuns(ctx, client, request, owner, repo, resourceID, pagination)
213226
case actionsActionTypeGetWorkflowJob:
214-
return getWorkflowJob(ctx, client, request, owner, repo, int64(resourceIDInt))
227+
return getWorkflowJob(ctx, client, request, owner, repo, resourceIDInt)
215228
case actionsActionTypeListWorkflowJobs:
216-
return listWorkflowJobs(ctx, client, request, owner, repo, int64(resourceIDInt), pagination)
229+
return listWorkflowJobs(ctx, client, request, owner, repo, resourceIDInt, pagination)
217230
case actionsActionTypeDownloadWorkflowArtifact:
218-
return downloadWorkflowArtifact(ctx, client, request, owner, repo, int64(resourceIDInt))
231+
return downloadWorkflowArtifact(ctx, client, request, owner, repo, resourceIDInt)
219232
case actionsActionTypeListWorkflowArtifacts:
220-
return listWorkflowArtifacts(ctx, client, request, owner, repo, int64(resourceIDInt), pagination)
233+
return listWorkflowArtifacts(ctx, client, request, owner, repo, resourceIDInt, pagination)
221234
case actionsActionTypeGetWorkflowRunUsage:
222-
return getWorkflowRunUsage(ctx, client, request, owner, repo, int64(resourceIDInt))
235+
return getWorkflowRunUsage(ctx, client, request, owner, repo, resourceIDInt)
223236
case actionsActionTypeUnknown:
224237
return mcp.NewToolResultError(fmt.Sprintf("unknown action: %s", actionTypeStr)), nil
225238
default:
@@ -229,8 +242,17 @@ func ActionsRead(getClient GetClientFn, t translations.TranslationHelperFunc) (t
229242
}
230243
}
231244

232-
func getWorkflow(ctx context.Context, client *github.Client, _ mcp.CallToolRequest, owner, repo string, resourceID int64) (*mcp.CallToolResult, error) {
233-
workflow, resp, err := client.Actions.GetWorkflowByID(ctx, owner, repo, resourceID)
245+
func getWorkflow(ctx context.Context, client *github.Client, _ mcp.CallToolRequest, owner, repo string, resourceID string) (*mcp.CallToolResult, error) {
246+
var workflow *github.Workflow
247+
var resp *github.Response
248+
var err error
249+
250+
if workflowIDInt, parseErr := strconv.ParseInt(resourceID, 10, 64); parseErr == nil {
251+
workflow, resp, err = client.Actions.GetWorkflowByID(ctx, owner, repo, workflowIDInt)
252+
} else {
253+
workflow, resp, err = client.Actions.GetWorkflowByFileName(ctx, owner, repo, resourceID)
254+
}
255+
234256
if err != nil {
235257
return ghErrors.NewGitHubAPIErrorResponse(ctx, "failed to get workflow", resp, err), nil
236258
}
@@ -257,7 +279,7 @@ func getWorkflowRun(ctx context.Context, client *github.Client, _ mcp.CallToolRe
257279
return mcp.NewToolResultText(string(r)), nil
258280
}
259281

260-
func listWorkflowRuns(ctx context.Context, client *github.Client, request mcp.CallToolRequest, owner, repo string, resourceID int64, pagination PaginationParams) (*mcp.CallToolResult, error) {
282+
func listWorkflowRuns(ctx context.Context, client *github.Client, request mcp.CallToolRequest, owner, repo string, resourceID string, pagination PaginationParams) (*mcp.CallToolResult, error) {
261283
filterArgs, err := OptionalParam[map[string]any](request, "workflow_runs_filter")
262284
if err != nil {
263285
return mcp.NewToolResultError(err.Error()), nil
@@ -272,7 +294,7 @@ func listWorkflowRuns(ctx context.Context, client *github.Client, request mcp.Ca
272294
}
273295
}
274296

275-
workflowRuns, resp, err := client.Actions.ListWorkflowRunsByID(ctx, owner, repo, resourceID, &github.ListWorkflowRunsOptions{
297+
listWorkflowRunsOptions := &github.ListWorkflowRunsOptions{
276298
Actor: filterArgsTyped["actor"],
277299
Branch: filterArgsTyped["branch"],
278300
Event: filterArgsTyped["event"],
@@ -281,7 +303,17 @@ func listWorkflowRuns(ctx context.Context, client *github.Client, request mcp.Ca
281303
Page: pagination.Page,
282304
PerPage: pagination.PerPage,
283305
},
284-
})
306+
}
307+
308+
var workflowRuns *github.WorkflowRuns
309+
var resp *github.Response
310+
311+
if workflowIDInt, parseErr := strconv.ParseInt(resourceID, 10, 64); parseErr == nil {
312+
workflowRuns, resp, err = client.Actions.ListWorkflowRunsByID(ctx, owner, repo, workflowIDInt, listWorkflowRunsOptions)
313+
} else {
314+
workflowRuns, resp, err = client.Actions.ListWorkflowRunsByFileName(ctx, owner, repo, resourceID, listWorkflowRunsOptions)
315+
}
316+
285317
if err != nil {
286318
return ghErrors.NewGitHubAPIErrorResponse(ctx, "failed to list workflow runs", resp, err), nil
287319
}

pkg/github/actions_test.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ func Test_ListWorkflowRunArtifacts(t *testing.T) {
480480
"action": "list_workflow_artifacts",
481481
"owner": "owner",
482482
"repo": "repo",
483-
"resource_id": float64(12345),
483+
"resource_id": "12345",
484484
},
485485
expectError: false,
486486
},
@@ -558,7 +558,7 @@ func Test_DownloadWorkflowRunArtifact(t *testing.T) {
558558
"action": "download_workflow_artifact",
559559
"owner": "owner",
560560
"repo": "repo",
561-
"resource_id": float64(123),
561+
"resource_id": "123",
562562
},
563563
expectError: false,
564564
},
@@ -722,7 +722,7 @@ func Test_GetWorkflowRunUsage(t *testing.T) {
722722
"action": "get_workflow_run_usage",
723723
"owner": "owner",
724724
"repo": "repo",
725-
"resource_id": float64(12345),
725+
"resource_id": "12345",
726726
},
727727
expectError: false,
728728
},
@@ -1307,7 +1307,7 @@ func Test_ActionsResourceRead(t *testing.T) {
13071307
requestArgs: map[string]any{
13081308
"owner": "owner",
13091309
"repo": "repo",
1310-
"resource_id": float64(123),
1310+
"resource_id": "123",
13111311
},
13121312
expectError: true,
13131313
expectedErrMsg: "missing required parameter: action",
@@ -1318,7 +1318,7 @@ func Test_ActionsResourceRead(t *testing.T) {
13181318
requestArgs: map[string]any{
13191319
"action": "get_workflow",
13201320
"repo": "repo",
1321-
"resource_id": float64(123),
1321+
"resource_id": "123",
13221322
},
13231323
expectError: true,
13241324
expectedErrMsg: "missing required parameter: owner",
@@ -1329,7 +1329,7 @@ func Test_ActionsResourceRead(t *testing.T) {
13291329
requestArgs: map[string]any{
13301330
"action": "get_workflow",
13311331
"owner": "owner",
1332-
"resource_id": float64(123),
1332+
"resource_id": "123",
13331333
},
13341334
expectError: true,
13351335
expectedErrMsg: "missing required parameter: repo",
@@ -1352,7 +1352,7 @@ func Test_ActionsResourceRead(t *testing.T) {
13521352
"action": "random",
13531353
"owner": "owner",
13541354
"repo": "repo",
1355-
"resource_id": float64(123),
1355+
"resource_id": "123",
13561356
},
13571357
expectError: true,
13581358
expectedErrMsg: "unknown action: random",
@@ -1416,7 +1416,7 @@ func Test_ActionsResourceRead_GetWorkflow(t *testing.T) {
14161416
"action": "get_workflow",
14171417
"owner": "owner",
14181418
"repo": "repo",
1419-
"resource_id": float64(1),
1419+
"resource_id": "1",
14201420
},
14211421
expectError: false,
14221422
},
@@ -1434,7 +1434,7 @@ func Test_ActionsResourceRead_GetWorkflow(t *testing.T) {
14341434
"action": "get_workflow",
14351435
"owner": "owner",
14361436
"repo": "repo",
1437-
"resource_id": float64(2),
1437+
"resource_id": "2",
14381438
},
14391439
expectError: true,
14401440
expectedErrMsgRegexp: regexp.MustCompile(`^failed to get workflow: GET .*/repos/owner/repo/actions/workflows/2: 404.*$`),
@@ -1511,7 +1511,7 @@ func Test_ActionsResourceRead_GetWorkflowRun(t *testing.T) {
15111511
"action": "get_workflow_run",
15121512
"owner": "owner",
15131513
"repo": "repo",
1514-
"resource_id": float64(12345),
1514+
"resource_id": "12345",
15151515
},
15161516
expectError: false,
15171517
},
@@ -1529,7 +1529,7 @@ func Test_ActionsResourceRead_GetWorkflowRun(t *testing.T) {
15291529
"action": "get_workflow_run",
15301530
"owner": "owner",
15311531
"repo": "repo",
1532-
"resource_id": float64(99999),
1532+
"resource_id": "99999",
15331533
},
15341534
expectError: true,
15351535
expectedErrMsgRegexp: regexp.MustCompile(`^failed to get workflow run: GET .*/repos/owner/repo/actions/runs/99999: 404.*$`),
@@ -1620,7 +1620,7 @@ func Test_ActionsResourceRead_ListWorkflowRuns(t *testing.T) {
16201620
"action": "list_workflow_runs",
16211621
"owner": "owner",
16221622
"repo": "repo",
1623-
"resource_id": float64(1),
1623+
"resource_id": "1",
16241624
},
16251625
expectError: false,
16261626
},
@@ -1659,7 +1659,7 @@ func Test_ActionsResourceRead_ListWorkflowRuns(t *testing.T) {
16591659
"action": "list_workflow_runs",
16601660
"owner": "owner",
16611661
"repo": "repo",
1662-
"resource_id": float64(1),
1662+
"resource_id": "1",
16631663
"workflow_runs_filter": map[string]any{
16641664
"actor": "omgitsads",
16651665
"status": "completed",
@@ -1681,7 +1681,7 @@ func Test_ActionsResourceRead_ListWorkflowRuns(t *testing.T) {
16811681
"action": "list_workflow_runs",
16821682
"owner": "owner",
16831683
"repo": "repo",
1684-
"resource_id": float64(99999),
1684+
"resource_id": "99999",
16851685
},
16861686
expectError: true,
16871687
expectedErrMsgRegexp: regexp.MustCompile(`^failed to list workflow runs: GET .*/repos/owner/repo/actions/workflows/99999/runs.* 404.*$`),
@@ -1749,7 +1749,7 @@ func Test_ActionsResourceRead_GetWorkflowJob(t *testing.T) {
17491749
"action": "get_workflow_job",
17501750
"owner": "owner",
17511751
"repo": "repo",
1752-
"resource_id": float64(12345),
1752+
"resource_id": "12345",
17531753
},
17541754
expectError: false,
17551755
},
@@ -1767,7 +1767,7 @@ func Test_ActionsResourceRead_GetWorkflowJob(t *testing.T) {
17671767
"action": "get_workflow_job",
17681768
"owner": "owner",
17691769
"repo": "repo",
1770-
"resource_id": float64(99999),
1770+
"resource_id": "99999",
17711771
},
17721772
expectError: true,
17731773
expectedErrMsgRegexp: regexp.MustCompile(`^failed to get workflow job: GET .*/repos/owner/repo/actions/jobs/99999: 404.*$`),
@@ -1846,7 +1846,7 @@ func Test_ActionsResourceRead_ListWorkflowJobs(t *testing.T) {
18461846
"action": "list_workflow_jobs",
18471847
"owner": "owner",
18481848
"repo": "repo",
1849-
"resource_id": float64(1),
1849+
"resource_id": "1",
18501850
},
18511851
expectError: false,
18521852
},
@@ -1864,7 +1864,7 @@ func Test_ActionsResourceRead_ListWorkflowJobs(t *testing.T) {
18641864
"action": "list_workflow_jobs",
18651865
"owner": "owner",
18661866
"repo": "repo",
1867-
"resource_id": float64(99999),
1867+
"resource_id": "99999",
18681868
},
18691869
expectError: true,
18701870
expectedErrMsgRegexp: regexp.MustCompile(`^failed to list workflow jobs: GET .*/repos/owner/repo/actions/runs/99999/jobs.* 404.*$`),
@@ -1926,7 +1926,7 @@ func Test_ActionsResourceRead_DownloadWorkflowArtifact(t *testing.T) {
19261926
"action": "download_workflow_artifact",
19271927
"owner": "owner",
19281928
"repo": "repo",
1929-
"resource_id": float64(12345),
1929+
"resource_id": "12345",
19301930
},
19311931
expectError: false,
19321932
},
@@ -1944,7 +1944,7 @@ func Test_ActionsResourceRead_DownloadWorkflowArtifact(t *testing.T) {
19441944
"action": "download_workflow_artifact",
19451945
"owner": "owner",
19461946
"repo": "repo",
1947-
"resource_id": float64(99999),
1947+
"resource_id": "99999",
19481948
},
19491949
expectError: true,
19501950
expectedErrMsg: "failed to get artifact download URL: unexpected status code: 404 Not Found",
@@ -2019,7 +2019,7 @@ func Test_ActionsResourceRead_ListWorkflowArtifacts(t *testing.T) {
20192019
"action": "list_workflow_artifacts",
20202020
"owner": "owner",
20212021
"repo": "repo",
2022-
"resource_id": float64(1),
2022+
"resource_id": "1",
20232023
},
20242024
expectError: false,
20252025
},
@@ -2037,7 +2037,7 @@ func Test_ActionsResourceRead_ListWorkflowArtifacts(t *testing.T) {
20372037
"action": "list_workflow_artifacts",
20382038
"owner": "owner",
20392039
"repo": "repo",
2040-
"resource_id": float64(99999),
2040+
"resource_id": "99999",
20412041
},
20422042
expectError: true,
20432043
expectedErrMsgRegexp: regexp.MustCompile(`^failed to list workflow run artifacts: GET .*/repos/owner/repo/actions/runs/99999/artifacts.* 404.*$`),
@@ -2114,7 +2114,7 @@ func Test_ActionsResourceRead_GetWorkflowUsage(t *testing.T) {
21142114
"action": "get_workflow_run_usage",
21152115
"owner": "owner",
21162116
"repo": "repo",
2117-
"resource_id": float64(1),
2117+
"resource_id": "1",
21182118
},
21192119
expectError: false,
21202120
},
@@ -2132,7 +2132,7 @@ func Test_ActionsResourceRead_GetWorkflowUsage(t *testing.T) {
21322132
"action": "get_workflow_run_usage",
21332133
"owner": "owner",
21342134
"repo": "repo",
2135-
"resource_id": float64(99999),
2135+
"resource_id": "99999",
21362136
},
21372137
expectError: true,
21382138
expectedErrMsgRegexp: regexp.MustCompile(`^failed to get workflow run usage: GET .*/repos/owner/repo/actions/runs/99999/timing.* 404.*$`),

0 commit comments

Comments
 (0)