|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/github/github-mcp-server/internal/githubv4mock" |
| 11 | + "github.com/github/github-mcp-server/pkg/translations" |
| 12 | + gh "github.com/google/go-github/v79/github" |
| 13 | + "github.com/shurcooL/githubv4" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +func Test_CreateProject(t *testing.T) { |
| 19 | + toolDef := CreateProject(translations.NullTranslationHelper) |
| 20 | + |
| 21 | + t.Run("success user project", func(t *testing.T) { |
| 22 | + mockedClient := githubv4mock.NewMockedHTTPClient( |
| 23 | + // Mock getOwnerNodeID for user |
| 24 | + githubv4mock.NewQueryMatcher( |
| 25 | + struct { |
| 26 | + User struct { |
| 27 | + ID string |
| 28 | + } `graphql:"user(login: $login)"` |
| 29 | + }{}, |
| 30 | + map[string]any{ |
| 31 | + "login": githubv4.String("octocat"), |
| 32 | + }, |
| 33 | + githubv4mock.DataResponse(map[string]any{ |
| 34 | + "user": map[string]any{ |
| 35 | + "id": "U_octocat", |
| 36 | + }, |
| 37 | + }), |
| 38 | + ), |
| 39 | + // Mock createProjectV2 mutation |
| 40 | + githubv4mock.NewMutationMatcher( |
| 41 | + struct { |
| 42 | + CreateProjectV2 struct { |
| 43 | + ProjectV2 struct { |
| 44 | + ID string |
| 45 | + Number int |
| 46 | + Title string |
| 47 | + URL string |
| 48 | + } |
| 49 | + } `graphql:"createProjectV2(input: $input)"` |
| 50 | + }{}, |
| 51 | + githubv4.CreateProjectV2Input{ |
| 52 | + OwnerID: githubv4.ID("U_octocat"), |
| 53 | + Title: githubv4.String("New Project"), |
| 54 | + }, |
| 55 | + nil, |
| 56 | + githubv4mock.DataResponse(map[string]any{ |
| 57 | + "createProjectV2": map[string]any{ |
| 58 | + "projectV2": map[string]any{ |
| 59 | + "id": "PVT_project123", |
| 60 | + "number": 1, |
| 61 | + "title": "New Project", |
| 62 | + "url": "https://github.com/users/octocat/projects/1", |
| 63 | + }, |
| 64 | + }, |
| 65 | + }), |
| 66 | + ), |
| 67 | + ) |
| 68 | + |
| 69 | + client := githubv4.NewClient(mockedClient) |
| 70 | + deps := BaseDeps{ |
| 71 | + GQLClient: client, |
| 72 | + } |
| 73 | + handler := toolDef.Handler(deps) |
| 74 | + request := createMCPRequest(map[string]any{ |
| 75 | + "owner": "octocat", |
| 76 | + "owner_type": "user", |
| 77 | + "title": "New Project", |
| 78 | + }) |
| 79 | + result, err := handler(ContextWithDeps(context.Background(), deps), &request) |
| 80 | + |
| 81 | + require.NoError(t, err) |
| 82 | + require.False(t, result.IsError) |
| 83 | + |
| 84 | + textContent := getTextResult(t, result) |
| 85 | + var response map[string]any |
| 86 | + err = json.Unmarshal([]byte(textContent.Text), &response) |
| 87 | + require.NoError(t, err) |
| 88 | + assert.Equal(t, "PVT_project123", response["ID"]) |
| 89 | + assert.Equal(t, float64(1), response["Number"]) |
| 90 | + }) |
| 91 | +} |
| 92 | + |
| 93 | +func Test_CreateIterationField(t *testing.T) { |
| 94 | + toolDef := CreateIterationField(translations.NullTranslationHelper) |
| 95 | + |
| 96 | + t.Run("success", func(t *testing.T) { |
| 97 | + // REST client for getProjectNodeID |
| 98 | + mockRESTClient := MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ |
| 99 | + GetOrgsProjectsV2ByProject: mockResponse(t, http.StatusOK, map[string]any{ |
| 100 | + "id": 1, |
| 101 | + "node_id": "PVT_project1", |
| 102 | + "title": "Org Project", |
| 103 | + }), |
| 104 | + }) |
| 105 | + |
| 106 | + // GraphQL client for mutations |
| 107 | + mockGQLClient := githubv4mock.NewMockedHTTPClient( |
| 108 | + // Step 1: Create Field |
| 109 | + githubv4mock.NewMutationMatcher( |
| 110 | + struct { |
| 111 | + CreateProjectV2Field struct { |
| 112 | + ProjectV2Field struct { |
| 113 | + ProjectV2IterationField struct { |
| 114 | + ID string |
| 115 | + Name string |
| 116 | + } `graphql:"... on ProjectV2IterationField"` |
| 117 | + } |
| 118 | + } `graphql:"createProjectV2Field(input: $input)"` |
| 119 | + }{}, |
| 120 | + githubv4.CreateProjectV2FieldInput{ |
| 121 | + ProjectID: githubv4.ID("PVT_project1"), |
| 122 | + DataType: githubv4.ProjectV2CustomFieldType("ITERATION"), |
| 123 | + Name: githubv4.String("Sprint"), |
| 124 | + }, |
| 125 | + nil, |
| 126 | + githubv4mock.DataResponse(map[string]any{ |
| 127 | + "createProjectV2Field": map[string]any{ |
| 128 | + "projectV2Field": map[string]any{ |
| 129 | + "id": "PVTIF_field1", |
| 130 | + "name": "Sprint", |
| 131 | + }, |
| 132 | + }, |
| 133 | + }), |
| 134 | + ), |
| 135 | + // Step 2: Update Field Configuration |
| 136 | + githubv4mock.NewMutationMatcher( |
| 137 | + struct { |
| 138 | + UpdateProjectV2Field struct { |
| 139 | + ProjectV2Field struct { |
| 140 | + ProjectV2IterationField struct { |
| 141 | + ID string |
| 142 | + Name string |
| 143 | + Configuration struct { |
| 144 | + Iterations []struct { |
| 145 | + ID string |
| 146 | + Title string |
| 147 | + StartDate string |
| 148 | + Duration int |
| 149 | + } |
| 150 | + } |
| 151 | + } `graphql:"... on ProjectV2IterationField"` |
| 152 | + } |
| 153 | + } `graphql:"updateProjectV2Field(input: $input)"` |
| 154 | + }{}, |
| 155 | + UpdateProjectV2FieldInput{ |
| 156 | + ProjectID: githubv4.ID("PVT_project1"), |
| 157 | + FieldID: githubv4.ID("PVTIF_field1"), |
| 158 | + IterationConfiguration: &ProjectV2IterationFieldConfigurationInput{ |
| 159 | + Duration: githubv4.Int(7), |
| 160 | + StartDate: githubv4.Date{Time: time.Date(2025, 1, 20, 0, 0, 0, 0, time.UTC)}, |
| 161 | + Iterations: &[]ProjectV2IterationFieldIterationInput{ |
| 162 | + { |
| 163 | + Title: githubv4.String("Sprint 1"), |
| 164 | + StartDate: githubv4.Date{Time: time.Date(2025, 1, 20, 0, 0, 0, 0, time.UTC)}, |
| 165 | + Duration: githubv4.Int(7), |
| 166 | + }, |
| 167 | + }, |
| 168 | + }, |
| 169 | + }, |
| 170 | + nil, |
| 171 | + githubv4mock.DataResponse(map[string]any{ |
| 172 | + "updateProjectV2Field": map[string]any{ |
| 173 | + "projectV2Field": map[string]any{ |
| 174 | + "id": "PVTIF_field1", |
| 175 | + "name": "Sprint", |
| 176 | + "configuration": map[string]any{ |
| 177 | + "iterations": []any{ |
| 178 | + map[string]any{ |
| 179 | + "id": "PVTI_iter1", |
| 180 | + "title": "Sprint 1", |
| 181 | + "startDate": "2025-01-20", |
| 182 | + "duration": 7, |
| 183 | + }, |
| 184 | + }, |
| 185 | + }, |
| 186 | + }, |
| 187 | + }, |
| 188 | + }), |
| 189 | + ), |
| 190 | + ) |
| 191 | + |
| 192 | + deps := BaseDeps{ |
| 193 | + Client: gh.NewClient(mockRESTClient), |
| 194 | + GQLClient: githubv4.NewClient(mockGQLClient), |
| 195 | + } |
| 196 | + handler := toolDef.Handler(deps) |
| 197 | + request := createMCPRequest(map[string]any{ |
| 198 | + "owner": "octo-org", |
| 199 | + "owner_type": "org", |
| 200 | + "project_number": float64(1), |
| 201 | + "field_name": "Sprint", |
| 202 | + "duration": float64(7), |
| 203 | + "start_date": "2025-01-20", |
| 204 | + "iterations": []any{ |
| 205 | + map[string]any{ |
| 206 | + "title": "Sprint 1", |
| 207 | + "startDate": "2025-01-20", |
| 208 | + "duration": float64(7), |
| 209 | + }, |
| 210 | + }, |
| 211 | + }) |
| 212 | + result, err := handler(ContextWithDeps(context.Background(), deps), &request) |
| 213 | + |
| 214 | + require.NoError(t, err) |
| 215 | + require.False(t, result.IsError) |
| 216 | + |
| 217 | + textContent := getTextResult(t, result) |
| 218 | + var response map[string]any |
| 219 | + err = json.Unmarshal([]byte(textContent.Text), &response) |
| 220 | + require.NoError(t, err) |
| 221 | + assert.Equal(t, "PVTIF_field1", response["ID"]) |
| 222 | + }) |
| 223 | +} |
0 commit comments