Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Use Dynamic Pipelines to add step to build pipeline based on pull request diffstat

Sergei Drevs May 16, 2024

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."
}
}


Code:

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

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

2 answers

1 accepted

0 votes
Answer accepted
Caterina Curti
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 16, 2024

Ho @Sergei Drevs

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

Sergei Drevs May 17, 2024

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 for pointing me into the right direction. 

Like Caterina Curti likes this
Caterina Curti
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 17, 2024

Hi @Sergei Drevs

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

Forge tunnel 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.


Nice use case by the way!

Caterina

Like Sergei Drevs likes this
Caterina Curti
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 17, 2024

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

0 votes
Saxea _Flowie_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 16, 2024

Hi @Sergei Drevs

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, which support this and other scenarios, and are simpler to configure than a full-blown Forge app.

 

Sergei Drevs May 16, 2024

Hi @Saxea _Flowie_ 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. 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
TAGS
AUG Leaders

Atlassian Community Events