Getting 403 error while triggering jenkins job from forge ui JIRA

Below is the code written forge ui foir jira…It is bale to trigger jenkins job remotely but when I tried from forge ui it is not working

const auth = Buffer.from(`${jenkinsUser}:${jenkinsToken}`).toString('base64');

try {
    const response = await api.asUser().requestJira(buildUrl, {
        method: 'POST',
        headers: {
            'Authorization': `Basic ${auth}`,
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        body: qs.stringify(parameters)
    });

    const responseData = await response.json();

    console.log(`Request URL: ${buildUrl}`);
    console.log(`Response: ${response.status} - ${response.statusText}`);
    console.log(`Response Content: ${JSON.stringify(responseData)}`);

    if (response.status >= 200 && response.status < 300) {
        console.log('Jenkins job triggered successfully.');
    } else {
        console.log(`Failed to trigger Jenkins job: ${response.status} - ${response.statusText}`);
    }
} catch (error) {
    console.error('Exception occurred:', error.message);
}

};

ERROR 2024-07-02T05:24:07.826Z 6e225de8-58c1-4050-a377-17c9237cb8d6 Exception occurred: Request failed with status code 403
INFO 2024-07-02T05:38:32.148Z 6af3cecd-4c51-4462-a6d5-2c06b0ac7c2a Starting application with values: [ ‘Entity’, ‘Task Attributes’ ]
INFO 2024-07-02T05:38:32.149Z 6af3cecd-4c51-4462-a6d5-2c06b0ac7c2a Starting Jenkins job
ERROR 2024-07-02T05:38:32.150Z 6af3cecd-4c51-4462-a6d5-2c06b0ac7c2a Exception occurred: You must create your route using the ‘route’ export from ‘@forge/api’.
See https://go.atlassian.com/forge-fetch-route for more information.
PS C:\Users\kindinti.anumanthu\OneDrive - BLACKLINE\Desktop\Automation\JiraPRTesting> ^C
PS C:\Users\kindinti.anumanthu\OneDrive - BLACKLINE\Desktop\Automation\JiraPRTesting>

Hello again @KindintiAnumanthu, when you use requestJira() you must use the route tagged template literal for your path, i.e.:

await api.asUser().requestJira(route`${buildUrl}`)

You can find the docs here. In addition, api.asUser().requestJira() will by default make an authenticated request to the API (using OAuth). If you wish to provide your own authentication, you should use the alternative API here.

e.g:

await api.requestJira(route`${buildUrl}`)

Now getting the below error

ERROR 2024-07-03T09:36:08.049Z 672e2398-1ee0-4471-aaf9-580937433c1d Exception occurred: Disallowing path manipulation attempt. For more information see: https://go.atlassian.com/product-fetch-api-route

1 Like