Last active
August 15, 2018 01:59
-
-
Save saihoooooooo/8908904 to your computer and use it in GitHub Desktop.
git merge & push in hubot.
This file contains hidden or 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
| # Description: | |
| # Merge git branch. | |
| # | |
| # Dependencies: | |
| # None | |
| # | |
| # Configuration: | |
| # None | |
| # | |
| # Commands: | |
| # hubot mggit <repo> <branch> - Merge <branch> into "develop" of <repo> repository (this command is dry-run). | |
| # hubot mggit push <repo> <branch> - Merge <branch> into "develop" of <repo> repository, and push it in <repo> ripository. | |
| # hubot show repos - Display repositories. | |
| # hubot clean repos - Clean git repository directory. | |
| # | |
| # Notes: | |
| # None | |
| # | |
| # Author: | |
| # saihoooooooo | |
| fs = require 'fs' | |
| path = require 'path' | |
| exec = require('child_process').exec | |
| async = require 'async' | |
| home = path.resolve '.' | |
| basedir = path.join home, 'gitrepos' | |
| repos = | |
| unko: 'http://example.com/gitrepos/unko.git' | |
| chinko: 'http://example.com/gitrepos/chinko.git' | |
| manko: 'http://example.com/gitrepos/manko.git' | |
| gitconfig = | |
| user: | |
| name: 'hubot' | |
| email: '[email protected]' | |
| username = 'yourusername' | |
| password = 'yourpassword' | |
| clean = (dir) -> | |
| fs.readdirSync(dir).forEach (item) -> | |
| filepath = path.join dir, item | |
| stat = fs.lstatSync filepath | |
| if stat.isFile() or stat.isSymbolicLink() | |
| fs.unlinkSync filepath | |
| else if stat.isDirectory() | |
| clean filepath | |
| fs.rmdirSync dir | |
| setUserPassword = (repo) -> | |
| repo.replace /http:\/\//, "http://#{username}:#{password}@" | |
| gitconfigToCommand = (gitconfig) -> | |
| command = [] | |
| toCommand = (gitconfig, previous = '') -> | |
| for key, config of gitconfig | |
| if typeof config == 'object' | |
| toCommand config, "#{previous}#{key}." | |
| return | |
| else | |
| command.push "git config #{previous}#{key} #{config}" | |
| toCommand(gitconfig) | |
| command | |
| module.exports = (robot) -> | |
| ################################################# | |
| # リポジトリ一覧 # | |
| ################################################# | |
| robot.respond /show repos/i, (msg) -> | |
| say = '' | |
| for name, repo of repos | |
| say += "[#{name}] #{repo}\n" | |
| msg.send say | |
| ################################################# | |
| # マージ(&プッシュ) # | |
| ################################################# | |
| robot.respond /mggit( push)? (.+) (.+)/i, (msg) -> | |
| push = Boolean msg.match[1] | |
| repo = repos[msg.match[2]] | |
| branch = msg.match[3] | |
| unless repo? | |
| msg.send "ERROR: repository \"#{msg.match[2]}\" not found. try \"hubot show repos\"." | |
| return | |
| repodir = path.join basedir, path.basename repo | |
| tasks = [] | |
| execAsync = (command, message, callback) -> | |
| (next) -> | |
| msg.send message if message? and message isnt '' | |
| exec command, (error, stdout, stderr) -> | |
| if error? | |
| msg.send stdout if stdout? and stdout isnt '' | |
| msg.send stderr if stderr? and stderr isnt '' | |
| process.chdir home | |
| clean basedir | |
| else | |
| callback() if callback? | |
| next(error) | |
| msg.send '========================================================' | |
| msg.send 'running...' | |
| fs.mkdirSync basedir | |
| # リポジトリをクローン | |
| tasks.push execAsync "git clone #{setUserPassword repo} #{repodir}", | |
| "clone repository from #{repo}.", | |
| -> process.chdir repodir | |
| # hubot固有のgit設定を行う | |
| if gitconfig? and Object.keys(gitconfig).length > 0 | |
| tasks.push execAsync gitconfigToCommand(gitconfig).join(' && '), | |
| 'setting git config.' | |
| # マージ元ブランチを作成 | |
| tasks.push execAsync "git branch -t #{branch} origin/#{branch}", | |
| "craete branch \"origin/#{branch}\" -> \"#{branch}\"." | |
| # マージ先ブランチを作成 | |
| tasks.push execAsync "git checkout -t -b develop origin/develop", | |
| "craete branch \"origin/develop\" -> \"develop\"." | |
| # マージ | |
| tasks.push execAsync "git merge --no-ff #{branch}", | |
| "merge branch \"#{branch}\" -> \"develop\"." | |
| # 共有リポジトリへプッシュ | |
| if push | |
| tasks.push execAsync "git push #{setUserPassword repo} develop", | |
| "push branch \"develop\" to #{repo}." | |
| async.series tasks, (error) -> | |
| if error? | |
| msg.send "ERROR: merge faild!!" | |
| msg.send if push then "SUCCESS: push complete!!" else "SUCCESS: merge complete!!" | |
| process.chdir home | |
| clean basedir | |
| ################################################# | |
| # ディレクトリの掃除 # | |
| ################################################# | |
| robot.respond /clean repos/i, (msg) -> | |
| clean basedir | |
| msg.send "clean git repositories." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment