[Bps-public-commit] rt-extension-pagerduty branch master updated. 72ece970ec2ced098c2f849a9d4e0fd47cbb0aae

BPS Git Server git at git.bestpractical.com
Thu Mar 24 14:53:05 UTC 2022


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "rt-extension-pagerduty".

The branch, master has been updated
       via  72ece970ec2ced098c2f849a9d4e0fd47cbb0aae (commit)
       via  d337bc30fb2362974bba18e2333b2e0bb0987513 (commit)
      from  0ba151c7f1a1ff56256ef7b51398eb617fabe7ec (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 72ece970ec2ced098c2f849a9d4e0fd47cbb0aae
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Thu Mar 24 22:50:46 2022 +0800

    Re-arrange document and tweak a few words
    
    The rearrangement idea generally is to put required RT config into one
    section.

diff --git a/README b/README
index 799f9be..e16f046 100644
--- a/README
+++ b/README
@@ -36,79 +36,88 @@ INSTALLATION
 
             Plugin('RT::Extension::PagerDuty');
 
-    Clear your mason cache
-            rm -rf /opt/rt5/var/mason_data/obj
-
-    Restart your webserver
+        To allow PagerDuty to send data to RT without a referrer:
 
-CONFIGURATION
-    To define the interactions between RT and PagerDuty use the $PagerDuty
-    config option. This option takes the form of:
+            Set( %ReferrerComponents,
+                '/PagerDuty/WebHook.html' => 1,
+            );
 
-        Set(
-            $PagerDuty,
-            {
-                services => {
-                    'PagerDuty Service ID' => {
-                        api_token => 'PagerDuty API Token',
-                        api_user => 'PagerDuty User',
-                        create_queue => 'General',
-                    }
-                },
-                queues => {
-                    'RT Queue Name' => {
-                        service => 'PagerDuty Service ID',
-                        acknowledged => 'open',
-                        resolved => 'resolved',
+        To define the interactions between RT and PagerDuty:
+
+            Set(
+                $PagerDuty,
+                {
+                    services => {
+                        'PagerDuty Service ID' => {
+                            api_token => 'PagerDuty API Token',
+                            api_user => 'PagerDuty User',
+                            create_queue => 'General',
+                        }
+                    },
+                    queues => {
+                        'RT Queue Name' => {
+                            service => 'PagerDuty Service ID',
+                            acknowledged => 'open',
+                            resolved => 'resolved',
+                        }
                     }
                 }
-            }
-        );
-
-    The services section maps a PagerDuty service id to the token and user
-    to use for API access. The api_token and api_user values are required.
-    The optional create_queue value is the RT queue name where new RT
-    tickets should be created if a PagerDuty incident creates a new RT
-    ticket. create_queue defaults to the General queue if not specified. Use
-    '*' as the PagerDuty service id to apply to multiple PagerDuty services.
-
-    The queues section maps an RT queue name to the PagerDuty service where
-    it should trigger new incidents when an RT ticket is created. The
-    service value is required and must be a PagerDuty service id. The
-    acknowledged and resolved optional values indicate what RT ticket status
-    to use when an incident is acknowledged or resolved on PagerDuty. If not
-    specified they default to acknowledged => 'open' and resolved =>
-    'resolved'. Use '*' as the RT queue name to apply to multiple RT queues.
-
-    To get the PagerDuty Service ID login to your PagerDuty account and go
-    to Services -> Service Directory. Click on the Service you want the ID
-    for and the the ID will be at the end of the URL. For example:
-
-        pagerduty.com/service-directory/P3AFFQR
+            );
 
-    the Service ID is P3AFFQR.
+        The services section maps a PagerDuty service id to the token and
+        user to use for API access. The api_token and api_user values are
+        required. The optional create_queue value is the RT queue name where
+        new RT tickets should be created if a PagerDuty incident creates a
+        new RT ticket. create_queue defaults to the General queue if not
+        specified. Use '*' as the PagerDuty Service ID to apply to multiple
+        PagerDuty services.
+
+        The queues section maps an RT queue name to the PagerDuty service
+        where it should trigger new incidents when an RT ticket is created.
+        The service value is required and must be a PagerDuty service id.
+        The acknowledged and resolved optional values indicate what RT
+        ticket status to use when an incident is acknowledged or resolved on
+        PagerDuty. If not specified they default to acknowledged => 'open'
+        and resolved => 'resolved'. Use '*' as the RT Queue Name to apply to
+        multiple RT queues.
+
+        To get the PagerDuty Service ID login to your PagerDuty account and
+        go to Services -> Service Directory. Click on the Service you want
+        the ID for and the the ID will be at the end of the URL. For
+        example:
+
+            pagerduty.com/service-directory/P3AFFQR
+
+        the Service ID is P3AFFQR.
+
+        To create an api_token login to your PagerDuty account and go to
+        Integrations -> API Access Keys. Click the Create New API Key
+        button. Add a description and click Create Key. Copy the key and
+        paste it into the $PagerDuty config as the api_token. You will not
+        be able to view the key again but you can generate a new one if you
+        lose the key.
+
+        The api_user is the email address for a valid PagerDuty user that
+        has access to the PagerDuty Service you are integrating with.
 
-    To create an api_token login to your PagerDuty account and go to
-    Integrations -> API Access Keys. Click the Create New API Key button.
-    Add a description and click Create Key. Copy the key and paste it into
-    the $PagerDuty config as the api_token. You will not be able to view the
-    key again but you can generate a new one if you lose the key.
+    Clear your mason cache
+            rm -rf /opt/rt5/var/mason_data/obj
 
-    The api_user is the email address for a valid PagerDuty user that has
-    access to the PagerDuty Service you are integrating with.
+    Restart your webserver
 
 Scrips
     This extension will install three new Scrips that do not apply to any
     queues: On Acknowledge PagerDuty Acknowledge, On Create PagerDuty
-    Trigger, and On Resolve PagerDuty Resolve.
+    Trigger and On Resolve PagerDuty Resolve.
 
-    Once you have added the configuration you can apply these Scrips to the
-    queues you want to integrate with PagerDuty.
+    You need to apply them to all the queues that integrate with PagerDuty.
 
-CUSTOM FIELDS
+Custom fields
     This extension adds two ticket custom fields: PagerDuty ID and PagerDuty
     URL.
 
+    You need to apply them to all the queues that integrate with PagerDuty.
+
     When an RT ticket creates an incident on PagerDuty or an incident on
     PagerDuty creates an RT ticket the custom fields are automatically
     filled in. The PagerDuty URL links directly to the incident on
@@ -124,28 +133,29 @@ CUSTOM FIELDS
             ],
         );
 
-WEBHOOK USAGE
-    To call the webhook from PagerDuty:
-
-    1. Create an auth token for a user with permissions to create tickets in
-    the PagerDuty create queue. To create an auth token go to Logged in as
-    -> Settings -> Auth Tokens and create a new token.
-    2. Go to the PagerDuty Service Integrations Webhooks
-    3. Add a new webhook, using:
-    https://your.rt.example/PagerDuty/WebHook.html as the webhook URL. Add a
-    custom header with the name Authorization and value 'token #-#-abc123'
-    where '#-#-abc123' is the value for the auth token you created in step
-    one. Currently the only event subscriptions supported are
-    incident.acknowledged, incident.resolved, and incident.triggered.
-    4. WEBHOOK WITH $RestrictReferrer ENABLED - If the RT config setting
-    $RestrictReferrer is enabled then the webhook will not work without
-    allowing it in the config:
-            Set( %ReferrerComponents,
-                '/PagerDuty/WebHook.html' => 1,
-            );
-
-    5. The PagerDuty Send Test Event button will send a message to the
-    webhook but nothing will happen as a result
+Set up a webhook in PagerDuty
+    1. Create an auth token in RT
+        Select or create an RT user that will be used for the webhook, then
+        create an auth token from the user admin page.
+
+        Note that the user needs to be able to create and update tickets,
+        usually you can grant SeeQueue, CreateTicket and ModifyTicket rights
+        to all the queues that integrate with PagerDuty.
+
+    2. Create the WebHook
+        Go to the PagerDuty Service Integrations Webhooks, add a new
+        webhook, use https://your.rt.example/PagerDuty/WebHook.html as the
+        webhook URL, note that you need to replace https://your.rt.example
+        with your real RT instance.
+
+        Add a custom header with the name Authorization and value token
+        #-#-abc123 where #-#-abc123 is the value of the auth token you
+        created in step one. Currently the only event subscriptions
+        supported are incident.acknowledged, incident.resolved and
+        incident.triggered.
+
+        Note that the Send Test Event button sends a ping event, no tickets
+        will be created.
 
 AUTHOR
     Best Practical Solutions, LLC <modules at bestpractical.com>
diff --git a/lib/RT/Extension/PagerDuty.pm b/lib/RT/Extension/PagerDuty.pm
index 4527180..0f766cb 100644
--- a/lib/RT/Extension/PagerDuty.pm
+++ b/lib/RT/Extension/PagerDuty.pm
@@ -54,18 +54,13 @@ Add this line:
 
     Plugin('RT::Extension::PagerDuty');
 
-=item Clear your mason cache
-
-    rm -rf /opt/rt5/var/mason_data/obj
-
-=item Restart your webserver
-
-=back
+To allow PagerDuty to send data to RT without a referrer:
 
-=head1 CONFIGURATION
+    Set( %ReferrerComponents,
+        '/PagerDuty/WebHook.html' => 1,
+    );
 
-To define the interactions between RT and PagerDuty use the C<$PagerDuty> config
-option. This option takes the form of:
+To define the interactions between RT and PagerDuty:
 
     Set(
         $PagerDuty,
@@ -87,54 +82,64 @@ option. This option takes the form of:
         }
     );
 
