Skip to content

Commit

Permalink
Implement uninstall-hook (#14)
Browse files Browse the repository at this point in the history
* Implement uninstall-hook

* Implement uninstall-hook: naming refactor

* Add uninstall-hook unit test
  • Loading branch information
tonyydl authored and marcuswu0814 committed Sep 7, 2018
1 parent e320495 commit b68ea21
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 1 deletion.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ $ redmine-bot install-hook

`Redmine-bot` will install `post-commit` and `post-rewrite` hook in specific repo. after execute `install-hook` command.

```bash
$ redmine-bot uninstall-hook
```

`Redmine-bot` will delete `post-commit` and `post-rewrite` hook after executing `uninstall-hook` command.

## Usage

### Example
Expand All @@ -122,4 +128,4 @@ Just like `post-commit`, `RedmineBot` will post a new note to let you know commi

## Contact

If you find an issue or have questions, feel free to [let me know](https://github.com/marcuswu0814/RedmineBot/issues/new). 👍
If you find an issue or have questions, feel free to [let me know](https://github.com/marcuswu0814/RedmineBot/issues/new). 👍
1 change: 1 addition & 0 deletions Sources/RedmineBot/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Group {

$0.addCommand("setup", SetupCommand.make())
$0.addCommand("install-hook", InstallHookCommand.make())
$0.addCommand("uninstall-hook", UninstallHookCommand.make())
$0.addCommand("version", VersionCommand.make())

$0.addCommand("post-commit-hook", PostCommitHookCommand.make())
Expand Down
84 changes: 84 additions & 0 deletions Sources/RedmineBotCore/Command/UninstallHookCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import Foundation
import Commander
import PathKit

public class UninstallHookCommand: CommandProtocol {

public static func make() -> CommandType {
let uninstallCommand = command {
let action = UnInstallHookCommandAction()

action.doAction()
}

return uninstallCommand
}

}

enum UninstallHookCommandActionError: Error {
case gitRepoNotExist
case deleteHookFileFail
}

class UnInstallHookCommandAction {

lazy var system: SystemProtocol = System()

lazy var postCommitHookJob = HookInstallJob(repoPath: Path.current + Path(".git"),
hookName: "post-commit",
hookContent: DefaultTemplate.postCommitHook())
lazy var postRewriteHookJob = HookInstallJob(repoPath: Path.current + Path(".git"),
hookName: "post-rewrite",
hookContent: DefaultTemplate.postRewriteHook())

func doAction() {
let uninstaller = HookUninstaller(system)

do {
try uninstaller.uninstall([postCommitHookJob, postRewriteHookJob])
} catch {
system.printFatalError(error.localizedDescription)
}
}
}

class HookUninstaller {

var system: SystemProtocol

init(_ system: SystemProtocol) {
self.system = system
}

func uninstall(_ jobs: [HookInstallJob]) throws {
for job in jobs {
try checkRepoExist(job)
try uninstallHook(to: job.hookPath, hookContent: job.hookContent)
}
}

private func checkRepoExist(_ job: HookInstallJob) throws {
if (!job.repoPath.exists) {
throw UninstallHookCommandActionError.gitRepoNotExist
}
}

private func uninstallHook(to path: Path, hookContent: String) throws {
if (path.exists) {
try deleteExistHook(to: path)
} else {
system.printWarning("The file \(path.absolute()) is not exists.")
}
}

private func deleteExistHook(to path: Path) throws {
do {
try path.delete()
} catch {
throw UninstallHookCommandActionError.deleteHookFileFail
}
system.printWarning("Hook \(path.lastComponent) uninstall to path \(path) success.")
}

}
62 changes: 62 additions & 0 deletions Tests/RedmineBotTest/UnInstallHookCommandAction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// UnInstallHookCommandAction.swift
// CircuitBreaker
//
// Created by Tony Yang on 2018/7/7.
//

import XCTest
import Foundation
import PathKit
@testable import RedmineBotCore

class UnInstallHookCommandActionTest: XCTestCase {

override class func setUp() {
TempGitRepo.setUP()
}

func test__uninstallHookWhenHookIsEmpty__shouldShowWarning() {
let mockSystem = MockSystem()
let unInstallAction = UnInstallHookCommandAction()
unInstallAction.system = mockSystem

XCTAssertFalse(unInstallAction.postCommitHookJob.hookPath.exists)
XCTAssertFalse(unInstallAction.postRewriteHookJob.hookPath.exists)

unInstallAction.doAction()

XCTAssertFalse(unInstallAction.postCommitHookJob.hookPath.exists)
XCTAssertFalse(unInstallAction.postRewriteHookJob.hookPath.exists)
XCTAssertTrue(mockSystem.printWarningBeInvoke)
}

func test__uninstallHookWhenHookAlreadyExist__shouldDeleteHook() {
let mockSystem = MockSystem()
let installAction = InstallHookCommandAction()
installAction.system = mockSystem

XCTAssertFalse(installAction.postCommitHookJob.hookPath.exists)
XCTAssertFalse(installAction.postRewriteHookJob.hookPath.exists)

installAction.doAction()

XCTAssertTrue(installAction.postCommitHookJob.hookPath.exists)
XCTAssertTrue(installAction.postRewriteHookJob.hookPath.exists)

let unInstallAction = UnInstallHookCommandAction()
unInstallAction.system = mockSystem

unInstallAction.doAction()

XCTAssertFalse(installAction.postCommitHookJob.hookPath.exists)
XCTAssertFalse(installAction.postRewriteHookJob.hookPath.exists)
}

override class func tearDown() {
TempGitRepo.tearDown()

super.tearDown()
}

}

0 comments on commit b68ea21

Please sign in to comment.