<?xml version="1.0" encoding="utf-8" ?>

<list>
  <item type="dictionary">
    <dictionary>
      <item key="data"><![CDATA[from Products.zms import standard
import os

def manage_repository_gitpull(self, request=None):
	printed = []
	request = self.REQUEST
	RESPONSE =  request.RESPONSE
	btn = request.form.get('btn')
	came_from = request.get('came_from',request['HTTP_REFERER'])
	if came_from.find('?') > 0:
		came_from = came_from[:came_from.find('?')]
	base_path = self.get_conf_basepath()
	branch = self.getConfProperty('ZMSRepository.git.server.branch','master').replace('"','').replace(';','')
	hardreset_cmd = 'git reset --hard origin/%s'%(branch)

	printed.append('<!DOCTYPE html>')
	printed.append('<html lang="en">')
	printed.append(self.zmi_html_head(self,request).encode('utf-8'))
	printed.append('<body class="repository_manager_main %s">'%(' '.join(['zmi',request['lang'],self.meta_id])))
	# printed.append(self.zmi_body_header(self,request,options=[{'action':'#','label':'%s...'%self.getZMILangStr('BTN_GITPULL')}]))
	printed.append(self.zmi_body_header(self,request,options=self.repository_manager.customize_manage_options()).encode('utf-8'))
	printed.append('<div id="zmi-tab">')
	printed.append(self.zmi_breadcrumbs(self,request,extra=[self.manage_sub_options()[0]]).encode('utf-8'))
	printed.append('<div class="card">')
	printed.append('<form class="form-horizontal" method="post" enctype="multipart/form-data">')
	printed.append('<input type="hidden" name="lang" value="%s"/>'%request['lang'])
	printed.append('<input type="hidden" name="came_from" value="%s"/>'%came_from)
	printed.append('<legend>%s, Current Branch %s</legend>'%(self.getZMILangStr('BTN_GITPULL'),branch))


	# --- PULL. +++IMPORTANT+++: Use SSH/cert and git credential manager
	# ---------------------------------
	if btn=='BTN_GITPULL':
		message = []
		### update from repository
		# userid = self.getConfProperty('ZMSRepository.git.server.userid')
		# password = self.getConfProperty('ZMSRepository.git.server.password') # TODO: decrypt
		# url = self.getConfProperty('ZMSRepository.git.server.url')
		if len([x for x in request['AUTHENTICATED_USER'].getRolesInContext(self) if x in ['Manager','ZMSAdminstrator']]) > 0:
			os.chdir(base_path)
			if request.get('hardreset'):
				result = os.system(hardreset_cmd)
				message.append('<code class="d-block">%s [%s]</code>'%(hardreset_cmd, str(result)))
			command = 'git checkout %s;git pull'%(branch)
			if request.get('revision')!='HEAD' and request.get('revision') is not None:
				command = 'git checkout %s'%(request.get('revision').replace('"','').replace(';',''))
			result = os.system(command)
			message.append('<code class="d-block mb-3">%s [%s]</code>'%(command, str(result)))
			### import from working-copy
			# success = self.updateChanges(REQUEST.get('ids',[]),btn=='override')
			# message.append(self.getZMILangStr('MSG_IMPORTED')%('<em>%s</em>'%' '.join(success)))
		else:
			message.append('Error: To execute this function a user role Manager or ZMSAdministrator is needed.')
		### return with message
		request.response.redirect(self.url_append_params('manage_main',{'lang':request['lang'],'manage_tabs_message':''.join(message)}))

	# --- Cancel.
	# ---------------------------------
	elif btn=='BTN_CANCEL':
		request.response.redirect(self.url_append_params(came_from,{'lang':request['lang']}))

	# --- Display initial form.
	# -------------------------
	else:
		printed.append('<div class="card-body">')
		printed.append('<div class="form-group row">')
		printed.append('<label for="revision" class="col-sm-2 control-label mandatory">Revision</label>')
		printed.append('<div class="col-sm-10"><input class="form-control" name="revision" type="text" size="25" value="HEAD" title="Default value HEAD pulls the latest revision. Please, enter the hexadecimal ID for checking out a specific revision." placeholder="Enter HEAD or Revision-ID"></div>')
		printed.append('</div><!-- .form-group -->')
		printed.append('<div class="form-group row">')
		printed.append('<label for="hardreset" class="col-sm-2 control-label mandatory">Use Hard Reset</label>')
		printed.append('<div class="col-sm-10"><input type="checkbox" name="hardreset" value="hardreset" title="git reset --hard origin/%s" /></div>'%(branch))
		printed.append('</div><!-- .form-group -->')
		printed.append('<div class="form-group">')
		printed.append('<div class="controls save">')
		printed.append('<button type="submit" name="btn" class="btn btn-primary" value="BTN_GITPULL">%s</button>'%(self.getZMILangStr('BTN_GITPULL')))
		printed.append('<button type="submit" name="btn" class="btn btn-secondary" value="BTN_CANCEL">%s</button>'%(self.getZMILangStr('BTN_CANCEL')))
		printed.append('</div>')
		printed.append('</div><!-- .form-group -->')
		# printed.append(self.manage_main_diff(self,request))
		printed.append('</div><!-- .card-body -->')
	# ---------------------------------

	printed.append('</form><!-- .form-horizontal -->')
	printed.append('</div><!-- .card -->')
	printed.append('</div><!-- #zmi-tab -->')
	printed.append(self.zmi_body_footer(self,request).encode('utf-8'))
	printed.append('<script>$ZMI.registerReady(function(){ $(\'#tabs_items li a\').removeClass(\'active\');$(\'#tabs_items li[data-action*=\"repository_manager\"] a\').addClass(\'active\'); })</script>')
	printed.append('</body>')
	printed.append('</html>')

	return '\n'.join(printed)]]>
      </item>
      <item key="description"></item>
      <item key="execution" type="int">0</item>
      <item key="icon_clazz"><![CDATA[fas fa-backward]]></item>
      <item key="id"><![CDATA[manage_repository_gitpull]]></item>
      <item key="meta_type"><![CDATA[External Method]]></item>
      <item key="meta_types" type="list">
        <list>
          <item><![CDATA[ZMS]]></item>
        </list>
      </item>
      <item key="name"><![CDATA[BTN_GITPULL]]></item>
      <item key="revision"><![CDATA[4.0.0]]></item>
      <item key="roles" type="list">
        <list>
          <item><![CDATA[ZMSAdministrator]]></item>
        </list>
      </item>
      <item key="title"><![CDATA[Git Pull]]></item>
    </dictionary>
  </item>
  <item type="dictionary">
    <dictionary>
      <item key="data"><![CDATA[from Products.zms import standard
import os

def manage_repository_gitpush(self, request=None):
	printed = []
	request = self.REQUEST
	RESPONSE =  request.RESPONSE
	btn = request.form.get('btn')
	came_from = request.get('came_from',request['HTTP_REFERER'])
	if came_from.find('?') > 0:
		came_from = came_from[:came_from.find('?')]
	base_path = self.get_conf_basepath()

	printed.append('<!DOCTYPE html>')
	printed.append('<html lang="en">')
	printed.append(self.zmi_html_head(self,request).encode('utf-8'))
	printed.append('<body class="repository_manager_main %s">'%(' '.join(['zmi',request['lang'],self.meta_id])))
	printed.append(self.zmi_body_header(self,request,options=self.repository_manager.customize_manage_options()).encode('utf-8'))
	printed.append('<div id="zmi-tab">')
	printed.append(self.zmi_breadcrumbs(self,request,extra=[self.manage_sub_options()[0]]).encode('utf-8'))
	printed.append('<div class="card">')
	printed.append('<form class="form-horizontal" method="post" enctype="multipart/form-data">')
	printed.append('<input type="hidden" name="lang" value="%s"/>'%request['lang'])
	printed.append('<input type="hidden" name="came_from" value="%s"/>'%came_from)
	printed.append('<legend>%s, Current Branch %s</legend>'%(self.getZMILangStr('BTN_GITPUSH'),self.getConfProperty('ZMSRepository.git.server.branch','master')))


	# --- COMMIT/PUSH. +++IMPORTANT+++: Use SSH/cert and git credential manager
	# ---------------------------------
	if btn=='BTN_GITPUSH':
		message = []
		if len([x for x in request['AUTHENTICATED_USER'].getRolesInContext(self) if x in ['Manager','ZMSAdminstrator']]) > 0:
			userid = str(request.get('AUTHENTICATED_USER'))[0:3]
			os.chdir(base_path)
			command1 = 'git add .'
			command2 = 'git commit -a -m "%s (%s)"'%(request.get('message').replace('"','').replace(';',''), userid)
			command3 = 'git push'
			result1 = os.system(command1)
			message.append('<code class="d-block">%s [%s]</code>'%(command1, str(result1)))
			result2 = os.system(command2)
			message.append('<code class="d-block">%s [%s]</code>'%(command2, str(result2)))
			result3 = os.system(command3)
			message.append('<code class="d-block mb-3">%s [%s]</code>'%(command3, str(result3)))
		else:
			message.append('Error: To execute this function a user role Manager or ZMSAdministrator is needed.')
		### return with message
		request.response.redirect(self.url_append_params('manage_main',{'lang':request['lang'],'manage_tabs_message':''.join(message)}))

	# --- Cancel.
	# ---------------------------------
	elif btn=='BTN_CANCEL':
		request.response.redirect(self.url_append_params(came_from,{'lang':request['lang']}))

	# --- Display initial form.
	# -------------------------
	else:

		printed.append('<div class="card-body">')
		printed.append('<div class="form-group row">')
		printed.append('<label for="message" class="col-sm-2 control-label mandatory">Message</label>')
		printed.append('<div class="col-sm-10"><input class="form-control" name="message" type="text" size="25" value="" placeholder="Enter commit message here"></div>')
		printed.append('</div><!-- .form-group -->')
		printed.append('<div class="form-group">')
		printed.append('<div class="controls save">')
		printed.append('<button type="submit" name="btn" class="btn btn-primary" value="BTN_GITPUSH">%s</button>'%(self.getZMILangStr('BTN_GITPUSH')))
		printed.append('<button type="submit" name="btn" class="btn btn-secondary" value="BTN_CANCEL">%s</button>'%(self.getZMILangStr('BTN_CANCEL')))
		printed.append('</div>')
		printed.append('</div><!-- .form-group -->')
		# printed.append(self.manage_main_diff(self,request))
		printed.append('</div>')
	# ---------------------------------

	printed.append('</form><!-- .form-horizontal -->')
	printed.append('</div><!-- .card -->')
	printed.append('</div><!-- #zmi-tab -->')
	printed.append(self.zmi_body_footer(self,request).encode('utf-8'))
	printed.append('<script>$ZMI.registerReady(function(){ $(\'#tabs_items li a\').removeClass(\'active\');$(\'#tabs_items li[data-action*=\"repository_manager\"] a\').addClass(\'active\'); })</script>')
	printed.append('</body>')
	printed.append('</html>')

	return '\n'.join(printed)]]>
      </item>
      <item key="description"></item>
      <item key="execution" type="int">0</item>
      <item key="icon_clazz"><![CDATA[fas fa-forward]]></item>
      <item key="id"><![CDATA[manage_repository_gitpush]]></item>
      <item key="meta_type"><![CDATA[External Method]]></item>
      <item key="meta_types" type="list">
        <list>
          <item><![CDATA[ZMS]]></item>
        </list>
      </item>
      <item key="name"><![CDATA[BTN_GITPUSH]]></item>
      <item key="revision"><![CDATA[4.0.0]]></item>
      <item key="roles" type="list">
        <list>
          <item><![CDATA[ZMSAdministrator]]></item>
        </list>
      </item>
      <item key="title"><![CDATA[Git Push]]></item>
    </dictionary>
  </item>
  <item type="dictionary">
    <dictionary>
      <item key="data"><![CDATA[from Products.zms import standard
import os

def manage_repository_gitconfig(self, request=None):
	printed = []
	request = self.REQUEST
	RESPONSE =  request.RESPONSE
	btn = request.form.get('btn')
	came_from = request.get('came_from',request['HTTP_REFERER'])
	if came_from.find('?') > 0:
		came_from = came_from[:came_from.find('?')]
	base_path = self.get_conf_basepath()
	base_status = ''
	try:
		standard.localfs_readPath(base_path)
	except:
		base_status = standard.writeError(self,'can\'t read base_path') 

	printed.append('<!DOCTYPE html>')
	printed.append('<html lang="en">')
	printed.append(self.zmi_html_head(self,request).encode('utf-8'))
	printed.append('<body class="%s">'%(' '.join(['zmi',request['lang'],self.meta_id])))
	printed.append(self.zmi_body_header(self,request,options=self.repository_manager.customize_manage_options()).encode('utf-8'))
	printed.append('<div id="zmi-tab">')
	printed.append(self.zmi_breadcrumbs(self,request,extra=[self.manage_sub_options()[0]]).encode('utf-8'))
	printed.append('<div class="card">')
	printed.append('<form class="form-horizontal" method="post" enctype="multipart/form-data">')
	printed.append('<input type="hidden" name="lang" value="%s"/>'%request['lang'])
	printed.append('<input type="hidden" name="came_from" value="%s"/>'%came_from)
	printed.append('<legend>GIT-%s, Current Branch %s</legend>'%(self.getZMILangStr('TAB_CONFIGURATION'),self.getConfProperty('ZMSRepository.git.server.branch','master')))

	# --- Change.
	# ---------------------------------
	if btn=='BTN_CHANGE':
		message = []
		self.setConfProperty('ZMSRepository.git.server.url',request['url'])
		self.setConfProperty('ZMSRepository.git.server.branch',request['branch'])
		message.append(self.getZMILangStr('MSG_CHANGED'))
		request.response.redirect(self.url_append_params('manage_main',{'lang':request['lang'],'manage_tabs_message':'<br/>'.join(message)}))

	# --- Checkout.
	# ---------------------------------
	elif btn=='BTN_CLONE':
		url = self.getConfProperty('ZMSRepository.git.server.url')
		os.chdir(base_path)
		command = "git clone %s ."%(url)
		result = os.system(command)
		printed.append('<div class="alert alert-info my-3"><code class="d-block">%s [%s]</code></div>'%(command, str(result)))

	# --- Cancel.
	# ---------------------------------
	elif btn=='BTN_CANCEL':
		request.response.redirect(self.url_append_params(came_from,{'lang':request['lang']}))

	# --- Display initial form.
	# -------------------------
	else:
		printed.append('<div class="card-body">')
		printed.append('<div class="alert alert-info m-0 mb-4">IMPORTANT NOTE: Please make sure that a certificate based communication with the GIT server is configured properly on the system</div>')
		printed.append('<div class="form-group row">')
		printed.append('<label for="url" class="col-sm-2 control-label mandatory">Working-copy</label>')
		printed.append('<div class="col-sm-10"><input class="form-control" name="path" type="text" size="25" value="%s" readonly></div>'%base_path)
		printed.append('</div><!-- .form-group -->')
		printed.append('<div class="form-group row">')
		printed.append('<label for="url" class="col-sm-2 control-label mandatory">Server</label>')
		printed.append('<div class="col-sm-10"><input class="form-control" name="url" type="text" size="25" value="%s"></div>'%self.getConfProperty('ZMSRepository.git.server.url','git@github.com:myname/myproject.git'))
		printed.append('</div><!-- .form-group -->')
		printed.append('<div class="form-group row">')
		printed.append('<label for="branch" class="col-sm-2 control-label mandatory">Branch Name</label>')
		printed.append('<div class="col-sm-10"><input class="form-control" name="branch" placeholder="master" type="text" size="25" value="%s"></div>'%self.getConfProperty('ZMSRepository.git.server.branch','master'))
		printed.append('</div><!-- .form-group -->')
		printed.append('<div class="form-group row">')
		printed.append('<div class="controls save">')
		printed.append('<button type="submit" name="btn" class="btn btn-primary" value="BTN_CHANGE">%s</button> '%(self.getZMILangStr('BTN_CHANGE')))
		printed.append('<button type="submit" name="btn" class="btn btn-warning" value="BTN_CLONE">%s</button> '%(self.getZMILangStr('BTN_CLONE')=='BTN_CLONE' and 'Clone' or self.getZMILangStr('BTN_CLONE')))
		printed.append('<button type="submit" name="btn" class="btn btn-secondary" value="BTN_CANCEL">%s</button> '%(self.getZMILangStr('BTN_CANCEL')))
		printed.append('</div>')
		printed.append('</div><!-- .form-group -->')
		printed.append('</div><!-- .card-body -->')

	# ---------------------------------

	printed.append('</form><!-- .form-horizontal -->')
	printed.append('</div><!-- .card -->')
	printed.append('</div><!-- #zmi-tab -->')
	printed.append(self.zmi_body_footer(self,request).encode('utf-8'))
	printed.append('<script>$ZMI.registerReady(function(){ $(\'#tabs_items li a\').removeClass(\'active\');$(\'#tabs_items li[data-action*=\"repository_manager\"] a\').addClass(\'active\'); })</script>')
	printed.append('</body>')
	printed.append('</html>')

	return '\n'.join(printed)]]>
      </item>
      <item key="description"></item>
      <item key="execution" type="int">0</item>
      <item key="icon_clazz"><![CDATA[fas fa-cogs]]></item>
      <item key="id"><![CDATA[manage_repository_gitconfig]]></item>
      <item key="meta_type"><![CDATA[External Method]]></item>
      <item key="meta_types" type="list">
        <list>
          <item><![CDATA[ZMS]]></item>
        </list>
      </item>
      <item key="name"><![CDATA[TAB_CONFIGURATION]]></item>
      <item key="revision"><![CDATA[4.0.0]]></item>
      <item key="roles" type="list">
        <list>
          <item><![CDATA[ZMSAdministrator]]></item>
        </list>
      </item>
      <item key="title"><![CDATA[Git-Configuration]]></item>
    </dictionary>
  </item>
</list>