Associating task (anchor) with Pull Request

How could I find the pull request that belongs to a given task? I’m listening to TaskUpdatedEvent and need to identify the pull request that the task belongs to. I’m a bit lost as to what info in the TaskAnchor will get me to the pull request the task is associated with.

Here are the Task fields.

image

If I’m not mistaken the context_type value determines if the task belongs to a pull request and the context_id value determines the pull request id?

Have you tried something like this?

Optional<PullRequest> pullRequest = event.getTask().getAnchor().accept(comment ->
        ofNullable(comment.getThread()
                .getCommentable()
                .accept(new AbstractCommentableVisitor<PullRequest>() {
                    @Override
                    public PullRequest visit(@Nonnull PullRequest pullRequest) {
                        return pullRequest;
                    }
                })));
2 Likes

Thanks Kristy! That solves the problem. Within the Listener context I had to use CommentService to fetch the comment based on the task anchor. Otherwise we’d get a LazyInitializationException.