diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index d68bb394..aa7dff32 100644 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -13,8 +13,16 @@ then uffizzi config set project "${UFFIZZI_PROJECT}" fi else - echo "Specify environment variables to login before executing Uffizzi CLI." - echo "UFFIZZI_USER, UFFIZZI_SERVER, UFFIZZI_PASSWORD, and optionally UFFIZZI_PROJECT" + if [ $GITHUB_ACTOR ] && + [ $GITHUB_GITHUB_TOKEN ] + then + echo "New params. GITHUB_ACTOR = ${GITHUB_ACTOR}. GITHUB_GITHUB_TOKEN = ${GITHUB_GITHUB_TOKEN}" + uffizzi login_by_github --username "${GITHUB_ACTOR}" --server "${UFFIZZI_SERVER}" + uffizzi config set project github + else + echo "Specify environment variables to login before executing Uffizzi CLI." + echo "UFFIZZI_USER, UFFIZZI_SERVER, UFFIZZI_PASSWORD, and optionally UFFIZZI_PROJECT" + fi fi if diff --git a/lib/uffizzi/cli.rb b/lib/uffizzi/cli.rb index 9324d2d4..d9a3319b 100644 --- a/lib/uffizzi/cli.rb +++ b/lib/uffizzi/cli.rb @@ -24,6 +24,14 @@ def login Login.new(options).run end + desc 'login_by_github [OPTIONS]', 'Login or register to Uffizzi to view and manage your previews' + method_option :server, required: false, aliases: '-s' + method_option :username, required: false, aliases: '-u' + def login_by_github + require_relative 'cli/login_by_github' + LoginByGithub.new(options).run + end + desc 'logout', 'Log out of a Uffizzi user account' def logout require_relative 'cli/logout' diff --git a/lib/uffizzi/cli/login_by_github.rb b/lib/uffizzi/cli/login_by_github.rb new file mode 100644 index 00000000..a5223d44 --- /dev/null +++ b/lib/uffizzi/cli/login_by_github.rb @@ -0,0 +1,68 @@ +# frozen_string_literal: true + +require 'uffizzi' +require 'uffizzi/response_helper' +require 'uffizzi/clients/api/api_client' + +module Uffizzi + class Cli::LoginByGithub + include ApiClient + + def initialize(options) + @options = options + end + + def run + server = set_server + username = set_username + token = set_token + params = prepare_request_params(username, token) + response = create_github_session(server, params) + + if ResponseHelper.created?(response) + handle_succeed_response(response, server, username) + else + ResponseHelper.handle_failed_response(response) + end + end + + private + + def set_server + config_server = ConfigFile.exists? && ConfigFile.option_has_value?(:server) ? ConfigFile.read_option(:server) : nil + @options[:server] || config_server + end + + def set_username + config_username = ConfigFile.exists? && ConfigFile.option_has_value?(:username) ? ConfigFile.read_option(:username) : nil + @options[:username] || config_username + end + + def set_token + ENV['GITHUB_TOKEN'] + end + + def prepare_request_params(username, token) + { + user: { + username: username, + token: token, + }, + } + end + + def handle_succeed_response(response, server, username) + account = response[:body][:user][:accounts].first + return Uffizzi.ui.say('No account related to this email') unless account_valid?(account) + + ConfigFile.write_option(:server, server) + ConfigFile.write_option(:username, username) + ConfigFile.write_option(:cookie, response[:headers]) + ConfigFile.write_option(:account_id, account[:id]) + end + + def account_valid?(account) + account[:state] == 'active' + end + end +end diff --git a/lib/uffizzi/clients/api/api_client.rb b/lib/uffizzi/clients/api/api_client.rb index f7edb204..48682ecb 100644 --- a/lib/uffizzi/clients/api/api_client.rb +++ b/lib/uffizzi/clients/api/api_client.rb @@ -13,6 +13,13 @@ def create_session(server, params = {}) build_response(response) end + def create_github_session(server, params = {}) + uri = github_session_uri(server) + response = http_client.make_post_request(uri, params) + + build_response(response) + end + def destroy_session(server) uri = session_uri(server) response = http_client.make_delete_request(uri) diff --git a/lib/uffizzi/clients/api/api_routes.rb b/lib/uffizzi/clients/api/api_routes.rb index ab137cfb..c55e5aeb 100644 --- a/lib/uffizzi/clients/api/api_routes.rb +++ b/lib/uffizzi/clients/api/api_routes.rb @@ -28,6 +28,10 @@ def session_uri(server) "#{server}/api/cli/v1/session" end + def github_session_uri(server) + "#{server}/api/cli/v1/github/session" + end + def validate_compose_file_uri(server, project_slug) "#{compose_files_uri(server, project_slug)}/validate" end