Search code examples
jirabitbucket-serverapplinksatlassian-plugin-sdk

Access issue between Stash and Jira


I have a small plugin checking several things, if the pull request description contains a valid Jira ticket among other things.

The following code was working fine when tested on Stash 3.2.4, but stopped worked the day after, when we upgraded Stash to 3.3.0 (it might not be the direct cause since the plugin is still under development).

public JiraServiceImpl(ApplicationLinkService applicationLinkService)
{
    this.applicationLinkService = applicationLinkService;
}

private ApplicationLink getJiraApplicationLink()
{
    ApplicationLink applicationLink = applicationLinkService.getPrimaryApplicationLink(JiraApplicationType.class);

    if (applicationLink == null)
    {
        throw new IllegalStateException("Primary JIRA application link does not exist!");
    }

    return applicationLink;
}

public boolean doesIssueExist(IssueKey issueKey) throws CredentialsRequiredException, ResponseException
{
    checkNotNull(issueKey, "issueKey is null");


    final ApplicationLinkRequestFactory fac = getJiraApplicationLink().createAuthenticatedRequestFactory();

    ApplicationLinkRequest req = fac.createRequest(Request.MethodType.GET, "/rest/api/2/issue/"+issueKey.getFullyQualifiedIssueKey());

    return req.execute(new ApplicationLinkResponseHandler<Boolean>()
    {
        @Override
        public Boolean credentialsRequired(Response response) throws ResponseException
        {
            throw new ResponseException(new CredentialsRequiredException(fac, "Token is invalid"));
        }

        @Override
        public Boolean handle(Response response) throws ResponseException
        {
            return response.isSuccessful();
        }
    });
}

An exception is thrown with the message : "You do not have an authorized access token for the remote resource."

I don't have the Stash admin rights and can't go back to 3.2.4. The application link was redone thinking that was the issue, but it's not the case. Testing further, we saw that it was working for those having admin rights, but not for the regular users.

Is there something I can change to fix that issue?


Solution

  • Since we are only in the process of switching to GIT, we don't use it much and did not think to click on a JIRA issue since I did not need to test the application link (it was working for the test users).

    When I clicked on an issue displayed in the commits list, I had a window asking me to allow read and write permissions between Stash and JIRA. Right after doing so, the plugin began to work. I had to do this for two users now, and I hope I won't have to do this for everyone of them when the time comes.