Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Add pull-request macro

Oliver Günther45cb7b736385
diff --git a/modules/github_integration/config/locales/js-en.yml b/modules/github_integration/config/locales/js-en.yml
index 2b6ff33c78e..fe8a11c3b9f 100644
--- a/modules/github_integration/config/locales/js-en.yml
+++ b/modules/github_integration/config/locales/js-en.yml
@@ -47,3 +47,14 @@ en:
empty: There are no pull requests linked yet. Link an existing PR by using the code <code>OP#%{wp_id}</code> in the PR description or create a new PR.
github_actions: Actions
+ pull_requests:
+ message: >
+ Pull request #%{pr_number} %{pr_link} for %{repository_link} has been %{pr_state} by %{github_user_link}.
+ referenced_message: >
+ %{github_user_link} referenced this work package in pull request #%{pr_number} %{pr_link} on %{repository_link}.
+ states:
+ opened: 'opened'
+ closed: 'closed'
+ draft: 'drafted'
+ merged: 'merged'
+ ready_for_review: 'marked ready for review'
diff --git a/modules/github_integration/frontend/module/main.ts b/modules/github_integration/frontend/module/main.ts
index 9d81e7db470..6f0bec286f3 100644
--- a/modules/github_integration/frontend/module/main.ts
+++ b/modules/github_integration/frontend/module/main.ts
@@ -24,7 +24,10 @@
//
// See COPYRIGHT and LICENSE files for more details.
-import { Injector, NgModule } from '@angular/core';
+import {
+ Injector,
+ NgModule,
+} from '@angular/core';
import { OpSharedModule } from 'core-app/shared/shared.module';
import { OpenprojectTabsModule } from 'core-app/shared/components/tabs/openproject-tabs.module';
import { WorkPackageTabsService } from 'core-app/features/work-packages/components/wp-tabs/services/wp-tabs/wp-tabs.service';
@@ -38,6 +41,12 @@ import { WorkPackageResource } from 'core-app/features/hal/resources/work-packag
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { GithubPullRequestResourceService } from './state/github-pull-request.service';
+import {
+ githubPullRequestMacroSelector,
+ PullRequestMacroComponent,
+} from './pull-request/pull-request-macro.component';
+import { HookService } from 'core-app/features/plugins/hook-service';
+import { DynamicBootstrapper } from 'core-app/core/setup/globals/dynamic-bootstrapper';
export function workPackageGithubPrsCount(
workPackage:WorkPackageResource,
@@ -77,6 +86,7 @@ export function initializeGithubIntegrationPlugin(injector:Injector) {
GitActionsMenuDirective,
GitActionsMenuComponent,
PullRequestComponent,
+ PullRequestMacroComponent,
],
exports: [
GitHubTabComponent,
@@ -84,10 +94,14 @@ export function initializeGithubIntegrationPlugin(injector:Injector) {
TabPrsComponent,
GitActionsMenuDirective,
GitActionsMenuComponent,
+ PullRequestMacroComponent,
],
})
export class PluginModule {
constructor(injector:Injector) {
initializeGithubIntegrationPlugin(injector);
+ DynamicBootstrapper.register(
+ { selector: githubPullRequestMacroSelector, cls: PullRequestMacroComponent, embeddable: true },
+ );
}
}
diff --git a/modules/github_integration/frontend/module/pull-request/pull-request-macro.component.html b/modules/github_integration/frontend/module/pull-request/pull-request-macro.component.html
new file mode 100644
index 00000000000..51fdd970ae6
--- /dev/null
+++ b/modules/github_integration/frontend/module/pull-request/pull-request-macro.component.html
@@ -0,0 +1,10 @@
+<ng-container *ngIf="(pullRequest$ | async) as pr; else loadingTemplate">
+ foo
+</ng-container>
+<ng-template #loadingTemplate>
+ <op-content-loader
+ viewBox="0 0 200 5"
+ >
+ <svg:rect x="0" y="0" width="100%" height="5" rx="1" />
+ </op-content-loader>
+</ng-template>
diff --git a/modules/github_integration/frontend/module/pull-request/pull-request-macro.component.ts b/modules/github_integration/frontend/module/pull-request/pull-request-macro.component.ts
index 7571beb894f..9dc2b093d8b 100644
--- a/modules/github_integration/frontend/module/pull-request/pull-request-macro.component.ts
+++ b/modules/github_integration/frontend/module/pull-request/pull-request-macro.component.ts
@@ -28,88 +28,44 @@
import {
ChangeDetectionStrategy,
- ChangeDetectorRef,
Component,
ElementRef,
- HostBinding,
Injector,
Input,
+ OnInit,
} from '@angular/core';
-import { HalResource } from 'core-app/features/hal/resources/hal-resource';
-import { SchemaCacheService } from 'core-app/core/schemas/schema-cache.service';
import { HalResourceEditingService } from 'core-app/shared/components/fields/edit/services/hal-resource-editing.service';
-import { DisplayFieldService } from 'core-app/shared/components/fields/display/display-field.service';
-import { I18nService } from 'core-app/core/i18n/i18n.service';
-import {
- AttributeModelLoaderService,
- SupportedAttributeModels,
-} from 'core-app/shared/components/fields/macros/attribute-model-loader.service';
-import { capitalize } from 'core-app/shared/helpers/string-helpers';
import { populateInputsFromDataset } from 'core-app/shared/components/dataset-inputs';
+import { IGithubPullRequest } from '../state/github-pull-request.model';
+import { GithubPullRequestResourceService } from '../state/github-pull-request.service';
+import { Observable } from 'rxjs';
-export const githubPullRequestMacroSelector = 'macro.github-pull-request';
+export const githubPullRequestMacroSelector = 'macro.github_pull_request';
@Component({
selector: githubPullRequestMacroSelector,
templateUrl: './pull-request-macro.component.html',
- styleUrls: ['./pull-request-macro.component.sass'],
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [
HalResourceEditingService,
],
})
-export class PullRequestMacroComponent {
+export class PullRequestMacroComponent implements OnInit {
@Input() pullRequestId:string;
+ pullRequest$:Observable<IGithubPullRequest>;
+
constructor(
readonly elementRef:ElementRef,
readonly injector:Injector,
- readonly resourceLoader:AttributeModelLoaderService,
- readonly schemaCache:SchemaCacheService,
- readonly displayField:DisplayFieldService,
- readonly I18n:I18nService,
- readonly cdRef:ChangeDetectorRef) {
+ readonly pullRequests:GithubPullRequestResourceService,
+ ) {
populateInputsFromDataset(this);
}
ngOnInit() {
- const element = this.elementRef.nativeElement as HTMLElement;
- const model:SupportedAttributeModels = element.dataset.model as any;
- const id:string = element.dataset.id!;
- const attributeName:string = element.dataset.attribute!;
- this.attributeScope = capitalize(model);
-
- this.loadResourceAttribute(model, id, attributeName);
- }
-
- private async loadResourceAttribute(model:SupportedAttributeModels, id:string, attributeName:string) {
- let resource:HalResource|null;
-
- try {
- this.resource = resource = await this.resourceLoader.require(model, id);
- } catch (e) {
- console.error(`Failed to render macro ${e}`);
- return this.markError(this.text.not_found);
- }
-
- if (!resource) {
- this.markError(this.text.not_found);
- return;
- }
-
- const schema = await this.schemaCache.ensureLoaded(resource);
- this.attribute = schema.attributeFromLocalizedName(attributeName) || attributeName;
- this.label = schema[this.attribute]?.name;
-
- if (!this.label) {
- this.markError(this.text.invalid_attribute(attributeName));
- }
-
- this.cdRef.detectChanges();
- }
-
- markError(message:string) {
- this.error = this.I18n.t('js.editor.macro.error', { message });
- this.cdRef.detectChanges();
+ this.pullRequest$ = this
+ .pullRequests
+ .requireSingle(this.pullRequestId);
}
}
diff --git a/modules/github_integration/frontend/module/state/github-pull-request.service.ts b/modules/github_integration/frontend/module/state/github-pull-request.service.ts
index a8059472c63..0d1e24a3bcf 100644
--- a/modules/github_integration/frontend/module/state/github-pull-request.service.ts
+++ b/modules/github_integration/frontend/module/state/github-pull-request.service.ts
@@ -28,48 +28,31 @@
import { Injectable } from '@angular/core';
import { WorkPackageResource } from 'core-app/features/hal/resources/work-package-resource';
-import { ApiV3ListParameters } from 'core-app/core/apiv3/paths/apiv3-list-resource.interface';
-import { Observable } from 'rxjs';
-import { IHALCollection } from 'core-app/core/apiv3/types/hal-collection.type';
-import {
- CollectionStore,
- ResourceCollectionLoadOptions,
- ResourceCollectionService,
-} from 'core-app/core/state/resource-collection.service';
import { IGithubPullRequest } from 'core-app/features/plugins/linked/openproject-github_integration/state/github-pull-request.model';
import { GithubPullRequestsStore } from 'core-app/features/plugins/linked/openproject-github_integration/state/github-pull-request.store';
import { ID } from '@datorama/akita';
+import { ResourceStoreService, ResourceStore } from 'core-app/core/state/resource-store.service';
+import { Observable } from 'rxjs';
@Injectable()
-export class GithubPullRequestResourceService extends ResourceCollectionService<IGithubPullRequest> {
- ofWorkPackage(workPackage:WorkPackageResource) {
- return this.requireEntity(`${workPackage.href as string}/github_pull_requests`);
+export class GithubPullRequestResourceService extends ResourceStoreService<IGithubPullRequest> {
+ ofWorkPackage(workPackage:WorkPackageResource):Observable<IGithubPullRequest[]> {
+ return this.requireCollection(`${workPackage.href as string}/github_pull_requests`);
}
- requireSingle(id:ID) {
+ requireSingle(id:ID):Observable<IGithubPullRequest> {
return this.requireEntity(this.entityPath(id));
}
- fetchCollection(
- params:ApiV3ListParameters|string,
- options:ResourceCollectionLoadOptions = { handleErrors: true },
- ):Observable<IHALCollection<IGithubPullRequest>> {
- if (typeof params !== 'string') {
- throw new Error('Github PR service can only deal with string collection keys being their full paths')
- }
-
- return this.request(params, params, options);
- }
-
protected basePath():string {
return this.apiV3Service.github_pull_requests.path;
}
- protected entityPath(id:ID) {
+ protected entityPath(id:ID):string {
return this.apiV3Service.github_pull_requests.id(id).path;
}
- protected createStore():CollectionStore<IGithubPullRequest> {
+ protected createStore():ResourceStore<IGithubPullRequest> {
return new GithubPullRequestsStore();
}
}
diff --git a/modules/github_integration/frontend/module/state/github-pull-request.store.ts b/modules/github_integration/frontend/module/state/github-pull-request.store.ts
index ac90f4d72a5..76dd375230a 100644
--- a/modules/github_integration/frontend/module/state/github-pull-request.store.ts
+++ b/modules/github_integration/frontend/module/state/github-pull-request.store.ts
@@ -1,13 +1,16 @@
import { EntityStore, StoreConfig } from '@datorama/akita';
-import { CollectionState, createInitialCollectionState } from 'core-app/core/state/collection-store';
import { IGithubPullRequest } from 'core-app/features/plugins/linked/openproject-github_integration/state/github-pull-request.model';
+import {
+ createInitialResourceState,
+ ResourceState,
+} from 'core-app/core/state/resource-store';
-export interface GithubPullRequestsState extends CollectionState<IGithubPullRequest> {
+export interface GithubPullRequestsState extends ResourceState<IGithubPullRequest> {
}
@StoreConfig({ name: 'github-pull-requests' })
export class GithubPullRequestsStore extends EntityStore<GithubPullRequestsState> {
constructor() {
- super(createInitialCollectionState());
+ super(createInitialResourceState());
}
}
diff --git a/modules/github_integration/frontend/module/tab-prs/tab-prs.component.html b/modules/github_integration/frontend/module/tab-prs/tab-prs.component.html
index 65632a922cb..1ad3aa9b9cd 100644
--- a/modules/github_integration/frontend/module/tab-prs/tab-prs.component.html
+++ b/modules/github_integration/frontend/module/tab-prs/tab-prs.component.html
@@ -1,5 +1,5 @@
<ng-container *ngIf="(pullRequests$ | async)?.length === 0">
- <p [innerHTML]="getEmptyText()"></p>
+ <p [innerHTML]="emptyText"></p>
</ng-container>
<op-github-pull-request
diff --git a/modules/github_integration/lib/open_project/github_integration/engine.rb b/modules/github_integration/lib/open_project/github_integration/engine.rb
index 78e2db519b1..7b8fd686cdd 100644
--- a/modules/github_integration/lib/open_project/github_integration/engine.rb
+++ b/modules/github_integration/lib/open_project/github_integration/engine.rb
@@ -27,11 +27,7 @@
#++
require 'open_project/plugins'
-
require_relative './patches/api/work_package_representer'
-require_relative './notification_handler'
-require_relative './hook_handler'
-require_relative './services'
module OpenProject::GithubIntegration
class Engine < ::Rails::Engine
diff --git a/modules/github_integration/lib/open_project/github_integration/notification_handler/issue_comment.rb b/modules/github_integration/lib/open_project/github_integration/notification_handler/issue_comment.rb
index 4ab91d0a30a..a0920174d0a 100644
--- a/modules/github_integration/lib/open_project/github_integration/notification_handler/issue_comment.rb
+++ b/modules/github_integration/lib/open_project/github_integration/notification_handler/issue_comment.rb
@@ -32,6 +32,8 @@ module OpenProject::GithubIntegration
# Handles GitHub issue comment notifications.
class IssueComment
include OpenProject::GithubIntegration::NotificationHandler::Helper
+ include ActionView::Helpers::TagHelper
+ include ::AngularHelper
COMMENT_ACTIONS = %w[
created
@@ -44,10 +46,14 @@ module OpenProject::GithubIntegration
github_system_user = User.find_by(id: payload.open_project_user_id)
work_packages = find_mentioned_work_packages(payload.comment.body, github_system_user)
+
new_work_packages = without_already_referenced(work_packages, pull_request)
+ return unless new_work_packages.any?
+
+ pull_request = upsert_partial_pull_request(new_work_packages)
+ notes = journal_entry(pull_request, payload)
- upsert_partial_pull_request(new_work_packages)
- comment_on_referenced_work_packages(new_work_packages, github_system_user, journal_entry) if new_work_packages.any?
+ comment_on_referenced_work_packages(new_work_packages, github_system_user, notes)
end
private
@@ -74,25 +80,16 @@ module OpenProject::GithubIntegration
)
end
- # rubocop:disable Metrics/AbcSize
- def journal_entry
+ def journal_entry(pull_request, payload)
return unless COMMENT_ACTIONS.include?(payload.action)
- issue = payload.issue
- comment = payload.comment
- repository = payload.repository
- user = comment.user
-
- I18n.t("github_integration.pull_request_referenced_comment",
- pr_number: issue.number,
- pr_title: issue.title,
- pr_url: comment.html_url,
- repository: repository.full_name,
- repository_url: repository.html_url,
- github_user: user.login,
- github_user_url: user.html_url)
+ angular_component_tag 'macro',
+ class: 'github_pull_request',
+ inputs: {
+ pullRequestId: pull_request.id,
+ pullRequestState: 'referenced'
+ }
end
- # rubocop:enable Metrics/AbcSize
end
end
end
diff --git a/modules/github_integration/lib/open_project/github_integration/notification_handler/pull_request.rb b/modules/github_integration/lib/open_project/github_integration/notification_handler/pull_request.rb
index bb4442b984f..4db582c8698 100644
--- a/modules/github_integration/lib/open_project/github_integration/notification_handler/pull_request.rb
+++ b/modules/github_integration/lib/open_project/github_integration/notification_handler/pull_request.rb
@@ -32,6 +32,8 @@ module OpenProject::GithubIntegration
# Handles GitHub pull request notifications.
class PullRequest
include OpenProject::GithubIntegration::NotificationHandler::Helper
+ include ActionView::Helpers::TagHelper
+ include ::AngularHelper
COMMENT_ACTIONS = %w[
closed
@@ -50,7 +52,7 @@ module OpenProject::GithubIntegration
comment_on_referenced_work_packages(
work_packages_to_comment_on(payload.action, pull_request, work_packages),
github_system_user,
- journal_entry(pull_request)
+ journal_entry(pull_request, payload)
)
end
@@ -73,8 +75,29 @@ module OpenProject::GithubIntegration
work_packages:)
end
- def journal_entry(pull_request)
- %(<macro class="github_pull_request" data-pull-request-id="#{pull_request.id}"></macro>)
+ def journal_entry(pull_request, payload)
+ angular_component_tag 'macro',
+ class: 'github_pull_request',
+ inputs: {
+ pullRequestId: pull_request.id,
+ pullRequestState: pull_request_state(payload)
+ }
+ end
+
+ def pull_request_state(payload)
+ key = {
+ 'opened' => 'opened',
+ 'reopened' => 'opened',
+ 'closed' => 'closed',
+ 'edited' => 'referenced',
+ 'referenced' => 'referenced',
+ 'ready_for_review' => 'ready_for_review'
+ }[payload.action]
+
+ return 'merged' if key == 'closed' && payload.pull_request.merged
+ return 'draft' if key == 'open' && payload.pull_request.draft
+
+ key
end
end
end