Making WordPress.org

Changeset 11544


Ignore:
Timestamp:
02/10/2022 09:38:45 AM (2 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.

Location:
sites/trunk
Files:
6 edited

Legend:

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

    r11255 r11544  
    103103 * Find a WordPress.org user by a Github login.
    104104 */
    105 function find_wporg_user_by_github( $github_user ) {
     105function find_wporg_user_by_github( $github_user, $what = 'user_login' ) {
    106106    global $wpdb;
    107107
     108    if ( ! in_array( $what, [ 'ID', 'user_login' ], true ) ) {
     109        return false;
     110    }
     111
    108112    return $wpdb->get_var( $wpdb->prepare(
    109         "SELECT u.user_login
     113        "SELECT u.{$what}
    110114            FROM wporg_github_users g
    111115                JOIN {$wpdb->users} u ON g.user_id = u.ID
  • 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
  • sites/trunk/api.wordpress.org/public_html/dotorg/trac/pr/webhook.php

    r11099 r11544  
    8181        // Step 3. If not in DB, or $pr_data->trac_ticket isn't yet in the DB, add a new row of it.
    8282        if ( $pr_data->trac_ticket && ( ! $existing_refs || ! $matched_existing_ref ) ) {
     83
     84            $user_id = (int) find_wporg_user_by_github( $pr_data->user->name, 'ID' );
     85
    8386            $wpdb->insert(
    8487                'trac_github_prs',
     
    9194                    'pr'           => $pr_number,
    9295                    'data'         => json_encode( $_pr_data_no_ticket ),
     96                    'author'       => $user_id,
    9397                ]
    9498            );
  • sites/trunk/trac.wordpress.org/templates/site.html

    r11540 r11544  
    55
    66<?python
    7     scripts_version = '161'
     7    scripts_version = '162'
    88    project_slug = req.environ['HTTP_HOST'].split(':')[0].split('.')[0]
    99    wporg_endpoint = 'https://make.wordpress.org/' + project_slug + '/'
  • sites/trunk/trac.wordpress.org/templates/wporg-head.html

    r11499 r11544  
    4646.has-text-align-justify{text-align:justify;}
    4747</style>
    48   <link rel="stylesheet" id="wporg-global-header-footer-css" href="https://wordpress.org/wp-content/mu-plugins/pub-sync/blocks/global-header-footer/build/style.css?ver=1643748587" type="text/css" media="all"/>
     48  <link rel="stylesheet" id="wporg-global-header-footer-css" href="https://wordpress.org/wp-content/mu-plugins/pub-sync/blocks/global-header-footer/build/style.css?ver=1643748639" type="text/css" media="all"/>
    4949  <link rel="stylesheet" id="mediaelement-css" href="https://wordpress.org/wp-includes/js/mediaelement/mediaelementplayer-legacy.min.css?ver=4.2.16" type="text/css" media="all"/>
    5050  <link rel="stylesheet" id="wp-mediaelement-css" href="https://wordpress.org/wp-includes/js/mediaelement/wp-mediaelement.min.css?ver=6.0-alpha-52660" type="text/css" media="all"/>
  • sites/trunk/wordpress.org/public_html/style/trac/wp-trac.js

    r11540 r11544  
    16661666                }
    16671667
     1668                if ( ! trac ) {
     1669                    return;
     1670                }
     1671
     1672                // Add ability to include GitHub tickets into a 'my-patches' report.
     1673                // "Just" include a variable called '$GITHUBTICKETS' in the Query.
     1674                var $warning = $("#warning.system-message:contains('GITHUBTICKETS')");
     1675                if ( $warning.length ) {
     1676                    renderReportLoadGitHubTickets( $warning );
     1677                }
     1678
    16681679                // This seems to be the easiest place to find the current Ticket ID..
    16691680                var canonical = $( 'link[rel="canonical"]' ).prop( 'href' );
     
    16721683                }
    16731684
    1674                 if ( ! trac || ! ticket ) {
     1685                if ( ! ticket ) {
    16751686                    return;
    16761687                }
     
    17331744                    return ! $div.hasClass( 'collapsed' );
    17341745                } );
     1746            }
     1747
     1748            function renderReportLoadGitHubTickets( $warning ) {
     1749                var user = wpTracCurrentUser,
     1750                    match = document.location.search.match( /USER=([^&]+)/ );
     1751                if ( match ) {
     1752                    user = match[1];
     1753                }
     1754
     1755                // Logged out requests without a user context.
     1756                if ( 'anonymous' === user ) {
     1757                    $warning.remove();
     1758                    return;
     1759                }
     1760
     1761                $warning.html(
     1762                    '<strong>Warning:</strong> Tickets with an attached GitHub PRs not included <button>Load PRs</button>' 
     1763                );
     1764
     1765                $warning.on( 'click', function() {
     1766                    $(this).find('button').prop( 'disabled', 'disabled' ).text( 'Please wait..' );
     1767
     1768                    $.ajax(
     1769                        apiEndpoint
     1770                            + '?trac=' + trac
     1771                            + '&author=' + user
     1772                            + ( authenticated ? '&authenticated=1' : '' )
     1773                    ).success( function( ticketList ) {
     1774                        document.location = document.location.toString() +
     1775                            ( document.location.search ? '&' : '?' ) +
     1776                            'GITHUBTICKETS=' + ticketList.join(',');
     1777                    } );
     1778                } )
     1779
    17351780            }
    17361781
Note: See TracChangeset for help on using the changeset viewer.