Use Dynamic Pipelines to add step to build pipeline based on pull request diffstat
Asked by Sergei Drevs:

Hi,

my intention is to use the Dynamic Pipelines feature to get the diffstat of the last PR which contains the commit from e.g. \`develop\` and check if there was a change in folder \`migration\`. If there was no change in that folder there is no need to execute the expensive build task.

But I'm already stuck at the example.. <https://developer.atlassian.com/platform/forge/orchestrate-your-builds-using-dynamic-pipelines/#step-1--create-your-app>

I get the error message:

```
{
  "type": "error",
  "error": {
    "message": "Token is invalid or not supported for this endpoint."
  }
}
```

<br />

Code:

```
const requestUrl = route`/2.0/repositories/${workspace}/${repository}/diffstat/${commitHash}`;
const response = await api.asApp().requestBitbucket(requestUrl);
```

<br />

Any ideas what I'm doing wrong?  
Kind regards,  
Sergei


Answers:
author: Caterina Curti
body: Ho [@Sergei Drevs](/t5/user/viewprofilepage/user-id/5499016)

I just did a quick test and the REST API <https://developer.atlassian.com/cloud/bitbucket/rest/api-group-commits/#api-repositories-workspace-repo-slug-diffstat-spec-get> is available in Forge as it lists the \`*read:repository:bitbucket* \` scope on the documentation.  

I just tested it and I can successfully retrieve the diffstat using that REST API from a Dynamic Pipeline.  

I tried a few things but I can't understand what's causing the error, so let me just go with some generic suggestions:

- if not using the *forge tunnel, try to redeploy the app (using forge deploy) and a \`forge install --upgrade\`*

- if using the *forge tunnel, try to restart the tunnel*

You could also try to have a simpler version of the *src/index.js file with just the content below and checking in the forge tunnel what is printed:*

import api, { route } from "@forge/api";

export const run = async (request, context) =\> {

const diffStat = await getPullRequestDiffStat(request.workspace, request.repository, request.target.commit.hash);  
console.log(\`diffStat: ${JSON.stringify(diffStat)}\`);

// return the pipeline as configured  
return request;  
};

const getPullRequestDiffStat = async (workSpaceUUID, repositoryUUID, commitHash) =\> {  

const res = await api  
.asApp()  
.requestBitbucket(

route\`/2.0/repositories/${workSpaceUUID}/${repositoryUUID}/diffstat/${commitHash} \`  
);  
return res.json();  
}

Let us know how it goes,  
Caterina

comments: author: Sergei Drevs
body: Gez...

It works with the example now. Even adjusted my code with another request which now works.

One thing I never did, after manifest change, because I assumed that it "will just work", is to:

```
forge deploy
```

And/or

```
forge install --upgrade
```

Which I **haven't** done after I added permissions scopes to the \`manifest.yml\`.

So it seems that if you adjust something in the manifest you should redeploy or upgrade the installation as \`tunnel\` doesn't seem to do it. Maybe it's documented somewhere and I just missed it, maybe not...

Anyway - Thank you [@Caterina Curti](/t5/user/viewprofilepage/user-id/551009) for pointing me into the right direction.


author: Caterina Curti
body: Hi [@Sergei Drevs](/t5/user/viewprofilepage/user-id/5499016)

I'm glad to hear it's working now and good job in adding the other request.

[Forge tunnel](https://developer.atlassian.com/platform/forge/tunneling/) runs the app locally and doesn't perform any deployment to the Atlassian infrastructure so yes, a forge deploy is required every time you want the changes to be available in the pipeline when not running forge tunnel.   
The primary reason for the forge tunnel is to speed up the dev loop.

Forge install --upgrade is only required in case of a permissions/scope or egress change. The forge deploy command will also indicate when that's required in its output.  

<br />

Nice use case by the way!  

Caterina


author: Caterina Curti
body: Two more things:

- I will also keep an eye on the topics tagged with \`dynamic-pipelines\` if you want to use them in the future.

- We have a dedicated group for "everything Forge" here if you want to join it and post there: <https://community.atlassian.com/t5/Forge-for-Bitbucket-Cloud/gh-p/bitbucket-cloud-forge>.   

Cheers,  
Caterina


author: Saxea _Flowie_
body: Hi [@Sergei Drevs](/t5/user/viewprofilepage/user-id/5499016)

I suspect it might not be available yet since not all APIs are?

As an easier alternative, you might also want to check out [Flowie smart pipelines](https://flowie.app/docs/en/plugins/pipelines), which support this and other scenarios, and are simpler to configure than a full-blown Forge app.

comments: author: Sergei Drevs
body: Hi [@Saxea _Flowie_](/t5/user/viewprofilepage/user-id/5230209) thanks, for the hint.

But as I said, I'm already struggling with the example. And I think that at least that should work \^\^

As for Flowie, I don't think that it will cover my cases as the mentioned use case is only the tip of the iceberg. I will have a look at it anyway, so thanks.


Tags: bitbucket-cloud

cloud

dynamic-pipeline

dynamic-pipelines

forge