From 7e109244257739ee281d835377525dd2a84f3d5d Mon Sep 17 00:00:00 2001 From: Ashwin Date Date: Wed, 25 Dec 2019 12:33:03 +0530 Subject: [PATCH] Fixes for Redmine 4 and rails 5.2. Also merged MR tracking from ogir-ok (#1) * Added MR tracking * Display options * Updated. Not all params are required * Updated view * Label width * update if not exists * Fix for Redmine 4 and Rails 5.2 * Fix migrations for Redmine 4 and Rails 5.2 --- app/controllers/gitlab_tracking_controller.rb | 37 +++++++++- app/models/gitlab_tracking_merge_request.rb | 36 +++++++++ ...ew_issues_show_description_bottom.html.erb | 74 +++++++++++++++++-- .../001_create_gitlab_tracking_commits.rb | 2 +- ...02_add_index_to_gitlab_tracking_commits.rb | 4 +- ...03_create_gitlab_tracking_merge_request.rb | 27 +++++++ glmr.json | 68 +++++++++++++++++ init.rb | 9 ++- lib/gitlab_tracking/hooks.rb | 5 +- 9 files changed, 246 insertions(+), 16 deletions(-) create mode 100644 app/models/gitlab_tracking_merge_request.rb create mode 100644 db/migrate/003_create_gitlab_tracking_merge_request.rb create mode 100644 glmr.json diff --git a/app/controllers/gitlab_tracking_controller.rb b/app/controllers/gitlab_tracking_controller.rb index e8a29ab..afc5e60 100644 --- a/app/controllers/gitlab_tracking_controller.rb +++ b/app/controllers/gitlab_tracking_controller.rb @@ -1,15 +1,46 @@ class GitlabTrackingController < ApplicationController unloadable - skip_before_filter :verify_authenticity_token - skip_before_filter :check_if_login_required + skip_before_action :verify_authenticity_token + skip_before_action :check_if_login_required def webhook_parsing - parse_push_hook(JSON.parse(request.body.read)) + body = JSON.parse(request.body.read) + if body['object_kind'] == 'push' + parse_push_hook(JSON.parse(request.body.read)) + elsif body['object_kind'] == 'merge_request' + parse_merge_request_hook(body) + end render status: 200, json: "OK".to_json end protected + def parse_merge_request_hook(body) + search_regexp = get_issue_regexp + merge_request = body['object_attributes'] + match_regexp = merge_request['title'].gsub search_regexp + author = merge_request['last_commit']['author'] + assignee = body['assignee'] + if not match_regexp + match_regexp = merge_request['source_branch'].gsub search_regexp + end + if not match_regexp + match_regexp = merge_request['last_commit']['message'].gsub search_regexp + end + + match_regexp.each do |issue_raw| + issue_raw =~ /(?\d+)/ + begin + issue = Issue.find(Regexp.last_match['issue_number'].to_i) + GitlabTrackingMergeRequest.parse_merge_request_and_create(issue, merge_request, author, assignee) + rescue ActiveRecord::RecordNotFound + # ignored + end + end + end + + protected + def parse_push_hook(body) search_regexp = get_issue_regexp branch = body['ref'] diff --git a/app/models/gitlab_tracking_merge_request.rb b/app/models/gitlab_tracking_merge_request.rb new file mode 100644 index 0000000..aedb85a --- /dev/null +++ b/app/models/gitlab_tracking_merge_request.rb @@ -0,0 +1,36 @@ +class GitlabTrackingMergeRequest < ActiveRecord::Base + unloadable + belongs_to :issue + + + class << self + + def parse_merge_request_and_create(issue, merge_request, author, assignee) + gtmr = find_or_create_by(gitlab_id: merge_request['id']) + gtmr.issue = issue + + gtmr.source = merge_request['source_branch'] + gtmr.target = merge_request['target_branch'] + gtmr.title = merge_request['title'] + + gtmr.state = merge_request['state'] + gtmr.merge_status = merge_request['merge_status'] + + gtmr.gitlab_url = merge_request['url'] + + if author + gtmr.author_username = author['username'] + gtmr.author_name = author['name'] + end + + if assignee + gtmr.assignee_username = assignee['username'] + gtmr.assignee_name = assignee['name'] + end + + gtmr.timestamp = Time.parse(merge_request['created_at']) + gtmr.save + end + end + +end diff --git a/app/views/hooks/gitlab_tracking/_view_issues_show_description_bottom.html.erb b/app/views/hooks/gitlab_tracking/_view_issues_show_description_bottom.html.erb index 041aa8c..c411ac0 100644 --- a/app/views/hooks/gitlab_tracking/_view_issues_show_description_bottom.html.erb +++ b/app/views/hooks/gitlab_tracking/_view_issues_show_description_bottom.html.erb @@ -1,5 +1,5 @@

- GitLab Tracking + Merge Requests + +

+ + <% gtc_merge_request_list.each do |merge_request| %> +
+
+ +
+
+
+ <%= I18n.l merge_request.timestamp.in_time_zone(User.current.time_zone), :format => :long %> +
+
+
+ <%= link_to "#{merge_request.title}", "#{merge_request.gitlab_url}", :target => "_blank" %> +
+
+
+
+ Author: <%= merge_request.author_name %> +
+
+ Assignee: <%= merge_request.assignee_name %> +
+
+
+ <%= merge_request.source =%> → <%= merge_request.target =%> +
+
+
+
+ <%= merge_request.state =%> +
+
+ <%= merge_request.merge_status =%> +
+
+
+
+ <% end %> + +

+ + Commmits

diff --git a/db/migrate/001_create_gitlab_tracking_commits.rb b/db/migrate/001_create_gitlab_tracking_commits.rb index 0d6ad89..194ae31 100644 --- a/db/migrate/001_create_gitlab_tracking_commits.rb +++ b/db/migrate/001_create_gitlab_tracking_commits.rb @@ -1,4 +1,4 @@ -class CreateGitlabTrackingCommits < ActiveRecord::Migration +class CreateGitlabTrackingCommits < Rails.version < '5.1' ? ActiveRecord::Migration : ActiveRecord::Migration[4.2] def change create_table :gitlab_tracking_commits do |t| t.integer :issue_id diff --git a/db/migrate/002_add_index_to_gitlab_tracking_commits.rb b/db/migrate/002_add_index_to_gitlab_tracking_commits.rb index 882dd14..27c0892 100644 --- a/db/migrate/002_add_index_to_gitlab_tracking_commits.rb +++ b/db/migrate/002_add_index_to_gitlab_tracking_commits.rb @@ -1,5 +1,5 @@ -class AddIndexToGitlabTrackingCommits < ActiveRecord::Migration +class AddIndexToGitlabTrackingCommits < Rails.version < '5.1' ? ActiveRecord::Migration : ActiveRecord::Migration[4.2] def change add_index :gitlab_tracking_commits, [:issue_id, :git_hash] end -end \ No newline at end of file +end diff --git a/db/migrate/003_create_gitlab_tracking_merge_request.rb b/db/migrate/003_create_gitlab_tracking_merge_request.rb new file mode 100644 index 0000000..daaf2ed --- /dev/null +++ b/db/migrate/003_create_gitlab_tracking_merge_request.rb @@ -0,0 +1,27 @@ +class CreateGitlabTrackingMergeRequest < Rails.version < '5.1' ? ActiveRecord::Migration : ActiveRecord::Migration[4.2] + def change + create_table :gitlab_tracking_merge_requests do |t| + t.integer :issue_id + t.integer :gitlab_id + t.string :source + t.string :target + t.string :title + + t.string :state + t.string :merge_status + t.string :gitlab_url, :limit => 4096 + + + t.string :author_username + t.string :author_name + + t.string :assignee_username + t.string :assignee_name + + t.timestamp :timestamp + + end + + add_index :gitlab_tracking_merge_requests, [:issue_id, :gitlab_id] + end +end diff --git a/glmr.json b/glmr.json new file mode 100644 index 0000000..a44cdec --- /dev/null +++ b/glmr.json @@ -0,0 +1,68 @@ + "object_attributes": { + "source_branch": "ms-viewport", + "source_project_id": 14, + "author_id": 51, + "assignee_id": 6, + "title": "MS-Viewport", + "created_at": "2013-12-03T17:23:34Z", + "updated_at": "2013-12-03T17:23:34Z", + "st_commits": null, + "st_diffs": null, + "milestone_id": null, + "state": "opened", + "merge_status": "unchecked", + "target_project_id": 14, + "iid": 1, + "description": "", + "source": { + "name":"Awesome Project", + "description":"Aut reprehenderit ut est.", + "web_url":"http://example.com/awesome_space/awesome_project", + "avatar_url":null, + "git_ssh_url":"git@example.com:awesome_space/awesome_project.git", + "git_http_url":"http://example.com/awesome_space/awesome_project.git", + "namespace":"Awesome Space", + "visibility_level":20, + "path_with_namespace":"awesome_space/awesome_project", + "default_branch":"master", + "homepage":"http://example.com/awesome_space/awesome_project", + "url":"http://example.com/awesome_space/awesome_project.git", + "ssh_url":"git@example.com:awesome_space/awesome_project.git", + "http_url":"http://example.com/awesome_space/awesome_project.git" + }, + "target": { + "name":"Awesome Project", + "description":"Aut reprehenderit ut est.", + "web_url":"http://example.com/awesome_space/awesome_project", + "avatar_url":null, + "git_ssh_url":"git@example.com:awesome_space/awesome_project.git", + "git_http_url":"http://example.com/awesome_space/awesome_project.git", + "namespace":"Awesome Space", + "visibility_level":20, + "path_with_namespace":"awesome_space/awesome_project", + "default_branch":"master", + "homepage":"http://example.com/awesome_space/awesome_project", + "url":"http://example.com/awesome_space/awesome_project.git", + "ssh_url":"git@example.com:awesome_space/awesome_project.git", + "http_url":"http://example.com/awesome_space/awesome_project.git" + }, + "last_commit": { + "id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", + "message": "fixed readme", + "timestamp": "2012-01-03T23:36:29+02:00", + "url": "http://example.com/awesome_space/awesome_project/commits/da1560886d4f094c3e6c9ef40349f7d38b5d27d7", + "author": { + "name": "GitLab dev user", + "email": "gitlabdev@dv6700.(none)" + } + }, + "work_in_progress": false, + "url": "http://example.com/diaspora/merge_requests/1", + "action": "open", + "assignee": { + "name": "User1", + "username": "user1", + "avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon" + } + }, + diff --git a/init.rb b/init.rb index 8e82fdb..2cb003c 100644 --- a/init.rb +++ b/init.rb @@ -5,15 +5,18 @@ name 'Gitlab Tracking plugin' author 'Sergey Kravchuk' description 'Tracking gitlab activity repo' - version '1.4' + version '1.5' url 'https://github.com/alfss/gitlab_tracking' author_url 'http://alfss.net' settings(:partial => 'settings/gitlab_tracking_settings', :default => { 'issue_regexp' => '((fix|ref)\s*#?[0-9]+)', - 'issue_regexp_options' => 'i' + 'issue_regexp_options' => 'i', + 'push_hook_enabled' => true, + 'merge_request_hook_enabled' => true, + 'assign_users' => true, }) end -require_dependency 'gitlab_tracking/hooks' \ No newline at end of file +require_dependency 'gitlab_tracking/hooks' diff --git a/lib/gitlab_tracking/hooks.rb b/lib/gitlab_tracking/hooks.rb index cb86e21..451febe 100644 --- a/lib/gitlab_tracking/hooks.rb +++ b/lib/gitlab_tracking/hooks.rb @@ -3,12 +3,13 @@ module GitlabTracking class Hooks < Redmine::Hook::ViewListener def view_issues_show_description_bottom(context={ }) - context[:gtc_commit_list] = GitlabTrackingCommit.where(issue_id: context[:issue]).order('id') + context[:gtc_merge_request_list] = GitlabTrackingMergeRequest.where(issue_id: context[:issue]).order('id') context[:controller].send(:render_to_string, { :partial => "hooks/gitlab_tracking/view_issues_show_description_bottom", :locals => context }) end + end -end \ No newline at end of file +end