Skip to content

Commit

Permalink
Merge pull request #6911 from ever-co/fix/#6823-github-sync-data
Browse files Browse the repository at this point in the history
Fix/#6823 GitHub sync data
  • Loading branch information
evereq authored Oct 2, 2023
2 parents 03f9306 + 331ae74 commit 3ba05cd
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,28 @@
class="card-scroll"
>
<nb-card-header>
<h5>{{ 'INTEGRATIONS.GITHUB_PAGE.NAME' | translate }}</h5>
<ngx-back-navigation class="float-left"></ngx-back-navigation>
<nb-actions class="float-left pt-2" size="small">
<nb-action class="toggle-layout p-0">
<h5>{{ 'INTEGRATIONS.GITHUB_PAGE.NAME' | translate }}</h5>
<nb-icon
icon="settings-2-outline"
[nbContextMenu]="contextMenuItems"
></nb-icon>
</nb-action>
</nb-actions>
</nb-card-header>
<nb-card-body>
<div class="mb-3"></div>
<div class="mb-3">
<button nbButton status="primary" class="mr-2">
<nb-icon class="mr-1" icon="edit-outline"></nb-icon>
{{ 'BUTTONS.SYNC' | translate }}
</button>
<button nbButton status="primary" class="mr-2">
<nb-icon class="mr-1" icon="edit-outline"></nb-icon>
{{ 'BUTTONS.AUTO_SYNC' | translate }}
</button>
</div>
<div class="issues-container">
<ng-select
class="mb-2"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { AfterViewInit, Component, OnInit } from '@angular/core';
import { TitleCasePipe } from '@angular/common';
import { ActivatedRoute } from '@angular/router';
import { ActivatedRoute, Data } from '@angular/router';
import { debounceTime, of } from 'rxjs';
import { Observable } from 'rxjs/internal/Observable';
import { catchError, filter, map, switchMap, tap } from 'rxjs/operators';
import { TranslateService } from '@ngx-translate/core';
import { NbMenuItem } from '@nebular/theme';
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
import { IGithubIssue, IGithubRepository, IGithubRepositoryResponse, IOrganization } from '@gauzy/contracts';
import { distinctUntilChange } from '@gauzy/common-angular';
Expand All @@ -25,13 +26,14 @@ import {
})
export class GithubViewComponent extends TranslationBaseComponent implements AfterViewInit, OnInit {

public contextMenuItems: NbMenuItem[] = [];
public settingsSmartTable: object;
public organization: IOrganization;
public loading: boolean;
public repositories: IGithubRepository[] = [];
public repositories$: Observable<IGithubRepository[]>;
public issues$: Observable<any[]>;
public issues: any[] = [];
public issues: IGithubIssue[] = [];

constructor(
public readonly _translateService: TranslateService,
Expand All @@ -47,12 +49,13 @@ export class GithubViewComponent extends TranslationBaseComponent implements Aft
ngOnInit(): void {
this._loadSmartTableSettings();
this._applyTranslationOnSmartTable();
this._getContextMenuItems();
}

ngAfterViewInit(): void {
this._store.selectedOrganization$
.pipe(
debounceTime(500),
debounceTime(200),
distinctUntilChange(),
filter((organization: IOrganization) => !!organization),
tap((organization: IOrganization) => this.organization = organization),
Expand All @@ -62,6 +65,18 @@ export class GithubViewComponent extends TranslationBaseComponent implements Aft
.subscribe();
}

/**
*
*/
private _getContextMenuItems() {
this.contextMenuItems = [
{
title: this.getTranslation('INTEGRATIONS.SETTINGS'),
icon: 'settings-2-outline'
}
];
}

/**
* Fetches repositories for a given integration and organization.
*/
Expand All @@ -72,16 +87,19 @@ export class GithubViewComponent extends TranslationBaseComponent implements Aft
}

this.loading = true;

// Extract organization properties
const { id: organizationId, tenantId } = this.organization;
this.repositories$ = this._activatedRoute.params.pipe(
this.repositories$ = this._activatedRoute.parent.data.pipe(
filter(({ integration }: Data) => !!integration),
switchMap(() => this._activatedRoute.params.pipe(
filter(({ integrationId }) => integrationId)
)),
// Get the 'integrationId' route parameter
switchMap(({ integrationId }) => {
return this._githubService.getRepositories(integrationId, {
organizationId,
tenantId
});
}),
switchMap(({ integrationId }) => this._githubService.getRepositories(integrationId, {
organizationId,
tenantId
})),
// Update component state with fetched repositories
tap(({ repositories }: IGithubRepositoryResponse) => {
this.repositories = repositories;
Expand All @@ -108,7 +126,12 @@ export class GithubViewComponent extends TranslationBaseComponent implements Aft
this.issues$ = repository ? this.getRepositoryIssue(repository) : of([]);
}

private getRepositoryIssue(repository: IGithubRepository) {
/**
*
* @param repository
* @returns
*/
private getRepositoryIssue(repository: IGithubRepository): Observable<IGithubIssue[]> {
// Ensure there is a valid organization
if (!this.organization) {
return;
Expand All @@ -119,7 +142,12 @@ export class GithubViewComponent extends TranslationBaseComponent implements Aft
this.loading = true;
// Extract organization properties
const { id: organizationId, tenantId } = this.organization;
return this._activatedRoute.params.pipe(

return this._activatedRoute.parent.data.pipe(
filter(({ integration }: Data) => !!integration),
switchMap(() => this._activatedRoute.params.pipe(
filter(({ integrationId }) => integrationId)
)),
// Get the 'integrationId' route parameter
switchMap(({ integrationId }) => {
return this._githubService.getRepositoryIssues(integrationId, owner, repo, {
Expand Down
10 changes: 8 additions & 2 deletions apps/gauzy/src/app/pages/integrations/github/github.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { NbCardModule, NbSpinnerModule } from '@nebular/theme';
import { NbActionsModule, NbButtonModule, NbCardModule, NbContextMenuModule, NbIconModule, NbSpinnerModule } from '@nebular/theme';
import { Ng2SmartTableModule } from 'ng2-smart-table';
import { NgSelectModule } from '@ng-select/ng-select';
import { TranslateModule } from './../../../@shared/translate/translate.module';
import { BackNavigationModule } from '../../../@shared/back-navigation/back-navigation.module';
import { GithubRoutingModule } from './github-routing.module';
import { GithubComponent } from './github.component';
import { GithubWizardComponent } from './components/wizard/wizard.component';
Expand All @@ -19,12 +20,17 @@ import { GithubViewComponent } from './components/view/view.component';
],
imports: [
CommonModule,
NbActionsModule,
NbButtonModule,
NbCardModule,
NbContextMenuModule,
NbIconModule,
NbSpinnerModule,
Ng2SmartTableModule,
NgSelectModule,
GithubRoutingModule,
TranslateModule
TranslateModule,
BackNavigationModule
]
})
export class GithubModule { }
6 changes: 4 additions & 2 deletions packages/contracts/src/integration.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ export interface IIntegrationSetting extends IBasePerTenantAndOrganizationEntity
}

export interface IIntegrationEntitySetting extends IBasePerTenantAndOrganizationEntityModel, IRelationalIntegrationTenant {
entity: string;
entity: IntegrationEntity;
sync: boolean;
tiedEntities?: IIntegrationEntitySettingTied[];
}

export interface IIntegrationEntitySettingTied extends IBasePerTenantAndOrganizationEntityModel {
entity: string;
entity: IntegrationEntity;
sync: boolean;
integrationEntitySetting?: IIntegrationEntitySetting;
integrationEntitySettingId?: IIntegrationEntitySetting['id'];
Expand Down Expand Up @@ -165,6 +165,8 @@ export enum IntegrationEntity {
NOTE = 'Note',
CLIENT = 'Client',
TASK = 'Task',
ISSUE = 'ISSUE',
LABEL = 'LABEL',
ACTIVITY = 'Activity',
USER = 'User',
EMPLOYEE = 'Employee',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Column, Entity, JoinColumn, RelationId, ManyToOne, Index } from 'typeorm';
import { IsBoolean, IsNotEmpty, IsOptional, IsUUID } from 'class-validator';
import { IsBoolean, IsEnum, IsNotEmpty, IsOptional, IsUUID } from 'class-validator';
import {
IIntegrationEntitySetting,
IIntegrationEntitySettingTied
IIntegrationEntitySettingTied,
IntegrationEntity
} from '@gauzy/contracts';
import {
IntegrationEntitySetting,
Expand All @@ -12,10 +13,11 @@ import {
@Entity('integration_entity_setting_tied')
export class IntegrationEntitySettingTied extends TenantOrganizationBaseEntity implements IIntegrationEntitySettingTied {

@ApiProperty({ type: () => String })
@ApiProperty({ type: () => String, enum: IntegrationEntity })
@IsNotEmpty()
@IsEnum(IntegrationEntity)
@Column()
entity: string;
entity: IntegrationEntity;

@ApiProperty({ type: () => Boolean })
@IsNotEmpty()
Expand All @@ -33,6 +35,7 @@ export class IntegrationEntitySettingTied extends TenantOrganizationBaseEntity i
* IntegrationEntitySetting
*/
@ManyToOne(() => IntegrationEntitySetting, (it) => it.tiedEntities, {
/** Database cascade action on delete. */
onDelete: 'CASCADE'
})
@JoinColumn()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import {
OneToMany,
Index
} from 'typeorm';
import { IsBoolean, IsNotEmpty, IsUUID } from 'class-validator';
import { IsBoolean, IsEnum, IsNotEmpty, IsUUID } from 'class-validator';
import {
IIntegrationEntitySetting,
IIntegrationEntitySettingTied,
IIntegrationTenant
IIntegrationTenant,
IntegrationEntity
} from '@gauzy/contracts';
import {
IntegrationEntitySettingTied,
Expand All @@ -23,10 +24,11 @@ import {
@Entity('integration_entity_setting')
export class IntegrationEntitySetting extends TenantOrganizationBaseEntity implements IIntegrationEntitySetting {

@ApiProperty({ type: () => String })
@ApiProperty({ type: () => String, enum: IntegrationEntity })
@IsNotEmpty()
@IsEnum(IntegrationEntity)
@Column()
entity: string;
entity: IntegrationEntity;

@ApiProperty({ type: () => Boolean })
@IsNotEmpty()
Expand Down Expand Up @@ -64,8 +66,10 @@ export class IntegrationEntitySetting extends TenantOrganizationBaseEntity imple
|--------------------------------------------------------------------------
*/

@ApiPropertyOptional({ type: IntegrationEntitySettingTied, isArray: true })
@OneToMany(() => IntegrationEntitySettingTied, (tiedEntity) => tiedEntity.integrationEntitySetting, {
/**
* IntegrationEntitySettingTied
*/
@OneToMany(() => IntegrationEntitySettingTied, (it) => it.integrationEntitySetting, {
cascade: true
})
@JoinColumn()
Expand Down
19 changes: 19 additions & 0 deletions packages/core/src/integration/github/github-entity-settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { IntegrationEntity } from '@gauzy/contracts';

export const DEFAULT_ENTITY_SETTINGS = [
{
entity: IntegrationEntity.PROJECT,
sync: true
}
];

export const PROJECT_TIED_ENTITIES = [
{
entity: IntegrationEntity.ISSUE,
sync: true
},
{
entity: IntegrationEntity.LABEL,
sync: true
}
];
25 changes: 23 additions & 2 deletions packages/core/src/integration/github/github.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { HttpService } from '@nestjs/axios';
import { catchError, lastValueFrom, switchMap } from 'rxjs';
import { filter } from 'rxjs/operators';
import { environment } from '@gauzy/config';
import { IGithubAppInstallInput, IIntegrationTenant, IntegrationEnum } from '@gauzy/contracts';
import { IGithubAppInstallInput, IIntegrationTenant, IntegrationEntity, IntegrationEnum } from '@gauzy/contracts';
import { IntegrationTenantFirstOrCreateCommand } from 'integration-tenant/commands';
import { IntegrationService } from 'integration/integration.service';
import { RequestContext } from '../../core/context';
import { GITHUB_ACCESS_TOKEN_URL } from './github.config';
import { DEFAULT_ENTITY_SETTINGS, PROJECT_TIED_ENTITIES } from './github-entity-settings';
const { github } = environment;

@Injectable()
Expand Down Expand Up @@ -51,6 +52,26 @@ export class GithubService {
}
});

const tiedEntities = PROJECT_TIED_ENTITIES.map(entity => ({
...entity,
organizationId,
tenantId
}));

const entitySettings = DEFAULT_ENTITY_SETTINGS.map((settingEntity) => {
if (settingEntity.entity === IntegrationEntity.PROJECT) {
return {
...settingEntity,
tiedEntities
};
}
return {
...settingEntity,
organizationId,
tenantId
};
});

/** Execute the command to create the integration tenant settings */
return await this._commandBus.execute(
new IntegrationTenantFirstOrCreateCommand({
Expand All @@ -65,7 +86,7 @@ export class GithubService {
integration,
tenantId,
organizationId,
entitySettings: [],
entitySettings: entitySettings,
settings: [
{
settingsName: 'installation_id',
Expand Down

0 comments on commit 3ba05cd

Please sign in to comment.