javascriptworkflowyoutrackyoutrack-api

Exception when checking if an issue field becomes "In Progress" with workflow


My issue fields have a State and an option called In Progress

enter image description here

So I wrote a Youtrack Workflow that launches a http post to my discord channel when an issue becomes "In Progress".

Here is the JavaScript code for that:


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

exports.rule = entities.Issue.onChange({
  // TODO: give the rule a human-readable title
  title: 'Open-discord-channel',
  guard: function(ctx) {
    return ctx.issue.fields.becomes(ctx.State, ctx.State.InProgress);
  },
  action: function(ctx) {
    var issue = ctx.issue;
    var connection = new http.Connection('https://discordapp.com');
    connection.addHeader('Content-Type', 'application/json');
    var response = connection.postSync('/api/webhooks/123/1DJucC8-vdZR-xxx', [], issue.description);
    if (response && response.code === 200) {
        issue.addComment(response.response);
    }

    // TODO: specify what to do when a change is applied to an issue
  },
  requirements: {
    // TODO: add requirements
  }
});

When activating this workflow this exception gets thrown:

TypeError: Cannot read property "InProgress" from undefined (open-discord-channel/open-discord-channel#16)
             org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4198)
            org.mozilla.javascript.gen.open_discord_channel_open_discord_channel_2052._c_anonymous_1(open-discord-channel/open-discord-channel:16)

It tells me Cannot read property "InProgress" but in fact return ctx.issue.fields.becomes(ctx.State, ctx.State.InProgress); the value InProgress was suggested by the embedded Youtrack Workflow editor.

Can anybody tell me how I can access the real "In Progress" value to make this code running?

EDIT

tried this return ctx.issue.fields.becomes(ctx.State.name, "In Progress");

Still gave me an exception

Processing issue COOPR-85:
TypeError: Cannot read property "name" from undefined (open-discord-channel/open-discord-channel#16)
             org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4198)
            org.mozilla.javascript.gen.open_discord_channel_open_discord_channel_2076._c_anonymous_1(open-discord-channel/open-discord-channel:16)

Solution

  • If you want to use the ctx.issue.fields.becomes(ctx.State, ctx.State.InProgress) syntax, add a definition for the 'In Progress' state to the requirements section:

    requirements: {
        State: {
            type: entities.State.fieldType,
            InProgress: {
                name: 'In Progress'
            }
        }
    }   
    
    

    Alternatively, to avoid the Cannot read property "name" from undefined error, check the State field for null values:

    return ctx.issue.fields.State && ctx.issue.fields.becomes(ctx.State.name, "In Progress");
    

    I hope it will be helpful.