-The services section maps a PagerDuty service id to the token and user to use
-for API access. The C<api_token> and C<api_user> values are required. The
-optional C<create_queue> value is the RT queue name where new RT tickets should
-be created if a PagerDuty incident creates a new RT ticket. C<create_queue>
-defaults to the General queue if not specified. Use '*' as the PagerDuty service
-id to apply to multiple PagerDuty services.
-
-The queues section maps an RT queue name to the PagerDuty service where it should
-trigger new incidents when an RT ticket is created. The C<service> value is
-required and must be a PagerDuty service id. The C<acknowledged> and C<resolved>
-optional values indicate what RT ticket status to use when an incident is
-acknowledged or resolved on PagerDuty. If not specified they default to
-acknowledged => 'open' and resolved => 'resolved'. Use '*' as the RT queue name
-to apply to multiple RT queues.
-
-To get the PagerDuty Service ID login to your PagerDuty account and go to
-Services -> Service Directory. Click on the Service you want the ID for and the
-the ID will be at the end of the URL. For example:
+The C<services> section maps a PagerDuty service id to the token and user to
+use for API access. The C<api_token> and C<api_user> values are required. The
+optional C<create_queue> value is the RT queue name where new RT tickets
+should be created if a PagerDuty incident creates a new RT ticket.
+C<create_queue> defaults to the General queue if not specified. Use '*' as
+the C<PagerDuty Service ID> to apply to multiple PagerDuty services.
+
+The C<queues> section maps an RT queue name to the PagerDuty service where it
+should trigger new incidents when an RT ticket is created. The C<service>
+value is required and must be a PagerDuty service id. The C<acknowledged> and
+C<resolved> optional values indicate what RT ticket status to use when an
+incident is acknowledged or resolved on PagerDuty. If not specified they
+default to acknowledged => 'open' and resolved => 'resolved'. Use '*' as the
+C<RT Queue Name> to apply to multiple RT queues.
+
+To get the C<PagerDuty Service ID> login to your PagerDuty account and go to
+Services -> Service Directory. Click on the Service you want the ID for and
+the the ID will be at the end of the URL. For example:
 
     pagerduty.com/service-directory/P3AFFQR
 
 the Service ID is P3AFFQR.
 
