Skip to content

Commit

Permalink
Moved graphql query into it's own module
Browse files Browse the repository at this point in the history
- References issue #4
  • Loading branch information
hex337 committed May 7, 2018
1 parent edac08b commit 4ab8218
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 53 deletions.
35 changes: 35 additions & 0 deletions lib/bendpr/github_graphql.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
defmodule Bendpr.GithubGraphql do
def open_prs_for_user(github_handle) do
query = """
query {
user(login: \"#{github_handle}\") {
pullRequests(first: 10, states: OPEN) {
edges {
node {
title
url
headRepository {
name
}
}
}
}
}
}
"""

send_query(query)
end

def send_query(query) do
github_token = Application.get_env(:bendpr, Bendpr.Slack)[:github]
url = "https://api.github.com/graphql"
headers = ["Authorization": "bearer #{github_token}", "Content-Type": "application/json"]

{_, body} = JSON.encode([query: query])
{:ok, response} = HTTPoison.post(url, body, headers)
{_, github_response} = JSON.decode(response.body)

github_response
end
end
58 changes: 7 additions & 51 deletions lib/bendpr/slack.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule Bendpr.SlackRtm do
use Slack

alias Bendpr.Parser
alias Bendpr.GithubGraphql

def handle_connect(slack, state) do
IO.puts "Connected as #{slack.me.name}."
Expand All @@ -10,62 +11,17 @@ defmodule Bendpr.SlackRtm do

def handle_event(message = %{type: "message"}, slack, state) do
IO.puts "There was a message."
IO.puts inspect(message, pretty: true)
user = message.user
text = message.text
token = Application.get_env(:bendpr, Bendpr.Slack)[:token]
slack_token = Application.get_env(:bendpr, Bendpr.Slack)[:token]

if text =~ "open prs" do
IO.puts "Someone is asking about open prs."

client = Tentacat.Client.new(%{access_token: Application.get_env(:bendpr, Bendpr.Slack)[:github]})
pr_attachments = Tentacat.Pulls.filter("1debit", "server-core", %{state: "open"}, client)
|> Parser.parse_prs
|> Enum.map(fn(pr) -> pr_to_attachment(pr) end)
|> JSX.encode!

IO.puts inspect(pr_attachments, pretty: true)

Slack.Web.Chat.post_message(message.channel, "Current open PR's:", %{
token: token,
attachments: [pr_attachments]
})
end

if text =~ "my prs" do
if text =~ "my prs" && Map.has_key?(message, :user) do
# There's no user key for bot messages
user = message.user
IO.puts "#{user} is asking about their prs."

github_handle = Bendpr.SlackUserToGithubHandle.map_user(user)
IO.puts github_handle

github_token = Application.get_env(:bendpr, Bendpr.Slack)[:github]
Neuron.Config.set(headers: ["Authorization": "bearer #{github_token}"])
Neuron.Config.set(url: "https://api.github.com/graphql")

query = """
query {
user(login: \"#{github_handle}\") {
pullRequests(first: 10, states: OPEN) {
edges {
node {
title
url
headRepository {
name
}
}
}
}
}
}
"""

url = "https://api.github.com/graphql"
headers = ["Authorization": "bearer #{github_token}", "Content-Type": "application/json"]
{_, body} = JSON.encode([query: query])
{:ok, response} = HTTPoison.post(url, body, headers)
github_response = GithubGraphql.open_prs_for_user(github_handle)

{_, github_response} = JSON.decode(response.body)
IO.puts inspect(github_response, pretty: true)

pr_attachments = github_response
Expand All @@ -74,7 +30,7 @@ query {
|> JSX.encode!

Slack.Web.Chat.post_message(message.channel, "Your open PR's:", %{
token: token,
token: slack_token,
attachments: [pr_attachments]
})
end
Expand Down
2 changes: 0 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ defmodule Bendpr.Mixfile do
{:gettext, "~> 0.11"},
{:cowboy, "~> 1.0"},
{:slack, "~> 0.12.0"},
{:tentacat, "~> 0.9"},
{:neuron, "~> 0.5.1"},
{:httpoison, "~> 1.0", override: true},
{:json, "~> 1.0"}
]
Expand Down

0 comments on commit 4ab8218

Please sign in to comment.