Skip to content

Commit

Permalink
fix: routes intersection
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniomuso committed Aug 28, 2023
1 parent 32aac85 commit ea6de8c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
34 changes: 17 additions & 17 deletions packages/gitlab-backend/src/service/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ describe('createRouter', () => {
// this is set to let msw pass test requests through the mock server
agent.set('User-Agent', 'supertest');
const response = await agent.get(
'/api/gitlab/non-existing-example.com/projects/434'
'/api/gitlab/rest/non-existing-example.com/projects/434'
);
expect(response.status).toEqual(200);
expect(response.body).toEqual({
Expand All @@ -123,7 +123,7 @@ describe('createRouter', () => {
// this is set to let msw pass test requests through the mock server
agent.set('User-Agent', 'supertest');
const response = await agent.get(
'/api/gitlab/non-existing-example-2.com/projects/434'
'/api/gitlab/rest/non-existing-example-2.com/projects/434'
);
expect(response.status).toEqual(200);
expect(response.body).toEqual({
Expand All @@ -144,7 +144,7 @@ describe('createRouter', () => {
// this is set to let msw pass test requests through the mock server
agent.set('User-Agent', 'supertest');
const response = await agent.post(
'/api/gitlab/non-existing-example.com/graphql'
'/api/gitlab/graphql/non-existing-example.com'
);
expect(response.status).toEqual(200);
expect(response.body).toEqual({
Expand All @@ -164,7 +164,7 @@ describe('createRouter', () => {
// this is set to let msw pass test requests through the mock server
agent.set('User-Agent', 'supertest');
const response = await agent.post(
'/api/gitlab/non-existing-example-2.com/graphql'
'/api/gitlab/graphql/non-existing-example-2.com'
);
expect(response.status).toEqual(200);
expect(response.body).toEqual({
Expand Down Expand Up @@ -194,7 +194,7 @@ describe('createRouter', () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const response = await agent?.[method](
'/api/gitlab/non-existing-example.com/graphql'
'/api/gitlab/graphql/non-existing-example.com'
);

expect(response.status).toEqual(404);
Expand All @@ -207,7 +207,7 @@ describe('createRouter', () => {
// this is set to let msw pass test requests through the mock server
agent.set('User-Agent', 'supertest');
const response = await agent
.post('/api/gitlab/non-existing-example-2.com/graphql')
.post('/api/gitlab/graphql/non-existing-example-2.com')
.send({
query: 'mutation { createIssue { id } }',
});
Expand All @@ -232,7 +232,7 @@ describe('createRouter', () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const response = await agent?.[method](
'/api/gitlab/non-existing-example-2.com/projects/434'
'/api/gitlab/rest/non-existing-example-2.com/projects/434'
);
expect(response.status).toEqual(404);
expect(response.body).toEqual({});
Expand All @@ -244,7 +244,7 @@ describe('createRouter', () => {
// this is set to let msw pass test requests through the mock server
agent.set('User-Agent', 'supertest');
const response = await agent.get(
'/api/gitlab/does.not.exist/projects/434'
'/api/gitlab/rest/does.not.exist/projects/434'
);
expect(response.status).toEqual(404);
expect(response.body).toEqual({});
Expand Down Expand Up @@ -354,7 +354,7 @@ describe('createRouter with baseUrl', () => {
// this is set to let msw pass test requests through the mock server
agent.set('User-Agent', 'supertest');
const response = await agent.get(
`${basePath}/api/gitlab/non-existing-example.com/projects/434`
`${basePath}/api/gitlab/rest/non-existing-example.com/projects/434`
);
expect(response.status).toEqual(200);
expect(response.body).toEqual({
Expand All @@ -373,7 +373,7 @@ describe('createRouter with baseUrl', () => {
// this is set to let msw pass test requests through the mock server
agent.set('User-Agent', 'supertest');
const response = await agent.get(
`${basePath}/api/gitlab/non-existing-example-2.com/projects/434`
`${basePath}/api/gitlab/rest/non-existing-example-2.com/projects/434`
);
expect(response.status).toEqual(200);
expect(response.body).toEqual({
Expand All @@ -392,7 +392,7 @@ describe('createRouter with baseUrl', () => {
// this is set to let msw pass test requests through the mock server
agent.set('User-Agent', 'supertest');
const response = await agent.get(
'/api/gitlab/non-existing-example.com/projects/434'
'/api/gitlab/rest/non-existing-example.com/projects/434'
);
expect(response.status).toEqual(404);
});
Expand All @@ -404,7 +404,7 @@ describe('createRouter with baseUrl', () => {
// this is set to let msw pass test requests through the mock server
agent.set('User-Agent', 'supertest');
const response = await agent.post(
`${basePath}/api/gitlab/non-existing-example.com/graphql`
`${basePath}/api/gitlab/graphql/non-existing-example.com`
);
expect(response.status).toEqual(200);
expect(response.body).toEqual({
Expand All @@ -424,7 +424,7 @@ describe('createRouter with baseUrl', () => {
// this is set to let msw pass test requests through the mock server
agent.set('User-Agent', 'supertest');
const response = await agent.post(
`${basePath}/api/gitlab/non-existing-example-2.com/graphql`
`${basePath}/api/gitlab/graphql/non-existing-example-2.com`
);
expect(response.status).toEqual(200);
expect(response.body).toEqual({
Expand Down Expand Up @@ -454,7 +454,7 @@ describe('createRouter with baseUrl', () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const response = await agent?.[method](
`${basePath}/api/gitlab/non-existing-example.com/graphql`
`${basePath}/api/gitlab/graphql/non-existing-example.com`
);

expect(response.status).toEqual(404);
Expand All @@ -468,7 +468,7 @@ describe('createRouter with baseUrl', () => {
agent.set('User-Agent', 'supertest');
const response = await agent
.post(
`${basePath}/api/gitlab/non-existing-example-2.com/graphql`
`${basePath}/api/gitlab/graphql/non-existing-example-2.com`
)
.send({
query: 'mutation { createIssue { id } }',
Expand All @@ -494,7 +494,7 @@ describe('createRouter with baseUrl', () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const response = await agent?.[method](
`${basePath}/api/gitlab/non-existing-example-2.com/projects/434`
`${basePath}/api/gitlab/rest/non-existing-example-2.com/projects/434`
);
expect(response.status).toEqual(404);
expect(response.body).toEqual({});
Expand All @@ -506,7 +506,7 @@ describe('createRouter with baseUrl', () => {
// this is set to let msw pass test requests through the mock server
agent.set('User-Agent', 'supertest');
const response = await agent.get(
`${basePath}/api/gitlab/does.not.exist/projects/434`
`${basePath}/api/gitlab/rest/does.not.exist/projects/434`
);
expect(response.status).toEqual(404);
expect(response.body).toEqual({});
Expand Down
16 changes: 8 additions & 8 deletions packages/gitlab-backend/src/service/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,27 @@ export async function createRouter(
);
};

for (const { host, apiBaseUrl, baseUrl, token } of gitlabIntegrations) {
const graphqlBaseUrl = new URL(baseUrl);
for (const { host, apiBaseUrl, token } of gitlabIntegrations) {
const apiUrl = new URL(apiBaseUrl);

router.use(
`/${host}/graphql`,
`/graphql/${host}`,
createProxyMiddleware(graphqlFilter, {
target: graphqlBaseUrl.origin,
target: apiUrl.origin,
changeOrigin: true,
headers: {
...(token ? { 'PRIVATE-TOKEN': token } : {}),
},
secure,
logProvider: () => logger,
pathRewrite: {
[`^${basePath}/api/gitlab/${host}/graphql`]: `/api/graphql`,
[`^${basePath}/api/gitlab/graphql/${host}`]: `/api/graphql`,
},
})
);

const apiUrl = new URL(apiBaseUrl);
router.use(
`/${host}`,
`/rest/${host}`,
createProxyMiddleware(filter, {
target: apiUrl.origin,
changeOrigin: true,
Expand All @@ -91,7 +91,7 @@ export async function createRouter(
secure,
logProvider: () => logger,
pathRewrite: {
[`^${basePath}/api/gitlab/${host}`]: apiUrl.pathname,
[`^${basePath}/api/gitlab/rest/${host}`]: apiUrl.pathname,
},
})
);
Expand Down
9 changes: 5 additions & 4 deletions packages/gitlab/src/api/GitlabCIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,12 @@ export class GitlabCIClient implements GitlabCIApi {
protected async callApi<T>(
path: string,
query: { [key in string]: string },
APIkind: 'rest' | 'graphql' = 'rest',
options: RequestInit = {}
): Promise<T | undefined> {
const apiUrl = `${await this.discoveryApi.getBaseUrl('gitlab')}/${
this.gitlabInstance
}`;
const apiUrl = `${await this.discoveryApi.getBaseUrl(
'gitlab'
)}/${APIkind}/${this.gitlabInstance}`;
const token = (await this.identityApi.getCredentials()).token;

if (token) {
Expand Down Expand Up @@ -120,7 +121,7 @@ export class GitlabCIClient implements GitlabCIApi {
body: JSON.stringify(query),
};

return this.callApi<T>('graphql', {}, options);
return this.callApi<T>('graphql', {}, 'graphql', options);
}

async getPipelineSummary(
Expand Down

0 comments on commit ea6de8c

Please sign in to comment.