Search in sources :

Example 1 with PullRequestService

use of com.meisolsson.githubsdk.service.pull_request.PullRequestService in project gh4a by slapperwan.

the class PullRequestActivity method updatePullRequestState.

private void updatePullRequestState(boolean open) {
    @StringRes int dialogMessageResId = open ? R.string.opening_msg : R.string.closing_msg;
    @StringRes int errorMessageResId = open ? R.string.issue_error_reopen : R.string.issue_error_close;
    String errorMessage = getString(errorMessageResId, mPullRequest.number());
    PullRequestService service = ServiceFactory.get(PullRequestService.class, false);
    EditPullRequest request = EditPullRequest.builder().state(open ? ApiHelpers.IssueState.OPEN : ApiHelpers.IssueState.CLOSED).build();
    service.editPullRequest(mRepoOwner, mRepoName, mPullRequestNumber, request).map(ApiHelpers::throwOnFailure).compose(RxUtils.wrapForBackgroundTask(this, dialogMessageResId, errorMessage)).subscribe(result -> {
        mPullRequest = result;
        handlePullRequestUpdate();
    }, error -> handleActionFailure("Updating pull request failed", error));
}
Also used : PullRequestService(com.meisolsson.githubsdk.service.pull_request.PullRequestService) EditPullRequest(com.meisolsson.githubsdk.model.request.pull_request.EditPullRequest) StringRes(android.support.annotation.StringRes) ApiHelpers(com.gh4a.utils.ApiHelpers)

Example 2 with PullRequestService

use of com.meisolsson.githubsdk.service.pull_request.PullRequestService in project gh4a by slapperwan.

the class PullRequestFilesFragment method loadFiles.

private void loadFiles(boolean force) {
    final PullRequestService service = ServiceFactory.get(PullRequestService.class, force);
    ApiHelpers.PageIterator.toSingle(page -> service.getPullRequestFiles(mRepoOwner, mRepoName, mPullRequestNumber, page)).compose(makeLoaderSingle(ID_LOADER_FILES, force)).subscribe(result -> {
        mFiles = result;
        populateUiIfReady();
    }, this::handleLoadFailure);
}
Also used : PullRequestService(com.meisolsson.githubsdk.service.pull_request.PullRequestService)

Example 3 with PullRequestService

use of com.meisolsson.githubsdk.service.pull_request.PullRequestService 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;
    });
}
Also used : CoordinatorLayout(android.support.design.widget.CoordinatorLayout) IssueCommentService(com.meisolsson.githubsdk.service.issues.IssueCommentService) Bundle(android.os.Bundle) PullRequestReviewService(com.meisolsson.githubsdk.service.pull_request.PullRequestReviewService) GitHubFile(com.meisolsson.githubsdk.model.GitHubFile) CreateReviewComment(com.meisolsson.githubsdk.model.request.pull_request.CreateReviewComment) FrameLayout(android.widget.FrameLayout) GitHubCommentBase(com.meisolsson.githubsdk.model.GitHubCommentBase) Intent(android.content.Intent) StringRes(android.support.annotation.StringRes) HashMap(java.util.HashMap) Response(retrofit2.Response) Single(io.reactivex.Single) ArrayList(java.util.ArrayList) PullRequestReviewCommentService(com.meisolsson.githubsdk.service.pull_request.PullRequestReviewCommentService) Reaction(com.meisolsson.githubsdk.model.Reaction) PullRequestService(com.meisolsson.githubsdk.service.pull_request.PullRequestService) R(com.gh4a.R) TimelineItemAdapter(com.gh4a.adapter.timeline.TimelineItemAdapter) View(android.view.View) IntentUtils(com.gh4a.utils.IntentUtils) Review(com.meisolsson.githubsdk.model.Review) ApiHelpers(com.gh4a.utils.ApiHelpers) ReactionService(com.meisolsson.githubsdk.service.reactions.ReactionService) LayoutInflater(android.view.LayoutInflater) ReviewComment(com.meisolsson.githubsdk.model.ReviewComment) EditPullRequestCommentActivity(com.gh4a.activities.EditPullRequestCommentActivity) ReactionRequest(com.meisolsson.githubsdk.model.request.ReactionRequest) ViewGroup(android.view.ViewGroup) RecyclerView(android.support.v7.widget.RecyclerView) RootAdapter(com.gh4a.adapter.RootAdapter) List(java.util.List) AlertDialog(android.support.v7.app.AlertDialog) RxUtils(com.gh4a.utils.RxUtils) EditorBottomSheet(com.gh4a.widget.EditorBottomSheet) Optional(com.gh4a.utils.Optional) EditIssueCommentActivity(com.gh4a.activities.EditIssueCommentActivity) TimelineItem(com.gh4a.model.TimelineItem) Nullable(android.support.annotation.Nullable) ServiceFactory(com.gh4a.ServiceFactory) Activity(android.app.Activity) Collections(java.util.Collections) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PullRequestService(com.meisolsson.githubsdk.service.pull_request.PullRequestService) GitHubFile(com.meisolsson.githubsdk.model.GitHubFile) ArrayList(java.util.ArrayList) List(java.util.List) PullRequestReviewCommentService(com.meisolsson.githubsdk.service.pull_request.PullRequestReviewCommentService) Optional(com.gh4a.utils.Optional) CreateReviewComment(com.meisolsson.githubsdk.model.request.pull_request.CreateReviewComment) ReviewComment(com.meisolsson.githubsdk.model.ReviewComment) TimelineItem(com.gh4a.model.TimelineItem) PullRequestReviewService(com.meisolsson.githubsdk.service.pull_request.PullRequestReviewService)

