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 59523df..acc2f6a 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? @@ -11,6 +11,42 @@ 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[: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? diff --git a/config/locales/en.yml b/config/locales/en.yml index 98bb8c9..c497bb3 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -3,5 +3,8 @@ 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." + 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})" 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'