本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.3KB

  1. // Copyright 2014 The go-github AUTHORS. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. package github
  6. import (
  7. "context"
  8. "fmt"
  9. )
  10. // Pages represents a GitHub Pages site configuration.
  11. type Pages struct {
  12. URL *string `json:"url,omitempty"`
  13. Status *string `json:"status,omitempty"`
  14. CNAME *string `json:"cname,omitempty"`
  15. Custom404 *bool `json:"custom_404,omitempty"`
  16. HTMLURL *string `json:"html_url,omitempty"`
  17. }
  18. // PagesError represents a build error for a GitHub Pages site.
  19. type PagesError struct {
  20. Message *string `json:"message,omitempty"`
  21. }
  22. // PagesBuild represents the build information for a GitHub Pages site.
  23. type PagesBuild struct {
  24. URL *string `json:"url,omitempty"`
  25. Status *string `json:"status,omitempty"`
  26. Error *PagesError `json:"error,omitempty"`
  27. Pusher *User `json:"pusher,omitempty"`
  28. Commit *string `json:"commit,omitempty"`
  29. Duration *int `json:"duration,omitempty"`
  30. CreatedAt *Timestamp `json:"created_at,omitempty"`
  31. UpdatedAt *Timestamp `json:"updated_at,omitempty"`
  32. }
  33. // GetPagesInfo fetches information about a GitHub Pages site.
  34. //
  35. // GitHub API docs: https://developer.github.com/v3/repos/pages/#get-information-about-a-pages-site
  36. func (s *RepositoriesService) GetPagesInfo(ctx context.Context, owner, repo string) (*Pages, *Response, error) {
  37. u := fmt.Sprintf("repos/%v/%v/pages", owner, repo)
  38. req, err := s.client.NewRequest("GET", u, nil)
  39. if err != nil {
  40. return nil, nil, err
  41. }
  42. // TODO: remove custom Accept header when this API fully launches.
  43. req.Header.Set("Accept", mediaTypePagesPreview)
  44. site := new(Pages)
  45. resp, err := s.client.Do(ctx, req, site)
  46. if err != nil {
  47. return nil, resp, err
  48. }
  49. return site, resp, nil
  50. }
  51. // ListPagesBuilds lists the builds for a GitHub Pages site.
  52. //
  53. // GitHub API docs: https://developer.github.com/v3/repos/pages/#list-pages-builds
  54. func (s *RepositoriesService) ListPagesBuilds(ctx context.Context, owner, repo string, opt *ListOptions) ([]*PagesBuild, *Response, error) {
  55. u := fmt.Sprintf("repos/%v/%v/pages/builds", owner, repo)
  56. u, err := addOptions(u, opt)
  57. if err != nil {
  58. return nil, nil, err
  59. }
  60. req, err := s.client.NewRequest("GET", u, nil)
  61. if err != nil {
  62. return nil, nil, err
  63. }
  64. var pages []*PagesBuild
  65. resp, err := s.client.Do(ctx, req, &pages)
  66. if err != nil {
  67. return nil, resp, err
  68. }
  69. return pages, resp, nil
  70. }
  71. // GetLatestPagesBuild fetches the latest build information for a GitHub pages site.
  72. //
  73. // GitHub API docs: https://developer.github.com/v3/repos/pages/#list-latest-pages-build
  74. func (s *RepositoriesService) GetLatestPagesBuild(ctx context.Context, owner, repo string) (*PagesBuild, *Response, error) {
  75. u := fmt.Sprintf("repos/%v/%v/pages/builds/latest", owner, repo)
  76. req, err := s.client.NewRequest("GET", u, nil)
  77. if err != nil {
  78. return nil, nil, err
  79. }
  80. build := new(PagesBuild)
  81. resp, err := s.client.Do(ctx, req, build)
  82. if err != nil {
  83. return nil, resp, err
  84. }
  85. return build, resp, nil
  86. }
  87. // GetPageBuild fetches the specific build information for a GitHub pages site.
  88. //
  89. // GitHub API docs: https://developer.github.com/v3/repos/pages/#list-a-specific-pages-build
  90. func (s *RepositoriesService) GetPageBuild(ctx context.Context, owner, repo string, id int64) (*PagesBuild, *Response, error) {
  91. u := fmt.Sprintf("repos/%v/%v/pages/builds/%v", owner, repo, id)
  92. req, err := s.client.NewRequest("GET", u, nil)
  93. if err != nil {
  94. return nil, nil, err
  95. }
  96. build := new(PagesBuild)
  97. resp, err := s.client.Do(ctx, req, build)
  98. if err != nil {
  99. return nil, resp, err
  100. }
  101. return build, resp, nil
  102. }
  103. // RequestPageBuild requests a build of a GitHub Pages site without needing to push new commit.
  104. //
  105. // GitHub API docs: https://developer.github.com/v3/repos/pages/#request-a-page-build
  106. func (s *RepositoriesService) RequestPageBuild(ctx context.Context, owner, repo string) (*PagesBuild, *Response, error) {
  107. u := fmt.Sprintf("repos/%v/%v/pages/builds", owner, repo)
  108. req, err := s.client.NewRequest("POST", u, nil)
  109. if err != nil {
  110. return nil, nil, err
  111. }
  112. // TODO: remove custom Accept header when this API fully launches.
  113. req.Header.Set("Accept", mediaTypePagesPreview)
  114. build := new(PagesBuild)
  115. resp, err := s.client.Do(ctx, req, build)
  116. if err != nil {
  117. return nil, resp, err
  118. }
  119. return build, resp, nil
  120. }
上海开阖软件有限公司 沪ICP备12045867号-1