JavaScript DevOps functions

Automation Rules include a library of DevOps functions that help automate your company workflows.

DevOps funcitons

DevOps functions are not available by default. They need to be loaded into JavaScript rule.
Here is example how you can do that:

const devOps = context.getService("devOps");

List of DevOps functions

Create branch

Creates branch in DevOps tool:
createBranch(payload: CreateBranchPayload, repositoryId: string | number): Promise<Branch>
Branch

interface CreateBranchPayload{
  name: string // name of the creating branch
  ref: string // name of the branch we create from
}
repositoryId: number | string // Internal integration Id or internal in DevOps tool Id or repository name.
const devOps = context.getService("devOps")
await devOps.createBranch({name: 'feature/us12345', ref: 'masster'}, 'my_repo_name')
// creates branch 'feature/us12345' from 'master' in repository 'my_repo_name'

Create merge request

Creates merge request in DevOps tool:
createMergeRequest(payload: CreateMergeRequestPayload): Promise<MergeRequest>
MergeRequest

interface CreateMergeRequestPayload {
  source: {
    branch: string // Source branch name
    repositoryId: number // In integration id of repository
  }
  target: {
    branch: string // Target branch name
    repositoryId: number // In integration id of repository
  }
  title: string // Merge request title
}
const devOps = context.getService("devOps")
await devOps.createMergeRequest({
  source: {
    branch: 'feature/us12345',
    repositoryId: 1
  },
  target: {
    branch: 'master',
    repositoryId: 1
  },
  title: 'US#12345 add sample merge request'
})
// creates merge request with title 'US#12345 add sample merge request' in repository with an integration id 1 from branch 'feature/us12345' to branch 'master'

Get branches

Returns DevOps data attached to specified Targetprocess entities
getBranches(filter: {entitties: number[]}): Promise<BranchFull>
BranchFull

const devOps = context.getService("devOps")
const branches = await devOps.getBranches({entities: [123, 124, 125]})
// Returns all branches attached to Targetprocess entitites with ids 123, 124 and 125 (directly or via merge request)

Merge merge request

Merges merge request with specified id (in integration) in DevOps tool
mergeMergeRequest(mergeRequestId: number): Promise<void>

const devOps = context.getService("devOps")
await devOps.mergeMergeRequest(1)
// Merges merge request with an integration id 1.

Merge request state change helpers:

When DevOps source is used devOps service provides list of useful helpers to check if some specific transition has occurred:
mergeRequestCreated(event: DevOpsChangedEntityEvent): boolean
mergeRequestDraftAdded(event: DevOpsChangedEntityEvent): boolean
mergeRequestDraftRemoved(event: DevOpsChangedEntityEvent): boolean
mergeRequestApproved(event: DevOpsChangedEntityEvent): boolean
mergeRequestRejected(event: DevOpsChangedEntityEvent): boolean
mergeRequestUnapproved(event: DevOpsChangedEntityEvent): boolean
mergeRequestMerged(event: DevOpsChangedEntityEvent): boolean
mergeRequestClosed(event: DevOpsChangedEntityEvent): boolean
DevOpsChangedEntityEvent

const devOps = context.getService("devOps")
// Args represents incoming event in js executor context. So if DevOps events source is selected in rule args will contain devops event.
const created = devOps.mergeRequestCreated(args) // will return true if current event is merge request created event
const draftAdded = devOps.mergeRequestDraftAdded(args) // will return true if merge request moved to draft state in DevOps tool. In some tool is is called WIP (Work in progress)
const draftRemoved = devOps.mergeRequestDraftRemoved(args) // will return true if merge request moved to not draft state in DevOps tool. In some tool is is called WIP (Work in progress) and will mean that merge request is not WIP any more
const mergeRequestApproved = devOps.mergeRequestApproved(args) // will return true if merge request got required amount of approvals by the tool policy. If 0 approvals required will become true when merge request gets at least one approve.
const mergeRequestRejected = devOps.mergeRequestRejected(args) // will return true if merge request got rejected after the code review. Some tools(like Phabricator for example) allow not only approve but also reject changes.
const mergeRequestUnapproved = devops.mergeRequestUnapproved(args) // will return true if merge request was in approved state but someone removed his approval and merge request has become not approved anymore according to the tool policies
const mergeRequestMerged  = devops.mergeRequestMerged(args) // wiil return if current event signalize about merge request merge in DevOps tool.
const mergeRequestClosed  = devops.mergeRequestClosed(args) // will return if current event signalizes a merge request close(this action has different name in different tools close, abandon, remove etc. Usualy means changes in merge request should not be integrated) in DevOps tool.