Example 4 with PullRequestService

use of com.meisolsson.githubsdk.service.pull_request.PullRequestService in project gh4a by slapperwan.

the class PullRequestFragment method onCreateDataSingle.

@Override
protected Single<List<TimelineItem>> onCreateDataSingle(boolean bypassCache) {
    final int issueNumber = mIssue.number();
    final IssueEventService eventService = ServiceFactory.get(IssueEventService.class, bypassCache);
    final IssueCommentService commentService = ServiceFactory.get(IssueCommentService.class, bypassCache);
    final PullRequestService prService = ServiceFactory.get(PullRequestService.class, bypassCache);
    final PullRequestReviewService reviewService = ServiceFactory.get(PullRequestReviewService.class, bypassCache);
    final PullRequestReviewCommentService prCommentService = ServiceFactory.get(PullRequestReviewCommentService.class, bypassCache);
    Single<List<TimelineItem>> issueCommentItemSingle = ApiHelpers.PageIterator.toSingle(page -> commentService.getIssueComments(mRepoOwner, mRepoName, issueNumber, page)).compose(RxUtils.mapList(TimelineItem.TimelineComment::new));
    Single<List<TimelineItem>> eventItemSingle = ApiHelpers.PageIterator.toSingle(page -> eventService.getIssueEvents(mRepoOwner, mRepoName, issueNumber, page)).compose(RxUtils.filter(event -> INTERESTING_EVENTS.contains(event.event()))).compose((RxUtils.mapList(TimelineItem.TimelineEvent::new)));
    Single<Map<String, GitHubFile>> filesByNameSingle = ApiHelpers.PageIterator.toSingle(page -> prService.getPullRequestFiles(mRepoOwner, mRepoName, issueNumber, page)).map(files -> {
        Map<String, GitHubFile> filesByName = new HashMap<>();
        for (GitHubFile file : files) {
            filesByName.put(file.filename(), file);
        }
        return filesByName;
    }).cache();
    Single<List<Review>> reviewSingle = ApiHelpers.PageIterator.toSingle(page -> reviewService.getReviews(mRepoOwner, mRepoName, issueNumber, page)).cache();
    Single<List<ReviewComment>> prCommentSingle = ApiHelpers.PageIterator.toSingle(page -> prCommentService.getPullRequestComments(mRepoOwner, mRepoName, issueNumber, page)).compose(RxUtils.sortList(ApiHelpers.COMMENT_COMPARATOR)).cache();
    Single<LongSparseArray<List<ReviewComment>>> reviewCommentsByIdSingle = reviewSingle.compose(RxUtils.filter(r -> r.state() == ReviewState.Pending)).toObservable().flatMap(reviews -> {
        List<Observable<Pair<Long, List<ReviewComment>>>> obsList = new ArrayList<>();
        for (Review r : reviews) {
            Single<List<ReviewComment>> single = ApiHelpers.PageIterator.toSingle(page -> reviewService.getReviewComments(mRepoOwner, mRepoName, issueNumber, r.id()));
            obsList.add(Single.zip(Single.just(r.id()), single, Pair::create).toObservable());
        }
        return Observable.concat(obsList);
    }).toList().map(list -> {
        LongSparseArray<List<ReviewComment>> result = new LongSparseArray<>();
        for (Pair<Long, List<ReviewComment>> pair : list) {
            result.put(pair.first, pair.second);
        }
        return result;
    });
    Single<List<TimelineItem.TimelineReview>> reviewTimelineSingle = Single.zip(reviewSingle, filesByNameSingle, prCommentSingle, reviewCommentsByIdSingle, (prReviews, filesByName, commitComments, pendingCommentsById) -> {
        LongSparseArray<TimelineItem.TimelineReview> reviewsById = new LongSparseArray<>();
        List<TimelineItem.TimelineReview> reviews = new ArrayList<>();
        for (Review review : prReviews) {
            TimelineItem.TimelineReview timelineReview = new TimelineItem.TimelineReview(review);
            reviewsById.put(review.id(), timelineReview);
            reviews.add(timelineReview);
            if (review.state() == ReviewState.Pending) {
                for (ReviewComment pendingComment : pendingCommentsById.get(review.id())) {
                    GitHubFile commitFile = filesByName.get(pendingComment.path());
                    timelineReview.addComment(pendingComment, commitFile, true);
                }
            }
        }
        Map<String, TimelineItem.TimelineReview> reviewsBySpecialId = new HashMap<>();
        for (ReviewComment commitComment : commitComments) {
            GitHubFile file = filesByName.get(commitComment.path());
            if (commitComment.pullRequestReviewId() != null) {
                String id = TimelineItem.Diff.getDiffHunkId(commitComment);
                TimelineItem.TimelineReview review = reviewsBySpecialId.get(id);
                if (review == null) {
                    review = reviewsById.get(commitComment.pullRequestReviewId());
                    reviewsBySpecialId.put(id, review);
                }
                review.addComment(commitComment, file, true);
            }
        }
        return reviews;
    }).compose(RxUtils.filter(item -> {
        // noinspection CodeBlock2Expr
        return item.review().state() != ReviewState.Commented || !TextUtils.isEmpty(item.review().body()) || !item.getDiffHunks().isEmpty();
    }));
    Single<List<TimelineItem.TimelineComment>> commitCommentWithoutReviewSingle = Single.zip(prCommentSingle.compose(RxUtils.filter(comment -> comment.pullRequestReviewId() == 0)), filesByNameSingle, (comments, files) -> {
        List<TimelineItem.TimelineComment> items = new ArrayList<>();
        for (ReviewComment comment : comments) {
            items.add(new TimelineItem.TimelineComment(comment, files.get(comment.path())));
        }
        return items;
    });
    return Single.zip(issueCommentItemSingle, eventItemSingle, reviewTimelineSingle, commitCommentWithoutReviewSingle, (comments, events, reviewItems, commentsWithoutReview) -> {
        ArrayList<TimelineItem> result = new ArrayList<>();
        result.addAll(comments);
        result.addAll(events);
        result.addAll(reviewItems);
        result.addAll(commentsWithoutReview);
        Collections.sort(result, TimelineItem.COMPARATOR);
        return result;
    });
}
Also used : HttpURLConnection(java.net.HttpURLConnection) IssueCommentService(com.meisolsson.githubsdk.service.issues.IssueCommentService) Bundle(android.os.Bundle) PullRequestReviewService(com.meisolsson.githubsdk.service.pull_request.PullRequestReviewService) Status(com.meisolsson.githubsdk.model.Status) GitHubFile(com.meisolsson.githubsdk.model.GitHubFile) RepositoryStatusService(com.meisolsson.githubsdk.service.repositories.RepositoryStatusService) PullRequest(com.meisolsson.githubsdk.model.PullRequest) PullRequestReviewCommentService(com.meisolsson.githubsdk.service.pull_request.PullRequestReviewCommentService) IssueState(com.meisolsson.githubsdk.model.IssueState) Map(java.util.Map) Issue(com.meisolsson.githubsdk.model.Issue) View(android.view.View) AttrRes(android.support.annotation.AttrRes) PullRequestBranchInfoView(com.gh4a.widget.PullRequestBranchInfoView) IntentUtils(com.gh4a.utils.IntentUtils) Review(com.meisolsson.githubsdk.model.Review) Set(java.util.Set) EditPullRequestCommentActivity(com.gh4a.activities.EditPullRequestCommentActivity) List(java.util.List) RxUtils(com.gh4a.utils.RxUtils) GitService(com.meisolsson.githubsdk.service.git.GitService) Optional(com.gh4a.utils.Optional) EditIssueCommentActivity(com.gh4a.activities.EditIssueCommentActivity) TimelineItem(com.gh4a.model.TimelineItem) ReviewState(com.meisolsson.githubsdk.model.ReviewState) Repository(com.meisolsson.githubsdk.model.Repository) LongSparseArray(android.support.v4.util.LongSparseArray) Pair(android.util.Pair) GitHubCommentBase(com.meisolsson.githubsdk.model.GitHubCommentBase) Intent(android.content.Intent) HashMap(java.util.HashMap) GitReference(com.meisolsson.githubsdk.model.git.GitReference) Response(retrofit2.Response) Single(io.reactivex.Single) MenuItem(android.view.MenuItem) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) PullRequestService(com.meisolsson.githubsdk.service.pull_request.PullRequestService) MenuInflater(android.view.MenuInflater) CommitStatusBox(com.gh4a.widget.CommitStatusBox) IssueEventService(com.meisolsson.githubsdk.service.issues.IssueEventService) Menu(android.view.Menu) R(com.gh4a.R) Observable(io.reactivex.Observable) CreateGitReference(com.meisolsson.githubsdk.model.request.git.CreateGitReference) ApiHelpers(com.gh4a.utils.ApiHelpers) Iterator(java.util.Iterator) PullRequestMarker(com.meisolsson.githubsdk.model.PullRequestMarker) ReviewComment(com.meisolsson.githubsdk.model.ReviewComment) TextUtils(android.text.TextUtils) AlertDialog(android.support.v7.app.AlertDialog) ServiceFactory(com.gh4a.ServiceFactory) Comparator(java.util.Comparator) Collections(java.util.Collections) IssueCommentService(com.meisolsson.githubsdk.service.issues.IssueCommentService) LongSparseArray(android.support.v4.util.LongSparseArray) ArrayList(java.util.ArrayList) Review(com.meisolsson.githubsdk.model.Review) PullRequestService(com.meisolsson.githubsdk.service.pull_request.PullRequestService) GitHubFile(com.meisolsson.githubsdk.model.GitHubFile) List(java.util.List) ArrayList(java.util.ArrayList) IssueEventService(com.meisolsson.githubsdk.service.issues.IssueEventService) PullRequestReviewCommentService(com.meisolsson.githubsdk.service.pull_request.PullRequestReviewCommentService) Pair(android.util.Pair) TimelineItem(com.gh4a.model.TimelineItem) ReviewComment(com.meisolsson.githubsdk.model.ReviewComment) Observable(io.reactivex.Observable) Map(java.util.Map) HashMap(java.util.HashMap) PullRequestReviewService(com.meisolsson.githubsdk.service.pull_request.PullRequestReviewService)

