javascript 从一个项目中的状态转换更新另一个项目中链接的跟踪问题状态

fnvucqvd  于 2023-02-15  发布在  Java
关注(0)|答案(1)|浏览(138)

对此的任何帮助都将不胜感激。
我有两个youtrack项目"支持"和"Bug DB"。在支持项目中,我有一个工作流规则,即转换到"等待修复"状态的特定状态会自动在"Bug DB"项目中生成一个问题,并使用"影响帮助台日志"链接类型链接回原始问题。
在"Bug DB"项目中,我想添加一个规则,以便在发生特定状态转换时自动将"Support"项目中的问题状态更新为"Action Required"。
我可以管理状态转换部分,但在"Bug DB"项目的代码中有两个突出的问题:
1.防护条件
目前,我有一个函数(我希望)只是检查一个链接存在-理想情况下,这应该检查链接具体指向一个问题,在'支持'项目。

const issue_0 = ctx.issue;
...
function hasLinkedSupportIssue() {
  //I think this is equivalent to a C# .Any()
   return issue_0.links.added
   }

1.更新
为了应用新的状态,我正在迭代链接的问题(因为我不知道如何获得特定的问题)

issue_0.links['affects helpdesk log'].added.forEach(updateState)

然后我有一个函数来完成实际的更新

const updateState = function(supportIssue) {
    //Check here that this is a 'Support' issue
    //because my guard is not sufficient
    if(supportIssue.project.key === 'Support') {
      //not sure I can do assignment like this
      supportIssue.state = ctx.SupportState.ActionRequired;
    }
  }
....
requirements: {
BugState: {
  name: "State",
  type: entities.State.fieldType,
  InProgress: {name: "In Progress"},
  Closed: {name: "Closed"}
},
SupportState: {
  name: "State",
  type: entities.State.fieldType,
  AwaitingFix: {name: "Awaiting Fix"},
  ActionRequired: {name: "Action Required"}
}
icomxhvb

icomxhvb1#

对于任何试图这样做的人来说,这是我想出的最终解决方案:

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

exports.rule = entities.Issue.onChange({
  title: 'Support_bug_resolved',
  guard: (ctx) => {
    const logger = new Logger(ctx.traceEnabled);
 
    const thisIssue = ctx.issue;

    function checkFieldState() {
      //check that state is changing from closed to resolved
      return thisIssue.fields.isChanged(ctx.BugState) &&     thisIssue.fields.was(ctx.BugState, ctx.BugState.Resolved) && thisIssue.fields.becomes(ctx.BugState, ctx.BugState.Closed);
   }

    function hasLinkedSupportIssue() {
      //Check we have a linked issue from the support project
      return thisIssue.links["helpdesk log affected by"] != 0;
    }

    function checkFieldFn() {
      var check = checkFieldState();
      var hasLink = hasLinkedSupportIssue();
      return check && hasLink;
    };

   try {
      return checkFieldFn();
    } catch (err) {
      logger.error(err.message);
    }
  },
  action: function(ctx) {        
    const logger = new Logger(ctx.traceEnabled);
    
    const thisIssue = ctx.issue;  
    //Get the first linked support issue - we assume there is always only one
    const supportIssue = thisIssue.links['helpdesk log affected by'].first();

    try {
      //If the current state of the linked issue is 'Awaitingfix', make it 'ActionRequired'
      if(supportIssue.State.name == "AwaitingFix") {
        supportIssue.State = ctx.SupportState.ActionRequired;
      }
    } catch(err) {
      logger.error(err.message);
    }
    
    
  },
  requirements: {
    BugState: {
      name: "State",
      type: entities.State.fieldType,
      Resolved: {name: "Resolved"},
      Closed: {name: "Closed"}
    },
    SupportState: {
      name: "State",
      type: entities.State.fieldType,
      AwaitingFix: {name: "AwaitingFix"},
      ActionRequired: {name: "ActionRequired"}
    }
  }
});

function Logger(useDebug = true) {
  return {
    log: (...args) => useDebug && console.log(...args),
    warn: (...args) => useDebug && console.warn(...args),
    error: (...args) => useDebug && console.error(...args)
  }
}

相关问题