Making WordPress.org


Ignore:
Timestamp:
02/10/2022 09:38:45 AM (3 years ago)
Author:
dd32
Message:

Trac: Add the ability to include Tickets that you've added a GitHub PR to, in the 'my patches' reports.

See https://core.trac.wordpress.org/my-patches + https://meta.trac.wordpress.org/report/12
Fixes #5596.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/api.wordpress.org/public_html/dotorg/trac/pr/index.php

    r11099 r11544  
    77$trac          = preg_replace( '![^a-z]!', '', $_GET['trac'] ?? '' );
    88$ticket        = intval( $_GET['ticket'] ?? 0 );
     9$author        = wp_unslash( $_GET['author'] ?? '' );
    910$authenticated = ! empty( $_GET['authenticated'] ); // Longer caches for logged out requests.
    1011
    11 if ( empty( $trac ) || empty( $ticket ) ) {
     12header( 'Content-Type: application/json' );
     13header( 'Access-Control-Allow-Origin: *' );
     14
     15if ( empty( $trac ) || ( empty( $ticket ) && empty( $author ) ) ) {
    1216    header( 'HTTP/1.0 400 Bad Request' );
    13     header( 'Content-Type: application/json' );
    14     die( '{"error":"Ticket number is invalid."}' );
     17    die( '{"error":"Trac, Ticket number, or Author is invalid."}' );
     18}
     19
     20// Type one: Return PRs by Author.
     21if ( $author ) {
     22    header( 'Cache-Control: max-age=' . HOUR_IN_SECONDS );
     23    header( 'Expires: ' . gmdate( 'D, d M Y H:i:s \G\M\T', time() + HOUR_IN_SECONDS ) );
     24
     25    $user_id = get_user_by( 'slug', $author )->ID ?? 0;
     26
     27    $tickets = $wpdb->get_col( $wpdb->prepare(
     28        "SELECT `ticket`
     29        FROM `trac_github_prs`
     30        WHERE trac = %s AND author = %d",
     31        $trac,
     32        $user_id
     33    ) );
     34
     35    echo wp_json_encode( $tickets );
     36    die();
    1537}
    1638
    1739// Fetch any linked PRs
    1840$prs = $wpdb->get_results( $wpdb->prepare(
    19     "SELECT `repo`, `pr`, `data`, `last_checked`
     41    "SELECT `repo`, `pr`, `data`, `last_checked`, `author`
    2042    FROM `trac_github_prs`
    2143    WHERE trac = %s AND ticket = %s",
     
    6587            unset( $data->data->trac_ticket );
    6688
     89            // Check if we now have an author for this PR, the author may link their account after creating the PR.
     90            if ( ! $data->author ) {
     91                $data->author = (int) find_wporg_user_by_github( $pr_data->user->name, 'ID' );
     92            }
     93
    6794            $wpdb->update(
    6895                'trac_github_prs',
     
    7097                    'data'         => json_encode( $pr_data ),
    7198                    'last_checked' => gmdate( 'Y-m-d H:i:s' ),
     99                    'author'       => $data->author,
    72100                ],
    73101                [
     
    98126header( 'Cache-Control: max-age=' . $expiry );
    99127header( 'Expires: ' . gmdate( 'D, d M Y H:i:s \G\M\T', time() + $expiry ) );
    100 header( 'Content-Type: application/json' );
    101 header( 'Access-Control-Allow-Origin: *' );
    102128
    103129// Only return the actual PR data needed
Note: See TracChangeset for help on using the changeset viewer.