Skip to content
This repository has been archived by the owner on Dec 4, 2018. It is now read-only.

Commit

Permalink
Merge pull request #284 from octokit/fetch-issues
Browse files Browse the repository at this point in the history
Add issue fetching
  • Loading branch information
mdiep committed Feb 17, 2016
2 parents 36dcc14 + b254c8c commit ee557d8
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 2 deletions.
19 changes: 19 additions & 0 deletions OctoKit/OCTClient+Issues.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

#import <OctoKit/OctoKit.h>

typedef NS_ENUM(NSInteger, OCTClientIssueState) {
OCTClientIssueStateOpen,
OCTClientIssueStateClosed,
OCTClientIssueStateAll,
};

@interface OCTClient (Issues)

/// Creates an issue.
Expand All @@ -28,4 +34,17 @@
/// Returns a signal which will send the created `OCTIssue` then complete, or error.
- (RACSignal *)createIssueWithTitle:(NSString *)title body:(NSString *)body assignee:(NSString *)assignee milestone:(NSNumber *)milestone labels:(NSArray *)labels inRepository:(OCTRepository *)repository;

/// Fetch the issues with the given state from the repository.
///
/// repository - The repository whose issues should be fetched. Cannot be nil.
/// state - The state of issues to return.
/// etag - An Etag from a previous request, used to avoid downloading
// unnecessary data. May be nil.
/// since - Only issues updated or created after this date will be fetched.
/// May be nil.
///
/// Returns a signal which will send each `OCTResponse`-wrapped `OCTIssue`s and
/// complete or error.
- (RACSignal *)fetchIssuesForRepository:(OCTRepository *)repository state:(OCTClientIssueState)state notMatchingEtag:(NSString *)etag since:(NSDate *)since;

@end
22 changes: 22 additions & 0 deletions OctoKit/OCTClient+Issues.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "OCTClient+Issues.h"
#import "NSDateFormatter+OCTFormattingAdditions.h"

@implementation OCTClient (Issues)

Expand All @@ -28,4 +29,25 @@ - (RACSignal *)createIssueWithTitle:(NSString *)title body:(NSString *)body assi
return [[self enqueueRequest:request resultClass:OCTIssue.class] oct_parsedResults];
}

- (RACSignal *)fetchIssuesForRepository:(OCTRepository *)repository state:(OCTClientIssueState)state notMatchingEtag:(NSString *)etag since:(NSDate *)since {
NSParameterAssert(repository != nil);

NSMutableDictionary *parameters = [NSMutableDictionary dictionary];

NSDictionary *stateToStateString = @{
@(OCTClientIssueStateOpen): @"open",
@(OCTClientIssueStateClosed): @"closed",
@(OCTClientIssueStateAll): @"all",
};
NSString *stateString = stateToStateString[@(state)];
NSAssert(stateString != nil, @"Unknown state: %@", @(state));

parameters[@"state"] = stateString;
if (since != nil) parameters[@"since"] = [NSDateFormatter oct_stringFromDate:since];

NSString *path = [NSString stringWithFormat:@"repos/%@/%@/issues", repository.ownerLogin, repository.name];
NSURLRequest *request = [self requestWithMethod:@"GET" path:path parameters:parameters notMatchingEtag:etag];
return [self enqueueRequest:request resultClass:OCTIssue.class];
}

@end
11 changes: 11 additions & 0 deletions OctoKit/OCTIssue.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

#import "OCTObject.h"

typedef NS_ENUM(NSInteger, OCTIssueState) {
OCTIssueStateOpen,
OCTIssueStateClosed,
};

@class OCTPullRequest;

// An issue on a repository.
Expand All @@ -26,4 +31,10 @@
// if this issue does not have code attached.
@property (nonatomic, copy, readonly) OCTPullRequest *pullRequest;

/// The state of the issue.
@property (nonatomic, assign, readonly) OCTIssueState state;

/// The issue number.
@property (nonatomic, copy, readonly) NSString *number;

@end
27 changes: 26 additions & 1 deletion OctoKit/OCTIssue.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ + (NSDictionary *)JSONKeyPathsByPropertyKey {
return [super.JSONKeyPathsByPropertyKey mtl_dictionaryByAddingEntriesFromDictionary:@{
@"URL": @"url",
@"HTMLURL": @"html_url",
@"objectID": @"number",
@"pullRequestHTMLURL": @"pull_request.html_url",
}];
}
Expand All @@ -56,4 +55,30 @@ + (NSValueTransformer *)pullRequestHTMLURLJSONTransformer {
return [NSValueTransformer valueTransformerForName:MTLURLValueTransformerName];
}

+ (NSValueTransformer *)numberJSONTransformer {
return [MTLValueTransformer
reversibleTransformerWithForwardBlock:^(NSNumber *num) {
return num.stringValue;
} reverseBlock:^ id (NSString *str) {
if (str == nil) return nil;

return [NSDecimalNumber decimalNumberWithString:str];
}];
}

+ (NSValueTransformer *)stateJSONTransformer {
NSDictionary *statesByName = @{
@"open": @(OCTIssueStateOpen),
@"closed": @(OCTIssueStateClosed),
};

return [MTLValueTransformer
reversibleTransformerWithForwardBlock:^(NSString *stateName) {
return statesByName[stateName];
}
reverseBlock:^(NSNumber *state) {
return [statesByName allKeysForObject:state].lastObject;
}];
}

@end
5 changes: 4 additions & 1 deletion OctoKitTests/OCTIssueSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
@"url": @"https://api.github.com/repos/octocat/Hello-World/issues/1",
@"html_url": @"https://github.com/octocat/Hello-World/issues/1",
@"number": @1347,
@"id": @1,
@"state": @"open",
@"title": @"Found a bug",
@"body": @"I'm having a problem with this.",
Expand Down Expand Up @@ -83,10 +84,12 @@
});

it(@"should initialize", ^{
expect(issue.objectID).to(equal(@"1347"));
expect(issue.objectID).to(equal(@"1"));
expect(issue.URL).to(equal([NSURL URLWithString:@"https://api.github.com/repos/octocat/Hello-World/issues/1"]));
expect(issue.HTMLURL).to(equal([NSURL URLWithString:@"https://github.com/octocat/Hello-World/issues/1"]));
expect(issue.title).to(equal(@"Found a bug"));
expect(@(issue.state)).to(equal(@(OCTIssueStateOpen)));
expect(issue.number).to(equal(@"1347"));

expect(issue.pullRequest).to(beAKindOf(OCTPullRequest.class));
expect(issue.pullRequest.objectID).to(equal(issue.objectID));
Expand Down

0 comments on commit ee557d8

Please sign in to comment.