Skip to content

Commit

Permalink
Merge pull request #488 from middlewarehq/feat/gitlab-integration
Browse files Browse the repository at this point in the history
Add GitLab integration functionality
  • Loading branch information
jayantbh authored Sep 8, 2024
2 parents 418662f + 96b781a commit ab920b9
Show file tree
Hide file tree
Showing 33 changed files with 1,504 additions and 845 deletions.
13 changes: 8 additions & 5 deletions backend/analytics_server/mhq/service/code/sync/etl_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytz

from mhq.store.models.code.enums import CodeProvider
from mhq.service.settings.configuration_settings import (
get_settings_service,
SettingsService,
Expand Down Expand Up @@ -42,11 +43,11 @@ def __init__(
self.bookmark_service = bookmark_service
self.settings_service = settings_service

def sync_org_repos(self, org_id: str):
def sync_org_repos(self, org_id: str, provider: CodeProvider):
if not self.etl_service.check_pat_validity():
LOG.error("Invalid PAT for code provider")
return
org_repos: List[OrgRepo] = self._sync_org_repos(org_id)
org_repos: List[OrgRepo] = self._sync_org_repos(org_id, provider)
for org_repo in org_repos:
try:
self._sync_repo_pull_requests_data(org_repo)
Expand All @@ -56,9 +57,11 @@ def sync_org_repos(self, org_id: str):
)
continue

def _sync_org_repos(self, org_id: str) -> List[OrgRepo]:
def _sync_org_repos(self, org_id: str, provider: CodeProvider) -> List[OrgRepo]:
try:
org_repos = self.code_repo_service.get_active_org_repos(org_id)
org_repos = self.code_repo_service.get_active_org_repos_for_provider(
org_id, provider
)
org_repos = self.etl_service.get_org_repos(org_repos)
self.code_repo_service.update_org_repos(org_repos)
return org_repos
Expand Down Expand Up @@ -141,7 +144,7 @@ def sync_code_repos(org_id: str):
get_bookmark_service(),
get_settings_service(),
)
code_etl_handler.sync_org_repos(org_id)
code_etl_handler.sync_org_repos(org_id, CodeProvider(provider))
LOG.info(f"Synced org repos for provider {provider}")
except Exception as e:
LOG.error(f"Error syncing org repos for provider {provider}: {str(e)}")
Expand Down
15 changes: 15 additions & 0 deletions backend/analytics_server/mhq/store/repos/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from operator import and_
from typing import Optional, List

from mhq.store.models.code.enums import CodeProvider
from sqlalchemy import or_
from sqlalchemy.orm import defer
from mhq.store.models.core import Team
Expand Down Expand Up @@ -35,6 +36,20 @@ def get_active_org_repos(self, org_id: str) -> List[OrgRepo]:
.all()
)

@rollback_on_exc
def get_active_org_repos_for_provider(
self, org_id: str, provider: CodeProvider
) -> List[OrgRepo]:
return (
self._db.session.query(OrgRepo)
.filter(
OrgRepo.org_id == org_id,
OrgRepo.is_active.is_(True),
OrgRepo.provider == provider.value,
)
.all()
)

@rollback_on_exc
def update_org_repos(self, org_repos: List[OrgRepo]):
[self._db.session.merge(org_repo) for org_repo in org_repos]
Expand Down
225 changes: 0 additions & 225 deletions web-server/pages/api/integrations/orgs.ts

This file was deleted.

11 changes: 6 additions & 5 deletions web-server/pages/api/integrations/selected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { db, dbRaw } from '@/utils/db';

const getSchema = yup.object().shape({
org_id: yup.string().uuid().required(),
provider: yup.string().oneOf(Object.values(Integration))
providers: yup.array(yup.string().oneOf(Object.values(Integration)))
});

const endpoint = new Endpoint(nullSchema);
Expand All @@ -23,14 +23,14 @@ endpoint.handle.GET(getSchema, async (req, res) => {
return res.send(selectedDBReposMock);
}

const { org_id, provider } = req.payload;
const { org_id, providers } = req.payload;

res.send(await getSelectedReposForOrg(org_id, provider as Integration));
res.send(await getSelectedReposForOrg(org_id, providers as Integration[]));
});

export const getSelectedReposForOrg = async (
org_id: ID,
provider: Integration
providers: Integration[]
): Promise<RepoWithMultipleWorkflows[]> => {
const dbRepos: RepoWithSingleWorkflow[] = await db(Table.OrgRepo)
.leftJoin({ rw: Table.RepoWorkflow }, function () {
Expand All @@ -47,7 +47,8 @@ export const getSelectedReposForOrg = async (
.select(dbRaw.raw('to_json(rw) as repo_workflow'))
.select('tr.deployment_type', 'tr.team_id')
.from('OrgRepo')
.where({ org_id, 'OrgRepo.provider': provider })
.where('org_id', org_id)
.and.whereIn('OrgRepo.provider', providers)
.andWhereNot('OrgRepo.is_active', false);

const repoToWorkflowMap = dbRepos.reduce(
Expand Down
Loading

0 comments on commit ab920b9

Please # to comment.