-To create an api_token login to your PagerDuty account and go to
-Integrations -> API Access Keys. Click the Create New API Key button. Add a
-description and click Create Key. Copy the key and paste it into the $PagerDuty
-config as the api_token. You will not be able to view the key again but you can generate a new
-one if you lose the key.
+To create an C<api_token> login to your PagerDuty account and go to
+Integrations -> API Access Keys. Click the C<Create New API Key> button. Add
+a description and click C<Create Key>. Copy the key and paste it into the
+C<$PagerDuty> config as the C<api_token>. You will not be able to view the
+key again but you can generate a new one if you lose the key.
 
-The api_user is the email address for a valid PagerDuty user that has access to
-the PagerDuty Service you are integrating with.
+The C<api_user> is the email address for a valid PagerDuty user that has
+access to the PagerDuty Service you are integrating with.
+
+=item Clear your mason cache
+
+    rm -rf /opt/rt5/var/mason_data/obj
+
+=item Restart your webserver
+
+=back
 
 =head1 Scrips
 
 This extension will install three new Scrips that do not apply to any queues:
-C<On Acknowledge PagerDuty Acknowledge>, C<On Create PagerDuty Trigger>, and
+C<On Acknowledge PagerDuty Acknowledge>, C<On Create PagerDuty Trigger> and
 C<On Resolve PagerDuty Resolve>.
 
