Created
June 13, 2012 06:07
-
-
Save akloboucnik/2922166 to your computer and use it in GitHub Desktop.
Issue API cruncher for JPC Redmine
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
{ | |
"name": "redmine-issue-api-cruncher", | |
"version": "0.0.1", | |
"dependencies" : { | |
"underscore": "~> 1.3.3", | |
"restler": "~> 2.0", | |
"date-utils": "~> 1.2" | |
} | |
} |
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
sys = require 'util' | |
rest = require './node_modules/restler' | |
_ = require('./node_modules/underscore')._ | |
require "./node_modules/date-utils" | |
class Redmine | |
constructor: (@host, @key) -> | |
@baseParams = | |
headers: | |
"X-Redmine-API-Key": @key | |
getIssues: (query, callback) -> | |
# console.log _.extend @baseParams, {query: query} | |
rest.get("#{@host}/issues.json", _.extend @baseParams, {query: query}).on 'complete', (result) -> | |
if result instanceof Error | |
callback(result) | |
else | |
callback(null, result) | |
getIssuesFromPage: (page, query, callback) -> | |
# console.log page, query | |
runQuery = _.extend(query, {page:page}) | |
@getIssues runQuery, (err, result) => | |
if err | |
return callback(err, result) | |
if result.total_count > (result.offset + result.limit) | |
@getIssuesFromPage page + 1, query, (error, nextPage) -> | |
callback(error, _.union(result.issues, _.reject(nextPage, (item) -> | |
_.include(_.pluck(result.issues, 'id'), item.id) | |
))) | |
else | |
callback(err, result.issues) | |
getAllIssues: (query, callback) -> | |
@getIssuesFromPage 0, query, callback | |
getIssue: (id, query, callback) -> | |
params = _.extend @baseParams, {query: query} | |
rest.get("#{@host}/issues/#{id}.json", params).on 'complete', (result) -> | |
if result instanceof Error | |
callback(result) | |
else | |
callback(null, result) | |
api = new Redmine "https://redmine.dev.ftmc.cz", "<API_KEY>" | |
baufinderQuery = { limit: 100, project_id: PROJECT_ID, status_id: "COMMA_SEPARATED_STATUSES"} | |
api.getAllIssues baufinderQuery, (err, issues) -> | |
for issue in issues | |
api.getIssue issue.id, {include: "journals"}, (err, result) -> | |
unless err | |
# all journals that updated status_id with interesting status | |
journals = _.filter(result.issue.journals, (journalItem) -> | |
_.include(_.pluck(journalItem.details, 'property'), 'attr') && | |
_.include(_.pluck(journalItem.details, 'name'), 'status_id') && | |
_.any(_.pluck(journalItem.details, 'new_value'), (item) -> | |
_.include(["ALL", "CLOSED","STATUS", "ID'S"], item) | |
) | |
) | |
# sort them by date ASC | |
journals = _.sortBy(journals, (journalItem) -> | |
new Date(journalItem.created_on) | |
) | |
issueId = result.issue.id | |
closedOn = new Date(_.last(journals).created_on) | |
console.log "Setting ##{issueId} - closed on: #{closedOn}" | |
params = | |
headers: | |
"X-Redmine-API-Key" : "<API_KEY>" | |
"Content-Type" : "application/xml" | |
data: "<?xml version=\"1.0\"?><issue><custom_fields type=\"array\"> | |
<custom_field id=\"24\"> | |
<value>#{closedOn.toFormat('YYYY-MM-DD')}</value> | |
</custom_field> | |
</custom_fields></issue>" | |
# console.log params.data | |
rest.put("#{api.host}/issues/#{issueId}.xml", params).on 'complete', (result, response) -> | |
if result instanceof Error | |
console.log "ERR ##{issueId} NOT updated: #{result}. #{response.statusCode}" | |
else | |
console.log "OK Setting ##{issueId} - closed on: #{closedOn} (#{response.statusCode})" | |
# console.log response.rawEncoded |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment