未验证 提交 57cc2100 编写于 作者: M Mislav Marohnić 提交者: GitHub

Merge pull request #1718 from andreasbaumann/delete_repository

Fixes https://github.com/github/hub/issues/494
......@@ -7,6 +7,7 @@ HELP_CMD = \
share/man/man1/hub-ci-status.1 \
share/man/man1/hub-compare.1 \
share/man/man1/hub-create.1 \
share/man/man1/hub-delete.1 \
share/man/man1/hub-fork.1 \
share/man/man1/hub-pr.1 \
share/man/man1/hub-pull-request.1 \
......
package commands
import (
"bufio"
"fmt"
"os"
"regexp"
"strings"
"github.com/github/hub/github"
"github.com/github/hub/ui"
"github.com/github/hub/utils"
)
var cmdDelete = &Command{
Run: deleteRepo,
Usage: "delete [-y] [<ORGANIZATION>/]<NAME>",
Long: `Delete an existing repository on GitHub.
## Options:
-y, --yes
Skip the confirmation prompt and immediately delete the repository.
[<ORGANIZATION>/]<NAME>
The name for the repository on GitHub.
## Examples:
$ hub delete recipes
[ personal repo deleted on GitHub ]
$ hub delete sinatra/recipes
[ repo deleted in GitHub organization ]
## See also:
hub-init(1), hub(1)
`,
}
var (
flagDeleteAssumeYes bool
)
func init() {
cmdDelete.Flag.BoolVarP(&flagDeleteAssumeYes, "--yes", "y", false, "YES")
CmdRunner.Use(cmdDelete)
}
func deleteRepo(command *Command, args *Args) {
var repoName string
if !args.IsParamsEmpty() {
repoName = args.FirstParam()
}
re := regexp.MustCompile(NameWithOwnerRe)
if !re.MatchString(repoName) {
utils.Check(fmt.Errorf(command.Synopsis()))
}
config := github.CurrentConfig()
host, err := config.DefaultHost()
if err != nil {
utils.Check(github.FormatError("deleting repository", err))
}
owner := host.User
if strings.Contains(repoName, "/") {
split := strings.SplitN(repoName, "/", 2)
owner, repoName = split[0], split[1]
}
project := github.NewProject(owner, repoName, host.Host)
gh := github.NewClient(project.Host)
if !flagDeleteAssumeYes {
ui.Printf("Really delete repository '%s' (yes/N)? ", project)
answer := ""
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
answer = strings.TrimSpace(scanner.Text())
}
utils.Check(scanner.Err())
if answer != "yes" {
utils.Check(fmt.Errorf("Please type 'yes' for confirmation."))
}
}
if args.Noop {
ui.Printf("Would delete repository '%s'.\n", project)
} else {
err = gh.DeleteRepository(project)
if err != nil && strings.Contains(err.Error(), "HTTP 403") {
ui.Errorf("Please edit the token used for hub at https://%s/settings/tokens\n", project.Host)
ui.Errorln("and verify that the `delete_repo` scope is enabled.")
}
utils.Check(err)
ui.Printf("Deleted repository '%s'.\n", project)
}
args.NoForward()
}
......@@ -169,6 +169,7 @@ These GitHub commands are provided by hub:
ci-status Show the CI status of a commit
compare Open a compare page on GitHub
create Create this repository on GitHub and add GitHub as origin
delete Delete a repository on GitHub
fork Make a fork of a remote repository on GitHub and add as remote
issue List or create issues
pr Work with pull requests
......
Feature: hub delete
Background:
Given I am "andreasbaumann" on github.com with OAuth token "OTOKEN"
Scenario: No argument in current repo
Given I am in "git://github.com/github/hub.git" git repo
When I run `hub delete`
Then the exit status should be 1
And the stderr should contain exactly:
"""
Usage: hub delete [-y] [<ORGANIZATION>/]<NAME>\n
"""
Scenario: Successful confirmation
Given the GitHub API server:
"""
delete('/repos/andreasbaumann/my-repo') {
status 204
}
"""
When I run `hub delete my-repo` interactively
And I type "yes"
Then the exit status should be 0
And the output should contain:
"""
Really delete repository 'andreasbaumann/my-repo' (yes/N)?
"""
And the output should contain:
"""
Deleted repository 'andreasbaumann/my-repo'.
"""
Scenario: Org repo
Given the GitHub API server:
"""
delete('/repos/our-org/my-repo') {
status 204
}
"""
When I run `hub delete our-org/my-repo` interactively
And I type "yes"
Then the exit status should be 0
And the output should contain:
"""
Really delete repository 'our-org/my-repo' (yes/N)?
"""
And the output should contain:
"""
Deleted repository 'our-org/my-repo'.
"""
Scenario: Invalid confirmation
When I run `hub delete my-repo` interactively
And I type "y"
Then the exit status should be 1
And the output should contain:
"""
Really delete repository 'andreasbaumann/my-repo' (yes/N)?
"""
And the stderr should contain exactly:
"""
Please type 'yes' for confirmation.\n
"""
Scenario: HTTP 403
Given the GitHub API server:
"""
delete('/repos/andreasbaumann/my-repo') {
status 403
}
"""
When I run `hub delete -y my-repo`
Then the exit status should be 1
And the stderr should contain:
"""
Please edit the token used for hub at https://github.com/settings/tokens
and verify that the `delete_repo` scope is enabled.
"""
Scenario: HTTP 403 on GitHub Enterprise
Given I am "mislav" on git.my.org with OAuth token "FITOKEN"
And $GITHUB_HOST is "git.my.org"
Given the GitHub API server:
"""
delete('/api/v3/repos/mislav/my-repo', :host_name => 'git.my.org') {
status 403
}
"""
When I run `hub delete -y my-repo`
Then the exit status should be 1
And the stderr should contain:
"""
Please edit the token used for hub at https://git.my.org/settings/tokens
and verify that the `delete_repo` scope is enabled.
"""
......@@ -247,6 +247,17 @@ func (client *Client) CreateRepository(project *Project, description, homepage s
return
}
func (client *Client) DeleteRepository(project *Project) error {
api, err := client.simpleApi()
if err != nil {
return err
}
repoURL := fmt.Sprintf("repos/%s/%s", project.Owner, project.Name)
res, err := api.Delete(repoURL)
return checkStatus(204, "deleting repository", res, err)
}
type Release struct {
Name string `json:"name"`
TagName string `json:"tag_name"`
......
......@@ -66,6 +66,9 @@ git but that are extended through hub, and custom ones that hub provides.
* hub-create(1):
Create a new repository on GitHub and add a git remote for it.
* hub-delete(1):
Delete a repository on Github.
* hub-fork(1):
Fork the current project on GitHub and add a git remote for it.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册