-Once you have added the configuration you can apply these Scrips to the queues
-you want to integrate with PagerDuty.
+You need to apply them to all the queues that integrate with PagerDuty.
+
+=head1 Custom fields
 
-=head1 CUSTOM FIELDS
+This extension adds two ticket custom fields: C<PagerDuty ID> and C<PagerDuty
+URL>.
 
-This extension adds two ticket custom fields: PagerDuty ID and PagerDuty URL.
+You need to apply them to all the queues that integrate with PagerDuty.
 
 When an RT ticket creates an incident on PagerDuty or an incident on PagerDuty
-creates an RT ticket the custom fields are automatically filled in. The PagerDuty
-URL links directly to the incident on PagerDuty.
+creates an RT ticket the custom fields are automatically filled in. The
+PagerDuty URL links directly to the incident on PagerDuty.
 
 If you would like to group the new custom fields in their own PagerDuty group
 you can use the CustomFieldGroupings config option:
@@ -146,35 +151,33 @@ you can use the CustomFieldGroupings config option:
         ],
     );
 
-=head1 WEBHOOK USAGE
-
-To call the webhook from PagerDuty:
+=head1 Set up a webhook in PagerDuty
 
 =over 4
 
+=item 1. Create an auth token in RT
 
-=item 1. Create an auth token for a user with permissions to create tickets in
-the PagerDuty create queue. To create an auth token go to
-Logged in as -> Settings -> Auth Tokens and create a new token.
+Select or create an RT user that will be used for the webhook, then create an
+auth token from the user admin page.
 
-=item 2. Go to the PagerDuty Service Integrations Webhooks
+Note that the user needs to be able to create and update tickets, usually you
+can grant C<SeeQueue>, C<CreateTicket> and C<ModifyTicket> rights to all the
+queues that integrate with PagerDuty.
 
-=item 3. Add a new webhook, using: C<https://your.rt.example/PagerDuty/WebHook.html>
-as the webhook URL. Add a custom header with the name Authorization and value
-'token #-#-abc123' where '#-#-abc123' is the value for the auth token you
-created in step one. Currently the only event subscriptions supported are
-incident.acknowledged, incident.resolved, and incident.triggered.
+=item 2. Create the WebHook
 
