-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgithub_commits_controller.rb
More file actions
116 lines (91 loc) · 4.05 KB
/
github_commits_controller.rb
File metadata and controls
116 lines (91 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class GithubCommitsController < ApplicationController
unloadable
skip_before_action :check_if_login_required
skip_before_action :verify_authenticity_token
before_action :verify_signature?
GITHUB_URL = "https://github.com/"
REDMINE_JOURNALIZED_TYPE = "Issue"
REDMINE_ISSUE_NUMBER_PREFIX = "#rm"
#added JPBD Jan2022
def github_change_notification
resp_json = nil
# if payload contains pr and action is opened (new pr)
if params[:pull_request].present? && params[:pull_request][:state] == "open"
#get issue ID
pr_title = params[:pull_request][:title]
issue_id = pr_title.partition(REDMINE_ISSUE_NUMBER_PREFIX).last.split(" ").first.to_i
issue = Issue.find_by(id: issue_id)
#use env var if defined, else fails
if ENV["CURRENT_REDMINE_STATE"].nil? or ENV["NEXT_REDMINE_STATE"].nil?
Rails.logger.info "Environment variables are wrong. Will not do anything."
resp_json = {success: false, error: t('lables.env_var_missing') }
else
#if issue id exists in redmine & status is matching currentstate integer (status code)
if issue.present? && issue.status_id == ENV["CURRENT_REDMINE_STATE"].to_i
issue.update(status_id: ENV["NEXT_REDMINE_STATE"].to_i)
resp_json = {success: true}
else
Rails.logger.info "Could not update issue " + issue_id.to_s + "."
resp_json = {success: false, error: t('lables.no_update') }
end
end
else # if not a pr payload
resp_json = {success: false, error: t('lables.no_pr_found') }
end
respond_to do |format|
format.json { render json: resp_json, status: :ok }
end
end
def create_comment
resp_json = nil
if params[:commits].present?
repository_name = params[:repository][:name]
branch = params[:ref].split("/").last
params[:commits].each do |last_commit|
message = last_commit[:message]
if message.present? && is_commit_to_be_tracked?(last_commit)
issue_id = message.partition(REDMINE_ISSUE_NUMBER_PREFIX).last.split(" ").first.to_i
issue = Issue.find_by(id: issue_id)
end
if last_commit.present? && issue.present?
email = EmailAddress.find_by(address: last_commit[:author][:email])
user = email.present? ? email.user : User.where(admin: true).first
author = last_commit[:author][:name]
notes = t('commit.message', author: author,
branch: branch,
message: message,
commit_id: last_commit[:id],
commit_url: last_commit[:url])
issue.journals.create(journalized_id: issue_id,
journalized_type: REDMINE_JOURNALIZED_TYPE,
user: user,
notes: notes
)
resp_json = {success: true}
else
resp_json = {success: false, error: t('lables.no_issue_found') }
end
end
else
resp_json = {success: false, error: t('lables.no_commit_data_found') }
end
respond_to do |format|
format.json { render json: resp_json, status: :ok }
end
end
def verify_signature?
if request.env['HTTP_X_HUB_SIGNATURE'].blank? || ENV["GITHUB_SECRET_TOKEN"].blank?
render json: {success: false},status: 500
else
request.body.rewind
payload_body = request.body.read
signature = 'sha1=' + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), ENV["GITHUB_SECRET_TOKEN"], payload_body)
render json: {success: false},status: 500 unless Rack::Utils.secure_compare(signature, request.env['HTTP_X_HUB_SIGNATURE'])
end
end
private
def is_commit_to_be_tracked?(commit_obj)
commit_obj[:distinct] == true && #is it a fresh commit ?
commit_obj[:message].include?(REDMINE_ISSUE_NUMBER_PREFIX) #Does it include the redmine issue prefix string pattern?
end
end