All files / src/git_platform zcl_abapgit_pr_enum_github.clas.abap

19.31% Statements 102/528
50% Branches 1/2
50% Functions 1/2
19.31% Lines 102/528

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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 5291x 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
      RAISING
        zcx_abapgit_exception.
 
    METHODS merge_pull_request
      IMPORTING
        iv_pull_number  TYPE i
        iv_commit_title TYPE clike OPTIONAL
      RAISING
        zcx_abapgit_exception.
 
    METHODS update_pull_request_title
      IMPORTING
        iv_pull_number TYPE i
        iv_title       TYPE clike
      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.
 
    METHODS ready_for_review
      IMPORTING
        iv_pull_number TYPE i
      RAISING
        zcx_abapgit_exception.
 
    TYPES: BEGIN OF ty_status,
             name       TYPE string,
             status     TYPE string,
             conclusion TYPE string,
             title      TYPE string,
           END OF ty_status.
    TYPES ty_status_tt TYPE STANDARD TABLE OF ty_status WITH DEFAULT KEY.
    METHODS get_check_runs
      IMPORTING
        iv_ref           TYPE string
      RETURNING
        VALUE(rt_status) TYPE ty_status_tt
      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 clean_url.
    rv_url = replace(
      val = iv_url
      regex = '\{.*\}$'
      with = '' ) ##REGEX_POSIX.
  ENDMETHOD.


  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' ).

    " https://docs.github.com/en/rest/about-the-rest-api/api-versions
    mi_http_agent->global_headers( )->set(
      iv_key = 'X-GitHub-Api-Version'
      iv_val = '2022-11-28' ).

    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 lt_labels TYPE string_table.
    DATA lv_label_i TYPE string.
    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| ).

      lt_labels = ii_json->members( |/{ lv_i }/labels| ).
      LOOP AT lt_labels INTO lv_label_i.
        APPEND ii_json->get( |/{ lv_i }/labels/{ lv_label_i }/name| ) TO <ls_p>-labels.
      ENDLOOP.

    ENDLOOP.

  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_title.
* https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#update-a-pull-request

    DATA lv_url      TYPE string.
    DATA li_json     TYPE REF TO zif_abapgit_ajson.
    DATA li_response TYPE REF TO zif_abapgit_http_response.
    DATA lx_ajson    TYPE REF TO zcx_abapgit_ajson_error.

    lv_url = mv_repo_url && '/pulls/' && iv_pull_number.

    TRY.
        li_json = zcl_abapgit_ajson=>create_empty( ).
        li_json->set_string(
          iv_path = '/title'
          iv_val  = iv_title ).

        li_response = mi_http_agent->request(
          iv_url     = lv_url
          iv_method  = zif_abapgit_http_agent=>c_methods-patch
          iv_payload = li_json->stringify( ) ).
      CATCH zcx_abapgit_ajson_error INTO lx_ajson.
        zcx_abapgit_exception=>raise_with_text( lx_ajson ).
    ENDTRY.

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

  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 merge_pull_request.
* https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#merge-a-pull-request

    DATA lv_url          TYPE string.
    DATA lv_commit_title TYPE string.
    DATA li_json         TYPE REF TO zif_abapgit_ajson.
    DATA li_response     TYPE REF TO zif_abapgit_http_response.
    DATA lx_ajson        TYPE REF TO zcx_abapgit_ajson_error.

    lv_url = mv_repo_url && '/pulls/' && iv_pull_number && '/merge'.

    IF iv_commit_title IS NOT INITIAL.
      lv_commit_title = iv_commit_title.
    ELSE.
      lv_commit_title = |Merge pull request #{ iv_pull_number }|.
    ENDIF.

    TRY.
        li_json = zcl_abapgit_ajson=>create_empty( ).
        li_json->set_string(
          iv_path = '/commit_title'
          iv_val  = lv_commit_title ).
        li_json->set_string(
          iv_path = '/merge_method'
          iv_val  = 'squash' ).

        li_response = mi_http_agent->request(
          iv_url     = lv_url
          iv_method  = zif_abapgit_http_agent=>c_methods-put
          iv_payload = li_json->stringify( ) ).
      CATCH zcx_abapgit_ajson_error INTO lx_ajson.
        zcx_abapgit_exception=>raise_with_text( lx_ajson ).
    ENDTRY.

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

  ENDMETHOD.


  METHOD ready_for_review.
* https://docs.github.com/en/graphql/reference/mutations#markpullrequestreadyforreview
* https://gist.github.com/jeromepl/02e70f3ea4a4e8103da6f96f14eb213c

    DATA lv_url      TYPE string.
    DATA lv_json     TYPE string.
    DATA li_response TYPE REF TO zif_abapgit_http_response.
    DATA lx_ajson    TYPE REF TO zcx_abapgit_ajson_error.
    DATA lv_node_id  TYPE string.

    lv_url = mv_repo_url && '/pulls/' && iv_pull_number.

    li_response = mi_http_agent->request(
      iv_url     = lv_url
      iv_method  = zif_abapgit_http_agent=>c_methods-get ).

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

    TRY.
        lv_node_id = li_response->json( )->get( |/node_id| ).
      CATCH zcx_abapgit_ajson_error INTO lx_ajson.
        zcx_abapgit_exception=>raise_with_text( lx_ajson ).
    ENDTRY.

    lv_json = |\{"query": "mutation \{ markPullRequestReadyForReview(input: | &&
      |\{ pullRequestId: \\"{ lv_node_id }\\" \}) \{ pullRequest \{ id \} \} \}" \}|.

    li_response = mi_http_agent->request(
      iv_url     = 'https://api.github.com/graphql'
      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 setting to ready: { 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_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'.

    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 get_check_runs.
* https://docs.github.com/en/rest/commits/statuses?apiVersion=2022-11-28#get-the-combined-status-for-a-specific-reference

    DATA lv_url      TYPE string.
    DATA li_response TYPE REF TO zif_abapgit_http_response.
    DATA li_json     TYPE REF TO zif_abapgit_ajson.
    DATA lt_statuses TYPE string_table.
    DATA ls_status   TYPE ty_status.
    DATA lv_item     LIKE LINE OF lt_statuses.
    DATA lx_ajson    TYPE REF TO zcx_abapgit_ajson_error.


    lv_url = mv_repo_url && '/commits/' && iv_ref && '/check-runs'.

    li_response = mi_http_agent->request(
      iv_url     = lv_url
      iv_method  = zif_abapgit_http_agent=>c_methods-get ).

    IF li_response->is_ok( ) = abap_false.
      zcx_abapgit_exception=>raise( |Error getting check-runs for ref { iv_ref }: { li_response->error( ) }| ).
    ENDIF.

    TRY.
        li_json = li_response->json( ).
      CATCH zcx_abapgit_ajson_error INTO lx_ajson.
        zcx_abapgit_exception=>raise_with_text( lx_ajson ).
    ENDTRY.

    lt_statuses = li_json->members( '/check_runs' ).
    LOOP AT lt_statuses INTO lv_item.
      ls_status-name = li_json->get( |/check_runs/{ lv_item }/name| ).
      ls_status-status = li_json->get( |/check_runs/{ lv_item }/status| ).
      ls_status-conclusion = li_json->get( |/check_runs/{ lv_item }/conclusion| ).
      ls_status-title = li_json->get( |/check_runs/{ lv_item }/output/title| ).
      APPEND ls_status TO rt_status.
    ENDLOOP.

  ENDMETHOD.

  METHOD zif_abapgit_pr_enum_provider~create_initial_branch.
* https://docs.github.com/en/rest/repos/contents?apiVersion=2022-11-28#create-or-update-file-contents--parameters

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

    lv_url = mv_repo_url && '/contents/README.md'.
    SPLIT mv_user_and_repo AT '/' INTO lv_owner lv_repo.

    lv_contents = iv_readme.

    IF lv_contents IS INITIAL.
      lv_contents = |# { to_upper( lv_repo ) }|.
    ENDIF.

    lv_json = |\{\n| &&
              |  "message": "Initial commit",\n| &&
              |  "content": "{ cl_http_utility=>encode_base64( lv_contents ) }",\n| &&
              |  "branch": "{ iv_branch_name }"\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 creating initial { iv_branch_name } branch: { li_response->error( ) }| ).
    ENDIF.

    rv_branch_name = iv_branch_name.

  ENDMETHOD.


  METHOD zif_abapgit_pr_enum_provider~create_repository.
* https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#create-an-organization-repository

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

    SPLIT mv_user_and_repo AT '/' INTO lv_owner lv_repo.

    " repo for organization or authenticated user
    IF iv_is_org = abap_true.
      lv_url = |https://api.github.com/orgs/{ lv_owner }/repos|.
    ELSE.
      lv_url = |https://api.github.com/user/repos|.
    ENDIF.

    IF iv_private = abap_true.
      lv_private = 'true'.
    ELSE.
      lv_private = 'false'.
    ENDIF.

    " create an initial commit with empty README
    IF iv_auto_init = abap_true.
      lv_auto_init = 'true'.
    ELSE.
      lv_auto_init = 'false'.
    ENDIF.

    lv_json = |\{\n| &&
              |  "name": "{ lv_repo }",\n| &&
              |  "description": "{ iv_description }",\n| &&
              |  "private": { lv_private },\n| &&
              |  "auto_init": { lv_auto_init }\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 repository { mv_user_and_repo }: { li_response->error( ) }| ).
    ENDIF.

  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.
ENDCLASS.