diff --git a/0pdd.rb b/0pdd.rb index 613e040..cf28c49 100644 --- a/0pdd.rb +++ b/0pdd.rb @@ -364,10 +364,12 @@ github = GithubRepo.new(settings.github, json, settings.config) return [400, "No access to #{github.repo.name}"] unless github.exists? unless ENV['RACK_ENV'] == 'test' - process_request(github) - puts "GitHub hook from #{github.repo.name}" + process_request(github) if github.repo.change_in_master? + puts "GitHub hook from #{github.repo.name} to branch #{github.repo.target}" end - "Thanks #{github.repo.name}" + ignore = github.repo.change_in_master? ? + "Push is not to master branch, nothing is done. " : '' + "#{ignore}Thanks #{github.repo.name}" end get '/hook/gitlab' do @@ -398,10 +400,12 @@ gitlab = GitlabRepo.new(settings.gitlab, json, settings.config) return [400, "No access to #{gitlab.repo.name}"] unless gitlab.exists? unless ENV['RACK_ENV'] == 'test' - process_request(gitlab) - puts "Gitlab hook from #{gitlab.repo.name}" + process_request(gitlab) if gitlab.repo.change_in_master? + puts "Gitlab hook from #{gitlab.repo.name} to branch #{gitlab.repo.target}" end - "Thanks #{gitlab.repo.name}" + ignore = github.repo.change_in_master? ? + "Push is not to master branch, nothing is done. " : '' + "#{ignore}Thanks #{gitlab.repo.name}" end get '/css/*.css' do diff --git a/README.md b/README.md index 9797aa3..277f699 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ to your repository, if it's private Then, add a `@todo` [puzzle](https://www.yegor256.com/2009/03/04/pdd.html) to the source code (format it [right](https://github.com/teamed/pdd)). -Then, `git push` something and see what happens. You should see a new +Then, `git push` to master branch something and see what happens. You should see a new issue created in your repository by [@0pdd](https://github.com/0pdd). The dependency tree of all puzzles in your repository you can find diff --git a/objects/git_repo.rb b/objects/git_repo.rb index cc5fd76..ae96eed 100644 --- a/objects/git_repo.rb +++ b/objects/git_repo.rb @@ -32,11 +32,12 @@ # Repository in Git # class GitRepo - attr_reader :uri, :name, :path, :master, :head_commit_hash + attr_reader :uri, :name, :path, :master, :head_commit_hash, :target def initialize( uri:, name:, + target: 'master', master: 'master', head_commit_hash: '', **options @@ -49,6 +50,7 @@ def initialize( @id_rsa = options[:id_rsa] || '' @master = master @head_commit_hash = head_commit_hash + @target = target end def lock @@ -85,6 +87,10 @@ def push end end + def change_in_master? + "refs/heads/#{master}".eql?(target) + end + private def clone diff --git a/objects/vcs/github.rb b/objects/vcs/github.rb index 43a1e09..b8309bd 100644 --- a/objects/vcs/github.rb +++ b/objects/vcs/github.rb @@ -140,12 +140,14 @@ def issue_link(issue_id) def git_repo(json, config) uri = json['repository']['ssh_url'] || json['repository']['url'] + target = json['ref'] name = json['repository']['full_name'] default_branch = json['repository']['master_branch'] head_commit_hash = json['head_commit'] ? json['head_commit']['id'] : '' GitRepo.new( uri: uri, name: name, + target: target, id_rsa: config['id_rsa'], master: default_branch, head_commit_hash: head_commit_hash diff --git a/objects/vcs/gitlab.rb b/objects/vcs/gitlab.rb index 5b26d7c..779c972 100644 --- a/objects/vcs/gitlab.rb +++ b/objects/vcs/gitlab.rb @@ -174,11 +174,13 @@ def issue_link(issue_id) def git_repo(json, config) uri = json['project']['url'] name = json['project']['path_with_namespace'] + target = json['ref'] default_branch = json['project']['default_branch'] head_commit_hash = json['checkout_sha'] GitRepo.new( uri: uri, name: name, + target: target, id_rsa: config['id_rsa'], master: default_branch, head_commit_hash: head_commit_hash diff --git a/test/test_0pdd.rb b/test/test_0pdd.rb index 054f870..0f08a27 100644 --- a/test/test_0pdd.rb +++ b/test/test_0pdd.rb @@ -101,18 +101,152 @@ def test_it_understands_push_from_github } post( '/hook/github', - [ - '{"head_commit":{"id":"-"},', - '"repository":{"url":"localhost",', - '"full_name":"yegor256-one/com.github.0pdd-test"},', - '"ref":"refs/heads/master"}' - ].join, + %w[{"head_commit":{"id":"-"}, + "repository":{"url":"localhost", + "full_name":"yegor256-one/com.github.0pdd-test"}, + "ref":"refs/heads/master"}].join, headers ) assert(last_response.ok?) assert(last_response.body.include?('Thanks')) end + def test_it_ignores_push_from_github_to_not_master + headers = { + 'CONTENT_TYPE' => 'application/json', + 'HTTP_USER_AGENT' => 'GitHub-Hookshot', + 'HTTP_X_GITHUB_EVENT' => 'push' + } + post( + '/hook/github', + %w[{"head_commit":{"id":"-"}, + "repository":{"url":"localhost", + "full_name":"yegor256-one/com.github.0pdd-test"}, + "ref":"refs/heads/main"}].join, + headers + ) + assert(last_response.ok?) + assert(last_response.body.include?('Thanks')) + assert(last_response.body.include?('nothing is done')) + end + + def test_it_accepts_push_from_github_to_not_default_master + headers = { + 'CONTENT_TYPE' => 'application/json', + 'HTTP_USER_AGENT' => 'GitHub-Hookshot', + 'HTTP_X_GITHUB_EVENT' => 'push' + } + post( + '/hook/github', + %w[{"head_commit":{"id":"-"}, + "repository":{"url":"localhost", + "master_branch":"main", + "full_name":"yegor256-one/com.github.0pdd-test"}, + "ref":"refs/heads/main"}].join, + headers + ) + assert(last_response.ok?) + assert(last_response.body.include?('Thanks')) + assert(!last_response.body.include?('nothing is done')) + end + + def test_it_ignore_push_from_github_to_not_default_master + headers = { + 'CONTENT_TYPE' => 'application/json', + 'HTTP_USER_AGENT' => 'GitHub-Hookshot', + 'HTTP_X_GITHUB_EVENT' => 'push' + } + post( + '/hook/github', + %w[{"head_commit":{"id":"-"}, + "repository":{"url":"localhost", + "master_branch":"main", + "full_name":"yegor256-one/com.github.0pdd-test"}, + "ref":"refs/heads/master"}].join, + headers + ) + assert(last_response.ok?) + assert(last_response.body.include?('Thanks')) + assert(last_response.body.include?('nothing is done')) + end + + def test_it_understands_push_from_gitlab + headers = { + 'CONTENT_TYPE' => 'application/json', + 'X-Gitlab-Event' => 'Push Hook' + } + post( + '/hook/github', + %w[{"head_commit":{"id":"-"}, + "checkout_sha": "da156088", + "project":{"url":"localhost", + "path_with_namespace":"yegor256-one/com.github.0pdd-test"}, + "ref":"refs/heads/master"}].join, + headers + ) + assert(last_response.ok?) + assert(last_response.body.include?('Thanks')) + end + + def test_it_ignores_push_from_gitlab_to_not_master + headers = { + 'CONTENT_TYPE' => 'application/json', + 'X-Gitlab-Event' => 'Push Hook' + } + post( + '/hook/github', + %w[{"head_commit":{"id":"-"}, + "checkout_sha": "da156088", + "repository":{"url":"localhost", + "path_with_namespace":"yegor256-one/com.github.0pdd-test"}, + "ref":"refs/heads/main"}].join, + headers + ) + assert(last_response.ok?) + assert(last_response.body.include?('Thanks')) + assert(last_response.body.include?('nothing is done')) + end + + def test_it_accepts_push_from_gitlab_to_not_default_master + headers = { + 'CONTENT_TYPE' => 'application/json', + 'X-Gitlab-Event' => 'Push Hook' + } + post( + '/hook/github', + %w[{"head_commit":{"id":"-"}, + "checkout_sha": "da156088", + "repository":{"url":"localhost", + "default_branch":"main", + "path_with_namespace":"yegor256-one/com.github.0pdd-test"}, + "ref":"refs/heads/main"}].join, + headers + ) + assert(last_response.ok?) + assert(last_response.body.include?('Thanks')) + assert(!last_response.body.include?('nothing is done')) + end + + def test_it_ignores_push_from_gitlab_to_not_default_master + headers = { + 'CONTENT_TYPE' => 'application/json', + 'X-Gitlab-Event' => 'Push Hook' + } + post( + '/hook/github', + %w[{"head_commit":{"id":"-"}, + "checkout_sha": "da156088", + "repository":{"url":"localhost", + "default_branch":"main", + "path_with_namespace":"yegor256-one/com.github.0pdd-test"}, + "ref":"refs/heads/master"}].join, + headers + ) + assert(last_response.ok?) + assert(last_response.body.include?('Thanks')) + assert(last_response.body.include?('nothing is done')) + end + def test_renders_html_puzzles get('/p?name=yegor256/pdd') assert(last_response.ok?)