Advertisement
Guest User

Untitled

a guest
Feb 1st, 2017
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. // Change values in this class according to you JIRA/Salesforce coordinates.
  2.  
  3. public static String baseUrl = 'JIRA URL'; // Base URL of your JIRA instance
  4. public static String systemId = '1'; // Salesforce Connector System ID in JIRA
  5. public static String username = 'your jira username'; // JIRA username
  6. public static String password = 'your jira password'; // JIRA password
  7.  
  8. public static String agentProfileName = 'JIRA Agent'; // Jira agent profile name in Salesforce
  9.  
  10. // Constructs Basic Http Authentication header from provided credentials
  11. public static String authHeader(String u, String p) {
  12. Blob headerValue = Blob.valueOf(u + ':' + p);
  13. return 'Basic ' + EncodingUtil.base64Encode(headerValue);
  14. }
  15.  
  16. // Sends a request and returns the response
  17. public static HttpResponse sendRequest(HttpRequest req) {
  18. Http http = new Http();
  19. return http.send(req);
  20. }
  21.  
  22. // Detects whether current user is not JIRA agent. By calling this you can make sure that
  23. // infinite loops won't happen in triggers (for instance when synchronizing an issue with JIRA)
  24. public static Boolean currentUserIsNotJiraAgent() {
  25. Boolean allow = false;
  26. List<Profile> jiraAgentProfile = [SELECT Id FROM Profile WHERE Name=:JIRA.agentProfileName];
  27. if (!jiraAgentProfile.isEmpty()) {
  28. String jiraProfileAgentId = String.valueOf(jiraAgentProfile[0].id);
  29. allow = UserInfo.getProfileId() != jiraProfileAgentId;
  30. }
  31. return allow || Test.isRunningTest();
  32. }
  33.  
  34. private String issuesJson;
  35.  
  36. // Constructor of this class. Note that a request will be sent to JIRA when this class is constructed.
  37. public JIRAFetchIssuesController(ApexPages.StandardController stdController) {
  38. Case theCase = (Case)stdController.getRecord();
  39. this.issuesJson = getResultJson('Case', theCase.id);
  40. }
  41.  
  42. // Sends request to JIRA and returns the request body which should be a valid JSON.
  43. private static String getResultJson(String objectType, String objectId) {
  44. try {
  45. HttpRequest req = buildRequest(JIRA.baseUrl, JIRA.username, JIRA.password, JIRA.systemId, objectType, objectId);
  46. HttpResponse res = JIRA.sendRequest(req);
  47. return res.getBody();
  48. } catch(System.CalloutException e) {
  49. System.debug(e);
  50. return '';
  51. }
  52. }
  53.  
  54. // Constructs request needed to fetch JIRA issues from provided parameters.
  55. @testVisible private static HttpRequest buildRequest(String baseUrl, String username, String password,
  56. String systemId, String objectType, String objectId) {
  57. HttpRequest req = new HttpRequest();
  58. String basicAuthHeader = JIRA.authHeader(username, password);
  59. String endpoint = getEndpoint(baseUrl, systemId, objectType, objectId);
  60. req.setHeader('Authorization', basicAuthHeader);
  61. req.setHeader('Content-Type','application/json');
  62. req.setMethod('GET');
  63. req.setEndpoint(endpoint);
  64. return req;
  65. }
  66.  
  67. // Creates the endpoint to fetch the issue from provided parameters.
  68. private static String getEndpoint(String baseUrl, String systemId, String objectType, String objectId) {
  69. return baseUrl + '/rest/customware/connector/1.0/' + systemId + '/' + objectType + '/' + objectId + '/issue/fetch.json';
  70. }
  71.  
  72. public String getIssuesJSON() {
  73. return this.issuesJson;
  74. }
  75.  
  76. public List<JIRAIssue> getIssues() {
  77. return (List<JIRAIssue>)JSON.deserialize(this.issuesJson , List<JIRAIssue>.class);
  78. }
  79.  
  80. // JIRA Issue Object.
  81. @testVisible class JIRAIssue {
  82.  
  83. public String summary { get; }
  84. public String project { get; }
  85. public String reporter { get; }
  86. public String key { get;}
  87. public String status { get; }
  88. public String resolution { get; }
  89. public String url { get; }
  90. public String type { get; }
  91. public String assignee { get; }
  92. public String description { get; }
  93. public String priority { get; }
  94. public String due_date { get; }
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement