Skip to content

Commit

Permalink
[yegor256#537] Accept push only to master branch
Browse files Browse the repository at this point in the history
  • Loading branch information
pnatashap committed Jan 29, 2024
1 parent 4be5dfe commit bcac4ae
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 14 deletions.
16 changes: 10 additions & 6 deletions 0pdd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion objects/git_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -49,6 +50,7 @@ def initialize(
@id_rsa = options[:id_rsa] || ''
@master = master
@head_commit_hash = head_commit_hash
@target = target
end

def lock
Expand Down Expand Up @@ -85,6 +87,10 @@ def push
end
end

def change_in_master?
"refs/heads/#{master}".eql?(target)
end

private

def clone
Expand Down
2 changes: 2 additions & 0 deletions objects/vcs/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions objects/vcs/gitlab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
146 changes: 140 additions & 6 deletions test/test_0pdd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?)
Expand Down

0 comments on commit bcac4ae

Please sign in to comment.