Example 5 with PullRequestService

use of com.meisolsson.githubsdk.service.pull_request.PullRequestService in project gh4a by slapperwan.

the class PullRequestDiffCommentLoadTask method getSingle.

@Override
protected Single<Optional<Intent>> getSingle() {
    PullRequestService service = ServiceFactory.get(PullRequestService.class, false);
    Single<PullRequest> pullRequestSingle = service.getPullRequest(mRepoOwner, mRepoName, mPullRequestNumber).map(ApiHelpers::throwOnFailure);
    final PullRequestReviewCommentService commentService = ServiceFactory.get(PullRequestReviewCommentService.class, false);
    Single<List<ReviewComment>> commentsSingle = ApiHelpers.PageIterator.toSingle(page -> commentService.getPullRequestComments(mRepoOwner, mRepoName, mPullRequestNumber, page)).compose(RxUtils.filter(c -> c.position() != null)).cache();
    Single<List<GitHubFile>> filesSingle = ApiHelpers.PageIterator.toSingle(page -> service.getPullRequestFiles(mRepoOwner, mRepoName, mPullRequestNumber, page));
    return commentsSingle.compose(RxUtils.filterAndMapToFirst(c -> mMarker.matches(c.id(), c.createdAt()))).zipWith(filesSingle, (commentOpt, files) -> commentOpt.map(comment -> {
        for (GitHubFile commitFile : files) {
            if (commitFile.filename().equals(comment.path())) {
                return Pair.create(true, commitFile);
            }
        }
        return Pair.create(comment != null, (GitHubFile) null);
    })).flatMap(result -> {
        if (result.isPresent()) {
            boolean foundComment = result.get().first;
            GitHubFile file = result.get().second;
            if (foundComment && file != null && !FileUtils.isImage(file.filename())) {
                return Single.zip(pullRequestSingle, commentsSingle, (pr, comments) -> {
                    // noinspection CodeBlock2Expr
                    return Optional.of(PullRequestDiffViewerActivity.makeIntent(mActivity, mRepoOwner, mRepoName, mPullRequestNumber, pr.head().sha(), file.filename(), file.patch(), comments, -1, -1, -1, false, mMarker));
                });
            }
            if (foundComment && file == null) {
                return Single.just(Optional.of(PullRequestActivity.makeIntent(mActivity, mRepoOwner, mRepoName, mPullRequestNumber, mPage, mMarker)));
            }
        }
        return Single.just(Optional.absent());
    });
}
Also used : PullRequestService(com.meisolsson.githubsdk.service.pull_request.PullRequestService) FileUtils(com.gh4a.utils.FileUtils) ApiHelpers(com.gh4a.utils.ApiHelpers) GitHubFile(com.meisolsson.githubsdk.model.GitHubFile) ReviewComment(com.meisolsson.githubsdk.model.ReviewComment) PullRequest(com.meisolsson.githubsdk.model.PullRequest) Intent(android.content.Intent) PullRequestDiffViewerActivity(com.gh4a.activities.PullRequestDiffViewerActivity) Single(io.reactivex.Single) PullRequestActivity(com.gh4a.activities.PullRequestActivity) PullRequestReviewCommentService(com.meisolsson.githubsdk.service.pull_request.PullRequestReviewCommentService) VisibleForTesting(android.support.annotation.VisibleForTesting) List(java.util.List) RxUtils(com.gh4a.utils.RxUtils) PullRequestService(com.meisolsson.githubsdk.service.pull_request.PullRequestService) FragmentActivity(android.support.v4.app.FragmentActivity) Pair(android.support.v4.util.Pair) Optional(com.gh4a.utils.Optional) ServiceFactory(com.gh4a.ServiceFactory) IntentUtils(com.gh4a.utils.IntentUtils) PullRequest(com.meisolsson.githubsdk.model.PullRequest) ApiHelpers(com.gh4a.utils.ApiHelpers) GitHubFile(com.meisolsson.githubsdk.model.GitHubFile) List(java.util.List) PullRequestReviewCommentService(com.meisolsson.githubsdk.service.pull_request.PullRequestReviewCommentService)

Aggregations

PullRequestService (com.meisolsson.githubsdk.service.pull_request.PullRequestService)7 ApiHelpers (com.gh4a.utils.ApiHelpers)6 Intent (android.content.Intent)3 ServiceFactory (com.gh4a.ServiceFactory)3 IntentUtils (com.gh4a.utils.IntentUtils)3 Optional (com.gh4a.utils.Optional)3 RxUtils (com.gh4a.utils.RxUtils)3 GitHubFile (com.meisolsson.githubsdk.model.GitHubFile)3 PullRequest (com.meisolsson.githubsdk.model.PullRequest)3 ReviewComment (com.meisolsson.githubsdk.model.ReviewComment)3 PullRequestReviewCommentService (com.meisolsson.githubsdk.service.pull_request.PullRequestReviewCommentService)3 Single (io.reactivex.Single)3 List (java.util.List)3 Bundle (android.os.Bundle)2 StringRes (android.support.annotation.StringRes)2 AlertDialog (android.support.v7.app.AlertDialog)2 View (android.view.View)2 R (com.gh4a.R)2 EditIssueCommentActivity (com.gh4a.activities.EditIssueCommentActivity)2 EditPullRequestCommentActivity (com.gh4a.activities.EditPullRequestCommentActivity)2