Skip to content

Fix getEffectiveProjectId to prioritize passed projectId over GITLAB_PROJECT_ID#320

Merged
zereight merged 5 commits intomainfrom
copilot/fix-geteffectiveprojectid-issue
Mar 22, 2026
Merged

Fix getEffectiveProjectId to prioritize passed projectId over GITLAB_PROJECT_ID#320
zereight merged 5 commits intomainfrom
copilot/fix-geteffectiveprojectid-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 18, 2026

When GITLAB_ALLOWED_PROJECT_IDS is unset, getEffectiveProjectId was returning GITLAB_PROJECT_ID even when a different projectId was explicitly passed, preventing users from querying other projects via tools like get_project.

Changes

  • index.ts: Reversed priority order to check projectId parameter before GITLAB_PROJECT_ID environment variable
  • test/test-geteffectiveprojectid.ts: Added test coverage for all three configuration modes (no allowlist, single project, multiple projects)

Before

return GITLAB_PROJECT_ID || projectId;

After

if (projectId) {
  return projectId;
}
if (GITLAB_PROJECT_ID) {
  return GITLAB_PROJECT_ID;
}
throw new Error("No project ID provided and GITLAB_PROJECT_ID is not set");

This allows get_project and similar tools to accept any project ID/path when no allowlist is configured, while maintaining backward compatibility when projectId is omitted.

Original prompt

This section details on the original issue you should resolve

<issue_title>getEffectiveProjectId cannot return projectId passed into the function when GITLAB_ALLOWED_PROJECT_IDS is unspecified</issue_title>
<issue_description>I want to retrieve the project ID of another project (different from GITLAB_PROJECT_ID) by the project path with namespace using get_project. It's supported by Gitlab project API: https://docs.gitlab.com/18.6/api/projects/#get-a-single-project

However, getEffectiveProjectId always returns GITLAB_PROJECT_ID when GITLAB_ALLOWED_PROJECT_IDS is unspecified. Can it have an option to return projectId passed into the function, or even let projectId have precedence over GITLAB_PROJECT_ID at https://github.com/zereight/gitlab-mcp/blob/v2.0.21/index.ts#L1307 ? Thanks.
</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Fix getEffectiveProjectId to return specified projectId Fix getEffectiveProjectId to prioritize passed projectId over GITLAB_PROJECT_ID Jan 18, 2026
Copilot AI requested a review from zereight January 18, 2026 14:15
Copy link
Copy Markdown

@Juoper Juoper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me

@ricolo
Copy link
Copy Markdown

ricolo commented Mar 18, 2026

Looks good and can fix the issue I raised

@zereight zereight closed this Mar 22, 2026
@zereight zereight reopened this Mar 22, 2026
@zereight zereight marked this pull request as ready for review March 22, 2026 12:29
Copilot AI review requested due to automatic review settings March 22, 2026 12:29
Copilot AI and others added 5 commits March 22, 2026 21:32
…PROJECT_ID

- Changed the function to return projectId first if provided, then GITLAB_PROJECT_ID
- Added proper error handling when neither projectId nor GITLAB_PROJECT_ID is available
- Added comprehensive test suite to validate the fix

Co-authored-by: zereight <42544600+zereight@users.noreply.github.com>
Co-authored-by: zereight <42544600+zereight@users.noreply.github.com>
…checks

Co-authored-by: zereight <42544600+zereight@users.noreply.github.com>
Co-authored-by: zereight <42544600+zereight@users.noreply.github.com>
@zereight zereight force-pushed the copilot/fix-geteffectiveprojectid-issue branch from 132366d to 096169d Compare March 22, 2026 12:34
@zereight zereight merged commit 3e11947 into main Mar 22, 2026
7 checks passed
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes project ID resolution so an explicitly provided projectId argument takes precedence over GITLAB_PROJECT_ID when no allowlist is configured, enabling tools like get_project to query arbitrary accessible projects by ID/path.

Changes:

  • Updated getEffectiveProjectId to prioritize the passed projectId over GITLAB_PROJECT_ID, and to throw a clearer error when neither is provided.
  • Added a new test suite covering project ID resolution behavior across three configuration modes (no allowlist, single allowed project, multiple allowed projects).
  • Ignored generated test-results*.json artifacts.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 2 comments.

File Description
index.ts Changes getEffectiveProjectId precedence and adds explicit error when no project ID source exists.
test/test-geteffectiveprojectid.ts Adds coverage for resolution behavior under different allowlist configurations via mock GitLab + MCP server.
.gitignore Ignores JSON test result outputs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +33 to +55
before(async () => {
// Start mock GitLab server
const mockPort = await findMockServerPort(9100);
mockGitLab = new MockGitLabServer({
port: mockPort,
validTokens: [MOCK_TOKEN]
});
await mockGitLab.start();
const mockGitLabUrl = mockGitLab.getUrl();

// Start MCP server WITHOUT GITLAB_ALLOWED_PROJECT_IDS
const mcpPort = await findAvailablePort(3100);
const server = await launchServer({
mode: TransportMode.STREAMABLE_HTTP,
port: mcpPort,
timeout: 5000,
env: {
STREAMABLE_HTTP: 'true',
GITLAB_API_URL: `${mockGitLabUrl}/api/v4`,
GITLAB_PROJECT_ID: DEFAULT_PROJECT_ID,
GITLAB_READ_ONLY_MODE: 'true',
}
});
Copy link

Copilot AI Mar 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test suite is not hermetic: launchServer() reads process.env.GITLAB_TOKEN_TEST and process.env.TEST_PROJECT_ID (and passes through process.env.GITLAB_ALLOWED_PROJECT_IDS unless explicitly overridden). As written, these tests will fail on environments where those vars aren’t set (or will behave differently if GITLAB_ALLOWED_PROJECT_IDS is set in the parent env). Consider setting/restoring process.env.GITLAB_TOKEN_TEST + process.env.TEST_PROJECT_ID in the before/after hooks, and explicitly setting GITLAB_ALLOWED_PROJECT_IDS: '' for the “No GITLAB_ALLOWED_PROJECT_IDS” case to override any inherited value.

Copilot uses AI. Check for mistakes.
Comment thread index.ts
Comment on lines 1420 to 1426
description: "Get details of a specific commit",
inputSchema: toJSONSchema(GetCommitSchema),
},
{
name: "get_commit_diff",
description: "Get changes/diffs of a specific commit",
inputSchema: toJSONSchema(GetCommitDiffSchema),
Copy link

Copilot AI Mar 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that the passed projectId takes precedence, callers that pass a URL-encoded path (which is documented for many project_id params) will start flowing through this branch more often. At least the get_project tool currently passes args.project_id straight into encodeURIComponent(...) without a prior decodeURIComponent, so an input like group%2Fproj will become double-encoded (group%252Fproj) and fail. Consider normalizing/decoding projectId before returning here (or ensuring all callers decode before calling this helper) so precedence changes don’t break URL-encoded project paths.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 132366d84f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread index.ts
Comment on lines +1420 to +1422
// Prioritize the passed projectId over GITLAB_PROJECT_ID to allow querying different projects
if (projectId) {
return projectId;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Unblock create/fork flows when GITLAB_PROJECT_ID is only a default

This helper now makes GITLAB_PROJECT_ID a fallback instead of a lock, but the fork_repository and create_repository handlers still reject whenever that env var is present (index.ts:5316-5318 and index.ts:5388-5391). In a deployment that keeps GITLAB_PROJECT_ID only to supply a default project, project-scoped tools can now target arbitrary project_ids while create/fork always fail, so the new semantics are only applied to part of the API surface.

Useful? React with 👍 / 👎.

Comment on lines +45 to +54
const server = await launchServer({
mode: TransportMode.STREAMABLE_HTTP,
port: mcpPort,
timeout: 5000,
env: {
STREAMABLE_HTTP: 'true',
GITLAB_API_URL: `${mockGitLabUrl}/api/v4`,
GITLAB_PROJECT_ID: DEFAULT_PROJECT_ID,
GITLAB_READ_ONLY_MODE: 'true',
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Seed launcher env before starting the new mock suites

These before() blocks call launchServer without first populating the parent GITLAB_TOKEN_TEST/TEST_PROJECT_ID, but test/utils/server-launcher.ts:40-57 reads both from process.env and throws if either is missing. Because this is a mock-only test, a clean local or CI run will fail before the MCP server starts unless the caller happens to have unrelated integration-test env vars exported.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

getEffectiveProjectId cannot return projectId passed into the function when GITLAB_ALLOWED_PROJECT_IDS is unspecified

5 participants