Example: Change state in Targetprocess, when merge/pull request updates

πŸ“˜

How to apply Raw JSON from examples

This rule reacts on a set of updates of merge/pull requests and triggers a state change of a Targetprocess item, related to updated branch or merge/pull request

  • WIP is removed from the merge request name;
  • Merge/pull request gets all required approvals;
  • Merge/pull request is merged;
  • Branch closes without code changes;
2420
const utils = require('utils')
const devOps = require('devOps')

var stateTo = ''
var cmds = []

if (args.ResourceType != 'MergeRequest') return []


var isWIPRemoved = devOps.mergeRequestDraftRemoved(args)
var ifApproved = devOps.mergeRequestApproved(args)
var ifMerged = devOps.mergeRequestMerged(args)
var ifClosed = devOps.mergeRequestClosed(args)

console.log(`isWIPRemoved: ${isWIPRemoved}`)
console.log(`ifApproved: ${ifApproved}`)
console.log(`ifMerged: ${ifMerged}`)
console.log(`ifClosed: ${ifClosed}`)

if (isWIPRemoved) {
  stateTo = 'Code Review'
}

if (ifApproved) {
  stateTo = 'To Test'
}

if (ifMerged) {
  stateTo = 'Integrated'
}

if (ifClosed) {
  stateTo = 'Open'
}

// Change state if needed
if (stateTo != '') {
  var entityIds = args.TargetprocessEntityIds;
  const entities = await api.queryAsync("Assignable", {
    select: "{id,entityType: entitytype.name}",
    where: `id in ${JSON.stringify(entityIds)}`
  });
  entities.map(value => cmds.push(
    utils.moveToStateByName(value.entityType, value.id, stateTo)
  ));
}

console.log(`State To: ${stateTo}`)


return cmds;

States of entities in Targetprocess are specified by name. Just put the right names in THEN clause of the rule instead of the ones in bold:

🚧

Put your state names in THEN of the rule for each event:

if (isWIPRemoved) {
stateTo = 'Code Review'
}

if (ifApproved) {
stateTo = 'To Test'
}

if (ifMerged) {
stateTo = 'Integrated'
}

if (ifClosed) {
stateTo = 'Open'
}

{
    "type": "source:devOps:EntityChanged",
    "entityTypes": [
      "MergeRequest"
    ],
    "modifications": {
      "created": false,
      "deleted": false,
      "updated": [
        "repository",
        "id",
        "sourceId",
        "title",
        "isDraft",
        "url",
        "state",
        "mergeStatus",
        "reviewStatus",
        "branch"
      ]
    }
  }