[Rt-commit] rt branch, 4.4/rt-munge-attachments, repushed

Craig Kaiser craig at bestpractical.com
Thu Nov 29 12:37:12 EST 2018


The branch 4.4/rt-munge-attachments was deleted and repushed:
       was 5be71a6d23a6452ff98a173f4ebf5114dfe4129d
       now 64ebf36fc2cfb5cd3dcb4766176aea698506de4d

1: 51b1225b6 ! 1: 34bcb0e99 Create ReplaceAttachments, ReplaceHeader and ReplaceContent Methods
    @@ -31,9 +31,9 @@
     +    return (0, 'No Search string provided') unless $args{Search};
     +
     +    foreach my $header ($self->SplitHeaders) {
    -+        if ( $header =~ /\Q$args{Search}\E/ig ) {
    -+            (my $tag) = $header =~ /^([^\:]+)/;
    -+            my ( $ret, $msg ) = $self->SetHeader($tag, $args{Replacement});
    ++        my ( $tag, $value ) = split /:/, $header, 2;
    ++        if ( $value =~ s/\Q$args{Search}\E/$args{Replacement}/ig ) {
    ++            my ( $ret, $msg ) = $self->SetHeader( $tag, $value );
     +            return ($ret, $msg) unless $ret;
     +        }
     +    }
    @@ -60,8 +60,7 @@
     +
     +    my $content = $self->Content;
     +
    -+    if ( $content ) {
    -+        $content =~ s/\Q$args{Search}\E/$args{Replacement}/ig;
    ++    if ( $content && $content =~ s/\Q$args{Search}\E/$args{Replacement}/ig ) {
     +        my ($ret, $msg) = $self->_Set( Field => 'Content', Value => $content );
     +        return ($ret, $msg);
     +    }
    @@ -105,7 +104,7 @@
     +        OPERATOR        => 'LIKE',
     +        VALUE           => $args{Search},
     +        SUBCLAUSE       => 'Attachments',
    -+    ) unless !$args{Headers};
    ++    ) if $args{Headers};
     +
     +    $self->Limit(
     +        ENTRYAGGREGATOR => 'OR',
    @@ -113,19 +112,44 @@
     +        OPERATOR        => 'LIKE',
     +        VALUE           => $args{Search},
     +        SUBCLAUSE       => 'Attachments',
    -+    ) unless !$args{Content};
    ++    ) if $args{Content};
    ++    $self->Limit(
    ++        FIELD           => 'ContentType',
    ++        OPERATOR        => 'IN',
    ++        VALUE           => ['text/plain', 'text/html'],
    ++    );
    ++    $self->Limit(
    ++        FIELD           => 'ContentEncoding',
    ++        VALUE           => 'none',
    ++    );
     +
    ++    my %tickets;
    ++    my ($ret, $msg);
     +    while (my $attachment = $self->Next) {
    ++        my $content_replaced;
     +        if ( $args{Headers} ) {
    -+            my ($ret, $msg) = $attachment->ReplaceHeaders(Search => $args{Search}, Replacement => $args{Replacement});
    ++            ($ret, $msg) = $attachment->ReplaceHeaders(Search => $args{Search}, Replacement => $args{Replacement});
     +            RT::Logger->error($msg) unless $ret;
    ++            $content_replaced = $ret || $content_replaced;
     +        }
    ++        return ($ret, $msg) unless $ret;
     +        if ( $args{Content} ) {
    -+            my ($ret, $msg) = $attachment->ReplaceContent(Search => $args{Search}, Replacement => $args{Replacement});
    ++            ($ret, $msg) = $attachment->ReplaceContent(Search => $args{Search}, Replacement => $args{Replacement});
     +            RT::Logger->error($msg) unless $ret;
    ++            $content_replaced = $ret || $content_replaced;
     +        }
    ++        return ($ret, $msg) unless $ret;
    ++        my $ticket = $attachment->TransactionObj->TicketObj;
    ++        $tickets{$ticket->Id} ||= $ticket if $content_replaced;
     +    }
    -+    return;
    ++    foreach my $ticket (values %tickets){
    ++        (my $trans, $msg) = $ticket->_NewTransaction(
    ++            Type     => "Munge",
    ++        );
    ++        RT::Logger->error($msg) unless $trans;
    ++    }
    ++    my $count = scalar keys %tickets;
    ++    return (1, "Updated $count ticket's attachment content");
     +}
     +
      RT::Base->_ImportOverlays();
