Connector Synchronize REST API - Automating the Synchronization Process

  • 1
  • Problem
  • Updated 3 years ago
We are getting an error "System.HttpResponse[Status=Forbidden, StatusCode=403]", on comment synchronization. 
Photo of Fahad Ali

Fahad Ali

  • 49 Posts
  • 0 Reply Likes

Posted 3 years ago

  • 1
Photo of Sattesh Maran

Sattesh Maran

  • 8 Posts
  • 0 Reply Likes
Hey Fahad,

Can you please check to see if the user profile in JIRA has sufficient permission to use the connector?

As stated in the documentations here, the user in JIRA must have Browse Project, Create Issues and Edit Issues permissions for the relevant projects.The user should also have Add Comment, if comments are also being synced.

You can also refer to this previous thread for more information.

Thank you.

Regards,
Sattesh
(Edited)
Photo of Fahad Ali

Fahad Ali

  • 49 Posts
  • 0 Reply Likes
Thanks for your reply.

I am using admin user. It has all permissions. Still getting error.
Photo of Sattesh Maran

Sattesh Maran

  • 8 Posts
  • 0 Reply Likes
Hey Fahad,

There is a possibility that even the admin user might not have all the permission needed.

Can I please get a full screenshot of your "Users and Roles" and "Permissions" page.

You can find the pages at Settings(the cog icon)>Projects>"Name-of-your-Project"
"Users and Roles" and "Permissions" should be on the left hand side panel of the project page.

Thank you.

Regards, 
Sattesh
(Edited)
Photo of Fahad Ali

Fahad Ali

  • 49 Posts
  • 0 Reply Likes
Photo of Fahad Ali

Fahad Ali

  • 49 Posts
  • 0 Reply Likes
Photo of Fahad Ali

Fahad Ali

  • 49 Posts
  • 0 Reply Likes
Photo of Fahad Ali

Fahad Ali

  • 49 Posts
  • 0 Reply Likes
Photo of Fahad Ali

Fahad Ali

  • 49 Posts
  • 0 Reply Likes
I have attached the required snapshots
Photo of Sattesh Maran

Sattesh Maran

  • 8 Posts
  • 0 Reply Likes
Hey Fahad,

Can you please confirm that you are logged in as user "Sprout Tester" or any other user that is in the "jira-administrator" group.
Just click on the icon at the top right corner of JIRA > Profile and check your username.

Also, are you only getting this error during comment synchronization or does it affect other cases like Issue creation?
You can follow the guide here on issue creation using the REST API.


Thank you.

Regards,
Sattesh
Photo of Fahad Ali

Fahad Ali

  • 49 Posts
  • 0 Reply Likes
I logged in as other user. I am getting this error on comment synchronization. 
Photo of Sattesh Maran

Sattesh Maran

  • 8 Posts
  • 0 Reply Likes
Hey Fahad,

It looks like the user you are logged as now might not be in the "jira-administration" group after all. Another way to just confirm this permission is by

i)  Going to Settings(the cog icon)>Projects>"Name-of-your-                                             Project">Permissions>Permission helper     
ii) Type in the username of the user you are currently logged in as, the issue which         you want the comment to sync with and select these permissions(Browse                   Project, Create IssuesEdit Issues and Add Comment) one by one and clicking     on submit.                                 

You should have a green tick for all your status.

I have some screenshots attached showing where to find the permission helper and what to expect.





I would also like to confirm two more things with you please;

i) Make sure you have logged into JIRA using your username and not your email
ii) Make sure that your JIRA Parent Apex class in Salesforce, that the username is indeed your username and some other user or email. 

If everything is indeed in order, could you please make a copy of all your Apex classes and Triggers and attach them here. You can remove or blank out the password part of the files.

Thank you.

Regards,
Sattesh
(Edited)
Photo of Fahad Ali

Fahad Ali

  • 49 Posts
  • 0 Reply Likes
I have confirmed that, the user belongs to the administrator group by following the steps ,its showing me the green tick on each permission i am attaching the apex code.
Photo of Fahad Ali

Fahad Ali

  • 49 Posts
  • 0 Reply Likes
JIRA parent class

