Adding/removing issues from boards based on assignee

I want to have a custom workflow rule that automatically assigns issues to a board based on the assignee's name. The rule is triggered whenever the assignee field is updated. However, I'm encountering an errors.

The expected behaviour: 

When the assignee field is updated, the workflow rule should check the assignee's name and assign the issue to the corresponding board. For example, if the assignee's first name is "John" the issue should be added to the "John schedule" board.

 

Here's the code I wrote with the help of ChatGPT, if it helps anything.

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

exports.rule = entities.Issue.onChange({
  title: 'Change-assignee-board',
  guard: (ctx) => {
    // TODO: specify the conditions for executing the rule
    return true;
  },
  action: (ctx) => {
    const issue = ctx.issue;
    const assignee = issue.fields.Assignee;

    if (assignee !== null) {
      const firstName = assignee.fullName.split(' ')[0];
      const boardName = `${firstName} schedule`;

      issue.addComment(`Adding issue '${issue.id}' to the board '${boardName}'`);

      const board = ctx.project.boards.find(board => board.name === boardName);
      if (board !== null  && !issue.isInProject(board.project)) {
        board.addIssue(issue);
        issue.addComment(`Issue '${issue.id}' added to the board '${boardName}'`);
      }
    } else {
      const previousAssignee = issue.fields.becomes(ctx.Assignee);
      if (previousAssignee !== null) {
        const previousFirstName = previousAssignee.fullName.split(' ')[0];
        const previousBoardName = `${previousFirstName} schedule`;

        issue.addComment(`Removing issue '${issue.id}' from the board '${previousBoardName}'`);

        const previousBoard = ctx.project.boards.find(board => board.name === previousBoardName);
        if (previousBoard !== null && issue.isInProject(previousBoard.project)) {
          previousBoard.removeIssue(issue);
          issue.addComment(`Issue '${issue.id}' removed from the board '${previousBoardName}'`);
        }
      }
    }
  },
  requirements: {
    // TODO: add requirements
  }
});

0
1 comment

Hello,

I'm Lena from the YouTrack Team.

Unfortunately, creating workflows from scratch upon request is out of the support scope, along with rewriting the code generated by ChatGPT. However, I can give you directions and some hints that you can use to create such a workflow. 

1. You should create a correct guard section. In this section, you should check which event triggers the workflow. In your case, the correct event is a field value change. You can copy this part from this workflow: https://www.jetbrains.com/help/youtrack/devportal/Workflow-Assignee-Visibility-Group.html

2. Once we are sure we triggered the correct event, we can proceed to the action part. Here you can use the code attached as a reference. You can use console.log() to debug the logic of the algorithm. 

3. Remember the requirements section - you should list the fields used in this workflow, without which the workflow will not work. 

 

In addition, I wish to highlight our docs:

https://www.jetbrains.com/help/youtrack/devportal/Default-Workflows.html - contains lots of ready-to-use code you can copy-paste to your solution.

https://www.jetbrains.com/help/youtrack/devportal/Workflows-in-JavaScript.html - describes how to write workflow and general patterns.

https://www.jetbrains.com/help/youtrack/devportal/YouTrack-Api-Documentation.html - particular API (methods and fields supported in workflows).

 

Please try to create a new workflow from scratch. Please don't hesitate to text me if you need any more help or help with particular lines. 

 

0

Please sign in to leave a comment.