├── .gitignore ├── CHANGELOG.md ├── Gemfile ├── LICENSE ├── README.md ├── Rakefile ├── app ├── controllers │ └── github_hook_controller.rb ├── services │ └── github_hook │ │ ├── message_logger.rb │ │ ├── null_logger.rb │ │ └── updater.rb └── views │ └── github_hook │ └── welcome.html.erb ├── config └── routes.rb ├── init.rb ├── lang └── en.yml ├── lib ├── redmine_github_hook.rb └── redmine_github_hook │ └── plugin.rb ├── redmine_github_hook.gemspec └── test ├── functional └── github_hook_controller_test.rb ├── test_helper.rb └── unit └── github_hook ├── message_logger_test.rb └── updater_test.rb /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .yardoc 6 | Gemfile.lock 7 | InstalledFiles 8 | _yardoc 9 | coverage 10 | doc/ 11 | lib/bundler/man 12 | pkg 13 | rdoc 14 | spec/reports 15 | test/tmp 16 | test/version_tmp 17 | tmp 18 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | This project adheres to [Semantic Versioning](http://semver.org/). 5 | 6 | ## [Unreleased] 7 | 8 | ### Added 9 | 10 | * 11 | 12 | ### Changes 13 | 14 | * 15 | 16 | ### Removed 17 | 18 | * 19 | 20 | 21 | ## 3.0.2 (2024-03-08) 22 | 23 | ### Added 24 | 25 | * Support for Redmine 5 (@taikii, @altheali533) 26 | 27 | ### Changes 28 | 29 | * Tags deleted on the remote will now be pruned from the local repository when we update it. 30 | 31 | 32 | ## 3.0.1 (2019-07-30) 33 | 34 | ### Added 35 | 36 | * A changelog! (you're looking at it). 37 | * Support for Rails 5, which means support for Redmine 4.x and no more deprecation warnings on Redmine 3.x (@bjakushka, @slamotte) 38 | 39 | ### Removed 40 | 41 | * Support for Redmine 2.x and earlier. If you need to use Redmine Github Hook with Redmine versions before 3.x, use the 2.x line of Redmine Github Hook. 42 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | # Use the gems defined in the plugins gemspec 4 | gemspec(:path => File.dirname(__FILE__)) 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2019 Jakob Skjerning 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Redmine GitHub Hook 2 | 3 | [](https://github.com/pickhardt/maintainers-wanted) 4 | 5 | This plugin allows you to update your local Git repositories in Redmine when changes have been pushed to GitHub. 6 | 7 | ## Project Status: Looking for maintainers 8 | 9 | This project is not under active development, although I will continue to provide support for current users, but you can change that by joining the team. 10 | 11 | If you use this project and would like to develop it further, please introduce yourself on the [maintainers wanted ticket](https://github.com/koppen/redmine_github_hook/issues/96). 12 | 13 | ## Description 14 | 15 | [Redmine](http://redmine.org) has supported Git repositories for a long time, allowing you to browse your code and view your changesets directly in Redmine. For this purpose, Redmine relies on local clones of the Git repositories. 16 | 17 | If your shared repository is on a remote machine - for example on GitHub - this unfortunately means a bit of legwork to keep the local, Redmine-accessible repository up-to-date. The common approach is to set up a cronjob that pulls in any changes with regular intervals and updates Redmine with them. 18 | 19 | That approach works perfectly fine, but is a bit heavy-handed and cumbersome. The Redmine GitHub Hook plugin allows GitHub to notify your Redmine installation when changes have been pushed to a repository, triggering an update of your local repository and Redmine data only when it is actually necessary. 20 | 21 | ## Getting started 22 | 23 | ### 1. Install the plugin 24 | 25 | 1. Add the gem to your Gemfile.local: 26 | `gem "redmine_github_hook"` 27 | 2. `bundle` 28 | 3. Run migrations: `bundle exec rake redmine:plugins:migrate RAILS_ENV=production` 29 | 4. Restart your Redmine 30 | 31 | ### 2. Add the repository to Redmine 32 | 33 | Adding a Git repository to a project (note, this should work whether you want to use Redmine GitHub Hook or not). 34 | 35 | 1. Simply follow the instructions for [keeping your git repository in sync](http://www.redmine.org/wiki/redmine/HowTo_keep_in_sync_your_git_repository_for_redmine). 36 | * You don't need to set up a cron task as described in the Redmine instructions. 37 | 38 | ### 3. Connecting GitHub to Redmine 39 | 40 | 1. Go to the repository Settings interface on GitHub. 41 | 2. Under "Webhooks & Services" add a new "WebHook". The "Payload URL" needs to be of the format: `[redmine_url]/github_hook` (for example `http://redmine.example.com/github_hook`). 42 | * By default, GitHub Hook assumes your GitHub repository name is the same as the *project identifier* in your Redmine installation. 43 | * If this is not the case, you can specify the actual Redmine project identifier in the Post-Receive URL by using the format `[redmine_url]/github_hook?project_id=[identifier]` (for example `http://redmine.example.com/github_hook?project_id=my_project`). 44 | * GitHub Hook will then update **all repositories** in the specified project. *Be aware, that this process may take a while if you have many repositories in your project.* 45 | * If you want GitHub Hook to **only update the current repository** you can specify it with an additional parameter in the Post-Receive URL by using the format `[redmine_url]/github_hook?project_id=[identifier]&repository_id=[repository]` (for example `http://redmine.example.com/github_hook?project_id=my_project&repository_id=my_repo`). 46 | * In most cases, just having the "push" event trigger the webhook should suffice, but you are free to customize the events as you desire. 47 | * *Note: Make sure you're adding a Webhook - which is what Redmine Github Hook expects. GitHub has some builtin Redmine integration; that's not what you're looking for.* 48 | 49 | That's it. GitHub will now send a HTTP POST to the Redmine GitHub Hook plugin whenever changes are pushed to GitHub. The plugin then takes care of pulling the changes to the local repositories and updating the Redmine database with them. 50 | 51 | 52 | ## Assumptions 53 | 54 | * Redmine running on a *nix-like system. Redmine versions before 2.0 should use the redmine_1.x branch. This gem has been reported to work with Redmine version 5.x, 4.x, 3.x., 2.x. 55 | * Git 1.5 or higher available on the commandline. 56 | 57 | 58 | ## Troubleshooting 59 | 60 | ### Check your logfile 61 | 62 | If you run into issues, your Redmine logfile might have some valuable information. Two things to check for: 63 | 64 | 1. Do POST requests to `/github_hook` show up in the logfile at all? If so, what's the resulting status code? 65 | 2. If the git command used to pull in changes fails for whatever reason, there should also be some details about the failure in the logfile. 66 | 67 | The logfile is usually found in your Redmine directory in `log/production.log` although your webserver logs may contain some additional clues. 68 | 69 | ### Permissions problems 70 | 71 | As for permissions, whatever user Redmine is running as needs permissions to do the following things: 72 | 73 | * Read from the remote repository on GitHub 74 | * Read and write to the local repository on the Redmine server 75 | 76 | What user you are running Redmine as depends on your system and how you've setup your Redmine installation. 77 | 78 | #### GitHub 79 | 80 | This means you need to add its SSH keys on GitHub. If the user doesn't already have an SSH key, generate one and add the public SSH key as a Deploy Key for the repository on GitHub (or as one of your own keys, if you prefer that). 81 | 82 | #### Local repository 83 | 84 | The user running Redmine needs permissions to read and write to the local repository on the server. 85 | 86 | 87 | ## What happens 88 | 89 | The interactions between the different parts of the process is outlined in the following sequence diagram: 90 | 91 | ```mermaid 92 | sequenceDiagram 93 | participant Dev as Development Machine 94 | participant GH as GitHub 95 | participant Rm as Redmine 96 | Dev->>GH: git push 97 | GH->>Rm: POST /github_hook 98 | Rm->>GH: git fetch 99 | GH-->>Rm: commits 100 | Rm->>Rm: Update repository 101 | ``` 102 | 103 | ## License 104 | 105 | Distributed under the MIT License. See LICENSE for more information. 106 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | require "rake/testtask" 3 | 4 | Rake::TestTask.new do |t| 5 | t.libs << "test" 6 | files = FileList["test/**/*test.rb"] 7 | t.test_files = files 8 | t.verbose = true 9 | end 10 | 11 | task :default => :test 12 | -------------------------------------------------------------------------------- /app/controllers/github_hook_controller.rb: -------------------------------------------------------------------------------- 1 | require "json" 2 | 3 | class GithubHookController < ApplicationController 4 | skip_before_action :verify_authenticity_token, :check_if_login_required 5 | 6 | def index 7 | message_logger = GithubHook::MessageLogger.new(logger) 8 | update_repository(message_logger) if request.post? 9 | messages = message_logger.messages.map { |log| log[:message] } 10 | render(:json => messages) 11 | 12 | rescue ActiveRecord::RecordNotFound => error 13 | render_error_as_json(error, 404) 14 | 15 | rescue TypeError => error 16 | render_error_as_json(error, 412) 17 | end 18 | 19 | def welcome 20 | # Render the default layout 21 | end 22 | 23 | private 24 | 25 | def parse_payload 26 | JSON.parse(params[:payload] || "{}") 27 | end 28 | 29 | def render_error_as_json(error, status) 30 | render( 31 | :json => { 32 | :title => error.class.to_s, 33 | :message => error.message 34 | }, 35 | :status => status 36 | ) 37 | end 38 | 39 | def update_repository(logger) 40 | updater = GithubHook::Updater.new(parse_payload, params) 41 | updater.logger = logger 42 | updater.call 43 | end 44 | end 45 | -------------------------------------------------------------------------------- /app/services/github_hook/message_logger.rb: -------------------------------------------------------------------------------- 1 | module GithubHook 2 | class MessageLogger 3 | attr_reader :messages, :wrapped_logger 4 | 5 | def initialize(wrapped_logger = nil) 6 | @messages = [] 7 | @wrapped_logger = wrapped_logger 8 | end 9 | 10 | def debug(message = yield) 11 | add_message(:debug, message) 12 | end 13 | 14 | def error(message = yield) 15 | add_message(:error, message) 16 | end 17 | 18 | def fatal(message = yield) 19 | add_message(:fatal, message) 20 | end 21 | 22 | def info(message = yield) 23 | add_message(:info, message) 24 | end 25 | 26 | def warn(message = yield) 27 | add_message(:warn, message) 28 | end 29 | 30 | private 31 | 32 | def add_message(level, message) 33 | if wrapped_logger 34 | wrapped_logger.send(level, message) 35 | end 36 | 37 | @messages << { 38 | :level => level.to_s, 39 | :message => message 40 | } 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /app/services/github_hook/null_logger.rb: -------------------------------------------------------------------------------- 1 | module GithubHook 2 | class NullLogger 3 | def debug(*_); end 4 | 5 | def info(*_); end 6 | 7 | def warn(*_); end 8 | 9 | def error(*_); end 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /app/services/github_hook/updater.rb: -------------------------------------------------------------------------------- 1 | module GithubHook 2 | class Updater 3 | GIT_BIN = Redmine::Configuration["scm_git_command"] || "git" 4 | 5 | attr_writer :logger 6 | 7 | def initialize(payload, params = {}) 8 | @payload = payload 9 | @params = params 10 | end 11 | 12 | def call 13 | repositories = find_repositories 14 | 15 | repositories.each do |repository| 16 | tg1 = Time.now 17 | # Fetch the changes from Github 18 | update_repository(repository) 19 | tg2 = Time.now 20 | 21 | tr1 = Time.now 22 | # Fetch the new changesets into Redmine 23 | repository.fetch_changesets 24 | tr2 = Time.now 25 | 26 | logger.info { " GithubHook: Redmine repository updated: #{repository.identifier} (Git: #{time_diff_milli(tg1, tg2)}ms, Redmine: #{time_diff_milli(tr1, tr2)}ms)" } 27 | end 28 | end 29 | 30 | private 31 | 32 | attr_reader :params, :payload 33 | 34 | # Executes shell command. Returns true if the shell command exits with a 35 | # success status code. 36 | # 37 | # If directory is given the current directory will be changed to that 38 | # directory before executing command. 39 | def exec(command, directory) 40 | logger.debug { " GithubHook: Executing command: '#{command}'" } 41 | 42 | # Get a path to a temp file 43 | logfile = Tempfile.new("github_hook_exec") 44 | logfile.close 45 | 46 | full_command = "#{command} > #{logfile.path} 2>&1" 47 | success = if directory.present? 48 | Dir.chdir(directory) do 49 | system(full_command) 50 | end 51 | else 52 | system(full_command) 53 | end 54 | 55 | output_from_command = File.readlines(logfile.path) 56 | if success 57 | logger.debug { " GithubHook: Command output: #{output_from_command.inspect}" } 58 | else 59 | logger.error { " GithubHook: Command '#{command}' didn't exit properly. Full output: #{output_from_command.inspect}" } 60 | end 61 | 62 | return success 63 | ensure 64 | logfile.unlink if logfile && logfile.respond_to?(:unlink) 65 | end 66 | 67 | # Finds the Redmine project in the database based on the given project 68 | # identifier 69 | def find_project 70 | identifier = get_identifier 71 | project = Project.find_by_identifier(identifier.downcase) 72 | fail( 73 | ActiveRecord::RecordNotFound, 74 | "No project found with identifier '#{identifier}'" 75 | ) if project.nil? 76 | project 77 | end 78 | 79 | # Returns the Redmine Repository object we are trying to update 80 | def find_repositories 81 | project = find_project 82 | repositories = git_repositories(project) 83 | 84 | # if a specific repository id is passed in url parameter "repository_id", 85 | # then try to find it in the list of current project repositories and use 86 | # only this and not all to pull changes from (issue #54) 87 | if params.key?(:repository_id) 88 | param_repo = repositories.select do |repo| 89 | repo.identifier == params[:repository_id] 90 | end 91 | 92 | if param_repo.nil? || param_repo.length == 0 93 | logger.info { 94 | "GithubHook: The repository '#{params[:repository_id]}' isn't " \ 95 | "in the list of projects repos. Updating all repos instead." 96 | } 97 | 98 | else 99 | repositories = param_repo 100 | end 101 | end 102 | 103 | repositories 104 | end 105 | 106 | # Gets the project identifier from the querystring parameters and if that's 107 | # not supplied, assume the Github repository name is the same as the project 108 | # identifier. 109 | def get_identifier 110 | identifier = get_project_name 111 | fail( 112 | ActiveRecord::RecordNotFound, 113 | "Project identifier not specified" 114 | ) if identifier.nil? 115 | identifier.to_s 116 | end 117 | 118 | # Attempts to find the project name. It first looks in the params, then in 119 | # the payload if params[:project_id] isn't given. 120 | def get_project_name 121 | project_id = params[:project_id] 122 | name_from_repository = payload.fetch("repository", {}).fetch("name", nil) 123 | project_id || name_from_repository 124 | end 125 | 126 | def git_command(command) 127 | GIT_BIN + " #{command}" 128 | end 129 | 130 | def git_repositories(project) 131 | repositories = project.repositories.select do |repo| 132 | repo.is_a?(Repository::Git) 133 | end 134 | if repositories.empty? 135 | fail( 136 | TypeError, 137 | "Project '#{project}' ('#{project.identifier}') has no repository" 138 | ) 139 | end 140 | repositories || [] 141 | end 142 | 143 | def logger 144 | @logger || NullLogger.new 145 | end 146 | 147 | def system(command) 148 | Kernel.system(command) 149 | end 150 | 151 | def time_diff_milli(start, finish) 152 | ((finish - start) * 1000.0).round(1) 153 | end 154 | 155 | # Fetches updates from the remote repository 156 | def update_repository(repository) 157 | command = git_command("fetch origin") 158 | fetch = exec(command, repository.url) 159 | return nil unless fetch 160 | 161 | command = git_command( 162 | "fetch --prune --prune-tags origin \"+refs/heads/*:refs/heads/*\"" 163 | ) 164 | exec(command, repository.url) 165 | end 166 | end 167 | end 168 | -------------------------------------------------------------------------------- /app/views/github_hook/welcome.html.erb: -------------------------------------------------------------------------------- 1 |
6 | This page is just a confirmation that you have successfully installed the plugin. 7 | Follow the rest of the installation: 8 | 9 | Redmine GitHub Hook Plugin on GitHub 10 | 11 |
12 | 13 |