Detecting a change to the state of a pull request

The only example of working with `pullRequests` in a workflow I found contains the following code:

ctx.issue.pullRequests.last().state.name !== ctx.issue.pullRequests.last().previousState.name &&

However, I don’t think this actually works as expected? In my testing, `previousState` remains the same even with any future changes to the issue, so, e.g. if a PR changed from Open to Merged, and then I make any modification to the issue, this workflow will get triggered, since `previousState` will still be Open for that PR, even though the transaction has nothing to do with pull requests.

I want to detect a transaction that specifically changes the state of a pull request to Merged, so that the workflow only gets triggered by that PR event and not by any future changes to the fields of the issue. The API reference docs are extremely confusing in this part.

2 comments
Comment actions Permalink

Kirill,

Welcome to the community forums. You're absolutely right, for now it is not possible to find out if a pull request state was changed in the current transaction. I've filed a feature request to support this behavior: https://youtrack.jetbrains.com/issue/JT-67792, please feel free to upvote it. This helps us gauge how much impact this feature will have on our customer base. 

Please let us know if you have any questions. 

0
Comment actions Permalink

Kirill,

I'm sorry for the misleading, it turned out it is possible to catch the transaction when a pull request state is changed. I'm attaching a code example for your reference:

return ctx.issue.pullRequests.added.isNotEmpty() && 
ctx.issue.pullRequests.added.last().previousState &&
ctx.issue.pullRequests.added.last().state.name !== ctx.issue.pullRequests.last().previousState.name &&
ctx.issue.pullRequests.added.last().state.name === "MERGED";

This code should trigger only when a pull request state is changed to MERGED. Please let me know if it works for you. 

 

0

Please sign in to leave a comment.