Skip to content

Instantly share code, notes, and snippets.

@smb
Created March 8, 2019 13:01
Show Gist options
  • Save smb/ae4f32e5ce2dee9670f101111808e3dc to your computer and use it in GitHub Desktop.
Save smb/ae4f32e5ce2dee9670f101111808e3dc to your computer and use it in GitHub Desktop.
abapgit installer
CLASS ztools_installer DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
CLASS-METHODS pull
IMPORTING
!iv_url TYPE string
!iv_branch_name TYPE string OPTIONAL
!iv_package TYPE devclass
!iv_package_descr TYPE string OPTIONAL
!iv_username TYPE string
!iv_password TYPE string
!iv_transport TYPE trkorr OPTIONAL
!iv_dlvunit TYPE dlvunit DEFAULT 'LOCAL'
!iv_devlayer TYPE devlayer OPTIONAL
!iv_parent TYPE parentcl OPTIONAL
RAISING zcx_abapgit_exception.
CLASS-METHODS install
IMPORTING
!iv_username TYPE string
!iv_password TYPE string
!iv_transport TYPE trkorr
RAISING zcx_abapgit_exception.
PROTECTED SECTION.
CLASS-METHODS decisions
IMPORTING
is_checks TYPE zif_abapgit_definitions=>ty_deserialize_checks
iv_transport TYPE trkorr
RETURNING
VALUE(rs_checks) TYPE zif_abapgit_definitions=>ty_deserialize_checks.
PRIVATE SECTION.
ENDCLASS.
CLASS ztools_installer IMPLEMENTATION.
METHOD install.
DATA(lt_repos) = VALUE zif_abapgit_persistence=>tt_repo(
( package = '/MY/TOOLS_P1' url = |https://my.git.repository/p1.git| )
( package = '/MY/TOOLS_P2' url = |https://my.git.repository/p2.git| )
).
LOOP AT lt_repos ASSIGNING FIELD-SYMBOL(<ls_repos>).
pull( iv_url = <ls_repos>-url
iv_package = <ls_repos>-package
iv_username = iv_username
iv_password = iv_password
iv_transport = iv_transport
iv_dlvunit = 'HOME'
iv_parent = '/MY/TOOLS'
).
ENDLOOP.
ENDMETHOD.
METHOD decisions.
* this part can be handled by presenting the warings to the user in UI, or set via logic in code
* this is an example, adjust to fit your business requirements
rs_checks = is_checks.
LOOP AT rs_checks-overwrite ASSIGNING FIELD-SYMBOL(<ls_overwrite>).
* Object has been modified locally
* decision field must be filled with Y or N. Y overwrites the object
<ls_overwrite>-decision = 'Y'.
ENDLOOP.
LOOP AT rs_checks-warning_package ASSIGNING FIELD-SYMBOL(<ls_warning_package>).
* Y or N if object from unexpected package should be overwritten
<ls_warning_package>-decision = 'Y'.
ENDLOOP.
IF rs_checks-requirements-met = 'N'.
* code must decide if deserialization should continue or not
rs_checks-requirements-decision = 'Y'.
ENDIF.
IF rs_checks-transport-required = abap_true.
rs_checks-transport-transport = iv_transport.
ENDIF.
ENDMETHOD.
METHOD pull.
DATA: lo_repo TYPE REF TO zcl_abapgit_repo_online.
" will be overwritten with description from git devc on successful pull
DATA(lv_package_descr) = COND string(
WHEN iv_package_descr IS NOT INITIAL THEN iv_package_descr
ELSE |GIT Package { iv_package }|
).
DATA(lv_branch_name) = COND string(
WHEN iv_branch_name IS NOT INITIAL THEN iv_branch_name
ELSE 'refs/heads/master'
).
IF iv_package IS INITIAL.
zcx_abapgit_exception=>raise( 'package must not be empty' ).
ENDIF.
IF iv_dlvunit <> 'LOCAL'.
zcl_abapgit_default_transport=>get_instance( )->set( iv_transport ).
ENDIF.
IF iv_parent IS NOT INITIAL.
zcl_abapgit_factory=>get_sap_package( iv_parent )->create_child( iv_child = iv_package ).
* CATCH zcx_abapgit_exception.
ELSE.
DATA(ls_package_data) = VALUE scompkdtln(
devclass = iv_package
ctext = lv_package_descr
language = sy-langu
masterlang = sy-langu
dlvunit = iv_dlvunit
pdevclass = iv_devlayer
as4user = sy-uname
parentcl = iv_parent
).
zcl_abapgit_factory=>get_sap_package( ls_package_data-devclass )->create( ls_package_data ).
ENDIF.
TRY.
DATA(lt_list) = zcl_abapgit_persist_factory=>get_repo( )->list( ).
DATA(ls_repo) = lt_list[ package = iv_package ].
IF to_lower( ls_repo-url ) <> to_lower( iv_url ).
zcx_abapgit_exception=>raise( 'url doesnt match - not overwriting' ).
ENDIF.
" delete old repo
lo_repo ?= zcl_abapgit_repo_srv=>get_instance( )->get( iv_key = ls_repo-key ).
zcl_abapgit_repo_srv=>get_instance( )->delete( io_repo = lo_repo ).
CATCH cx_sy_itab_line_not_found.
" Repo not found -> no problem
ENDTRY.
zcl_abapgit_login_manager=>set(
iv_uri = iv_url
iv_username = iv_username
iv_password = iv_password
).
lo_repo = zcl_abapgit_repo_srv=>get_instance( )->new_online(
iv_url = iv_url
iv_branch_name = lv_branch_name
iv_package = iv_package
).
DATA(ls_checks) = lo_repo->deserialize_checks( ).
* the code must decide what to do with warnings, see example below
ls_checks = decisions( is_checks = ls_checks iv_transport = iv_transport ).
lo_repo->deserialize( ls_checks ).
zcl_abapgit_default_transport=>get_instance( )->reset( ).
ENDMETHOD.
ENDCLASS.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment