Skip to content

Commit

Permalink
Merge branch 'development' into stage
Browse files Browse the repository at this point in the history
  • Loading branch information
syphax-bouazzouni committed Nov 30, 2023
2 parents 127d9cb + 14e9a2b commit caf12ec
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 79 deletions.
5 changes: 4 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
API_URL=http://localhost:9393
API_URL=http://localhost:9393
ONTOLOGIES_LINKED_DATA_PATH=
GOO_PATH=
SPARQL_CLIENT_PATH=
2 changes: 1 addition & 1 deletion .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
uses: docker/build-push-action@v4
with:
context: .
platforms: linux/amd64, linux/arm64
platforms: linux/amd64,linux/arm64
build-args: |
RUBY_VERSION=2.7.8
push: true
Expand Down
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ Goals:
```
### Configuration
```
cp .env.sample .env
```


### Run dev
```bash
Expand Down
197 changes: 135 additions & 62 deletions bin/ontoportal
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,61 @@

# Function to display script usage information
show_help() {
echo "Usage: $0 {dev|test|run|help} [--reset-cache] [--api-url API_URL] [--api-key API_KEY]"
echo " dev : Start the Ontoportal API development server."
echo " Example: $0 dev --api-url http://localhost:9393"
echo " Use --reset-cache to remove volumes: $0 dev --reset-cache"
echo " test : Run tests."
echo " run : Run a command in the Ontoportal API Docker container."
echo " help : Show this help message."
echo
echo "Description:"
echo " This script provides convenient commands for managing an Ontoportal API"
echo " application using Docker Compose. It includes options for starting the development server,"
echo " running tests, and executing commands within the Ontoportal API Docker container."
echo
echo "Goals:"
echo " - Simplify common tasks related to Ontoportal API development using Docker."
echo " - Provide a consistent and easy-to-use interface for common actions."
cat << EOL
Usage: $0 {dev|test|run|help} [--reset-cache] [--api-url API_URL] [--api-key API_KEY] [--old-path OLD_PATH] [--goo-path GOO_PATH] [--sparql-client-path SPARQL_CLIENT_PATH]
dev : Start the Ontoportal API development server.
Example: $0 dev --api-url http://localhost:9393
Use --reset-cache to remove volumes: $0 dev --reset-cache
test : Run tests. Specify either a test file or use 'all'.
Example: $0 test test/controllers/test_users_controller.rb -v --name=name_of_the_test
Example (run all tests): $0 test all -v
run : Run a command in the Ontoportal API Docker container.
help : Show this help message.
Description:
This script provides convenient commands for managing an Ontoportal API
application using Docker Compose. It includes options for starting the development server,
running tests, and executing commands within the Ontoportal API Docker container.
Options:
--reset-cache : Remove Docker volumes (used with 'dev').
--api-url API_URL : Specify the API URL.
--api-key API_KEY : Specify the API key.
--old-path OLD_PATH : Specify the path for ontologies_linked_data.
--goo-path GOO_PATH : Specify the path for goo.
--sparql-client-path : Specify the path for sparql-client.
test_file | all : Specify either a test file or all the tests will be run.
-v : Enable verbosity.
--name=name_of_the_test : Specify the name of the test.
Goals:
- Simplify common tasks related to Ontoportal API development using Docker.
- Provide a consistent and easy-to-use interface for common actions.
EOL
}