2: c9cfaacec ! 2: df92eea70 Create rt-munge-attachments executable
    @@ -117,7 +117,7 @@
     +use Getopt::Long;
     +GetOptions( \%opts, "help|h", "search=s", "replacement=s", );
     +
    -+if ( $opts{'help'} ) {
    ++if ( $opts{'help'} || !$opts{'search'} ) {
     +    require Pod::Usage;
     +    print Pod::Usage::pod2usage( -verbose => 2 );
     +    exit;
    @@ -145,7 +145,7 @@
     +=head1 DESCRIPTION
     +
     +When a match is found in the Headers column, the header is deleted unless a replacement
    -+value was provided. If a match is found in the Content column then the matching substing
    ++value was provided. If a match is found in the Content column then the matching substring
     +will be replaced with a blank or provided value.
     +
     +=head1 OPTIONS
    @@ -163,4 +163,10 @@
     +attachments table.
     +
     +=back
    ++
    ++=head1 SYNOPSIS
    ++
    ++    $sbin/rt-munge-attachments --search="user1 at example.com" --replace="Ex-Employee"
    ++
    ++=cut
     
4: 8ff98c5c5 ! 3: 521d6783e Add new transaction type for munging attachments
    @@ -11,7 +11,7 @@
          },
     +    Munge => sub {
     +        my $self = shift;
    -+        return $self->loc("Attachment content modified");
    ++        return "Attachment content modified";
     +    },
      );
      
3: 4f1157ffc ! 4: 64ebf36fc Add tests for methods that munge the attachments table
    @@ -59,7 +59,7 @@
     +
     +    $attachments->CleanSlate;
     +
    -+     $attachments->Limit(
    ++    $attachments->Limit(
     +        FIELD           => 'Headers',
     +        OPERATOR        => 'LIKE',
     +        VALUE           => 'API',
    @@ -69,25 +69,44 @@
     +    # Replace attachment value for 'API' in Header col
     +    ($ret, $msg) = $attachments->ReplaceAttachments(Search => 'API', Replacement => 'replacement', Content => 0);
     +    ok $ret, $msg;
    -+
     +    $attachments->CleanSlate;
     +
    -+     $attachments->Limit(
    ++    $attachments->Limit(
     +        FIELD           => 'Headers',
     +        OPERATOR        => 'LIKE',
     +        VALUE           => 'API',
     +    );
     +    is $attachments->Count, 0, 'Found no header with content "API"';
    -+
     +    $attachments->CleanSlate;
     +
    -+     $attachments->Limit(
    ++    $attachments->Limit(
     +        FIELD           => 'Headers',
     +        OPERATOR        => 'LIKE',
     +        VALUE           => 'replacement',
     +    );
     +    is $attachments->Count, 1, 'Found header with content "replacement"';
    ++
    ++    ($ret, $msg) = $attachments->ReplaceAttachments(Search => 'new_value', Replacement => 'replacement', Content => 0);
    ++    ok $ret, $msg;
    ++
    ++    $attachments->CleanSlate;
    ++    $attachments->Limit(
    ++        FIELD           => 'Content',
    ++        OPERATOR        => 'LIKE',
    ++        VALUE           => 'new_value',
    ++    );
    ++    is $attachments->Count, 1, 'Content is not changed when flagged as false';
    ++
    ++    ($ret, $msg) = $attachments->ReplaceAttachments(Search => 'replacement', Replacement => 'new_value', Headers => 0);
    ++    ok $ret, $msg;
    ++
    ++    $attachments->CleanSlate;
    ++    $attachments->Limit(
    ++        FIELD           => 'Headers',
    ++        OPERATOR        => 'LIKE',
    ++        VALUE           => 'replacement',
    ++    );
    ++    is $attachments->Count, 1, 'Headers are not replaced when flagged as false';
     +}
     +
     +done_testing();
    -
5: b50ab76e9 < -:  ------- Record transaction when munging attachment content
6: 5be71a6d2 < -:  ------- Return $ret and $msg from "ReplaceAttachments"



More information about the rt-commit mailing list