use of com.gh4a.utils.ApiHelpers in project gh4a by slapperwan.
the class CommitCommentLoadTask method load.
public static Single<Optional<Intent>> load(Context context, String repoOwner, String repoName, String commitSha, IntentUtils.InitialCommentMarker marker) {
RepositoryCommitService commitService = ServiceFactory.get(RepositoryCommitService.class, false);
RepositoryCommentService commentService = ServiceFactory.get(RepositoryCommentService.class, false);
Single<Commit> commitSingle = commitService.getCommit(repoOwner, repoName, commitSha).map(ApiHelpers::throwOnFailure);
Single<List<GitComment>> commentSingle = ApiHelpers.PageIterator.toSingle(page -> commentService.getCommitComments(repoOwner, repoName, commitSha, page)).cache();
Single<Optional<GitHubFile>> fileSingle = commentSingle.compose(RxUtils.filterAndMapToFirst(c -> marker.matches(c.id(), c.createdAt()))).zipWith(commitSingle, (comment, commit) -> {
if (comment.isPresent()) {
for (GitHubFile commitFile : commit.files()) {
if (commitFile.filename().equals(comment.get().path())) {
return Optional.of(commitFile);
}
}
}
return Optional.absent();
});
return Single.zip(commitSingle, commentSingle, fileSingle, (commit, comments, fileOpt) -> {
GitHubFile file = fileOpt.orNull();
if (file != null && !FileUtils.isImage(file.filename())) {
return Optional.of(CommitDiffViewerActivity.makeIntent(context, repoOwner, repoName, commitSha, file.filename(), file.patch(), comments, -1, -1, false, marker));
} else if (file == null) {
return Optional.of(CommitActivity.makeIntent(context, repoOwner, repoName, commitSha, marker));
}
return Optional.absent();
});
}
use of com.gh4a.utils.ApiHelpers in project gh4a by slapperwan.
the class PullRequestReviewDiffLoadTask method getSingle.
@Override
protected Single<Optional<Intent>> getSingle() {
final PullRequestReviewCommentService service = ServiceFactory.get(PullRequestReviewCommentService.class, false);
final PullRequestReviewService reviewService = ServiceFactory.get(PullRequestReviewService.class, false);
long diffCommentId = Long.parseLong(mDiffId.fileHash);
return ApiHelpers.PageIterator.toSingle(page -> service.getPullRequestComments(mRepoOwner, mRepoName, mPullRequestNumber, page)).compose(RxUtils.filterAndMapToFirst(c -> c.id() == diffCommentId)).flatMap(commentOpt -> commentOpt.flatMap(comment -> {
long reviewId = comment.pullRequestReviewId();
return reviewService.getReview(mRepoOwner, mRepoName, mPullRequestNumber, reviewId).map(ApiHelpers::throwOnFailure);
})).map(reviewOpt -> reviewOpt.map(review -> ReviewActivity.makeIntent(mActivity, mRepoOwner, mRepoName, mPullRequestNumber, review, new IntentUtils.InitialCommentMarker(diffCommentId))));
}
use of com.gh4a.utils.ApiHelpers in project gh4a by slapperwan.
the class RepositoryFragment method loadReadme.
private void loadReadme(boolean force) {
Context context = getActivity();
Long id = mRepository.id();
String repoOwner = mRepository.owner().login();
String repoName = mRepository.name();
RepositoryContentService service = ServiceFactory.get(RepositoryContentService.class, force);
service.getReadmeHtml(repoOwner, repoName, mRef).map(ApiHelpers::throwOnFailure).map(Optional::of).compose(RxUtils.mapFailureToValue(HttpURLConnection.HTTP_NOT_FOUND, Optional.<String>absent())).map(htmlOpt -> {
if (htmlOpt.isPresent()) {
String html = HtmlUtils.rewriteRelativeUrls(htmlOpt.get(), repoOwner, repoName, mRef != null ? mRef : mRepository.defaultBranch());
mImageGetter.encode(context, id, html);
return Optional.of(html);
}
return Optional.<String>absent();
}).compose(makeLoaderSingle(ID_LOADER_README, force)).doOnSubscribe(disposable -> {
mIsReadmeLoaded = false;
updateReadmeVisibility();
}).subscribe(readmeOpt -> {
if (readmeOpt.isPresent()) {
mReadmeView.setMovementMethod(UiUtils.CHECKING_LINK_METHOD);
mImageGetter.bind(mReadmeView, readmeOpt.get(), id);
} else {
mReadmeView.setText(R.string.repo_no_readme);
mReadmeView.setTypeface(Typeface.DEFAULT, Typeface.ITALIC);
}
mIsReadmeLoaded = true;
updateReadmeVisibility();
}, this::handleLoadFailure);
}
use of com.gh4a.utils.ApiHelpers in project gh4a by slapperwan.
the class ReviewFragment method addReaction.
@Override
public Single<Reaction> addReaction(GitHubCommentBase comment, String content) {
final ReactionService service = ServiceFactory.get(ReactionService.class, false);
final ReactionRequest request = ReactionRequest.builder().content(content).build();
final Single<Response<Reaction>> responseSingle = comment instanceof ReviewComment ? service.createPullRequestReviewCommentReaction(mRepoOwner, mRepoName, comment.id(), request) : service.createIssueCommentReaction(mRepoOwner, mRepoName, comment.id(), request);
return responseSingle.map(ApiHelpers::throwOnFailure);
}
use of com.gh4a.utils.ApiHelpers in project gh4a by slapperwan.
the class ReviewFragment method onCreateDataSingle.
@Override
protected Single<List<TimelineItem>> onCreateDataSingle(boolean bypassCache) {
final PullRequestService prService = ServiceFactory.get(PullRequestService.class, bypassCache);
final PullRequestReviewService reviewService = ServiceFactory.get(PullRequestReviewService.class, bypassCache);
final PullRequestReviewCommentService commentService = ServiceFactory.get(PullRequestReviewCommentService.class, bypassCache);
Single<TimelineItem.TimelineReview> reviewItemSingle = reviewService.getReview(mRepoOwner, mRepoName, mIssueNumber, mReview.id()).map(ApiHelpers::throwOnFailure).map(TimelineItem.TimelineReview::new);
Single<List<ReviewComment>> reviewCommentsSingle = ApiHelpers.PageIterator.toSingle(page -> reviewService.getReviewComments(mRepoOwner, mRepoName, mIssueNumber, mReview.id())).compose(RxUtils.sortList(ApiHelpers.COMMENT_COMPARATOR)).cache();
Single<Boolean> hasCommentsSingle = reviewCommentsSingle.map(comments -> !comments.isEmpty());
Single<Optional<List<GitHubFile>>> filesSingle = hasCommentsSingle.flatMap(hasComments -> {
if (!hasComments) {
return Single.just(Optional.absent());
}
return ApiHelpers.PageIterator.toSingle(page -> prService.getPullRequestFiles(mRepoOwner, mRepoName, mIssueNumber, page)).map(Optional::of);
});
Single<Optional<List<ReviewComment>>> commentsSingle = hasCommentsSingle.flatMap(hasComments -> {
if (!hasComments) {
return Single.just(Optional.absent());
}
return ApiHelpers.PageIterator.toSingle(page -> commentService.getPullRequestComments(mRepoOwner, mRepoName, mIssueNumber, page)).compose(RxUtils.sortList(ApiHelpers.COMMENT_COMPARATOR)).map(Optional::of);
});
return Single.zip(reviewItemSingle, reviewCommentsSingle, filesSingle, commentsSingle, (reviewItem, reviewComments, filesOpt, commentsOpt) -> {
if (!reviewComments.isEmpty()) {
HashMap<String, GitHubFile> filesByName = new HashMap<>();
if (filesOpt.isPresent()) {
for (GitHubFile file : filesOpt.get()) {
filesByName.put(file.filename(), file);
}
}
// Add all of the review comments to the review item creating necessary diff hunks
for (ReviewComment reviewComment : reviewComments) {
GitHubFile file = filesByName.get(reviewComment.path());
reviewItem.addComment(reviewComment, file, true);
}
if (commentsOpt.isPresent()) {
for (ReviewComment commitComment : commentsOpt.get()) {
if (reviewComments.contains(commitComment)) {
continue;
}
// Rest of the comments should be added only if they are under the same
// diff hunks as the original review comments.
GitHubFile file = filesByName.get(commitComment.path());
reviewItem.addComment(commitComment, file, false);
}
}
}
List<TimelineItem> items = new ArrayList<>();
items.add(reviewItem);
List<TimelineItem.Diff> diffHunks = new ArrayList<>(reviewItem.getDiffHunks());
Collections.sort(diffHunks);
for (TimelineItem.Diff diffHunk : diffHunks) {
items.add(diffHunk);
items.addAll(diffHunk.comments);
if (!diffHunk.isReply()) {
items.add(new TimelineItem.Reply(diffHunk.getInitialTimelineComment()));
}
}
return items;
});
}
Aggregations