Skip to content

Instantly share code, notes, and snippets.

@kerbyfc
Last active August 29, 2015 14:05
Show Gist options
  • Save kerbyfc/1686c7b4865c6674689e to your computer and use it in GitHub Desktop.
Save kerbyfc/1686c7b4865c6674689e to your computer and use it in GitHub Desktop.
Grunt таска для удаления всех локальных и удаленных git-веток с ссылками на закрытые Redmine-тикеты
grunt = require('grunt')
exec = require("child_process").exec
grunt.loadNpmTasks('grunt-shell')
request = require('request')
cheerio = require('cheerio')
grunt.registerTask 'check_status', (branch) ->
if branch = branch.replace(/^[\s]*|[\s]*$/, '')
done = @async()
ticket = branch.match(/[\d]{5,5}/g)
request "http://pm.undev.cc/issues/#{ticket}", (error, responce, body) ->
if error?
grunt.fail.warn error
else
if responce.statusCode is 200
$ = cheerio.load(body)
status = $('td.status').first().text().toLowerCase()
grunt.log.writeln status
if status is "closed"
cmd = if branch.match(/^remotes/)?
"git push origin #{branch.replace(/^remotes\/origin\//, '')} --delete"
else
"git branch -d #{branch}"
exec cmd, (error, stdout, stderr) ->
if error?
grunt.fail.warn error
grunt.log.ok "#{cmd}"
grunt.log.writeln stdout
done()
else
grunt.fail.warn JSON.stringify(responce, null, 2)
done()
module.exports = ->
cookie = grunt.option 'cookie'
unless cookie
grunt.fail.fatal '--cookie option is required (_session_id cookie)'
grunt.option 'cookie', cookie
grunt.option 'force', true
j = request.jar()
j.setCookie("_session_id=#{cookie};", "http://pm.undev.cc")
grunt.log.writeln(JSON.stringify(j, null, 2))
request = request.defaults({jar:j})
grunt.initConfig
shell:
get_branches:
command: "git branch -a | grep '[0-9]\\{5,5\\}'"
options:
stdout: false
callback: (error, stdout, stderr, cb) ->
for branch in stdout.split(/\n[\s]*/)
if branch
do (branch) =>
grunt.task.run "check_status:#{branch}"
cb()
grunt.task.run "shell:get_branches"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment