Making WordPress.org

Changeset 12678


Ignore:
Timestamp:
06/28/2023 02:11:16 AM (3 years ago)
Author:
dd32
Message:

Make: GitHub Invite Tool: Allow specifying a GitHub user URL, and if the user is a member of the org already, just add them to the team(s).

See #7082.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-github-invite
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-github-invite/admin-post.php

    r12671 r12678  
    2121        $team_ids = array_map( 'intval', $team_ids );
    2222
    23         $updated = 'success';
    24         $message = null;
    25         $invite  = false;
     23        $updated         = 'success';
     24        $message         = null;
     25        $invite          = false;
     26        $github_username = false;
    2627
    2728        if ( ! $team_ids ) {
    2829                $updated = 'error';
    2930                $message = 'No teams selected';
    30         } elseif (
    31                 preg_match( '!^https://profiles.wordpress.org/(?<slug>[^/]+)!i', $input, $m ) ||
    32                 ! is_email( $input )
    33         ) {
     31        } elseif ( is_email( $input ) ) {
     32                $invite = $input;
     33        } elseif ( preg_match( '!^https://github.com/(?<slug>[^/]+)!i', $input, $m ) ) {
     34                $gh_user         = api( '/users/' . $m['slug'] );
     35                $github_username = $gh_user->login ?? false;
     36                $invite          = $gh_user->id ?? 0;
     37        } else {
     38                preg_match( '!^https://profiles.wordpress.org/(?<slug>[^/]+)!i', $input, $m );
     39
    3440                $user           = get_user_by( 'slug', $m['slug'] ?? $input );
    35                 $github_details = json_decode( $wpdb->get_var( $wpdb->prepare(
    36                         'SELECT user_details FROM wporg_github_users WHERE user_id = %d',
     41                $github_details = $wpdb->get_row( $wpdb->prepare(
     42                        'SELECT github_user, user_details FROM wporg_github_users WHERE user_id = %d',
    3743                        $user->ID
    38                 ) ) );
     44                ) );
     45                if ( $github_details ) {
     46                        $github_details->user_details = json_decode( $github_details->user_details );
     47                }
    3948
    40                 if ( ! $user || ! $github_details ) {
     49                if ( ! $user || ! $github_details || ! $github_details->user_details ) {
    4150                        $updated = 'no-github';
    4251                } else {
    43                         $invite = $github_details->id;
     52                        $invite          = $github_details->user_details->id;
     53                        $github_username = $github_details->github_user;
    4454                }
    45         } elseif ( is_email( $input ) ) {
    46                 $invite = $input;
    47         } else {
    48                 $updated = 'error';
    4955        }
    5056
    … …  
    5258                $result = invite_member( $invite, $team_ids );
    5359
    54                 if ( $result->id ) {
    55                         // Note that it was invited via this site..
    56                         $invited_gh_users = get_option( 'invited_gh_users', [] );
    57                         $invited_gh_users[] = $result->id;
    58                         update_option( 'invited_gh_users', $invited_gh_users );
     60                if (
     61                        is_wp_error( $result ) &&
     62                        $github_username &&
     63                        'Invitee is already a part of this org' === $result->get_error_data( 'api_error' )->errors[0]->message ?? ''
     64                ) {
     65                        // Already a GitHub member, just add them to the team.
     66                        $result  = add_to_team( $github_username, $team_ids );
     67                        $message = 'User already a GitHub member, Added to team(s).';
     68                }
    5969
    60                         delete_site_transient( 'gh_invites' );
     70                if ( ! is_wp_error( $result ) ) {
     71                        if ( ! empty( $result->id ) ) {
     72                                // Note that it was invited via this site..
     73                                $invited_gh_users = get_option( 'invited_gh_users', [] );
     74                                $invited_gh_users[] = $result->id;
     75                                update_option( 'invited_gh_users', $invited_gh_users );
     76
     77                                delete_site_transient( 'gh_invites' );
     78                        }
    6179
    6280                        // Log it to Slack.
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-github-invite/admin.php

    r12671 r12678  
    8080                <table class="form-table">
    8181                        <tr>
    82                                 <th scope="row"><label for="invite">GitHub Email, WordPress.org user slug, or WordPress.org Profile URL</label></th>
     82                                <th scope="row"><label for="invite">GitHub Email, GitHub URL, WordPress.org user slug, or WordPress.org Profile URL</label></th>
    8383                                <td><input type="text" name="invite" id="invite" class="regular-text" placeholder="https://profiles.wordpress.org/<?php echo wp_get_current_user()->user_nicename; ?>/"></td>
    8484                        </tr>
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-github-invite/api.php

    r12671 r12678  
    8383
    8484/**
     85 * Add an organisation member to a team.
     86 *
     87 * @param int|string $who      The GitHub user login.
     88 * @param array      $team_ids The team IDs to add the user to.
     89 */
     90function add_to_team( $who, array $team_ids ) {
     91        $error = new WP_Error;
     92
     93        foreach ( $team_ids as $team_id ) {
     94                $response = api(
     95                        "/orgs/{ORG}/team/{$team_id}/memberships/{$who}",
     96                        false,
     97                        'PUT'
     98                );
     99
     100                if ( is_wp_error( $response ) ) {
     101                        $error->merge_from( $response );
     102                }
     103        }
     104
     105        if ( $error->get_error_code() ) {
     106                return $error;
     107        }
     108
     109        return $response;
     110}
     111
     112/**
    85113 * Cancel an invitation by ID
    86114 */
    … …  
    92120        );
    93121}
    94 
    95122
    96123/**
Note: See TracChangeset for help on using the changeset viewer.