Created
March 9, 2017 17:19
-
-
Save mrueegg/0f2a2b4043d96c16586091f6b72ad96a to your computer and use it in GitHub Desktop.
JavaScript AMD module to resolve open tasks in Bitbucket Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define('plugin/openTaskResolver', [ | |
'jquery', | |
'underscore', | |
'aui/flag', | |
'exports'], function ($, _, flag, exports) { | |
'use strict'; | |
function resolve(tasks) { | |
var taskUrl = AJS.contextPath() + '/rest/api/1.0/tasks'; | |
var resolveRequests = _.map(tasks, function (task) { | |
return $.ajax({ | |
url: taskUrl + '/' + task.id, | |
type: 'PUT', | |
data: JSON.stringify({state: 'RESOLVED'}), | |
contentType: 'application/json' | |
}); | |
}); | |
$.when(resolveRequests) | |
.then(function () { | |
flag({ | |
type: 'success', | |
body: 'All open tasks are resolved now.', | |
close: 'auto' | |
}); | |
}) | |
.fail(function () { | |
flag({ | |
type: 'error', | |
body: 'Error occurred while resolved open tasks.', | |
close: 'manual' | |
}); | |
}); | |
} | |
function findTasksInPullRequest(state) { | |
var projectKey = state.getProject().key; | |
var repoSlug = state.getRepository().slug; | |
var pullRequestId = state.getPullRequest().id; | |
var allTasksUrl = AJS.contextPath() + '/rest/api/1.0/projects/' + | |
projectKey + '/repos/' + repoSlug + '/pull-requests/' + pullRequestId + '/tasks'; | |
return $.getJSON(allTasksUrl) | |
} | |
exports.resolveTasks = function (state) { | |
findTasksInPullRequest(state).done(function (data) { | |
var openTasks = _.filter(data.values, function (task) { | |
return task.state === 'OPEN'; | |
}); | |
resolve(openTasks) | |
}); | |
}; | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment