How to access JIRA issues comments from scheduled job?

Hello. I’m quite new to developing plugins, so please bear with my ignorance.

I was searching for a while to find a way of scheduling a job in my plugin that runs in regular intervals. I have found that you can use the PluginScheduler class from Java API to do that.

What I can’t figure out is how to access JIRA content from the scheduled job ? Say I wanted the job to pull all the comments associated with specific project and do some processing on them. How do I do that ? I’ve gotten the job to run like I wanted, but it’s useless if I can’t actually get it to do anything. I tried to pass JiraAuthenticationContext with Spring’s autowiring to a class implementing LifecycleAware interface, but when I call getLoggedInUser() it returns null.

How do I get the job to do some actual work, like pulling comments, updating issues etc. ? Or am I terribly mistaken about my approach and it should be done differently, or not at all ?

You should think about how you want to get your projects and issues.

When you have a scheduled job you can inject e.g. the ProjectManager into this job and get all currently available projects by calling getProjectObjects(). Then you would have to iterate through them , find what you need and do you work.

When you know your project (maybe by key or by iterating) you could retrieve all issues for it using the SearchService. You can write JQL and search for issues. Again you would have to look for the issues you are interested in by yourself by iterating your search results. You could inject the CommentManager in your job, get all comments for an issue with getComments(Issue issue) and work on them.

Does this help?

Hello. Thank you for your answer.

It doesn’t help that much, because the main problem for me is authenticating the user that gets the projects and issues data. This is a problem because search service requires ApplicationUser against which to test permissions to be passed in when the search method is called. I’m sorry, maybe I haven’t emphasized that enough.

Nevertheless, I have managed to achieve what I wanted, although it’s a temporary solution.

Firstly, I decided to use SchedulerService instead of PluginScheduler. Then I created a helper class that is injected into my implementation of JobRunner. That helper class has injected user manager among other things into it, and it calls getUserByName to get a user to pass to search service which I use to get issues, from which I get comments. Projects are configurable by the user of the plugin, so they are just passed in.

But I say that this is a temporary solution because I call the getUserByName with hardcoded “admin” value for now. Later I will have to deal with this differently, but I still don’t know how to. The only solution that comes to mind is asking the user to input the username in the plugin configuration screen, but this is far from perfect.

So I understand that your problem is to find the correct user who shall be used when your scheduled jobs starts, correct?

I don’t know if is a “elegant” way but you could save the username of the administrator who installed the plugin after installation and before scheduling your jobs using Active Objects. And later use it in your scheduled jobs to perform user depending tasks.

Oh, that I didn’t think about. But that is actually a pretty nice way of handling things.

I will definitely try this. It’s way better than what I thought about. And paired with a condition on a plugin module it accomplishes what i want.