Skip to content

Commit

Permalink
Merge pull request #1659 from ever-co/fix/github-repo-selection
Browse files Browse the repository at this point in the history
fix: Github Repo selection
  • Loading branch information
evereq authored Oct 26, 2023
2 parents ca3a6b8 + 580a29e commit 55adff9
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 26 deletions.
23 changes: 21 additions & 2 deletions apps/web/app/hooks/integrations/useGitHubIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useCallback } from 'react';
import { useRecoilState } from 'recoil';
import { useQuery } from '../useQuery';
import { IProjectRepository } from '@app/interfaces';
import { editOrganizationProjectSettingAPI } from '@app/services/client/api';

export function useGitHubIntegration() {
const [user] = useRecoilState(userState);
Expand Down Expand Up @@ -72,11 +73,29 @@ export function useGitHubIntegration() {
[repositoriesQueryCall, setIntegrationGithubRepositories]
);
const syncGitHubRepository = useCallback(
(integrationId: string, repository: IProjectRepository) => {
(
installationId: string,
repository: IProjectRepository,
projectId: string,
tenantId: string,
organizationId: string
) => {
return syncGitHubRepositoryQueryCall({
integrationId,
installationId,
repository
}).then((response) => {
if (response.data.data.id) {
editOrganizationProjectSettingAPI(
projectId,
{
tenantId,
organizationId,
repositoryId: response.data.data.id
},
tenantId
);
}

return response.data.data;
});
},
Expand Down
9 changes: 7 additions & 2 deletions apps/web/app/services/server/requests/integrations/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function getGithubIntegrationRepositoriesRequest(

export function projectRepositorySyncRequest(
body: {
integrationId: string;
installationId: string;
organizationId: string;
tenantId: string;
repository: IProjectRepository;
Expand All @@ -90,7 +90,12 @@ export function projectRepositorySyncRequest(
path: '/integration/github/repository/sync',
method: 'POST',
bearer_token,
body,
body: {
installation_id: body.installationId,
organizationId: body.organizationId,
tenantId: body.tenantId,
repository: body.repository
},
tenantId: body.tenantId
});
}
43 changes: 23 additions & 20 deletions apps/web/lib/settings/integration-setting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,24 @@ export const IntegrationSetting = () => {
}
}, [activeTeam]);

const { integrationGithubRepositories, getRepositories } = useGitHubIntegration();
const {
integrationGithubRepositories,
getRepositories,
syncGitHubRepository,
integrationGithubMetadata,
metaData
} = useGitHubIntegration();

const { editOrganizationProjectSetting, editOrganizationProject } = useOrganizationProjects();

const { getIntegrationTenant, loading: integrationTenantLoading, integrationTenant } = useIntegrationTenant();

useEffect(() => {
if (integrationTenant && integrationTenant.length) {
metaData(integrationTenant[0].id);
}
}, [metaData, integrationTenant]);

const { loading: loadingIntegrationTypes, integrationTypes, getIntegrationTypes } = useIntegrationTypes();

useEffect(() => {
Expand Down Expand Up @@ -81,27 +93,18 @@ export const IntegrationSetting = () => {

const repo = integrationGithubRepositories?.repositories.find((item) => item.id === +value);

editOrganizationProjectSetting(projectId, {
repository: {
repositoryId: repo?.id,
name: repo?.name,
fullName: repo?.full_name,
owner: repo?.owner.login,
integrationId: integrationTenant[0].id,
...(activeTeam?.projects && activeTeam?.projects?.length
? {
...(activeTeam?.projects[0].repository || {})
}
: {})
},
isTasksAutoSync: true,
isTasksAutoSyncOnLabel: true,
tenantId: activeTeam?.tenantId,
organizationId: activeTeam?.organizationId
});
if (repo && integrationGithubMetadata?.id) {
syncGitHubRepository(
`${integrationGithubMetadata?.id}`,
repo as any,
projectId,
activeTeam?.tenantId as string,
activeTeam?.organizationId as string
);
}
}
},
[activeTeam, editOrganizationProjectSetting, integrationGithubRepositories, integrationTenant]
[activeTeam, integrationGithubRepositories, integrationGithubMetadata, syncGitHubRepository]
);
const handleRemoveRepo = useCallback(() => {
const projectId = getActiveProjectIdCookie();
Expand Down
4 changes: 2 additions & 2 deletions apps/web/pages/api/integration/github/repository/sync.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { authenticatedGuard } from '@app/services/server/guards/authenticated-guard';
import { installGitHubIntegration } from '@app/services/server/requests';
import { projectRepositorySyncRequest } from '@app/services/server/requests';
import { NextApiRequest, NextApiResponse } from 'next';

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
Expand All @@ -10,7 +10,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
return $res.status(405).json({});
}

const response = await installGitHubIntegration(
const response = await projectRepositorySyncRequest(
{
...req.body,
tenantId,
Expand Down

0 comments on commit 55adff9

Please sign in to comment.