Skip to content

New feature in same plugin we did code in house and want to share #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
40 changes: 38 additions & 2 deletions app/controllers/github_commits_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,51 @@ 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?

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?
Expand Down
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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})"
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -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'