Skip to content

Instantly share code, notes, and snippets.

@fabriciocolombo
Created July 23, 2014 01:37
Show Gist options
  • Save fabriciocolombo/90df3df1c32d67e47d03 to your computer and use it in GitHub Desktop.
Save fabriciocolombo/90df3df1c32d67e47d03 to your computer and use it in GitHub Desktop.
Parse Jira Response
program Project2;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
DbxJsonUnMarshal,
RestClient,
System.Generics.Collections;
const
{$REGION 'Jira Issue Response'}
JIRA_ISSUE_RESPONSE = '{' +
' "expand": "renderedFields,names,schema,transitions,editmeta,changelog",' +
' "id": "10000",' +
' "self": "http://localhost:8090/jira/rest/api/2/issue/10000",' +
' "key": "MKY-1",' +
' "fields": {' +
' "summary": "First Test Issue",' +
' "issuetype": {' +
' "self": "http://localhost:8090/jira/rest/api/2/issuetype/1",' +
' "id": "1",' +
' "description": "A problem which impairs or prevents the functions of the product.",' +
' "iconUrl": "http://localhost:8090/jira/images/icons/bug.gif",' +
' "name": "Bug",' +
' "subtask": false' +
' },' +
' "status": {' +
' "self": "http://localhost:8090/jira/rest/api/2/status/1",' +
' "description": "The issue is open and ready for the assignee to start work on it.",' +
' "iconUrl": "http://localhost:8090/jira/images/icons/status_open.gif",' +
' "name": "Open",' +
' "id": "1"' +
' },' +
' "labels": ["label1", "label2"],' +
' "votes": {' +
' "self": "http://localhost:8090/jira/rest/api/2/issue/MKY-1/votes",' +
' "votes": 0,' +
' "hasVoted": false' +
' },' +
' "workratio": -1,' +
' "assignee": {' +
' "self": "http://localhost:8090/jira/rest/api/2/user?username=admin",' +
' "name": "admin",' +
' "emailAddress": "admin@example.com",' +
' "avatarUrls": {' +
' "16x16": "http://localhost:8090/jira/secure/useravatar?size=small&avatarId=10062",' +
' "48x48": "http://localhost:8090/jira/secure/useravatar?avatarId=10062"' +
' },' +
' "displayName": "Administrator",' +
' "active": true' +
' },' +
' "fixVersions": [],' +
' "resolution": null,' +
' "attachment": [],' +
' "resolutiondate": null,' +
' "project": {' +
' "self": "http://localhost:8090/jira/rest/api/2/project/MKY",' +
' "id": "10001",' +
' "key": "MKY",' +
' "name": "monkey",' +
' "avatarUrls": {' +
' "16x16": "http://localhost:8090/jira/secure/projectavatar?size=small&pid=10001&avatarId=10011",' +
' "48x48": "http://localhost:8090/jira/secure/projectavatar?pid=10001&avatarId=10011"' +
' }' +
' },' +
' "versions": [],' +
' "environment": null,' +
' "updated": "2011-11-22T09:23:02.302-0300",' +
' "created": "2011-11-22T09:22:59.899-0300",' +
' "priority": {' +
' "self": "http://localhost:8090/jira/rest/api/2/priority/3",' +
' "iconUrl": "http://localhost:8090/jira/images/icons/priority_major.gif",' +
' "name": "Major",' +
' "id": "3"' +
' },' +
' "description": null,' +
' "duedate": null,' +
' "components": [],' +
' "watches": {' +
' "self": "http://localhost:8090/jira/rest/api/2/issue/MKY-1/watchers",' +
' "watchCount": 0,' +
' "isWatching": false' +
' }' +
' }' +
'}';
{$ENDREGION}
type
{$REGION 'Jira DTO Classes'}
TJiraIssueStatus = class
public
name: string;
end;
TJiraIssueType = class
public
name: string;
end;
TJiraIssueFields = class
public
summary: string;
issuetype: TJiraIssueType;
status: TJiraIssueStatus;
labels: TList<string>;
updated: TDateTime;
created: TDateTime;
destructor Destroy; override;
end;
TJiraIssueResponse = class
public
id: Integer;
key: string;
fields: TJiraIssueFields;
destructor Destroy; override;
end;
{$ENDREGION}
{ TJiraIssueFields }
destructor TJiraIssueFields.Destroy;
begin
issuetype.Free;
inherited;
end;
{ TJiraIssueResponse }
destructor TJiraIssueResponse.Destroy;
begin
fields.Free;
inherited;
end;
const
JIRA_URL = 'http://localhost:8090/jira/rest/api/2/issue/';
var
vIssue: TJiraIssueResponse;
begin
try
vIssue := TDbxJsonUnMarshal.FromJson<TJiraIssueResponse>(JIRA_ISSUE_RESPONSE);
//vIssue := vRestClient.Resource(JIRA_URL + '10000').Get<TJiraIssueResponse>;
try
Assert(Assigned(vIssue), 'Issue');
Assert(Assigned(vIssue.Fields), 'Issue.Fields');
Assert(Assigned(vIssue.Fields.Labels), 'Issue.Fields.Labels');
Assert(Assigned(vIssue.Fields.IssueType), 'Issue.Fields.IssueType');
Assert(Assigned(vIssue.Fields.Status), 'Issue.Fields.Status');
Writeln('Issue.Id: ', vIssue.Id);
Writeln('Issue.Key: ', vIssue.Key);
Writeln('Fields.Summary: ', vIssue.Fields.Summary);
Writeln('Fields.Created: ', DateTimeToStr(vIssue.Fields.Created));
Writeln('Fields.Labels.Count: ' , vIssue.Fields.Labels.Count);
Writeln('Fields.Labels.First: ', vIssue.Fields.Labels.First);
Writeln('Fields.IssueType.Name: ', vIssue.Fields.IssueType.Name);
Writeln('Fields.Status.Name: ', vIssue.Fields.Status.Name);
finally
vIssue.Free;
end;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment