Issues.create() fails with "title is invalid" in @gitbeaker/rest
# Issues.create() fails with "title is invalid" in @gitbeaker/rest
**Description**
- Node.js version: v20.x
- Gitbeaker version: 43.8.0
- Gitbeaker release: rest
- OS & version: Windows 11 / Linux (tested on both)
The `Issues.create()` method consistently fails with error "title is invalid" (HTTP 400), even though the exact same API call succeeds when using native `fetch()`. This affects both `Issues.create()` and `Issues.update()` methods.
**Error Output:**
```
GitbeakerRequestError: title is invalid
at Object.create_issue (src/issues.js:260:23)
Response: {
status: 400,
statusText: 'Bad Request',
body: { message: 'title is invalid' }
}
```
**Steps to reproduce**
1. Install `@gitbeaker/rest` v43.8.0
2. Initialize GitLab client with valid token and host
3. Attempt to create an issue
```javascript
import { Gitlab } from '@gitbeaker/rest';
const gitlab = new Gitlab({
token: process.env.GITLAB_TOKEN,
host: 'https://gitlab.com'
});
// This FAILS with "title is invalid"
const issue = await gitlab.Issues.create(projectId, {
title: 'Test Issue',
description: 'Test description'
});
```
**Working Demo:**
I've created a complete reproduction script that demonstrates the issue:
```javascript
#!/usr/bin/env node
import { Gitlab } from '@gitbeaker/rest';
async function testIssueCreation() {
const gitlab = new Gitlab({
token: process.env.GITLAB_TOKEN,
host: 'https://gitlab.com'
});
const projectId = 79663690; // Replace with your project ID
// Test 1: Using gitbeaker (FAILS)
console.log('Test 1: Using gitbeaker...');
try {
const issue = await gitlab.Issues.create(projectId, {
title: 'Test Issue',
description: 'Test description'
});
console.log('✓ SUCCESS:', issue);
} catch (error) {
console.error('✗ FAILED:', error.message);
console.error('Status:', error.cause?.response?.status);
}
// Test 2: Using native fetch (SUCCEEDS)
console.log('\nTest 2: Using native fetch...');
try {
const response = await fetch(`https://gitlab.com/api/v4/projects/${projectId}/issues`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'PRIVATE-TOKEN': process.env.GITLAB_TOKEN
},
body: JSON.stringify({
title: 'Test Issue',
description: 'Test description'
})
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const issue = await response.json();
console.log('✓ SUCCESS:', issue.iid, issue.title);
} catch (error) {
console.error('✗ FAILED:', error.message);
}
}
testIssueCreation();
```
**Expected behaviour**
`gitlab.Issues.create()` should successfully create an issue and return the issue object, just like the native fetch API call does.
**Actual behaviour**
`gitlab.Issues.create()` fails with:
```
GitbeakerRequestError: title is invalid
```
HTTP Status: 400 Bad Request
However, the exact same API call using native `fetch()` with identical parameters succeeds and creates the issue.
**Additional Context:**
- The token has proper permissions (verified by successful fetch call and other gitbeaker methods)
- The project exists and is accessible
- Other gitbeaker methods work fine: `Issues.all()`, `Issues.show()`, `Projects.all()`, etc.
- Only `Issues.create()` and `Issues.update()` are affected
- The error message "title is invalid" is misleading - the title parameter is correctly provided
- Tested on both gitlab.com (SaaS) and self-hosted GitLab instances
**Possible fixes**
The issue appears to be in how `@gitbeaker/rest` formats the request body or headers when calling the GitLab API. Possible areas to investigate:
1. **Request body formatting**: Check if the body is being serialized correctly
2. **Header configuration**: Verify Content-Type and other headers match GitLab API requirements
3. **Parameter mapping**: Ensure parameters are being mapped to the correct API field names
4. **API version compatibility**: Verify the request format matches GitLab API v4 specification
**Temporary Workaround:**
Until this is fixed, using native `fetch()` works:
```javascript
const token = process.env.GITLAB_TOKEN;
const host = 'https://gitlab.com';
const response = await fetch(`${host}/api/v4/projects/${projectId}/issues`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'PRIVATE-TOKEN': token
},
body: JSON.stringify({ title, description })
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.message || `HTTP ${response.status}`);
}
const issue = await response.json();
```
**Checklist**
- [x] I have checked that this is not a duplicate issue.
- [x] I have read the documentation.
[test-create-issue.js](https://github.com/user-attachments/files/25471410/test-create-issue.js)
0 条评论