Merge pull request #30170 from CptnFizzbin/fix-gitlab-get-projectid

Fix(Integration - GitLab): use authorization header for GitLab requests
This commit is contained in:
Ben Lambert
2025-06-24 09:50:02 +02:00
committed by GitHub
6 changed files with 56 additions and 60 deletions
+4 -21
View File
@@ -188,7 +188,7 @@ describe('gitlab core', () => {
});
describe('getGitLabRequestOptions', () => {
it('should return Authorization header when oauthToken is provided', () => {
it('should return Authorization bearer header when a token is provided', () => {
const token = '1234567890';
const result = getGitLabRequestOptions(
configSelfHosteWithRelativePath,
@@ -202,29 +202,12 @@ describe('gitlab core', () => {
});
});
it('should return private-token header when gl-token is provided', () => {
const token = 'glpat-1234566';
const result = getGitLabRequestOptions(
configSelfHosteWithRelativePath,
token,
);
it('should return Authorization bearer header using the config token when no token is provided', () => {
const result = getGitLabRequestOptions(configSelfHosteWithRelativePath);
expect(result).toEqual({
headers: {
'PRIVATE-TOKEN': token,
},
});
});
it('should return private-token header when oauthToken is undefined', () => {
const oauthToken = undefined;
const result = getGitLabRequestOptions(
configSelfHosteWithRelativePath,
oauthToken,
);
expect(result).toEqual({
headers: {
'PRIVATE-TOKEN': configSelfHosteWithRelativePath.token,
Authorization: `Bearer ${configSelfHosteWithRelativePath.token}`,
},
});
});
+15 -12
View File
@@ -57,20 +57,17 @@ export function getGitLabRequestOptions(
config: GitLabIntegrationConfig,
token?: string,
): { headers: Record<string, string> } {
if (token) {
// If token comes from the user and starts with "gl", it's a private token (see https://docs.gitlab.com/ee/security/token_overview.html#token-prefixes)
return {
headers: token.startsWith('gl')
? { 'PRIVATE-TOKEN': token }
: { Authorization: `Bearer ${token}` }, // Otherwise, it's a bearer token
};
const headers: Record<string, string> = {};
const accessToken = token || config.token;
if (accessToken) {
// OAuth, Personal, Project, and Group access tokens can all be passed via
// a bearer authorization header
// https://docs.gitlab.com/api/rest/authentication/#personalprojectgroup-access-tokens
headers.Authorization = `Bearer ${accessToken}`;
}
// If token not provided, fetch the integration token
const { token: configToken = '' } = config;
return {
headers: { 'PRIVATE-TOKEN': configToken },
};
return { headers };
}
// Converts
@@ -151,6 +148,12 @@ export async function getProjectId(
const data = await response.json();
if (!response.ok) {
if (response.status === 401) {
throw new Error(
'GitLab Error: 401 - Unauthorized. The access token used is either expired, or does not have permission to read the project',
);
}
throw new Error(
`GitLab Error '${data.error}', ${data.error_description}`,
);