Ejemplo n.º 1
0
/** Return the list of user id and committer emails who should get emails */
function lookup_emails_to_send($errors, $buildid, $projectid, $buildtype, $fixes = false, $collectUnregisteredCommitters = false)
{
    require_once 'models/userproject.php';
    $userids = array();
    $committeremails = array();
    // Check if we know to whom we should send the email
    $updatefiles = pdo_query('SELECT author,email,committeremail FROM updatefile AS uf,build2update AS b2u
                            WHERE b2u.updateid=uf.updateid AND b2u.buildid=' . qnum($buildid));
    add_last_sql_error('sendmail', $projectid, $buildid);
    while ($updatefiles_array = pdo_fetch_array($updatefiles)) {
        $author = $updatefiles_array['author'];
        // Skip the Local User, old CVS/SVN issue
        if ($author == 'Local User') {
            continue;
        }
        $emails = array();
        $authorEmail = $updatefiles_array['email'];
        $emails[] = $authorEmail;
        $committerEmail = $updatefiles_array['committeremail'];
        if ($committerEmail != '' && $committerEmail != $authorEmail) {
            $emails[] = $committerEmail;
        }
        foreach ($emails as $email) {
            $UserProject = new UserProject();
            $UserProject->RepositoryCredential = $author;
            $UserProject->ProjectId = $projectid;
            $filled = false;
            if ($email != '') {
                $result = pdo_query('SELECT id FROM ' . qid('user') . " WHERE email='{$email}'");
                if (pdo_num_rows($result) != 0) {
                    $user_array = pdo_fetch_array($result);
                    $id = $user_array['id'];
                    $UserProject->UserId = $id;
                    $filled = $UserProject->FillFromUserId();
                }
            }
            if (!$filled && !$UserProject->FillFromRepositoryCredential()) {
                global $CDASH_WARN_ABOUT_UNREGISTERED_COMMITTERS;
                global $CDASH_TESTING_MODE;
                if (!$CDASH_TESTING_MODE && $CDASH_WARN_ABOUT_UNREGISTERED_COMMITTERS) {
                    $name = $email == '' ? $author : $email;
                    // Daily updates send an email to tell adminsitrator that the user
                    // is not registered but we log anyway
                    add_log('User: '******' is not registered (or has no email) for the project ' . $projectid, 'SendEmail', LOG_WARNING, $projectid, $buildid);
                }
                if ($collectUnregisteredCommitters && $email != '' && !in_array($email, $committeremails)) {
                    $committeremails[] = $email;
                }
                continue;
            }
            // If we already have this user in the array, don't bother checking
            // user preferences again... (avoid below calls to checkEmail*
            // functions)
            if (in_array($UserProject->UserId, $userids)) {
                continue;
            }
            // If the user doesn't want to receive email
            if ($fixes && !$UserProject->EmailSuccess) {
                continue;
            }
            // Check the categories
            if (!checkEmailPreferences($UserProject->EmailCategory, $errors, $fixes)) {
                continue;
            }
            // Check if the labels are defined for this user
            if (!checkEmailLabel($projectid, $UserProject->UserId, $buildid, $UserProject->EmailCategory)) {
                continue;
            }
            $userids[] = $UserProject->UserId;
        }
    }
    // If it's fixes only concerned users should get the email
    if ($fixes) {
        $result = array();
        $result['userids'] = $userids;
        $result['committeremails'] = $committeremails;
        return $result;
    }
    // Select the users who want to receive all emails
    $user = pdo_query('SELECT emailtype,emailcategory,userid FROM user2project WHERE user2project.projectid=' . qnum($projectid) . ' AND user2project.emailtype>1');
    add_last_sql_error('sendmail');
    while ($user_array = pdo_fetch_array($user)) {
        if (in_array($user_array['userid'], $userids)) {
            continue;
        }
        // If the user doesn't want to receive email
        if (!checkEmailPreferences($user_array['emailcategory'], $errors)) {
            continue;
        }
        // Check if the labels are defined for this user
        if (!checkEmailLabel($projectid, $user_array['userid'], $buildid, $user_array['emailcategory'])) {
            continue;
        }
        // Nightly build notification
        if ($user_array['emailtype'] == 2 && $buildtype == 'Nightly') {
            $userids[] = $user_array['userid'];
        } elseif ($user_array['emailtype'] == 3) {
            // want to receive all emails
            $userids[] = $user_array['userid'];
        }
    }
    $result = array();
    $result['userids'] = $userids;
    $result['committeremails'] = $committeremails;
    return $result;
}