How to get the project key from the project ID through programming

We have a custom built auditing plugin that goes through issue keys and then outputs a response depending if the issue key has a certain customfield enabled.

For the plugin to work properly, we need to feed it issue keys in the correct format (like ABCD-1234), meaning the project key followed by the hyphen and number.

These issue keys are fed to the plugin from all over jira (search pages, gadgets, other plugins and so on).

Currently, I am working on trying to extract issue keys from the Roadmap plugin, but the issue is, the projects are stored as project IDs and not ProjectKeys. My question is, what would be a good way to get the corresponding Project Key from the Project ID?

I have heard people mention the use of the ProjectManager interface, but I am not sure how to implement it so the method would accept a projectID and return the corresponding projectKey.

Sorry, I have little experience in Java, all the help is appreciated!

You should be fine with:

import com.atlassian.jira.project.ProjectManager;

//....

    try {
      Project project = projectManager.getProjectObj(Long projectId);
      if (project != null) {
        String projectKey = project.getKey();
      }
    } catch(DataAccessException e) {
      // log stuff or handle exception
    }

Thanks a bunch @clouless , will try it!

Regards

This might also come in handy.
https://docs.atlassian.com/software/jira/docs/api/8.16.2/
Also check this thread out as well: