Skip to content

Commit

Permalink
Merge pull request #27 from resuelve/feature/github_actions
Browse files Browse the repository at this point in the history
add support for GH actions
  • Loading branch information
alanraul authored Oct 31, 2024
2 parents 59f0395 + aa87996 commit 9ed49ab
Show file tree
Hide file tree
Showing 17 changed files with 260 additions and 133 deletions.
4 changes: 4 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["mix.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
28 changes: 28 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
name: CI

on:
pull_request:
branches: master

env:
PROJECTS_INFO: W3t9XQ==
GOOGLE_CREDENTIALS: W3t9XQ==

jobs:
build-and-test:
name: Build and test
runs-on: ubuntu-latest
container: elixir:1.14-alpine
steps:
- uses: actions/checkout@v2
- name: Install build dependencies
run: |
apk add --update bash openssl git
mix local.hex --force && mix local.rebar --force
- name: Install elixir dependencies
run: mix deps.get
- name: Run CI Tests
env:
MIX_ENV: test
run: mix ci
30 changes: 30 additions & 0 deletions .github/workflows/publish-hexpm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
name: Publish to hex.pm

on:
push:
tags:
- "*"

env:
PROJECTS_INFO: W3t9XQ==
GOOGLE_CREDENTIALS: W3t9XQ==

jobs:
publish-hexpm:
name: publish to hex.pm
runs-on: ubuntu-latest
container: elixir:1.14-alpine
steps:
- uses: actions/checkout@v2
- name: Install dependencies
run: |
apk add --update bash openssl git
mix local.hex --force && mix local.rebar --force
- name: Install elixir dependencies
run: mix deps.get
- name: publish to hex.pm
run: mix hex.publish --yes
env:
HEX_API_KEY: ${{ secrets.HEX_KEY_PUBLISH }}

4 changes: 2 additions & 2 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config
import Config

# This configuration is loaded before any dependency and is restricted
# to this project. If another project depends on this project, this
Expand Down Expand Up @@ -29,4 +29,4 @@ use Mix.Config
#
# import_config "#{Mix.env}.exs"

import_config "#{Mix.env}.exs"
import_config "#{Mix.env()}.exs"
6 changes: 3 additions & 3 deletions config/dev.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use Mix.Config
import Config

config :flowex, host: System.get_env("DIALOGFLOW_URL")
config :flowex, projects_info: System.get_env("PROJECTS_INFO") |> File.read!
config :flowex, projects_info: System.get_env("PROJECTS_INFO") |> Base.decode64!()

config :goth, json: System.get_env("GOOGLE_CREDENTIALS") |> File.read!
config :goth, json: System.get_env("GOOGLE_CREDENTIALS") |> Base.decode64!()
6 changes: 3 additions & 3 deletions config/prod.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use Mix.Config
import Config

config :flowex, host: "${DIALOGFLOW_URL}"
config :flowex, project_info: "${PROJECTS_INFO}"
config :flowex, project_info: "${PROJECTS_INFO}" |> Base.decode64!()

config :goth, json: "${GOOGLE_CREDENTIALS}" |> File.read!
config :goth, json: "${GOOGLE_CREDENTIALS}" |> Base.decode64!()
24 changes: 14 additions & 10 deletions config/test.exs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use Mix.Config
import Config

config :flowex, host: "api.dialogflow.com"
config :flowex, projects_info: "[\n{\n\"email\": \"client-access@lbot-8992.iam.gserviceaccount.com\"" <>
",\n\"id\":\"lbot-170198\"\n}\n]\n"

config :flowex,
projects_info:
"[\n{\n\"email\": \"client-access@lbot-8992.iam.gserviceaccount.com\"" <>
",\n\"id\":\"lbot-170198\"\n}\n]\n"

config :goth,
json: "[{\n \"type\": \"test_service_account\",\n \"project_id\": "<>
"\"test_project\",\n \"private_key_id\": \"0000\",\n \"private_key\""<>
": \"-----BEGIN PRIVATE KEY-----\\ntestPrivateKey\\n-----END PRIVATE KEY-----\\n\""<>
",\n \"client_email\": \"resuelve-bot-api@test_project.test.com\",\n "<>
" \"client_id\": \"0000\",\n \"auth_uri\": \"fakeurl\",\n \"token_uri\":"<>
" \"fakeurl\",\n \"auth_provider_x509_cert_url\": \"fakeurl\",\n "<>
"\"client_x509_cert_url\": \"fakeurl\"\n}]\n"
json:
"[{\n \"type\": \"test_service_account\",\n \"project_id\": " <>
"\"test_project\",\n \"private_key_id\": \"0000\",\n \"private_key\"" <>
": \"-----BEGIN PRIVATE KEY-----\\ntestPrivateKey\\n-----END PRIVATE KEY-----\\n\"" <>
",\n \"client_email\": \"resuelve-bot-api@test_project.test.com\",\n " <>
" \"client_id\": \"0000\",\n \"auth_uri\": \"fakeurl\",\n \"token_uri\":" <>
" \"fakeurl\",\n \"auth_provider_x509_cert_url\": \"fakeurl\",\n " <>
"\"client_x509_cert_url\": \"fakeurl\"\n}]\n"
9 changes: 6 additions & 3 deletions lib/flowex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ defmodule Flowex do
case HTTPoison.request(method, url, _body(body), _headers(email)) do
{:ok, %HTTPoison.Response{status_code: status, body: body}} when status in 200..299 ->
{:ok, Poison.decode!(body)}

{:ok, %HTTPoison.Response{status_code: status, body: body}} when status in 400..499 ->
{:error, Poison.decode!(body)}

{:ok, %HTTPoison.Response{status_code: status, body: body}} when status >= 500 ->
{:error, Poison.decode!(body)}

{:error, %HTTPoison.Error{reason: reason}} ->
{:error, Poison.decode!(reason)}
end
Expand All @@ -26,14 +29,14 @@ defmodule Flowex do
# ---------------------------------------------------------------------------
# Encode body
# ---------------------------------------------------------------------------
@spec _body(String.t() | map) :: String.t
@spec _body(String.t() | map) :: String.t()
defp _body(""), do: ""
defp _body(body), do: Poison.encode!(body)

# ---------------------------------------------------------------------------
# Obtine el host de Dialogflow API
# ---------------------------------------------------------------------------
@spec _host :: String.t
@spec _host :: String.t()
defp _host(), do: Application.get_env(:flowex, :host)

# ---------------------------------------------------------------------------
Expand All @@ -52,7 +55,7 @@ defmodule Flowex do
# ---------------------------------------------------------------------------
# Obtiene el id y email de un proyecto de dialogflow.
# ---------------------------------------------------------------------------
@spec _project_info(String.t) :: map
@spec _project_info(String.t()) :: map
defp _project_info(project_id) do
:flowex
|> Application.get_env(:projects_info)
Expand Down
2 changes: 1 addition & 1 deletion lib/flowex/service/agent.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Flowex.Service.Agent do
@doc """
Obtiene el agente al que está asociado el projecto.
"""
@spec get(Strint.t) :: tuple
@spec get(Strint.t()) :: tuple
def get(project) do
Flowex.request(project, :get, "", "")
end
Expand Down
41 changes: 26 additions & 15 deletions lib/flowex/service/intents.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ defmodule Flowex.Service.Intents do
@doc """
Lista todos los intents de un agente por pageToken.
"""
@spec list(String.t, String.t, String.t, String.t | nil, list) :: tuple
@spec list(String.t(), String.t(), String.t(), String.t() | nil, list) :: tuple
def list(project, language \\ "es", view \\ "INTENT_VIEW_UNSPECIFIED", token \\ nil, acc \\ []) do
case list_by_page(project, language, view, 100, token) do
{:ok, %{"intents" => intents, "nextPageToken" => nextPageToken}} ->
list(project, language, view, nextPageToken, acc ++ intents)
{:ok, %{"intents" => intents}} ->

{:ok, %{"intents" => intents}} ->
{:ok, acc ++ intents}

{:error, error} ->
{:error, error}
end
Expand All @@ -23,19 +25,25 @@ defmodule Flowex.Service.Intents do
@doc """
Lista los de intents de un agente por pageToken y definiendo tamaño de pagina.
"""
@spec list_by_page(String.t, String.t, String.t, integer, String.t | nil) :: tuple
def list_by_page(project, language \\ "es", view \\ "INTENT_VIEW_UNSPECIFIED", pageSize \\ 100, token \\ nil) do
@spec list_by_page(String.t(), String.t(), String.t(), integer, String.t() | nil) :: tuple
def list_by_page(
project,
language \\ "es",
view \\ "INTENT_VIEW_UNSPECIFIED",
pageSize \\ 100,
token \\ nil
) do
url =
"intents?languageCode=#{language}&intentView=#{view}&" <>
"pageSize=#{pageSize}&pageToken=#{token}"
"pageSize=#{pageSize}&pageToken=#{token}"

Flowex.request(project, :get, url, "")
end

@doc """
Obtiene un intent buscando por id.
"""
@spec get(String.t, String.t, String.t, String.t) :: tuple
@spec get(String.t(), String.t(), String.t(), String.t()) :: tuple
def get(project, id, language \\ "es", view \\ "INTENT_VIEW_UNSPECIFIED") do
url = "intents/#{id}?languageCode=#{language}&intentView=#{view}"

Expand All @@ -45,7 +53,7 @@ defmodule Flowex.Service.Intents do
@doc """
Crea un intent.
"""
@spec create(String.t, map, String.t) :: tuple
@spec create(String.t(), map, String.t()) :: tuple
def create(project, body, language \\ "es") do
url = "intents?languageCode=#{language}"

Expand All @@ -55,7 +63,7 @@ defmodule Flowex.Service.Intents do
@doc """
Añade un frase de entrenamiento a un intent.
"""
@spec add_training_phrase(String.t, String.t, String.t, String.t) :: tuple
@spec add_training_phrase(String.t(), String.t(), String.t(), String.t()) :: tuple
def add_training_phrase(project, id, text, language \\ "es") do
url = "intents/#{id}?languageCode=#{language}&intentView=INTENT_VIEW_FULL"

Expand All @@ -71,7 +79,7 @@ defmodule Flowex.Service.Intents do
@doc """
Actualiza un intent view full.
"""
@spec update(String.t, String.t, map, String.t) :: tuple
@spec update(String.t(), String.t(), map, String.t()) :: tuple
def update(project, id, intent, language \\ "es") do
url = "intents/#{id}?languageCode=#{language}&intentView=INTENT_VIEW_FULL"

Expand All @@ -81,14 +89,17 @@ defmodule Flowex.Service.Intents do
# ---------------------------------------------------------------------------
# Define una frase de entrenamiento.
# ---------------------------------------------------------------------------
@spec _set_training_phrase(nil | list, String.t) :: list
@spec _set_training_phrase(nil | list, String.t()) :: list
defp _set_training_phrase(nil, text) do
[%{
"name" => UUID.uuid4(),
"parts" => [%{"text" => text}],
"type" => "EXAMPLE"
}]
[
%{
"name" => UUID.uuid4(),
"parts" => [%{"text" => text}],
"type" => "EXAMPLE"
}
]
end

defp _set_training_phrase(training_phrases, text) do
[
%{
Expand Down
2 changes: 1 addition & 1 deletion lib/flowex/service/sessions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Flowex.Service.Sessions do
Procesa una consulta en lenguaje natural para detectar un intent con la respuesta
apropiada.
"""
@spec detect_intent(String.t, String.t, String.t, String.t) :: tuple
@spec detect_intent(String.t(), String.t(), String.t(), String.t()) :: tuple
def detect_intent(project, text, session_id, language \\ "es") do
body = %{
queryInput: %{
Expand Down
47 changes: 34 additions & 13 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,26 @@ defmodule Flowex.Mixfile do

def project do
[
app: :flowex,
version: "1.0.0",
elixir: "~> 1.5",
start_permanent: Mix.env == :prod,
deps: deps(),
test_coverage: [tool: ExCoveralls],
app: :flowex,
version: "2.0.0",
elixir: "~> 1.14",
package: package(),
start_permanent: Mix.env() == :prod,
deps: deps(),
aliases: aliases(),
test_coverage: [tool: ExCoveralls],
source_url: "https://github.com/resuelve/flowex",
docs: [
main: "Flowex"
]
]
end

defp package do
[
organization: "resuelve",
licenses: [],
links: %{"GitHub" => "https://github.com/resuelve/flowex"}
]
end

Expand All @@ -23,13 +37,20 @@ defmodule Flowex.Mixfile do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:credo, "~> 0.8", only: [:dev, :test]},
{:elixir_uuid, "~> 1.2" },
{:excoveralls, "~> 0.10", only: :test},
{:goth, "~> 0.9.0"},
{:httpoison, "~> 1.4"},
{:mock, "~> 0.3.0", only: :test},
{:poison, "~> 3.1"},
{:credo, "~> 1.6.7", only: [:dev, :test]},
{:elixir_uuid, "~> 1.2"},
{:excoveralls, "~> 0.12", only: :test},
{:goth, "~> 1.2.0"},
{:httpoison, "~> 1.8"},
{:mock, "~> 0.3.0", only: :test},
{:poison, "~> 3.1"},
{:ex_doc, "~> 0.27", only: :dev, runtime: false}
]
end

defp aliases do
[
ci: ["format --check-formatted"]
]
end
end
Loading

0 comments on commit 9ed49ab

Please sign in to comment.