From ce051f4aa0354740b3518f258eb558aaa8b36c64 Mon Sep 17 00:00:00 2001 From: JPBD Date: Wed, 8 Dec 2021 11:12:03 -0500 Subject: [PATCH 1/9] skip_before_filter becomes skip_before_action to work with redmine 4.1.1 --- app/controllers/github_commits_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/github_commits_controller.rb b/app/controllers/github_commits_controller.rb index 59523df..1f63586 100644 --- a/app/controllers/github_commits_controller.rb +++ b/app/controllers/github_commits_controller.rb @@ -2,8 +2,8 @@ class GithubCommitsController < ApplicationController unloadable - skip_before_filter :check_if_login_required - skip_before_filter :verify_authenticity_token + skip_before_action :check_if_login_required + skip_before_action :verify_authenticity_token before_action :verify_signature? From e41427b70ccfc823f6e4a5a4fe27313e2507d423 Mon Sep 17 00:00:00 2001 From: JPBD Date: Thu, 13 Jan 2022 19:05:52 -0500 Subject: [PATCH 2/9] initial commit test --- test1 | 1 + 1 file changed, 1 insertion(+) create mode 100644 test1 diff --git a/test1 b/test1 new file mode 100644 index 0000000..f9e259b --- /dev/null +++ b/test1 @@ -0,0 +1 @@ +initial commit test file From e8bedd87179dc57aae108f9427795fc7e18038d2 Mon Sep 17 00:00:00 2001 From: JPBD Date: Tue, 18 Jan 2022 21:04:53 -0500 Subject: [PATCH 3/9] first test plugin upgrade --- app/controllers/github_commits_controller.rb | 32 ++++++++++++++++++++ config/locales/en.yml | 2 ++ config/routes.rb | 1 + 3 files changed, 35 insertions(+) diff --git a/app/controllers/github_commits_controller.rb b/app/controllers/github_commits_controller.rb index 1f63586..527e08b 100644 --- a/app/controllers/github_commits_controller.rb +++ b/app/controllers/github_commits_controller.rb @@ -11,6 +11,38 @@ class GithubCommitsController < ApplicationController 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[:action] == "opened" + + #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) + + #if issue id exists in redmine & status is in progress (2) + if issue.present? && issue.status_id == 2 + issue.status = 14 #jenkins validation + resp_json = {success: true} + else + resp_json = {success: false, error: t('lables.no_pr_found') } + end + + resp_json = {success: true} + else # if not a pr payload + resp_json = {success: false, error: t('lables.no_update') } + 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? diff --git a/config/locales/en.yml b/config/locales/en.yml index 98bb8c9..9d5ddb1 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -3,5 +3,7 @@ en: lables: no_commit_data_found: "Request does not have commit data." no_issue_found: "Issue not found." + no_pr_found: "Request does not have PR data." + no_update: "status was not updated for missing requirements." commit: message: ! "__Github Commit__ - __%{author}__ has commited on __%{branch}__ branch with message: __%{message}__ , [%{commit_id}](%{commit_url})" diff --git a/config/routes.rb b/config/routes.rb index a80f84b..d89f78b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,4 @@ # Plugin's routes # See: http://guides.rubyonrails.org/routing.html post 'github_commits/create_comment.json', to: 'github_commits#create_comment' +post 'github_commits/github_change_notification.json', to: 'github_commits#github_change_notification' From 70b5696bcc27f4ba63ad47eba8eac179dd9c16b4 Mon Sep 17 00:00:00 2001 From: JPBD Date: Thu, 20 Jan 2022 00:30:19 -0500 Subject: [PATCH 4/9] functioning status update from webhook --- app/controllers/github_commits_controller.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/controllers/github_commits_controller.rb b/app/controllers/github_commits_controller.rb index 527e08b..61db6b3 100644 --- a/app/controllers/github_commits_controller.rb +++ b/app/controllers/github_commits_controller.rb @@ -15,16 +15,18 @@ class GithubCommitsController < ApplicationController def github_change_notification resp_json = nil # if payload contains pr and action is opened (new pr) - if params[:pull_request].present? && params[:action] == "opened" - + 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) - #if issue id exists in redmine & status is in progress (2) if issue.present? && issue.status_id == 2 - issue.status = 14 #jenkins validation + #we are using status id 14 for "jenkins validation" which we created. + #you can easily adapt this plugin to your need by changing + #the "from" status id in the previous if conditional statement and + #the "to" status id in next line update statement + issue.update(status_id: 14) #jenkins validation resp_json = {success: true} else resp_json = {success: false, error: t('lables.no_pr_found') } @@ -40,8 +42,6 @@ def github_change_notification end end - # -- - def create_comment resp_json = nil From ba83b80a6ba6b4671dfc25e20dda1d44e60ead81 Mon Sep 17 00:00:00 2001 From: JPBD Date: Thu, 20 Jan 2022 00:32:32 -0500 Subject: [PATCH 5/9] removed previous useless test1 file from code repo --- test1 | 1 - 1 file changed, 1 deletion(-) delete mode 100644 test1 diff --git a/test1 b/test1 deleted file mode 100644 index f9e259b..0000000 --- a/test1 +++ /dev/null @@ -1 +0,0 @@ -initial commit test file From 54518bb0756b7e40b84e13b28f36005744aa1e1a Mon Sep 17 00:00:00 2001 From: JPBD Date: Thu, 20 Jan 2022 17:30:35 -0500 Subject: [PATCH 6/9] added env variable support for status, failsafe in case none are set --- app/controllers/github_commits_controller.rb | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/app/controllers/github_commits_controller.rb b/app/controllers/github_commits_controller.rb index 61db6b3..78fb67f 100644 --- a/app/controllers/github_commits_controller.rb +++ b/app/controllers/github_commits_controller.rb @@ -20,13 +20,18 @@ def github_change_notification 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 use default + currentstate = ENV["CURRENT_REDMINE_STATE"] + if currentstate.nil? + currentstate = 2 #default for in progress + end + nextstate = ENV["NEXT_REDMINE_STATE"] + if nextstate.nil? + nextstate = 3 #default for in review + end #if issue id exists in redmine & status is in progress (2) - if issue.present? && issue.status_id == 2 - #we are using status id 14 for "jenkins validation" which we created. - #you can easily adapt this plugin to your need by changing - #the "from" status id in the previous if conditional statement and - #the "to" status id in next line update statement - issue.update(status_id: 14) #jenkins validation + if issue.present? && issue.status_id == currentstate.to_i + issue.update(status_id: nextstate.to_i) resp_json = {success: true} else resp_json = {success: false, error: t('lables.no_pr_found') } From a664f9ea855f18b29a13e451b976d7307be2c102 Mon Sep 17 00:00:00 2001 From: JPBD Date: Thu, 27 Jan 2022 20:50:54 -0500 Subject: [PATCH 7/9] modified code to be less prone to error, graceful failures in case of missing env var (no default), updated readme --- README.md | 8 +++++ app/controllers/github_commits_controller.rb | 32 ++++++++++---------- config/locales/en.yml | 1 + 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index c8dd78e..dc7f742 100644 --- a/README.md +++ b/README.md @@ -15,3 +15,11 @@ This plugin adds a comment in redmine issue whenever user commits to github with 5. For repository, webhook should be created with payload-url as `localhost:3000/github_commits/create_comment.json` where in url replace `localhost:3000` with your host address. 6. In Redmine, Go to Administration -> Settings -> General Tab and change text formatting to `Markdown`. This will show the comment message properly. + +## Steps to use second feature (github change notification) which moves issues when there is a PR + +1. Create webhook like previous feature but with `/github_commits/github_change_notification.json` as URL. Send Pull Request only. + +2. Set ENV Variables like when you did for `GITHUB_SECRET_TOKEN` but for `CURRENT_REDMINE_STATE` (single integer) and `NEXT_REDMINE_STATE` (single integer). + +3. When doing a PR add `#rmXXX` like previous in the title to point which redmine ticket number must be automatically updated. diff --git a/app/controllers/github_commits_controller.rb b/app/controllers/github_commits_controller.rb index 78fb67f..67e5de7 100644 --- a/app/controllers/github_commits_controller.rb +++ b/app/controllers/github_commits_controller.rb @@ -20,26 +20,25 @@ def github_change_notification 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 use default - currentstate = ENV["CURRENT_REDMINE_STATE"] - if currentstate.nil? - currentstate = 2 #default for in progress - end - nextstate = ENV["NEXT_REDMINE_STATE"] - if nextstate.nil? - nextstate = 3 #default for in review - end - #if issue id exists in redmine & status is in progress (2) - if issue.present? && issue.status_id == currentstate.to_i - issue.update(status_id: nextstate.to_i) - resp_json = {success: true} + #use env var if defined, else fails + if ENV["CURRENT_REDMINE_STATE"].nil? or ENV["NEXT_REDMINE_STATE"].nil? + Rails.logger.info "Environment variable missing. Will not do anything." + Rails.logger.info "You must set an integer value for Both env var CURRENT_REDMINE_STATE and NEXT_REDMINE_STATE." + resp_json = {success: false, error: t('lables.env_var_missing') } else - resp_json = {success: false, error: t('lables.no_pr_found') } + #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 + " with given ENV vars. Is the issue number correct and are ENV var both integers?" + resp_json = {success: false, error: t('lables.no_update') } + end + end - resp_json = {success: true} else # if not a pr payload - resp_json = {success: false, error: t('lables.no_update') } + resp_json = {success: false, error: t('lables.no_pr_found') } end respond_to do |format| @@ -48,6 +47,7 @@ def github_change_notification end + def create_comment resp_json = nil if params[:commits].present? diff --git a/config/locales/en.yml b/config/locales/en.yml index 9d5ddb1..c497bb3 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -5,5 +5,6 @@ en: no_issue_found: "Issue not found." no_pr_found: "Request does not have PR data." no_update: "status was not updated for missing requirements." + env_var_missing: "Environment variable missing. Will not do anything." commit: message: ! "__Github Commit__ - __%{author}__ has commited on __%{branch}__ branch with message: __%{message}__ , [%{commit_id}](%{commit_url})" From b2d57df2f5cdd7de62df67a64c4623f6c05bced9 Mon Sep 17 00:00:00 2001 From: JPBD Date: Thu, 27 Jan 2022 22:29:44 -0500 Subject: [PATCH 8/9] simplified error messages --- app/controllers/github_commits_controller.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/controllers/github_commits_controller.rb b/app/controllers/github_commits_controller.rb index 67e5de7..6bdb0c6 100644 --- a/app/controllers/github_commits_controller.rb +++ b/app/controllers/github_commits_controller.rb @@ -22,8 +22,7 @@ def github_change_notification 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 variable missing. Will not do anything." - Rails.logger.info "You must set an integer value for Both env var CURRENT_REDMINE_STATE and NEXT_REDMINE_STATE." + Rails.logger.info "Environment variable 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) @@ -31,7 +30,7 @@ def github_change_notification 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 + " with given ENV vars. Is the issue number correct and are ENV var both integers?" + Rails.logger.info "Could not update issue " + issue_id.to_s + "." resp_json = {success: false, error: t('lables.no_update') } end From d6c61722c00d37debddb529cc05c1cd15518728e Mon Sep 17 00:00:00 2001 From: JPBD Date: Thu, 27 Jan 2022 22:31:16 -0500 Subject: [PATCH 9/9] corrected error message --- app/controllers/github_commits_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/github_commits_controller.rb b/app/controllers/github_commits_controller.rb index 6bdb0c6..acc2f6a 100644 --- a/app/controllers/github_commits_controller.rb +++ b/app/controllers/github_commits_controller.rb @@ -22,7 +22,7 @@ def github_change_notification 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 variable wrong. Will not do anything." + 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)