Skip to content

Commit

Permalink
Fixes for Redmine 4 and rails 5.2. Also merged MR tracking from ogir-…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
coolbung authored and Kravchuk S.V committed Dec 25, 2019
1 parent a1e8ddf commit 7e10924
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 16 deletions.
37 changes: 34 additions & 3 deletions app/controllers/gitlab_tracking_controller.rb
Original file line number Diff line number Diff line change
@@ -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 =~ /(?<issue_number>\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']
Expand Down
36 changes: 36 additions & 0 deletions app/models/gitlab_tracking_merge_request.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<style type='text/css'>
.gitlab-tracking-row {padding: 5px; margin-top:15px; width: 600px; background-color:#fff; border: solid 1px #d5d5d5;}
.gitlab-tracking-row {padding: 5px; margin-top:15px; width: 99%; background-color:#fff; border: solid 1px #d5d5d5;}
.gitlab-tracking-avatar { padding-right: 10px; float: left;width: 65px;}
.gitlab-tracking-date {
border: solid 1px #d5d5d5;
Expand All @@ -11,17 +11,81 @@
top: -16px;
background-color: #fff;}
.gitlab-tracking-hash {margin-top: 5px;}
.gitlab-tracking-message {margin-top: 5px; }
.gitlab-tracking-content { width: 525px; float: left;}
.gitlab-tracking-message {margin-top: 5px; font-size: small; font-style: italic}
.gitlab-tracking-content {float: left;}
.gitlab-tracking-labels {float: right; margin-top: 15px}
.gitlab-tracking-author {margin-top: -15px;}
.gitlab-tracking-clear { clear: left; }
.gitlab-tracking-clear {clear: left;}
.gitlab-tracking-label {overflow: hidden; text-overflow: elipsis; white-space: nowrap; float: right; padding: 2px; border-radius: 3px; border: 1px solid black; margin: 3px}

.gitlab-tracking-mr-title {font-weight: bold;}
.gitlab-tracking-mr-row-author {font-size: x-small; font-style: italic;}
.gitlab-tracking-mr-assignee {}
.mr-state--opened {border-color: #5bc0de; color: #5bc0de;}
.mr-state--opened::before {content: "❂"}
.mr-state--closed {border-color: #d9534f; color: #d9534f;}
.mr-state--closed::before {content: "✕"}
.mr-state--merged {border-color: #5cb85c; color: #5cb8bc;}
.mr-state--merged::before {content: "✓"}

.mr-status--unchecked {border-color: #5bc0de; color: #5bc0de;}
.mr-status--unchecked::before {content: "❂"}
.mr-status--cannot_be_merged {border-color: #d9534f; color: #d9534f;}
.mr-status--cannot_be_merged::before {content: "✕"}
.mr-status--can_be_merged {border-color: #5cb85c; color: #5cb8bc;}
.mr-status--can_be_merged::before {content: "✓"}

</style>

<hr>
<div id="gitlab_tracking">
<p>
<strong>
GitLab Tracking
Merge Requests
</strong>
</p>

<% gtc_merge_request_list.each do |merge_request| %>
<div class="gitlab-tracking-row">
<div class="gitlab-tracking-avatar" style="">
<img style="width: 64px; height: 64px;" src="https://gitlab.com/gitlab-com/gitlab-artwork/raw/master/wordmark/stacked_wm_no_bg.png" />
</div>
<div class="gitlab-tracking-content">
<div class="gitlab-tracking-date">
<%= I18n.l merge_request.timestamp.in_time_zone(User.current.time_zone), :format => :long %>
</div>
<div class="gitlab-tracking-mr-row-title">
<div class="gitlab-tracking-mr-title">
<%= link_to "#{merge_request.title}", "#{merge_request.gitlab_url}", :target => "_blank" %>
</div>
</div>
<div class="gitlab-tracking-mr-row-author">
<div class="gitlab-tracking-mr-author">
Author: <%= merge_request.author_name %>
</div>
<div class="gitlab-tracking-mr-assignee">
Assignee: <%= merge_request.assignee_name %>
</div>
</div>
<div class="gitlab-tracking-message">
<%= merge_request.source =%> &#8594; <%= merge_request.target =%>
</div>
</div>
<div class="gitlab-tracking-labels">
<div class="gitlab-tracking-label mr-state--<%= merge_request.state %>">
<%= merge_request.state =%>
</div>
<div class="gitlab-tracking-label mr-status mr-status--<%= merge_request.merge_status %>">
<%= merge_request.merge_status =%>
</div>
</div>
<div class="gitlab-tracking-clear"></div>
</div>
<% end %>

<p>
<strong>
Commmits
</strong>
</p>

Expand Down
2 changes: 1 addition & 1 deletion db/migrate/001_create_gitlab_tracking_commits.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions db/migrate/002_add_index_to_gitlab_tracking_commits.rb
Original file line number Diff line number Diff line change
@@ -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
end
27 changes: 27 additions & 0 deletions db/migrate/003_create_gitlab_tracking_merge_request.rb
Original file line number Diff line number Diff line change
@@ -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
68 changes: 68 additions & 0 deletions glmr.json
Original file line number Diff line number Diff line change
@@ -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":"[email protected]: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":"[email protected]: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":"[email protected]: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":"[email protected]: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"
}
},

9 changes: 6 additions & 3 deletions init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
require_dependency 'gitlab_tracking/hooks'
5 changes: 3 additions & 2 deletions lib/gitlab_tracking/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
end

0 comments on commit 7e10924

Please sign in to comment.