All files / src/git_platform zcl_abapgit_pr_enum_github.clas.abap

75.94% Statements 180/237
100% Branches 0/0
0% Functions 0/2
75.94% Lines 180/237

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 2381x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                 1x 1x                                                   1x 1x 1x 1x 1x 1x 1x 1x  
CLASS zcl_abapgit_pr_enum_github DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .
 
  PUBLIC SECTION.
 
    INTERFACES zif_abapgit_pr_enum_provider .
 
    METHODS constructor
      IMPORTING
        !iv_user_and_repo TYPE string
        !ii_http_agent    TYPE REF TO zif_abapgit_http_agent
      RAISING
        zcx_abapgit_exception.
 
    METHODS create_pull_request
      IMPORTING
        iv_title TYPE clike
        iv_body  TYPE clike OPTIONAL
        iv_head  TYPE string
        iv_base  TYPE string DEFAULT 'main'
      RAISING
        zcx_abapgit_exception.
 
    METHODS update_pull_request_branch
      IMPORTING
        iv_pull_number       TYPE i
        iv_expected_head_sha TYPE zif_abapgit_git_definitions=>ty_sha1
      RAISING
        zcx_abapgit_exception.
  PROTECTED SECTION.
  PRIVATE SECTION.
 
    TYPES:
      BEGIN OF ty_info,
        repo_json TYPE REF TO zif_abapgit_ajson,
        pulls     TYPE zif_abapgit_pr_enum_provider=>ty_pull_requests,
      END OF ty_info.
 
    DATA mi_http_agent TYPE REF TO zif_abapgit_http_agent.
    DATA mv_repo_url TYPE string.
    DATA mv_user_and_repo TYPE string.
 
    METHODS fetch_repo_by_url
      IMPORTING
        iv_repo_url    TYPE string
      RETURNING
        VALUE(rs_info) TYPE ty_info
      RAISING
        zcx_abapgit_exception.
 
    METHODS convert_list
      IMPORTING
        ii_json         TYPE REF TO zif_abapgit_ajson
      RETURNING
        VALUE(rt_pulls) TYPE zif_abapgit_pr_enum_provider=>ty_pull_requests.
 
    METHODS clean_url
      IMPORTING
        iv_url        TYPE string
      RETURNING
        VALUE(rv_url) TYPE string.
ENDCLASS.
 
 
 
CLASS ZCL_ABAPGIT_PR_ENUM_GITHUB IMPLEMENTATION.
 
  METHOD constructor.
 
    DATA lv_search TYPE string.
 
    mv_user_and_repo = iv_user_and_repo.
    mv_repo_url   = |https://api.github.com/repos/{ iv_user_and_repo }|.
    mi_http_agent = ii_http_agent.
    mi_http_agent->global_headers( )->set(
      iv_key = 'Accept'
      iv_val = 'application/vnd.github.v3+json' ).
 
    IF zcl_abapgit_login_manager=>get( mv_repo_url ) IS NOT INITIAL.
      mi_http_agent->global_headers( )->set(
        iv_key = 'Authorization'
        iv_val = zcl_abapgit_login_manager=>get( mv_repo_url ) ).
    ELSE.
