本文整理匯總了Golang中github.com/github/hub/github.NewClient函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewClient函數的具體用法?Golang NewClient怎麽用?Golang NewClient使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewClient函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: downloadRelease
func downloadRelease(cmd *Command, args *Args) {
tagName := cmd.Arg(0)
if tagName == "" {
utils.Check(fmt.Errorf("Missing argument TAG"))
}
localRepo, err := github.LocalRepo()
utils.Check(err)
project, err := localRepo.MainProject()
utils.Check(err)
gh := github.NewClient(project.Host)
release, err := gh.FetchRelease(project, tagName)
utils.Check(err)
for _, asset := range release.Assets {
ui.Printf("Downloading %s ...\n", asset.Name)
err := downloadReleaseAsset(asset, gh)
utils.Check(err)
}
args.NoForward()
}
示例2: fetchCiStatus
func fetchCiStatus(p *github.Project, sha string) (state, targetURL string, exitCode int, err error) {
gh := github.NewClient(p.Host)
status, err := gh.CIStatus(p, sha)
if err != nil {
return
}
if status == nil {
state = "no status"
} else {
state = status.State
targetURL = status.TargetURL
}
switch state {
case "success":
exitCode = 0
case "failure", "error":
exitCode = 1
case "pending":
exitCode = 2
default:
exitCode = 3
}
return
}
示例3: tranformFetchArgs
func tranformFetchArgs(args *Args) error {
names := parseRemoteNames(args)
localRepo, err := github.LocalRepo()
utils.Check(err)
currentProject, currentProjectErr := localRepo.CurrentProject()
projects := make(map[*github.Project]bool)
ownerRegexp := regexp.MustCompile(fmt.Sprintf("^%s$", OwnerRe))
for _, name := range names {
if ownerRegexp.MatchString(name) && !isCloneable(name) {
_, err := localRepo.RemoteByName(name)
if err != nil {
utils.Check(currentProjectErr)
project := github.NewProject(name, currentProject.Name, "")
gh := github.NewClient(project.Host)
repo, err := gh.Repository(project)
if err != nil {
continue
}
projects[project] = repo.Private
}
}
}
for project, private := range projects {
args.Before("git", "remote", "add", project.Owner, project.GitURL("", "", private))
}
return nil
}
示例4: create
func create(command *Command, args *Args) {
_, err := git.Dir()
if err != nil {
err = fmt.Errorf("'create' must be run from inside a git repository")
utils.Check(err)
}
var newRepoName string
if args.IsParamsEmpty() {
dirName, err := git.WorkdirName()
utils.Check(err)
newRepoName = github.SanitizeProjectName(dirName)
} else {
reg := regexp.MustCompile("^[^-]")
if !reg.MatchString(args.FirstParam()) {
err = fmt.Errorf("invalid argument: %s", args.FirstParam())
utils.Check(err)
}
newRepoName = args.FirstParam()
}
config := github.CurrentConfig()
host, err := config.DefaultHost()
if err != nil {
utils.Check(github.FormatError("creating repository", err))
}
owner := host.User
if strings.Contains(newRepoName, "/") {
split := strings.SplitN(newRepoName, "/", 2)
owner = split[0]
newRepoName = split[1]
}
project := github.NewProject(owner, newRepoName, host.Host)
gh := github.NewClient(project.Host)
if gh.IsRepositoryExist(project) {
ui.Errorln("Existing repository detected. Updating git remote")
} else {
if !args.Noop {
repo, err := gh.CreateRepository(project, flagCreateDescription, flagCreateHomepage, flagCreatePrivate)
utils.Check(err)
project = github.NewProject(repo.FullName, "", project.Host)
}
}
localRepo, err := github.LocalRepo()
utils.Check(err)
remote, _ := localRepo.OriginRemote()
if remote == nil || remote.Name != "origin" {
url := project.GitURL("", "", true)
args.Before("git", "remote", "add", "-f", "origin", url)
}
webUrl := project.WebURL("", "", "")
args.NoForward()
printBrowseOrCopy(args, webUrl, flagCreateBrowse, flagCreateCopy)
}
示例5: transformApplyArgs
func transformApplyArgs(args *Args) {
gistRegexp := regexp.MustCompile("^https?://gist\\.github\\.com/([\\w.-]+/)?([a-f0-9]+)")
pullRegexp := regexp.MustCompile("^(pull|commit)/([0-9a-f]+)")
for _, arg := range args.Params {
var (
patch io.ReadCloser
apiError error
)
projectURL, err := github.ParseURL(arg)
if err == nil {
gh := github.NewClient(projectURL.Project.Host)
match := pullRegexp.FindStringSubmatch(projectURL.ProjectPath())
if match != nil {
if match[1] == "pull" {
patch, apiError = gh.PullRequestPatch(projectURL.Project, match[2])
} else {
patch, apiError = gh.CommitPatch(projectURL.Project, match[2])
}
}
} else {
match := gistRegexp.FindStringSubmatch(arg)
if match != nil {
// TODO: support Enterprise gist
gh := github.NewClient(github.GitHubHost)
patch, apiError = gh.GistPatch(match[2])
}
}
utils.Check(apiError)
if patch == nil {
continue
}
idx := args.IndexOfParam(arg)
patchFile, err := ioutil.TempFile("", "hub")
utils.Check(err)
_, err = io.Copy(patchFile, patch)
utils.Check(err)
patchFile.Close()
patch.Close()
args.Params[idx] = patchFile.Name()
}
}
示例6: fork
func fork(cmd *Command, args *Args) {
localRepo, err := github.LocalRepo()
utils.Check(err)
project, err := localRepo.MainProject()
if err != nil {
utils.Check(fmt.Errorf("Error: repository under 'origin' remote is not a GitHub project"))
}
config := github.CurrentConfig()
host, err := config.PromptForHost(project.Host)
if err != nil {
utils.Check(github.FormatError("forking repository", err))
}
originRemote, err := localRepo.OriginRemote()
if err != nil {
utils.Check(fmt.Errorf("Error creating fork: %s", err))
}
forkProject := github.NewProject(host.User, project.Name, project.Host)
newRemoteName := forkProject.Owner
client := github.NewClient(project.Host)
existingRepo, err := client.Repository(forkProject)
if err == nil {
var parentURL *github.URL
if parent := existingRepo.Parent; parent != nil {
parentURL, _ = github.ParseURL(parent.HTMLURL)
}
if parentURL == nil || !project.SameAs(parentURL.Project) {
err = fmt.Errorf("Error creating fork: %s already exists on %s",
forkProject, forkProject.Host)
utils.Check(err)
}
} else {
if !args.Noop {
newRepo, err := client.ForkRepository(project)
utils.Check(err)
forkProject.Owner = newRepo.Owner.Login
forkProject.Name = newRepo.Name
}
}
args.NoForward()
if !flagForkNoRemote {
originURL := originRemote.URL.String()
url := forkProject.GitURL("", "", true)
args.Before("git", "remote", "add", "-f", newRemoteName, originURL)
args.Before("git", "remote", "set-url", newRemoteName, url)
args.AfterFn(func() error {
ui.Printf("new remote: %s\n", newRemoteName)
return nil
})
}
}
示例7: transformMergeArgs
func transformMergeArgs(args *Args) error {
words := args.Words()
if len(words) == 0 {
return nil
}
mergeURL := words[0]
url, err := github.ParseURL(mergeURL)
if err != nil {
return nil
}
pullURLRegex := regexp.MustCompile("^pull/(\\d+)")
projectPath := url.ProjectPath()
if !pullURLRegex.MatchString(projectPath) {
return nil
}
id := pullURLRegex.FindStringSubmatch(projectPath)[1]
gh := github.NewClient(url.Project.Host)
pullRequest, err := gh.PullRequest(url.Project, id)
if err != nil {
return err
}
repo, err := github.LocalRepo()
if err != nil {
return err
}
remote, err := repo.RemoteForRepo(pullRequest.Base.Repo)
if err != nil {
return err
}
branch := pullRequest.Head.Ref
headRepo := pullRequest.Head.Repo
if headRepo == nil {
return fmt.Errorf("Error: that fork is not available anymore")
}
args.Before("git", "fetch", remote.Name, fmt.Sprintf("refs/pull/%s/head", id))
// Remove pull request URL
idx := args.IndexOfParam(mergeURL)
args.RemoveParam(idx)
mergeMsg := fmt.Sprintf("Merge pull request #%s from %s/%s\n\n%s", id, headRepo.Owner.Login, branch, pullRequest.Title)
args.AppendParams("FETCH_HEAD", "-m", mergeMsg)
if args.IndexOfParam("--ff-only") == -1 && args.IndexOfParam("--squash") == -1 && args.IndexOfParam("--ff") == -1 {
i := args.IndexOfParam("-m")
args.InsertParam(i, "--no-ff")
}
return nil
}
示例8: ciStatus
func ciStatus(cmd *Command, args *Args) {
ref := "HEAD"
if !args.IsParamsEmpty() {
ref = args.RemoveParam(0)
}
localRepo, err := github.LocalRepo()
utils.Check(err)
project, err := localRepo.MainProject()
utils.Check(err)
sha, err := git.Ref(ref)
if err != nil {
err = fmt.Errorf("Aborted: no revision could be determined from '%s'", ref)
}
utils.Check(err)
if args.Noop {
ui.Printf("Would request CI status for %s\n", sha)
} else {
gh := github.NewClient(project.Host)
response, err := gh.FetchCIStatus(project, sha)
utils.Check(err)
state := response.State
if len(response.Statuses) == 0 {
state = ""
}
var exitCode int
switch state {
case "success":
exitCode = 0
case "failure", "error":
exitCode = 1
case "pending":
exitCode = 2
default:
exitCode = 3
}
if flagCiStatusVerbose && len(response.Statuses) > 0 {
verboseFormat(response.Statuses)
} else {
if state != "" {
ui.Println(state)
} else {
ui.Println("no status")
}
}
os.Exit(exitCode)
}
}
示例9: runInLocalRepo
func runInLocalRepo(fn func(localRepo *github.GitHubRepo, project *github.Project, client *github.Client)) {
localRepo, err := github.LocalRepo()
utils.Check(err)
project, err := localRepo.CurrentProject()
utils.Check(err)
client := github.NewClient(project.Host)
fn(localRepo, project, client)
os.Exit(0)
}
示例10: transformCloneArgs
func transformCloneArgs(args *Args) {
isSSH := parseClonePrivateFlag(args)
hasValueRegxp := regexp.MustCompile("^(--(upload-pack|template|depth|origin|branch|reference|name)|-[ubo])$")
nameWithOwnerRegexp := regexp.MustCompile(NameWithOwnerRe)
for i := 0; i < args.ParamsSize(); i++ {
a := args.Params[i]
if strings.HasPrefix(a, "-") {
if hasValueRegxp.MatchString(a) {
i++
}
} else {
if nameWithOwnerRegexp.MatchString(a) && !isDir(a) {
name, owner := parseCloneNameAndOwner(a)
var host *github.Host
if owner == "" {
config := github.CurrentConfig()
h, err := config.DefaultHost()
if err != nil {
utils.Check(github.FormatError("cloning repository", err))
}
host = h
owner = host.User
}
var hostStr string
if host != nil {
hostStr = host.Host
}
project := github.NewProject(owner, name, hostStr)
if !isSSH &&
args.Command != "submodule" &&
!github.IsHttpsProtocol() {
client := github.NewClient(project.Host)
repo, err := client.Repository(project)
isSSH = (err == nil) && (repo.Private || repo.Permissions.Push)
}
url := project.GitURL(name, owner, isSSH)
args.ReplaceParam(i, url)
}
break
}
}
}
示例11: discover
func discover(cmd *Command, args *Args) {
if args.ParamsSize() < 1 {
utils.Check(fmt.Errorf("Error: you must specify a username."))
}
client := github.NewClient(github.GitHubHost)
repos, err := client.Repositories(args.GetParam(0))
if err != nil {
utils.Check(err)
}
for _, repo := range repos {
fmt.Println(repo.Name)
}
os.Exit(0)
}
示例12: parseCherryPickProjectAndSha
func parseCherryPickProjectAndSha(ref string) (project *github.Project, sha string, isPrivate bool) {
shaRe := "[a-f0-9]{7,40}"
var mainProject *github.Project
localRepo, mainProjectErr := github.LocalRepo()
if mainProjectErr == nil {
mainProject, mainProjectErr = localRepo.MainProject()
}
url, err := github.ParseURL(ref)
if err == nil {
projectPath := url.ProjectPath()
commitRegex := regexp.MustCompile(fmt.Sprintf("^commit/(%s)", shaRe))
if matches := commitRegex.FindStringSubmatch(projectPath); len(matches) > 0 {
sha = matches[1]
project = url.Project
return
}
pullRegex := regexp.MustCompile(fmt.Sprintf(`^pull/(\d+)/commits/(%s)`, shaRe))
if matches := pullRegex.FindStringSubmatch(projectPath); len(matches) > 0 {
pullId := matches[1]
sha = matches[2]
utils.Check(mainProjectErr)
api := github.NewClient(mainProject.Host)
pullRequest, err := api.PullRequest(url.Project, pullId)
utils.Check(err)
headRepo := pullRequest.Head.Repo
project = github.NewProject(headRepo.Owner.Login, headRepo.Name, mainProject.Host)
isPrivate = headRepo.Private
return
}
}
ownerWithShaRegexp := regexp.MustCompile(fmt.Sprintf("^(%s)@(%s)$", OwnerRe, shaRe))
if matches := ownerWithShaRegexp.FindStringSubmatch(ref); len(matches) > 0 {
utils.Check(mainProjectErr)
project = mainProject
project.Owner = matches[1]
sha = matches[2]
}
return
}
示例13: listIssues
func listIssues(cmd *Command, args *Args) {
localRepo, err := github.LocalRepo()
utils.Check(err)
project, err := localRepo.MainProject()
utils.Check(err)
gh := github.NewClient(project.Host)
if args.Noop {
ui.Printf("Would request list of issues for %s\n", project)
} else {
filters := map[string]interface{}{}
if cmd.FlagPassed("state") {
filters["state"] = flagIssueState
}
if cmd.FlagPassed("assignee") {
filters["assignee"] = flagIssueAssignee
}
issues, err := gh.FetchIssues(project, filters)
utils.Check(err)
maxNumWidth := 0
for _, issue := range issues {
if numWidth := len(strconv.Itoa(issue.Number)); numWidth > maxNumWidth {
maxNumWidth = numWidth
}
}
colorize := ui.IsTerminal(os.Stdout)
for _, issue := range issues {
if issue.PullRequest != nil {
continue
}
ui.Printf(formatIssue(issue, flagIssueFormat, colorize))
}
}
os.Exit(0)
}
示例14: showRelease
func showRelease(cmd *Command, args *Args) {
tagName := cmd.Arg(0)
if tagName == "" {
utils.Check(fmt.Errorf("Missing argument TAG"))
}
localRepo, err := github.LocalRepo()
utils.Check(err)
project, err := localRepo.MainProject()
utils.Check(err)
gh := github.NewClient(project.Host)
if args.Noop {
ui.Printf("Would display information for `%s' release\n", tagName)
} else {
release, err := gh.FetchRelease(project, tagName)
utils.Check(err)
body := strings.TrimSpace(release.Body)
ui.Println(release.Name)
if body != "" {
ui.Printf("\n%s\n", body)
}
if flagReleaseShowDownloads {
ui.Printf("\n## Downloads\n\n")
for _, asset := range release.Assets {
ui.Println(asset.DownloadUrl)
}
if release.ZipballUrl != "" {
ui.Println(release.ZipballUrl)
ui.Println(release.TarballUrl)
}
}
}
args.NoForward()
}
示例15: listReleases
func listReleases(cmd *Command, args *Args) {
localRepo, err := github.LocalRepo()
utils.Check(err)
project, err := localRepo.MainProject()
utils.Check(err)
gh := github.NewClient(project.Host)
if args.Noop {
ui.Printf("Would request list of releases for %s\n", project)
} else {
releases, err := gh.FetchReleases(project)
utils.Check(err)
for _, release := range releases {
if !release.Draft || flagReleaseIncludeDrafts {
ui.Println(release.TagName)
}
}
}
args.NoForward()
}