소스 검색

Changelog & html email

Maniack Crudelis 7 년 전
부모
커밋
2229f78173
6개의 변경된 파일170개의 추가작업 그리고 18개의 파일을 삭제
  1. 11 0
      config_panel.json
  2. 120 5
      scripts/_common.sh
  3. 11 0
      scripts/config
  4. 9 4
      scripts/install
  5. 5 5
      scripts/restore
  6. 14 4
      scripts/upgrade

+ 11 - 0
config_panel.json

@@ -35,6 +35,17 @@
 				"type": "bool",
 				"default": true
 			}]
+		},
+		{
+			"name": "Global configuration",
+			"id": "global_config",
+			"options": [{
+				"name": "Send HTML email to admin ?",
+				"help": "Allow app scripts to send HTML mails instead of plain text.",
+				"id": "email_type",
+				"type": "bool",
+				"default": true
+			}]
 		}]
 	}
 ]

+ 120 - 5
scripts/_common.sh

@@ -710,7 +710,7 @@ ynh_script_progression () {
 # Send an email to inform the administrator
 #
 # usage: ynh_send_readme_to_admin --app_message=app_message [--recipients=recipients] [--type=type]
-# | arg: -m --app_message= - The message to send to the administrator.
+# | arg: -m --app_message= - The file with the content to send to the administrator.
 # | arg: -r, --recipients= - The recipients of this email. Use spaces to separate multiples recipients. - default: root
 #	example: "root admin@domain"
 #	If you give the name of a YunoHost user, ynh_send_readme_to_admin will find its email adress for you
@@ -723,11 +723,16 @@ ynh_send_readme_to_admin() {
 	local recipients
 	local type
 	# Manage arguments with getopts
+
 	ynh_handle_getopts_args "$@"
-	app_message="${app_message:-...No specific information...}"
+	app_message="${app_message:-}"
 	recipients="${recipients:-root}"
 	type="${type:-install}"
 
+	# Get the value of admin_mail_html
+	admin_mail_html=$(ynh_app_setting_get $app admin_mail_html)
+	admin_mail_html="${admin_mail_html:-0}"
+
 	# Retrieve the email of users
 	find_mails () {
 		local list_mails="$1"
@@ -774,12 +779,53 @@ ynh_send_readme_to_admin() {
 
 Specific information for the application $app.
 
-$app_message
+$(if [ -n "$app_message" ]
+then
+	cat "$app_message"
+else
+	echo "...No specific information..."
+fi)
 
 ---
 Automatic diagnosis data from YunoHost
 
-$(yunohost tools diagnosis | grep -B 100 "services:" | sed '/services:/d')"
+__PRE_TAG1__$(yunohost tools diagnosis | grep -B 100 "services:" | sed '/services:/d')__PRE_TAG2__"
+
+	# Store the message into a file for further modifications.
+	echo "$mail_message" > mail_to_send
+
+	# If a html email is required. Apply html tags to the message.
+ 	if [ "$admin_mail_html" -eq 1 ]
+ 	then
+		# Insert 'br' tags at each ending of lines.
+		ynh_replace_string "$" "<br>" mail_to_send
+
+		# Insert starting HTML tags
+		sed --in-place '1s@^@<!DOCTYPE html>\n<html>\n<head></head>\n<body>\n@' mail_to_send
+
+		# Keep tabulations
+		ynh_replace_string "  " "\&#160;\&#160;" mail_to_send
+		ynh_replace_string "\t" "\&#160;\&#160;" mail_to_send
+
+		# Insert url links tags
+		ynh_replace_string "__URL_TAG1__\(.*\)__URL_TAG2__\(.*\)__URL_TAG3__" "<a href=\"\2\">\1</a>" mail_to_send
+
+		# Insert pre tags
+		ynh_replace_string "__PRE_TAG1__" "<pre>" mail_to_send
+		ynh_replace_string "__PRE_TAG2__" "<\pre>" mail_to_send
+
+		# Insert finishing HTML tags
+		echo -e "\n</body>\n</html>" >> mail_to_send
+
+	# Otherwise, remove tags to keep a plain text.
+	else
+		# Remove URL tags
+		ynh_replace_string "__URL_TAG[1,3]__" "" mail_to_send
+		ynh_replace_string "__URL_TAG2__" ": " mail_to_send
+
+		# Remove PRE tags
+		ynh_replace_string "__PRE_TAG[1-2]__" "" mail_to_send
+	fi
 
 	# Define binary to use for mail command
 	if [ -e /usr/bin/bsd-mailx ]
@@ -789,8 +835,15 @@ $(yunohost tools diagnosis | grep -B 100 "services:" | sed '/services:/d')"
 		local mail_bin=/usr/bin/mail.mailutils
 	fi
 
+	if [ "$admin_mail_html" -eq 1 ]
+	then
+		content_type="text/html"
+	else
+		content_type="text/plain"
+	fi
+
 	# Send the email to the recipients
-	echo "$mail_message" | $mail_bin -a "Content-Type: text/plain; charset=UTF-8" -s "$mail_subject" "$recipients"
+	cat mail_to_send | $mail_bin -a "Content-Type: $content_type; charset=UTF-8" -s "$mail_subject" "$recipients"
 }
 
 #=================================================
@@ -961,3 +1014,65 @@ ynh_download_file () {
 	# Move the file to its destination
 	mv $filename $dest_dir
 }
+
+#=================================================
+
+# Create a changelog for an app after an upgrade.
+#
+# The changelog is printed into the file ./changelog for the time of the upgrade.
+#
+# In order to create a changelog, ynh_app_changelog will get info from /etc/yunohost/apps/$app/status.json
+# In order to find the current commit use by the app.
+# The remote repository, and the branch.
+# The changelog will be only the commits since the current revision.
+#
+# Because of the need of those info, ynh_app_changelog works only
+# with apps that have been installed from a list.
+#
+# usage: ynh_app_changelog
+ynh_app_changelog () {
+	get_value_from_settings ()
+	{
+		local value="$1"
+		# Extract a value from the status.json file of an installed app.
+
+		grep "$value\": \"" /etc/yunohost/apps/$app/status.json | sed "s/.*$value\": \"\([^\"]*\).*/\1/"
+	}
+
+	local current_revision="$(get_value_from_settings revision)"
+	local repo="$(get_value_from_settings url)"
+	local branch="$(get_value_from_settings branch)"
+	# ynh_app_changelog works only with an app installed from a list.
+	if [ -z "$current_revision" ] || [ -z "$repo" ] || [ -z "$branch" ]
+	then
+		ynh_print_warn "Unable to build the changelog..."
+		touch changelog
+		return 0
+	fi
+
+	# Fetch the history of the repository, without cloning it
+	mkdir git_history
+	(cd git_history
+	ynh_exec_warn_less git init
+	ynh_exec_warn_less git remote add -f origin $repo
+	# Get the line of the current commit of the installed app in the history.
+	local line_to_head=$(git log origin/$branch --pretty=oneline | grep --line-number "$current_revision" | cut -d':' -f1)
+	# Cut the history before the current commit, to keep only newer commits.
+	# Then use sed to reorganise each lines and have a nice list of commits since the last upgrade.
+	# This list is redirected into the file changelog
+	git log origin/$branch --pretty=oneline | head --lines=$(($line_to_head-1)) | sed 's/^\([[:alnum:]]*\)\(.*\)/*(\1) -> \2/g' > ../changelog)
+	# Remove 'Merge pull request' commits
+	sed -i '/Merge pull request #[[:digit:]]* from/d' changelog
+	# As well as conflict resolving commits
+	sed -i '/Merge branch .* into/d' changelog
+
+	# Get the value of admin_mail_html
+	admin_mail_html=$(ynh_app_setting_get $app admin_mail_html)
+	admin_mail_html="${admin_mail_html:-0}"
+
+	# If a html email is required. Apply html to the changelog.
+ 	if [ "$admin_mail_html" -eq 1 ]
+ 	then
+		sed -in-place "s@\*(\([[:alnum:]]*\)) -> \(.*\)@* __URL_TAG1__\2__URL_TAG2__${repo}/commit/\1__URL_TAG3__@g" changelog
+ 	fi
+}

+ 11 - 0
scripts/config

@@ -46,6 +46,11 @@ old_overwrite_phpfpm="$(ynh_app_setting_get $app overwrite_phpfpm)"
 old_overwrite_phpfpm=$(bool_to_true_false $old_overwrite_phpfpm)
 overwrite_phpfpm="${YNH_CONFIG_MAIN_OVERWRITE_FILES_OVERWRITE_PHPFPM:-$old_overwrite_phpfpm}"
 
+# Type of admin mail configuration
+old_admin_mail_html="$(ynh_app_setting_get $app admin_mail_html)"
+old_admin_mail_html=$(bool_to_true_false $old_admin_mail_html)
+admin_mail_html="${YNH_CONFIG_MAIN_GLOBAL_CONFIG_EMAIL_TYPE:-$old_admin_mail_html}"
+
 #=================================================
 # SHOW_CONFIG FUNCTION FOR 'SHOW' COMMAND
 #=================================================
@@ -58,6 +63,8 @@ show_config() {
 	echo "YNH_CONFIG_MAIN_OVERWRITE_FILES_OVERWRITE_FTL=$overwrite_ftl"
 	echo "YNH_CONFIG_MAIN_OVERWRITE_FILES_OVERWRITE_NGINX=$overwrite_nginx"
 	echo "YNH_CONFIG_MAIN_OVERWRITE_FILES_OVERWRITE_PHPFPM=$overwrite_phpfpm"
+
+	echo "YNH_CONFIG_MAIN_GLOBAL_CONFIG_EMAIL_TYPE=$admin_mail_html"
 }
 
 #=================================================
@@ -77,6 +84,10 @@ apply_config() {
 	# Set overwrite_phpfpm
 	overwrite_phpfpm=$(bool_to_01 $overwrite_phpfpm)
 	ynh_app_setting_set $app overwrite_phpfpm "$overwrite_phpfpm"
+
+	# Set admin_mail_html
+	admin_mail_html=$(bool_to_01 $admin_mail_html)
+	ynh_app_setting_set $app admin_mail_html "$admin_mail_html"
 }
 
 #=================================================

+ 9 - 4
scripts/install

@@ -55,10 +55,12 @@ ynh_app_setting_set $app path $path_url
 ynh_app_setting_set $app admin $admin
 ynh_app_setting_set $app query_logging $query_logging
 ynh_app_setting_set $app enable_dhcp $enable_dhcp
+
 ynh_app_setting_set $app overwrite_setupvars "1"
 ynh_app_setting_set $app overwrite_ftl "1"
 ynh_app_setting_set $app overwrite_nginx "1"
 ynh_app_setting_set $app overwrite_phpfpm "1"
+ynh_app_setting_set $app admin_mail_html "1"
 
 #=================================================
 # STANDARD MODIFICATIONS
@@ -381,17 +383,20 @@ admin_panel="https://$(grep portal_domain /etc/ssowat/conf.json | cut -d'"' -f4)
 if [ $enable_dhcp -eq 1 ]
 then
 	dhcp_alert="You asked to use the internal DHCP server of dnsmasq with PiHole.
-You should really read the documentation about that, https://github.com/YunoHost-Apps/pihole_ynh/blob/master/dhcp.md
+You should really read the __URL_TAG1__documentation about that__URL_TAG2__https://github.com/YunoHost-Apps/pihole_ynh/blob/master/dhcp.md__URL_TAG3__
 
 "
 else
 	dhcp_alert=""
 fi
 
-message="${dhcp_alert}You can configure this app easily by using the experimental config-panel feature: $admin_panel/config-panel.
-You can also find some specific actions for this app by using the experimental action feature: $admin_panel/actions.
+echo "${dhcp_alert}You can configure this app easily by using the experimental __URL_TAG1__config-panel feature__URL_TAG2__$admin_panel/config-panel__URL_TAG3__.
+You can also find some specific actions for this app by using the experimental __URL_TAG1__action feature__URL_TAG2__$admin_panel/actions__URL_TAG3__.
+
+If you're facing an issue or want to improve this app, please open a new issue in this __URL_TAG1__project__URL_TAG2__https://github.com/YunoHost-Apps/pihole_ynh__URL_TAG3__." > mail_to_send
+
+ynh_send_readme_to_admin --app_message="mail_to_send" --recipients="$admin" --type="install"
 
-If you're facing an issue or want to improve this app, please open a new issue in this project: https://github.com/YunoHost-Apps/pihole_ynh"
 #=================================================
 # END OF SCRIPT
 #=================================================

+ 5 - 5
scripts/restore

@@ -228,19 +228,19 @@ admin_panel="https://$(grep portal_domain /etc/ssowat/conf.json | cut -d'"' -f4)
 if [ $enable_dhcp -eq 1 ]
 then
 	dhcp_alert="You asked to use the internal DHCP server of dnsmasq with PiHole.
-You should really read the documentation about that, https://github.com/YunoHost-Apps/pihole_ynh/blob/master/dhcp.md
+You should really read the __URL_TAG1__documentation about that__URL_TAG2__https://github.com/YunoHost-Apps/pihole_ynh/blob/master/dhcp.md__URL_TAG3__
 
 "
 else
 	dhcp_alert=""
 fi
 
-message="${dhcp_alert}You can configure this app easily by using the experimental config-panel feature: $admin_panel/config-panel.
-You can also find some specific actions for this app by using the experimental action feature: $admin_panel/actions.
+echo "${dhcp_alert}You can configure this app easily by using the experimental __URL_TAG1__config-panel feature__URL_TAG2__$admin_panel/config-panel__URL_TAG3__.
+You can also find some specific actions for this app by using the experimental __URL_TAG1__action feature__URL_TAG2__$admin_panel/actions__URL_TAG3__.
 
-If you're facing an issue or want to improve this app, please open a new issue in this project: https://github.com/YunoHost-Apps/pihole_ynh"
+If you're facing an issue or want to improve this app, please open a new issue in this __URL_TAG1__project__URL_TAG2__https://github.com/YunoHost-Apps/pihole_ynh__URL_TAG3__." > mail_to_send
 
-ynh_send_readme_to_admin --app_message="$message" --recipients="$admin" --type="restore"
+ynh_send_readme_to_admin --app_message="mail_to_send" --recipients="$admin" --type="restore"
 
 #=================================================
 # END OF SCRIPT

+ 14 - 4
scripts/upgrade

@@ -294,6 +294,9 @@ ynh_maintenance_mode_OFF
 # Get main domain and buid the url of the admin panel of the app.
 admin_panel="https://$(grep portal_domain /etc/ssowat/conf.json | cut -d'"' -f4)/yunohost/admin/#/apps/$app"
 
+# Build the changelog
+ynh_app_changelog || true
+
 if [ $enable_dhcp -eq 1 ]
 then
 	dhcp_alert="You asked to use the internal DHCP server of dnsmasq with PiHole.
@@ -304,13 +307,20 @@ else
 	dhcp_alert=""
 fi
 
-message="${dhcp_alert}You can configure this app easily by using the experimental config-panel feature: $admin_panel/config-panel.
-You can also find some specific actions for this app by using the experimental action feature: $admin_panel/actions.
+echo "${dhcp_alert}You can configure this app easily by using the experimental __URL_TAG1__config-panel feature__URL_TAG2__$admin_panel/config-panel__URL_TAG3__.
+You can also find some specific actions for this app by using the experimental __URL_TAG1__action feature__URL_TAG2__$admin_panel/actions__URL_TAG3__.
+
+If you're facing an issue or want to improve this app, please open a new issue in this __URL_TAG1__project__URL_TAG2__https://github.com/YunoHost-Apps/pihole_ynh__URL_TAG3__.
+
+---
+
+Changelog since your last upgrade:
+$(cat changelog)" > mail_to_send
+
+ynh_send_readme_to_admin --app_message="mail_to_send" --recipients="$admin" --type="upgrade"
 
-If you're facing an issue or want to improve this app, please open a new issue in this project: https://github.com/YunoHost-Apps/pihole_ynh"
 #=================================================
 # END OF SCRIPT
 #=================================================
 
-ynh_send_readme_to_admin --app_message="$message" --recipients="$admin" --type="upgrade"
 ynh_script_progression --message="Upgrade completed" --last