-=item 4. WEBHOOK WITH $RestrictReferrer ENABLED - If the RT config setting
-$RestrictReferrer is enabled then the webhook will not work without allowing it
-in the config:
+Go to the PagerDuty Service Integrations Webhooks, add a new webhook, use
+C<https://your.rt.example/PagerDuty/WebHook.html> as the webhook URL, note
+that you need to replace C<https://your.rt.example> with your real RT
+instance.
 
-    Set( %ReferrerComponents,
-        '/PagerDuty/WebHook.html' => 1,
-    );
+Add a custom header with the name C<Authorization> and value
+C<token #-#-abc123> where C<#-#-abc123> is the value of the auth token you
+created in step one. Currently the only event subscriptions supported are
+C<incident.acknowledged>, C<incident.resolved> and C<incident.triggered>.
 
-=item 5. The PagerDuty Send Test Event button will send a message to the webhook
-but nothing will happen as a result
+Note that the C<Send Test Event> button sends a C<ping> event, no tickets will
+be created.
 
 =back
 
commit d337bc30fb2362974bba18e2333b2e0bb0987513
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Thu Mar 24 06:55:27 2022 +0800

    Avoid updating status in PagerDuty if the status does not change
    
    PagerDuty doesn't like updating status with current value and returns
    "Bad Request" with error messages like:
    
        "Incident Already Resolved"

diff --git a/lib/RT/Action/UpdatePagerDuty.pm b/lib/RT/Action/UpdatePagerDuty.pm
index ef04768..a32679a 100644
--- a/lib/RT/Action/UpdatePagerDuty.pm
+++ b/lib/RT/Action/UpdatePagerDuty.pm
@@ -4,7 +4,7 @@ use strict;
 use warnings;
 use base 'RT::Action';
 
-use HTTP::Request::Common qw(POST PUT);
+use HTTP::Request::Common qw(POST PUT GET);
 use LWP::UserAgent;
 
 sub Prepare {
@@ -169,6 +169,29 @@ sub _update {
     #       or do we just assume no pd id means no incident to worry about?
     return 1 unless $pd_id;
 
+    RT->Logger->debug("PagerDuty GET $pd_id");
+    my $req = GET(
+        'https://api.pagerduty.com/incidents/' . $pd_id,
+        'Accept',
+        'application/vnd.pagerduty+json;version=2',
+        'Authorization',
+        "Token token=$token",
+        'Content-Type',
+        'application/json',
+    );
+    my $resp = $ua->request($req);
+    if ( $resp->is_success ) {
+        my $return = JSON::from_json( $resp->decoded_content );
+        if ( $return->{incident}{status} eq $status ) {
+            RT->Logger->debug("PagerDuty incident is already $status");
+            return 1;
+        }
+    }
+    else {
+        RT->Logger->error( 'PagerDuty request failed: ' . $resp->status_line );
+        # Not return here as we still want to try to PUT even if GET fails
+    }
+
     my %content = (
         incident => {
             type   => "incident",
@@ -181,7 +204,7 @@ sub _update {
     RT->Logger->debug("PagerDuty PUT: $content");
 
 # https://developer.pagerduty.com/api-reference/b3A6Mjc0ODE0Mg-update-an-incident
-    my $req = PUT(
+    $req = PUT(
         'https://api.pagerduty.com/incidents/' . $pd_id,
         'Accept',
         'application/vnd.pagerduty+json;version=2',
@@ -194,7 +217,7 @@ sub _update {
         CONTENT => $content
     );
 
-    my $resp = $ua->request($req);
+    $resp = $ua->request($req);
 
     RT->Logger->debug( 'PagerDuty got response: '
             . $resp->status_line . ' '
-----------------------------------------------------------------------

Summary of changes:
 README                           | 168 +++++++++++++++++++++------------------
 lib/RT/Action/UpdatePagerDuty.pm |  29 ++++++-
 lib/RT/Extension/PagerDuty.pm    | 127 ++++++++++++++---------------
 3 files changed, 180 insertions(+), 144 deletions(-)


hooks/post-receive
-- 
rt-extension-pagerduty


More information about the Bps-public-commit mailing list