* fallback, try searching for the git credentials
      lv_search = mv_repo_url.
      REPLACE FIRST OCCURRENCE OF 'api.github.com/repos' IN lv_search WITH 'github.com'.
      IF zcl_abapgit_login_manager=>get( lv_search ) IS NOT INITIAL.
        mi_http_agent->global_headers( )->set(
          iv_key = 'Authorization'
          iv_val = zcl_abapgit_login_manager=>get( lv_search ) ).
      ENDIF.
 
    ENDIF.
 
  ENDMETHOD.
 
 
  METHOD convert_list.
 
    DATA lt_items TYPE string_table.
    DATA lv_i TYPE string.
    FIELD-SYMBOLS <ls_p> LIKE LINE OF rt_pulls.
 
    lt_items = ii_json->members( '/' ).
 
    LOOP AT lt_items INTO lv_i.
      APPEND INITIAL LINE TO rt_pulls ASSIGNING <ls_p>.
      <ls_p>-base_url        = ii_json->get( |/{ lv_i }/base/repo/clone_url| ).
      <ls_p>-number          = ii_json->get( |/{ lv_i }/number| ).
      <ls_p>-title           = ii_json->get( |/{ lv_i }/title| ).
      <ls_p>-user            = ii_json->get( |/{ lv_i }/user/login| ).
      <ls_p>-head_url        = ii_json->get( |/{ lv_i }/head/repo/clone_url| ).
      <ls_p>-head_branch     = ii_json->get( |/{ lv_i }/head/ref| ).
      <ls_p>-created_at      = ii_json->get( |/{ lv_i }/created_at| ).
      <ls_p>-draft           = ii_json->get_boolean( |/{ lv_i }/draft| ).
      <ls_p>-html_url        = ii_json->get( |/{ lv_i }/html_url| ).
    ENDLOOP.
 
  ENDMETHOD.
 
 
  METHOD fetch_repo_by_url.
 
    DATA li_pulls_json TYPE REF TO zif_abapgit_ajson.
    DATA lv_pull_url TYPE string.
    DATA li_response TYPE REF TO zif_abapgit_http_response.
    DATA lx_ajson TYPE REF TO zcx_abapgit_ajson_error.
 
    li_response = mi_http_agent->request( iv_repo_url ).
 
    TRY.
        rs_info-repo_json = li_response->json( ).
        li_response->headers( ). " for debug
        lv_pull_url = clean_url( rs_info-repo_json->get( '/pulls_url' ) ).
        IF lv_pull_url IS INITIAL OR rs_info-repo_json->get( '/message' ) = 'Not Found'.
          RETURN.
        ENDIF.
        li_pulls_json = mi_http_agent->request( lv_pull_url )->json( ).
      CATCH zcx_abapgit_ajson_error INTO lx_ajson.
        zcx_abapgit_exception=>raise_with_text( lx_ajson ).
    ENDTRY.
 
    rs_info-pulls = convert_list( li_pulls_json ).
 
  ENDMETHOD.
 
 
  METHOD zif_abapgit_pr_enum_provider~list_pull_requests.
 
    DATA lv_upstream_url TYPE string.
    DATA ls_repo_info TYPE ty_info.
    FIELD-SYMBOLS <ls_p> LIKE LINE OF ls_repo_info-pulls.
 
    ls_repo_info = fetch_repo_by_url( mv_repo_url ).
    APPEND LINES OF ls_repo_info-pulls TO rt_pulls.
 
    IF ls_repo_info-repo_json->get_boolean( '/fork' ) = abap_true.
      lv_upstream_url = ls_repo_info-repo_json->get( '/source/url' ). " parent ?
      ls_repo_info = fetch_repo_by_url( lv_upstream_url ).
      LOOP AT ls_repo_info-pulls ASSIGNING <ls_p>.
        <ls_p>-is_for_upstream = abap_true.
        APPEND <ls_p> TO rt_pulls.
      ENDLOOP.
    ENDIF.
 
  ENDMETHOD.
 
  METHOD create_pull_request.
* https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#create-a-pull-request

    DATA lv_owner    TYPE string.
    DATA lv_repo     TYPE string.
    DATA lv_url      TYPE string.
    DATA lv_json     TYPE string.
    DATA li_response TYPE REF TO zif_abapgit_http_response.

    lv_url = mv_repo_url && '/pulls'.
    SPLIT mv_user_and_repo AT '/' INTO lv_owner lv_repo.

    lv_json = |\{\n| &&
              |  "owner": "{ lv_owner }",\n| &&
              |  "repo": "{ lv_repo }",\n| &&
              |  "title": "{ iv_title }",\n| &&
              |  "head": "{ iv_head }",\n| &&
              |  "body": "{ iv_body }",\n| &&
              |  "maintainer_can_modify": true,\n| &&
              |  "draft": true,\n| &&
              |  "base": "{ iv_base }"\n| &&
              |\}|.

    li_response = mi_http_agent->request(
      iv_url     = lv_url
      iv_method  = zif_abapgit_http_agent=>c_methods-post
      iv_payload = lv_json ).

    IF li_response->is_ok( ) = abap_false.
      zcx_abapgit_exception=>raise( |Error creating pull request: { li_response->error( ) }| ).
    ENDIF.

  ENDMETHOD.
 
  METHOD update_pull_request_branch.
* https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#update-a-pull-request-branch

    DATA lv_owner    TYPE string.
    DATA lv_repo     TYPE string.
    DATA lv_url      TYPE string.
    DATA lv_json     TYPE string.
    DATA li_response TYPE REF TO zif_abapgit_http_response.

    lv_url = mv_repo_url && '/pulls/' && iv_pull_number && '/update-branch'.
    SPLIT mv_user_and_repo AT '/' INTO lv_owner lv_repo.

    lv_json = |\{\n| &&
              |  "expected_head_sha": "{ to_lower( iv_expected_head_sha ) }"\n| &&
              |\}|.

    li_response = mi_http_agent->request(
      iv_url     = lv_url
      iv_method  = zif_abapgit_http_agent=>c_methods-put
      iv_payload = lv_json ).

    IF li_response->is_ok( ) = abap_false.
      zcx_abapgit_exception=>raise( |Error updating pull request branch: { li_response->error( ) }| ).
    ENDIF.

  ENDMETHOD.
 
  METHOD clean_url.
    rv_url = replace(
      val = iv_url
      regex = '\{.*\}$'
      with = '' ).
  ENDMETHOD.
ENDCLASS.