From bb8888c5ac54b8242c3d6baf94fdaf0ae55d30c1 Mon Sep 17 00:00:00 2001 From: Sergey Kirillov Date: Mon, 1 Aug 2022 17:13:11 +0400 Subject: [PATCH 1/2] Added login_by_github command --- lib/uffizzi/cli.rb | 8 ++++ lib/uffizzi/cli/login_by_github.rb | 68 +++++++++++++++++++++++++++ lib/uffizzi/clients/api/api_client.rb | 7 +++ lib/uffizzi/clients/api/api_routes.rb | 4 ++ 4 files changed, 87 insertions(+) create mode 100644 lib/uffizzi/cli/login_by_github.rb 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 From 4bc5950b352d54d524ba66fdc5018b8eeab924fe Mon Sep 17 00:00:00 2001 From: Sergey Kirillov Date: Tue, 2 Aug 2022 11:44:56 +0400 Subject: [PATCH 2/2] Updated docker-entry-point to autologin by github --- docker-entrypoint.sh | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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