Search in sources :

Example 46 with ApiHelpers

use of com.gh4a.utils.ApiHelpers in project gh4a by slapperwan.

the class IssueFragmentBase method onEditorDoSend.

@Override
public Single<?> onEditorDoSend(String comment) {
    IssueCommentService service = ServiceFactory.get(IssueCommentService.class, false);
    CommentRequest request = CommentRequest.builder().body(comment).build();
    return service.createIssueComment(mRepoOwner, mRepoName, mIssue.number(), request).map(ApiHelpers::throwOnFailure);
}
Also used : CommentRequest(com.meisolsson.githubsdk.model.request.CommentRequest) IssueCommentService(com.meisolsson.githubsdk.service.issues.IssueCommentService) ApiHelpers(com.gh4a.utils.ApiHelpers)

Example 47 with ApiHelpers

use of com.gh4a.utils.ApiHelpers in project gh4a by slapperwan.

the class UserPasswordLoginDialogFragment method makeRequestSingle.

private Single<LoginService.AuthorizationRequest> makeRequestSingle() {
    String description = "Octodroid - " + Build.MANUFACTURER + " " + Build.MODEL;
    String fingerprint = getHashedDeviceId();
    LoginService service = getService();
    String scopes = getArguments().getString("scopes");
    return service.getAuthorizations().map(ApiHelpers::throwOnFailure).compose(RxUtils::doInBackground).retryWhen(handler -> handler.flatMap(error -> {
        if (error instanceof ApiRequestException) {
            ApiRequestException are = (ApiRequestException) error;
            if (are.getStatus() == 401 && are.getResponse().message().contains("OTP code")) {
                mWaitingForOtpCode = true;
                mHandler.post(() -> updateContainerVisibility(false));
                // getAuthorizations() doesn't trigger the OTP SMS for whatever reason,
                // so make a dummy create request (which we know will fail) just to
                // actually trigger SMS sending
                LoginService.AuthorizationRequest dummyRequest = new LoginService.AuthorizationRequest("", "dummy", "");
                service.createAuthorization(dummyRequest).compose(RxUtils::doInBackground).subscribe(ignoredResponse -> {
                }, ignoredError -> {
                });
            }
        }
        if (!mWaitingForOtpCode) {
            mRetryProcessor.onError(error);
        }
        return mRetryProcessor;
    })).compose(RxUtils.filter(authorization -> {
        String note = authorization.note();
        return note != null && note.startsWith(description);
    })).flatMap(existingAuthorizations -> {
        Single<Void> deleteSingle = null;
        Iterator<LoginService.AuthorizationResponse> iter = existingAuthorizations.iterator();
        while (iter.hasNext()) {
            LoginService.AuthorizationResponse auth = iter.next();
            if (fingerprint.equals(auth.fingerprint())) {
                deleteSingle = service.deleteAuthorization(auth.id()).map(ApiHelpers::throwOnFailure).compose(RxUtils::doInBackground);
                iter.remove();
            }
        }
        String finalDescription = description;
        if (!existingAuthorizations.isEmpty()) {
            finalDescription += " #" + (existingAuthorizations.size() + 1);
        }
        LoginService.AuthorizationRequest request = new LoginService.AuthorizationRequest(scopes, finalDescription, fingerprint);
        if (deleteSingle != null) {
            return deleteSingle.map(response -> request);
        } else {
            return Single.just(request);
        }
    });
}
Also used : Context(android.content.Context) Bundle(android.os.Bundle) MessageDigest(java.security.MessageDigest) Dialog(android.app.Dialog) NonNull(android.support.annotation.NonNull) DialogFragment(android.support.v4.app.DialogFragment) Single(io.reactivex.Single) Editable(android.text.Editable) TextInputLayout(android.support.design.widget.TextInputLayout) User(com.meisolsson.githubsdk.model.User) Locale(java.util.Locale) Handler(android.os.Handler) R(com.gh4a.R) View(android.view.View) Button(android.widget.Button) Settings(android.provider.Settings) ApiRequestException(com.gh4a.ApiRequestException) Build(android.os.Build) DialogInterface(android.content.DialogInterface) ApiHelpers(com.gh4a.utils.ApiHelpers) Iterator(java.util.Iterator) LayoutInflater(android.view.LayoutInflater) TextUtils(android.text.TextUtils) UserService(com.meisolsson.githubsdk.service.users.UserService) IdRes(android.support.annotation.IdRes) AlertDialog(android.support.v7.app.AlertDialog) RxUtils(com.gh4a.utils.RxUtils) Pair(android.support.v4.util.Pair) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) TextInputEditText(android.support.design.widget.TextInputEditText) ServiceFactory(com.gh4a.ServiceFactory) PublishProcessor(io.reactivex.processors.PublishProcessor) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LoginService(com.gh4a.ServiceFactory.LoginService) TextWatcher(android.text.TextWatcher) RxUtils(com.gh4a.utils.RxUtils) LoginService(com.gh4a.ServiceFactory.LoginService) ApiRequestException(com.gh4a.ApiRequestException) ApiHelpers(com.gh4a.utils.ApiHelpers)

Example 48 with ApiHelpers

use of com.gh4a.utils.ApiHelpers 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)

Example 49 with ApiHelpers

use of com.gh4a.utils.ApiHelpers in project gh4a by slapperwan.