public with sharing class JIRA { 
 
    public static String baseUrl = 'https://jiradev.gracenote.com'; // Base URL of your JIRA instance
    public static String systemId = '2'; // Salesforce Connector System ID in JIRA
    public static String username = 'sdogar';  // JIRA username
    public static String password = 'xxxxxx'; // JIRA password
 
    public static String agentProfileName = 'System Administrator'; // Jira agent profile name in Salesforce
 
    // Constructs Basic Http Authentication header from provided credentials
    public static String authHeader(String u, String p) {
        Blob headerValue = Blob.valueOf(u + ':' + p);
        return 'Basic ' + EncodingUtil.base64Encode(headerValue);
    }
 
    // Sends a request and returns the response
    public static HttpResponse sendRequest(HttpRequest req) {
        Http http = new Http();
        return http.send(req);
    }
 
    // Detects whether current user is not JIRA agent. By calling this you can make sure that
    // infinite loops won't happen in triggers (for instance when synchronizing an issue with JIRA)
    public static Boolean currentUserIsNotJiraAgent() {
        Boolean allow = false;
        List<Profile> jiraAgentProfile = [SELECT Id FROM Profile WHERE Name=:JIRA.agentProfileName];
        if (!jiraAgentProfile.isEmpty()) {
            String jiraProfileAgentId = String.valueOf(jiraAgentProfile[0].id);
            allow = UserInfo.getProfileId() != jiraProfileAgentId;
        }
        return true || Test.isRunningTest();
    }
 
}
Photo of Fahad Ali

Fahad Ali

  • 49 Posts
  • 0 Reply Likes
global class JIRAConnectorWebserviceCalloutSync { 
    @future (callout=true) WebService static void synchronizeWithJIRAIssue(String baseUrl, String systemId, String objectType, String caseId) {
        try {

            HttpRequest req = buildRequest(baseUrl, JIRA.username, JIRA.password, systemId, objectType, caseId);
            JIRA.sendRequest(req);
        } catch(System.CalloutException e) {        
            System.debug(e);
        }
    }
 
    // Constructs request needed to synchronize a JIRA issue from provided parameters.
    //public static HttpRequest buildRequest(String systemId, String objectType, String caseId) {
    public static HttpRequest buildRequest(String baseUrl, String username, String password,
                                           String systemId, String objectType, String caseId) {
      
        
        HttpRequest req = new HttpRequest();
        String basicAuthHeader = JIRA.authHeader(username, password);
        String endpoint = getEndpoint(baseUrl, systemId, objectType, caseId);
        req.setHeader('Authorization', basicAuthHeader);
        system.debug('FA Testing '+basicAuthHeader);
        req.setHeader('Content-Type', 'application/json');
        req.setHeader('Content-Length', '0');
        req.setEndpoint(endpoint);
        req.setMethod('PUT');
        return req;
    }
 
    // Creates the endpoint to synchronize the issue from provided parameters.
    private static String getEndpoint(String baseUrl,String systemId, String objectType, String caseId) {        
        return baseUrl + '/rest/customware/connector/1.0/' + systemId + '/' + objectType +  '/' + caseId + '/issue/synchronize.json';
    }
 
}
Photo of Fahad Ali

Fahad Ali

  • 49 Posts
  • 0 Reply Likes
trigger CaseCommentSyncTrg on CaseComment (after insert, after update) {    
    // Check whether current user is not JIRA agent so that we don't create an infinite loop.
    if (JIRA.currentUserIsNotJiraAgent()) {
        for (CaseComment cc : Trigger.new) { 
            String objectType ='Case'; // Please change this according to the object type.     
            String objectId = cc.ParentId;
            // Calls the actual callout to synchronize with the JIRA issue.
            JIRAConnectorWebserviceCalloutSync.synchronizeWithJIRAIssue(JIRA.baseUrl, JIRA.systemId, objectType, objectId);
        }
    }
 
}
Photo of Sattesh Maran

Sattesh Maran

  • 8 Posts
  • 0 Reply Likes
Hey Fahad, 

To recap, we have confirmed that;

  1. You are logged in as user "sdogar" in JIRA using your username and not your email
  2. Checked that user "sdogar" has Browse Project, Create IssuesEdit Issues and Add Comment permission using the permission checker
  3. You can sync case/issues but cannot sync case comments (The problem)
  4. The username in your Salesforce Apex class is your username and not your email

We have looked at your Apex classes and triggers and everything seems to be in order.

If you are sure everything else you have provided is accurate, please do contact us at support@servicerocket.com so that we can arrange a screen share session to troubleshoot the problem.

Thank you.

Regards,
Sattesh
Photo of Fahad Ali

Fahad Ali

  • 49 Posts
  • 0 Reply Likes
Yes, we are logging in by using username not email and also using this username in Salesforce Apex class.
We are getting issue in comments sync by using API call, but comments are synchronizing successfully by using plugin standard functionality.
Photo of Sattesh Maran

Sattesh Maran

  • 8 Posts
  • 0 Reply Likes
Hey Fahad,

Thanks for confirming that.

Please contact us at support@servicerocket.com so that we can arrange a screen share session to troubleshoot the problem.

Thank you.

Regards,
Sattesh