Skip to content

Commit

Permalink
feat(optimize-project-search): Enhance project search to extract proj…
Browse files Browse the repository at this point in the history
…ect name from GitLab URLs (#61)

- Modify searchProjects function to handle GitLab repository URLs
- Extract project name when search input is a full GitLab repository URL
- Maintain existing behavior for non-URL search inputs
  • Loading branch information
M0nkeySan authored Oct 31, 2024
1 parent 5a2164c commit f470aaa
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
9 changes: 5 additions & 4 deletions __tests__/__fixtures__/projectFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ export const projectFixture: GitlabProjectDetails = {
visibility: 'private',
ssh_url_to_repo: '[email protected]:diaspora/diaspora-project-site.git',
http_url_to_repo: 'http://example.com/diaspora/diaspora-project-site.git',
web_url: 'http://example.com/diaspora/diaspora-project-site',
web_url: 'https://my-git.domain.com/diaspora/diaspora-project-site',
readme_url:
'http://example.com/diaspora/diaspora-project-site/blob/master/README.md',
'https://my-git.domain.com/diaspora/diaspora-project-site/blob/master/README.md',
tag_list: ['example', 'disapora project'],
owner: {
id: 3,
Expand Down Expand Up @@ -64,9 +64,10 @@ export const projectFixture: GitlabProjectDetails = {
},
},
archived: false,
avatar_url: 'http://example.com/uploads/project/avatar/3/uploads/avatar.png',
avatar_url:
'https://my-git.domain.com/uploads/project/avatar/3/uploads/avatar.png',
license_url:
'http://example.com/diaspora/diaspora-client/blob/master/LICENSE',
'https://my-git.domain.com/diaspora/diaspora-client/blob/master/LICENSE',
license: {
key: 'lgpl-3.0',
name: 'GNU Lesser General Public License v3.0',
Expand Down
39 changes: 38 additions & 1 deletion __tests__/project/addProject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getSlackHeaders } from '../utils/getSlackHeaders';
import { mockGitlabCall } from '../utils/mockGitlabCall';

describe('project > addProject', () => {
it('should add project by name', async () => {
it('should add project by search', async () => {
// Given
const channelId = 'channelId';
const search = 'search';
Expand Down Expand Up @@ -45,6 +45,43 @@ describe('project > addProject', () => {
});
});

it('should add project by web_url', async () => {
// Given
const { web_url, path } = projectFixture;
const channelId = 'channelId';
const userId = 'userId';
const body = {
channel_id: channelId,
text: `project add ${web_url}`,
user_id: userId,
};

mockGitlabCall(`/projects?search=${path}`, [projectFixture]);

// When
const response = await fetch('/api/v1/homer/command', {
body,
headers: getSlackHeaders(body),
});

// Then
const { hasModelEntry } = (await import('sequelize')) as any;
expect(response.status).toEqual(HTTP_STATUS_NO_CONTENT);
expect(
await hasModelEntry('Project', {
channelId,
projectId: projectFixture.id,
})
).toEqual(true);
expect(slackBotWebClient.chat.postEphemeral).toHaveBeenNthCalledWith(1, {
channel: channelId,
text: expect.stringContaining(
`\`${projectFixture.path_with_namespace}\` added`
),
user: userId,
});
});

it('should add project by id', async () => {
// Given
const channelId = 'channelId';
Expand Down
4 changes: 2 additions & 2 deletions __tests__/project/listProjects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ describe('project > listProjects', () => {
{
text: {
text: slackifyMarkdown(`**Channel projects:**
- [diaspora/diaspora-project-site](http://example.com/diaspora/diaspora-project-site)
- [diaspora/diaspora-project-site](http://example.com/diaspora/diaspora-project-site)`),
- [diaspora/diaspora-project-site](https://my-git.domain.com/diaspora/diaspora-project-site)
- [diaspora/diaspora-project-site](https://my-git.domain.com/diaspora/diaspora-project-site)`),
type: 'mrkdwn',
},
type: 'section',
Expand Down
9 changes: 8 additions & 1 deletion src/core/services/gitlab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import type { GitlabTag } from '@/core/typings/GitlabTag';
import type { GitlabUser, GitlabUserDetails } from '@/core/typings/GitlabUser';
import { getEnvVariable } from '@/core/utils/getEnvVariable';

const BASE_API_URL = `${getEnvVariable('GITLAB_URL')}/api/v4`;
const GITLAB_URL = getEnvVariable('GITLAB_URL');
const BASE_API_URL = `${GITLAB_URL}/api/v4`;
const GITLAB_TOKEN = getEnvVariable('GITLAB_TOKEN');
const MERGE_REQUEST_ID_REGEX = /^!?\d+$/;
const MERGE_REQUEST_URL_REGEX = /^http.*\/merge_requests\/(\d+)$/;
Expand Down Expand Up @@ -401,6 +402,12 @@ async function searchMergeRequestsByText(
}

export async function searchProjects(search: string): Promise<GitlabProject[]> {
if (search.startsWith(GITLAB_URL)) {
const projectName = search.replace(`${GITLAB_URL}/`, '').split('/').pop();
return callAPI(
`/projects?search=${encodeURIComponent(projectName || search)}`
);
}
return callAPI(`/projects?search=${encodeURIComponent(search)}`);
}

Expand Down

0 comments on commit f470aaa

Please sign in to comment.