# Function to update or create the .env file with API_URL and API_KEY
update_env_file() {
# Update the .env file with the provided values
local api_url="$1"
local old_path="$2"
local goo_path="$3"
local sparql_client_path="$4"

# Update the .env file with the provided values
file_content=$(<.env)

# Make changes to the variable
while IFS= read -r line; do
if [[ "$line" == "API_URL="* ]]; then
if [[ "$line" == "API_URL="* && -n "$api_url" ]]; then
echo "API_URL=$api_url"
elif [[ "$line" == "ONTOLOGIES_LINKED_DATA_PATH="* ]]; then
echo "ONTOLOGIES_LINKED_DATA_PATH=$old_path"
elif [[ "$line" == "GOO_PATH="* ]]; then
echo "GOO_PATH=$goo_path"
elif [[ "$line" == "SPARQL_CLIENT_PATH="* ]]; then
echo "SPARQL_CLIENT_PATH=$sparql_client_path"
else
echo "$line"
fi
Expand All @@ -38,26 +65,52 @@ update_env_file() {

# Function to create configuration files if they don't exist
create_config_files() {
if [ ! -f ".env" ]; then
echo "Creating .env file from env.sample"
cp .env.sample .env
fi
[ -f ".env" ] || cp .env.sample .env
[ -f "config/environments/development.rb" ] || cp config/environments/config.rb.sample config/environments/development.rb
}

if [ ! -f "config/environments/development.rb" ]; then
echo "Creating config/environments/development.rb file from config/environments/config.rb.sample"
cp config/bioportal_config_env.rb.sample config/bioportal_config_development.rb
fi
# Function to build Docker run command with conditionally added bind mounts
build_docker_run_cmd() {
local custom_command="$1"
local old_path="$2"
local goo_path="$3"
local sparql_client_path="$4"

local docker_run_cmd="docker compose run --rm -it"
local bash_cmd=""

# Conditionally add bind mounts only if the paths are not empty
for path_var in "old_path:ontologies_linked_data" "goo_path:goo" "sparql_client_path:sparql-client"; do
IFS=':' read -r path value <<< "$path_var"

if [ -n "${!path}" ]; then
host_path="$(realpath "$(dirname "${!path}")")/$value"
echo "Run: bundle config local.$value ${!path}"
container_path="/srv/ontoportal/$value"
docker_run_cmd+=" -v $host_path:$container_path"
bash_cmd+="(git config --global --add safe.directory $container_path && bundle config local.$value $container_path) &&"
else
bash_cmd+=" (bundle config unset local.$value) &&"
fi
done

bash_cmd+=" (bundle check || bundle install || bundle update) && $custom_command"
docker_run_cmd+=" --service-ports api bash -c \"$bash_cmd\""

eval "$docker_run_cmd"
}

# Function to handle the "dev" option
dev() {
echo "Starting Ontoportal API development server..."

create_config_files
# Function to handle the "dev" and "test" options
run_command() {
local custom_command="$1"

local reset_cache=false
local api_url=""
local old_path=""
local goo_path=""
local sparql_client_path=""


shift
# Check for command line arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
Expand All @@ -69,6 +122,18 @@ dev() {
api_url="$2"
shift 2
;;
--old-path)
old_path="$2"
shift 2
;;
--goo-path)
goo_path="$2"
shift 2
;;
--sparql-client-path)
sparql_client_path="$2"
shift 2
;;
*)
echo "Unknown option: $1"
show_help
Expand All @@ -77,48 +142,58 @@ dev() {
esac
done


# Check if --reset-cache is present and execute docker compose down --volumes
if [ "$reset_cache" = true ]; then
echo "Resetting cache. Running: docker compose down --volumes"
docker compose down --volumes
fi

# Check if arguments are provided
if [ -n "$api_url" ] ; then
# If arguments are provided, update the .env file
update_env_file "$api_url"
else
# If no arguments, fetch values from the .env file
source .env
api_url="$API_URL"
fi
update_env_file "$api_url" "$old_path" "$goo_path" "$sparql_client_path"



# If no arguments, fetch values from the .env file
source .env
api_url="$API_URL"
old_path="$ONTOLOGIES_LINKED_DATA_PATH"
goo_path="$GOO_PATH"
sparql_client_path="$SPARQL_CLIENT_PATH"


if [ -z "$api_url" ] ; then
echo "Error: Missing required arguments. Please provide both --api-url or update them in your .env"
exit 1
fi

# Check if --reset-cache is present and execute docker compose down --volumes
if [ "$reset_cache" = true ]; then
echo "Resetting cache. Running: docker compose down --volumes"
docker compose down --volumes
fi

echo "Run: bundle exec api s -b 0.0.0.0 -p 3000"
docker compose run --rm -it --service-ports api bash -c "(bundle check || bundle install) && bundle exec rackup -o 0.0.0.0 --port 9393"

# Build the Docker run command
echo "Run: $custom_command"
build_docker_run_cmd "$custom_command" "$old_path" "$goo_path" "$sparql_client_path"
}

# Function to handle the "test" option
test() {
# Function to handle the "dev" option
dev() {
echo "Starting OntoPortal API development server..."

local custom_command="bundle exec shotgun --host 0.0.0.0 --env=development"
run_command "$custom_command" "$@"
}

local api_url=""
# Function to handle the "test" option
test() {
echo "Running tests..."
local test_path=""
local test_options=""

local all_arguments=()
# Check for command line arguments
while [ "$#" -gt 0 ]; do
case "$1" in
--api-url)
shift
api_url="$1"
;;
--api-url | --reset-cache | --old-path | --goo-path | --sparql-client-path)
all_arguments+=("$1" "$2")
shift 2
;;
*)
if [ -z "$test_path" ]; then
test_path="$1"
Expand All @@ -130,13 +205,9 @@ test() {
shift
done



script="API_URL=$api_url bundle exec rake test TEST=\"$test_path\" TESTOPTS=\"$test_options\""
echo "Running tests..."
echo "Run: $script"

docker compose run --rm -it api bash -c "(bundle check || bundle install) && $script"
local custom_command="bundle exec rake test TEST='$test_path' TESTOPTS='$test_options'"
echo "run : $custom_command"
run_command "$custom_command" "${all_arguments[@]}"
}

# Function to handle the "run" option
Expand All @@ -145,6 +216,8 @@ run() {
docker compose run --rm -it api bash -c "$*"
}

create_config_files

# Main script logic
case "$1" in
"run")
Expand Down
5 changes: 4 additions & 1 deletion helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ def populate_from_params(obj, params)
value = retrieved_values
elsif attribute_settings && attribute_settings[:enforce] && attribute_settings[:enforce].include?(:date_time)
# TODO: Remove this awful hack when obj.class.model_settings[:range][attribute] contains DateTime class
value = DateTime.parse(value)
is_array = value.is_a?(Array)
value = Array(value).map{ |v| DateTime.parse(v) }
value = value.first unless is_array
value
elsif attribute_settings && attribute_settings[:enforce] && attribute_settings[:enforce].include?(:uri) && attribute_settings[:enforce].include?(:list)
# in case its a list of URI, convert all value to IRI
value = value.map { |v| RDF::IRI.new(v) }
Expand Down
27 changes: 17 additions & 10 deletions helpers/request_params_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,24 @@ def add_inverse_filters(inverse_filters, query)
end

def add_acronym_name_filters(query)
if params[:acronym]
filter = Goo::Filter.new(extract_attr(:ontology_acronym)).regex(params[:acronym])
if params[:name]
filter.or(Goo::Filter.new(extract_attr(:ontology_name)).regex(params[:name]))
end
query = query.filter(filter)
elsif params[:name]
filter = Goo::Filter.new(extract_attr(:ontology_name)).regex(params[:name])
query = query.filter(filter)
filters = {
acronym: :ontology_acronym,
name: :ontology_name,
description: :description
}.map do |key, attr|
(params[key].nil? || params[key].empty?) ? nil : [extract_attr(attr), params[key]]
end.compact

return query if filters.empty?

key, val = filters.first
filter = Goo::Filter.new(key).regex(val)

filters.drop(1).each do |k, v|
filter = filter.or(Goo::Filter.new(k).regex(v))
end
query

query.filter(filter)
end

def add_order_by_patterns(query)
Expand Down
5 changes: 5 additions & 0 deletions test/controllers/test_ontology_submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ def test_submissions_pagination_filter
ontologies.first.save
sub = ontologies.first.latest_submission(status: :any).bring_remaining
sub.status = 'retired'
sub.description = "234"
sub.creationDate = DateTime.yesterday.to_datetime
sub.hasOntologyLanguage = LinkedData::Models::OntologyFormat.find('SKOS').first
sub.save
Expand Down Expand Up @@ -363,6 +364,10 @@ def test_submissions_pagination_filter
submissions = MultiJson.load(last_response.body)
refute_empty submissions["collection"]
assert_equal ontologies.size - 1 , submissions["collection"].size
get "/submissions?page=1&pagesize=100&description=234&acronym=234&name=234"
assert last_response.ok?
submissions = MultiJson.load(last_response.body)
assert_equal 1 , submissions["collection"].size
end

def test_submissions_default_includes
Expand Down

0 comments on commit caf12ec

Please sign in to comment.