Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create PullRequest object when payload is recieved
- Loading branch information
EJ Allen
committed
Dec 11, 2014
1 parent
f5bd0ff
commit e2ae16e
Showing
10 changed files
with
540 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
--color | ||
--warnings | ||
--require spec_helper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class GithubPayloadsController < ApplicationController | ||
def create | ||
if pr_is_new? | ||
PullRequest.create(pr_params) | ||
end | ||
|
||
render nothing: true | ||
end | ||
|
||
private | ||
|
||
def pr_is_new? | ||
parser.action == "opened" | ||
end | ||
|
||
def parser | ||
@_parser ||= PayloadParser.new(params[:payload]) | ||
end | ||
|
||
def pr_params | ||
parser.params | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class PullRequest < ActiveRecord::Base | ||
validates :github_issue_id, presence: true | ||
validates :github_url, presence: true | ||
validates :repo_name, presence: true | ||
validates :status, presence: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
class PayloadParser | ||
def initialize(payload) | ||
@payload = payload | ||
end | ||
|
||
def action | ||
payload["action"] | ||
end | ||
|
||
def params | ||
{ | ||
github_issue_id: github_issue_id, | ||
github_url: github_url, | ||
repo_name: repo_name, | ||
} | ||
end | ||
|
||
private | ||
|
||
def github_issue_id | ||
payload["pull_request"]["id"] | ||
end | ||
|
||
def github_url | ||
payload["pull_request"]["html_url"] | ||
end | ||
|
||
def repo_name | ||
payload["pull_request"]["head"]["repo"]["full_name"] | ||
end | ||
|
||
def payload | ||
@_payload ||= JSON.parse(@payload) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
Rails.application.routes.draw do | ||
resources :github_payloads, only: [:create] | ||
root to: "pull_requests#index" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class CreatePullRequests < ActiveRecord::Migration | ||
def change | ||
create_table :pull_requests do |t| | ||
t.timestamps null: false | ||
|
||
t.integer :github_issue_id, null: false | ||
t.string :github_url, null: false | ||
t.string :repo_name, null: false | ||
t.string :status, null: false, default: 'needs review', index: true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require "rails_helper" | ||
|
||
describe GithubPayloadsController do | ||
context "POST#create" do | ||
describe "when the action is 'opened'" do | ||
it "creates a new PullRequest" do | ||
send_pull_request_payload(action: "opened") | ||
expect(last_pull_request.status).to eq("needs review") | ||
end | ||
end | ||
|
||
describe "when the action is not 'opened'" do | ||
it "does not create a pull request" do | ||
send_pull_request_payload(action: "merged") | ||
expect(last_pull_request).to be_nil | ||
end | ||
end | ||
end | ||
|
||
def send_pull_request_payload(action:) | ||
post :create, payload: pull_request_payload(action: action) | ||
end | ||
|
||
def last_pull_request | ||
PullRequest.last | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
require "rails_helper" | ||
|
||
describe PullRequest do | ||
it { should validate_presence_of(:github_issue_id) } | ||
it { should validate_presence_of(:github_url) } | ||
it { should validate_presence_of(:repo_name) } | ||
it { should validate_presence_of(:status) } | ||
end |
Large diffs are not rendered by default.
Oops, something went wrong.