javascript - How to set Youtrack issues state on Pull Requests update via JS workflows - Stack Overflow

I need to put a Youtrack issue into a Done state when the corresponding Pull Request is merged. I am us

I need to put a Youtrack issue into a Done state when the corresponding Pull Request is merged. I am using Javascript workflows and listening for issue changes.

Expected and Actual

I expected to be able to know which Pull Request was added, its current state (OPEN, MERGED, DECLINED) and its state before the current transaction.

But I see that every item in ctx.issue.pullRequests has isNew=false no matter what I do. Contrary to what it says in documentation

I tried

  • Get ctx.issue.oldValue('pullRequests'), but it returns the same as ctx.issue.pullRequests.
  • Calling isChanged() for each pullRequest item in the list, as well as for the pullRequest.state field. But it always false.

My temporary solution

So the only solution I have is to get the last fetched PR when updating the list of issue PRs and check if its state is MERGED.

Here is my code:

const entities = require('@jetbrains/youtrack-scripting-api/entities');

exports.rule = entities.Issue.onChange({
  title: 'Move task to "Done" when PR merged',
  guard: (ctx) => {
    if (ctx.issue.isChanged('pullRequests')) {

      const pullRequests = [];
      ctx.issue.pullRequests.forEach((pullRequest) => pullRequests.push(pullRequest));
      
      pullRequests.sort((a, b) => b.fetched - a.fetched);

      return pullRequests[pullRequests.length - 1].state.name == 'MERGED';
    } else {
      return false;
    }
  },
  action: (ctx) => {
    if (ctx.issue.fields.State && !ctx.issue.fields.State.is(ctx.State.Done)) {
      ctx.issue.fields.State = ctx.State.Done;
    }
  },
  requirements: {
    State: {
      type: entities.State.fieldType,
      name: 'State',
      Done: {}
    }
  }
});

I also can't figure out what the ctx.issue.pullRequests object represents. I can only call .forEach() on it.

I need to put a Youtrack issue into a Done state when the corresponding Pull Request is merged. I am using Javascript workflows and listening for issue changes.

Expected and Actual

I expected to be able to know which Pull Request was added, its current state (OPEN, MERGED, DECLINED) and its state before the current transaction.

But I see that every item in ctx.issue.pullRequests has isNew=false no matter what I do. Contrary to what it says in documentation

I tried

  • Get ctx.issue.oldValue('pullRequests'), but it returns the same as ctx.issue.pullRequests.
  • Calling isChanged() for each pullRequest item in the list, as well as for the pullRequest.state field. But it always false.

My temporary solution

So the only solution I have is to get the last fetched PR when updating the list of issue PRs and check if its state is MERGED.

Here is my code:

const entities = require('@jetbrains/youtrack-scripting-api/entities');

exports.rule = entities.Issue.onChange({
  title: 'Move task to "Done" when PR merged',
  guard: (ctx) => {
    if (ctx.issue.isChanged('pullRequests')) {

      const pullRequests = [];
      ctx.issue.pullRequests.forEach((pullRequest) => pullRequests.push(pullRequest));
      
      pullRequests.sort((a, b) => b.fetched - a.fetched);

      return pullRequests[pullRequests.length - 1].state.name == 'MERGED';
    } else {
      return false;
    }
  },
  action: (ctx) => {
    if (ctx.issue.fields.State && !ctx.issue.fields.State.is(ctx.State.Done)) {
      ctx.issue.fields.State = ctx.State.Done;
    }
  },
  requirements: {
    State: {
      type: entities.State.fieldType,
      name: 'State',
      Done: {}
    }
  }
});

I also can't figure out what the ctx.issue.pullRequests object represents. I can only call .forEach() on it.

Share Improve this question asked Feb 21 at 1:06 Sergei LobanovSergei Lobanov 1219 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I checked Youtrack's team own youtrack issue board and found answer here)

And here is my final solution:

const entities = require('@jetbrains/youtrack-scripting-api/entities');

const DONE_STATE = 'Done';

exports.rule = entities.Issue.onChange({
  title: 'Move task to "Done" when PR merged',
  guard: (ctx) => {
    return ctx.issue.pullRequests?.added?.isNotEmpty() && ctx.issue.pullRequests.added.last().state.name === 'MERGED';
  },
  action: (ctx) => {
    if (ctx.issue.fields.State && ctx.issue.fields.State.name !== DONE_STATE) {
      ctx.issue.fields.State = ctx.State.findValueByName(DONE_STATE);
    }
  },
  requirements: {
    State: {
      type: entities.State.fieldType,
      name: 'State',
      [DONE_STATE]: {}
    }
  }
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745171688a4614964.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信