the class PullRequestReviewCommentLoadTask method load.

public static Single<Optional<Intent>> load(Context context, String repoOwner, String repoName, int pullRequestNumber, IntentUtils.InitialCommentMarker marker) {
    final PullRequestReviewService reviewService = ServiceFactory.get(PullRequestReviewService.class, false);
    final PullRequestReviewCommentService commentService = ServiceFactory.get(PullRequestReviewCommentService.class, false);
    return ApiHelpers.PageIterator.toSingle(page -> commentService.getPullRequestComments(repoOwner, repoName, pullRequestNumber, page)).compose(RxUtils.sortList(ApiHelpers.COMMENT_COMPARATOR)).flatMap(comments -> {
        Map<String, ReviewComment> commentsByDiffHunkId = new HashMap<>();
        for (ReviewComment comment : comments) {
            String id = TimelineItem.Diff.getDiffHunkId(comment);
            if (!commentsByDiffHunkId.containsKey(id)) {
                // Because the comment we are looking for could be a reply to another
                // review we have to keep track of initial comments for each diff hunk
                commentsByDiffHunkId.put(id, comment);
            }
            if (marker.matches(comment.id(), null)) {
                // Once found the comment we are looking for get a correct review id from
                // the initial diff hunk comment
                ReviewComment initialComment = commentsByDiffHunkId.get(id);
                long reviewId = initialComment.pullRequestReviewId();
                return reviewService.getReview(repoOwner, repoName, pullRequestNumber, reviewId).map(ApiHelpers::throwOnFailure).map(Optional::of);
            }
        }
        return Single.just(Optional.<Review>absent());
    }).map(reviewOpt -> reviewOpt.map(review -> ReviewActivity.makeIntent(context, repoOwner, repoName, pullRequestNumber, review, marker)));
}
Also used : Context(android.content.Context) ReviewActivity(com.gh4a.activities.ReviewActivity) Review(com.meisolsson.githubsdk.model.Review) PullRequestReviewService(com.meisolsson.githubsdk.service.pull_request.PullRequestReviewService) ApiHelpers(com.gh4a.utils.ApiHelpers) ReviewComment(com.meisolsson.githubsdk.model.ReviewComment) Intent(android.content.Intent) HashMap(java.util.HashMap) Single(io.reactivex.Single) PullRequestReviewCommentService(com.meisolsson.githubsdk.service.pull_request.PullRequestReviewCommentService) VisibleForTesting(android.support.annotation.VisibleForTesting) RxUtils(com.gh4a.utils.RxUtils) FragmentActivity(android.support.v4.app.FragmentActivity) Optional(com.gh4a.utils.Optional) Map(java.util.Map) TimelineItem(com.gh4a.model.TimelineItem) ServiceFactory(com.gh4a.ServiceFactory) IntentUtils(com.gh4a.utils.IntentUtils) ReviewComment(com.meisolsson.githubsdk.model.ReviewComment) ApiHelpers(com.gh4a.utils.ApiHelpers) Review(com.meisolsson.githubsdk.model.Review) HashMap(java.util.HashMap) Map(java.util.Map) PullRequestReviewService(com.meisolsson.githubsdk.service.pull_request.PullRequestReviewService) PullRequestReviewCommentService(com.meisolsson.githubsdk.service.pull_request.PullRequestReviewCommentService)

Example 50 with ApiHelpers

use of com.gh4a.utils.ApiHelpers in project gh4a by slapperwan.

the class CommitNoteFragment method onEditorDoSend.

@Override
public Single<?> onEditorDoSend(String comment) {
    RepositoryCommentService service = ServiceFactory.get(RepositoryCommentService.class, false);
    CreateCommitComment request = CreateCommitComment.builder().body(comment).build();
    return service.createCommitComment(mRepoOwner, mRepoName, mObjectSha, request).map(ApiHelpers::throwOnFailure);
}
Also used : ApiHelpers(com.gh4a.utils.ApiHelpers) RepositoryCommentService(com.meisolsson.githubsdk.service.repositories.RepositoryCommentService) CreateCommitComment(com.meisolsson.githubsdk.model.request.repository.CreateCommitComment)

Aggregations

ApiHelpers (com.gh4a.utils.ApiHelpers)68 Response (retrofit2.Response)13 RxUtils (com.gh4a.utils.RxUtils)12 ServiceFactory (com.gh4a.ServiceFactory)11 Intent (android.content.Intent)10 Bundle (android.os.Bundle)10 Optional (com.gh4a.utils.Optional)10 PullRequestReviewCommentService (com.meisolsson.githubsdk.service.pull_request.PullRequestReviewCommentService)10 Single (io.reactivex.Single)9 Context (android.content.Context)7 LayoutInflater (android.view.LayoutInflater)7 CommentRequest (com.meisolsson.githubsdk.model.request.CommentRequest)7 List (java.util.List)7 View (android.view.View)6 R (com.gh4a.R)6 IntentUtils (com.gh4a.utils.IntentUtils)6 User (com.meisolsson.githubsdk.model.User)6 ReactionRequest (com.meisolsson.githubsdk.model.request.ReactionRequest)6 CreateReviewComment (com.meisolsson.githubsdk.model.request.pull_request.CreateReviewComment)6 ReactionService (com.meisolsson.githubsdk.service.reactions.ReactionService)6