本站源代码
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.

1198 lines
44KB

  1. // Copyright 2013 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. "strings"
  10. )
  11. // RepositoriesService handles communication with the repository related
  12. // methods of the GitHub API.
  13. //
  14. // GitHub API docs: https://developer.github.com/v3/repos/
  15. type RepositoriesService service
  16. // Repository represents a GitHub repository.
  17. type Repository struct {
  18. ID *int64 `json:"id,omitempty"`
  19. NodeID *string `json:"node_id,omitempty"`
  20. Owner *User `json:"owner,omitempty"`
  21. Name *string `json:"name,omitempty"`
  22. FullName *string `json:"full_name,omitempty"`
  23. Description *string `json:"description,omitempty"`
  24. Homepage *string `json:"homepage,omitempty"`
  25. CodeOfConduct *CodeOfConduct `json:"code_of_conduct,omitempty"`
  26. DefaultBranch *string `json:"default_branch,omitempty"`
  27. MasterBranch *string `json:"master_branch,omitempty"`
  28. CreatedAt *Timestamp `json:"created_at,omitempty"`
  29. PushedAt *Timestamp `json:"pushed_at,omitempty"`
  30. UpdatedAt *Timestamp `json:"updated_at,omitempty"`
  31. HTMLURL *string `json:"html_url,omitempty"`
  32. CloneURL *string `json:"clone_url,omitempty"`
  33. GitURL *string `json:"git_url,omitempty"`
  34. MirrorURL *string `json:"mirror_url,omitempty"`
  35. SSHURL *string `json:"ssh_url,omitempty"`
  36. SVNURL *string `json:"svn_url,omitempty"`
  37. Language *string `json:"language,omitempty"`
  38. Fork *bool `json:"fork,omitempty"`
  39. ForksCount *int `json:"forks_count,omitempty"`
  40. NetworkCount *int `json:"network_count,omitempty"`
  41. OpenIssuesCount *int `json:"open_issues_count,omitempty"`
  42. StargazersCount *int `json:"stargazers_count,omitempty"`
  43. SubscribersCount *int `json:"subscribers_count,omitempty"`
  44. WatchersCount *int `json:"watchers_count,omitempty"`
  45. Size *int `json:"size,omitempty"`
  46. AutoInit *bool `json:"auto_init,omitempty"`
  47. Parent *Repository `json:"parent,omitempty"`
  48. Source *Repository `json:"source,omitempty"`
  49. Organization *Organization `json:"organization,omitempty"`
  50. Permissions *map[string]bool `json:"permissions,omitempty"`
  51. AllowRebaseMerge *bool `json:"allow_rebase_merge,omitempty"`
  52. AllowSquashMerge *bool `json:"allow_squash_merge,omitempty"`
  53. AllowMergeCommit *bool `json:"allow_merge_commit,omitempty"`
  54. Topics []string `json:"topics,omitempty"`
  55. Archived *bool `json:"archived,omitempty"`
  56. // Only provided when using RepositoriesService.Get while in preview
  57. License *License `json:"license,omitempty"`
  58. // Additional mutable fields when creating and editing a repository
  59. Private *bool `json:"private,omitempty"`
  60. HasIssues *bool `json:"has_issues,omitempty"`
  61. HasWiki *bool `json:"has_wiki,omitempty"`
  62. HasPages *bool `json:"has_pages,omitempty"`
  63. HasProjects *bool `json:"has_projects,omitempty"`
  64. HasDownloads *bool `json:"has_downloads,omitempty"`
  65. LicenseTemplate *string `json:"license_template,omitempty"`
  66. GitignoreTemplate *string `json:"gitignore_template,omitempty"`
  67. // Creating an organization repository. Required for non-owners.
  68. TeamID *int64 `json:"team_id,omitempty"`
  69. // API URLs
  70. URL *string `json:"url,omitempty"`
  71. ArchiveURL *string `json:"archive_url,omitempty"`
  72. AssigneesURL *string `json:"assignees_url,omitempty"`
  73. BlobsURL *string `json:"blobs_url,omitempty"`
  74. BranchesURL *string `json:"branches_url,omitempty"`
  75. CollaboratorsURL *string `json:"collaborators_url,omitempty"`
  76. CommentsURL *string `json:"comments_url,omitempty"`
  77. CommitsURL *string `json:"commits_url,omitempty"`
  78. CompareURL *string `json:"compare_url,omitempty"`
  79. ContentsURL *string `json:"contents_url,omitempty"`
  80. ContributorsURL *string `json:"contributors_url,omitempty"`
  81. DeploymentsURL *string `json:"deployments_url,omitempty"`
  82. DownloadsURL *string `json:"downloads_url,omitempty"`
  83. EventsURL *string `json:"events_url,omitempty"`
  84. ForksURL *string `json:"forks_url,omitempty"`
  85. GitCommitsURL *string `json:"git_commits_url,omitempty"`
  86. GitRefsURL *string `json:"git_refs_url,omitempty"`
  87. GitTagsURL *string `json:"git_tags_url,omitempty"`
  88. HooksURL *string `json:"hooks_url,omitempty"`
  89. IssueCommentURL *string `json:"issue_comment_url,omitempty"`
  90. IssueEventsURL *string `json:"issue_events_url,omitempty"`
  91. IssuesURL *string `json:"issues_url,omitempty"`
  92. KeysURL *string `json:"keys_url,omitempty"`
  93. LabelsURL *string `json:"labels_url,omitempty"`
  94. LanguagesURL *string `json:"languages_url,omitempty"`
  95. MergesURL *string `json:"merges_url,omitempty"`
  96. MilestonesURL *string `json:"milestones_url,omitempty"`
  97. NotificationsURL *string `json:"notifications_url,omitempty"`
  98. PullsURL *string `json:"pulls_url,omitempty"`
  99. ReleasesURL *string `json:"releases_url,omitempty"`
  100. StargazersURL *string `json:"stargazers_url,omitempty"`
  101. StatusesURL *string `json:"statuses_url,omitempty"`
  102. SubscribersURL *string `json:"subscribers_url,omitempty"`
  103. SubscriptionURL *string `json:"subscription_url,omitempty"`
  104. TagsURL *string `json:"tags_url,omitempty"`
  105. TreesURL *string `json:"trees_url,omitempty"`
  106. TeamsURL *string `json:"teams_url,omitempty"`
  107. // TextMatches is only populated from search results that request text matches
  108. // See: search.go and https://developer.github.com/v3/search/#text-match-metadata
  109. TextMatches []TextMatch `json:"text_matches,omitempty"`
  110. }
  111. func (r Repository) String() string {
  112. return Stringify(r)
  113. }
  114. // RepositoryListOptions specifies the optional parameters to the
  115. // RepositoriesService.List method.
  116. type RepositoryListOptions struct {
  117. // Visibility of repositories to list. Can be one of all, public, or private.
  118. // Default: all
  119. Visibility string `url:"visibility,omitempty"`
  120. // List repos of given affiliation[s].
  121. // Comma-separated list of values. Can include:
  122. // * owner: Repositories that are owned by the authenticated user.
  123. // * collaborator: Repositories that the user has been added to as a
  124. // collaborator.
  125. // * organization_member: Repositories that the user has access to through
  126. // being a member of an organization. This includes every repository on
  127. // every team that the user is on.
  128. // Default: owner,collaborator,organization_member
  129. Affiliation string `url:"affiliation,omitempty"`
  130. // Type of repositories to list.
  131. // Can be one of all, owner, public, private, member. Default: all
  132. // Will cause a 422 error if used in the same request as visibility or
  133. // affiliation.
  134. Type string `url:"type,omitempty"`
  135. // How to sort the repository list. Can be one of created, updated, pushed,
  136. // full_name. Default: full_name
  137. Sort string `url:"sort,omitempty"`
  138. // Direction in which to sort repositories. Can be one of asc or desc.
  139. // Default: when using full_name: asc; otherwise desc
  140. Direction string `url:"direction,omitempty"`
  141. ListOptions
  142. }
  143. // List the repositories for a user. Passing the empty string will list
  144. // repositories for the authenticated user.
  145. //
  146. // GitHub API docs: https://developer.github.com/v3/repos/#list-user-repositories
  147. func (s *RepositoriesService) List(ctx context.Context, user string, opt *RepositoryListOptions) ([]*Repository, *Response, error) {
  148. var u string
  149. if user != "" {
  150. u = fmt.Sprintf("users/%v/repos", user)
  151. } else {
  152. u = "user/repos"
  153. }
  154. u, err := addOptions(u, opt)
  155. if err != nil {
  156. return nil, nil, err
  157. }
  158. req, err := s.client.NewRequest("GET", u, nil)
  159. if err != nil {
  160. return nil, nil, err
  161. }
  162. // TODO: remove custom Accept headers when APIs fully launch.
  163. acceptHeaders := []string{mediaTypeTopicsPreview}
  164. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  165. var repos []*Repository
  166. resp, err := s.client.Do(ctx, req, &repos)
  167. if err != nil {
  168. return nil, resp, err
  169. }
  170. return repos, resp, nil
  171. }
  172. // RepositoryListByOrgOptions specifies the optional parameters to the
  173. // RepositoriesService.ListByOrg method.
  174. type RepositoryListByOrgOptions struct {
  175. // Type of repositories to list. Possible values are: all, public, private,
  176. // forks, sources, member. Default is "all".
  177. Type string `url:"type,omitempty"`
  178. ListOptions
  179. }
  180. // ListByOrg lists the repositories for an organization.
  181. //
  182. // GitHub API docs: https://developer.github.com/v3/repos/#list-organization-repositories
  183. func (s *RepositoriesService) ListByOrg(ctx context.Context, org string, opt *RepositoryListByOrgOptions) ([]*Repository, *Response, error) {
  184. u := fmt.Sprintf("orgs/%v/repos", org)
  185. u, err := addOptions(u, opt)
  186. if err != nil {
  187. return nil, nil, err
  188. }
  189. req, err := s.client.NewRequest("GET", u, nil)
  190. if err != nil {
  191. return nil, nil, err
  192. }
  193. // TODO: remove custom Accept headers when APIs fully launch.
  194. acceptHeaders := []string{mediaTypeTopicsPreview}
  195. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  196. var repos []*Repository
  197. resp, err := s.client.Do(ctx, req, &repos)
  198. if err != nil {
  199. return nil, resp, err
  200. }
  201. return repos, resp, nil
  202. }
  203. // RepositoryListAllOptions specifies the optional parameters to the
  204. // RepositoriesService.ListAll method.
  205. type RepositoryListAllOptions struct {
  206. // ID of the last repository seen
  207. Since int64 `url:"since,omitempty"`
  208. }
  209. // ListAll lists all GitHub repositories in the order that they were created.
  210. //
  211. // GitHub API docs: https://developer.github.com/v3/repos/#list-all-public-repositories
  212. func (s *RepositoriesService) ListAll(ctx context.Context, opt *RepositoryListAllOptions) ([]*Repository, *Response, error) {
  213. u, err := addOptions("repositories", opt)
  214. if err != nil {
  215. return nil, nil, err
  216. }
  217. req, err := s.client.NewRequest("GET", u, nil)
  218. if err != nil {
  219. return nil, nil, err
  220. }
  221. var repos []*Repository
  222. resp, err := s.client.Do(ctx, req, &repos)
  223. if err != nil {
  224. return nil, resp, err
  225. }
  226. return repos, resp, nil
  227. }
  228. // createRepoRequest is a subset of Repository and is used internally
  229. // by Create to pass only the known fields for the endpoint.
  230. //
  231. // See https://github.com/google/go-github/issues/1014 for more
  232. // information.
  233. type createRepoRequest struct {
  234. // Name is required when creating a repo.
  235. Name *string `json:"name,omitempty"`
  236. Description *string `json:"description,omitempty"`
  237. Homepage *string `json:"homepage,omitempty"`
  238. Private *bool `json:"private,omitempty"`
  239. HasIssues *bool `json:"has_issues,omitempty"`
  240. HasProjects *bool `json:"has_projects,omitempty"`
  241. HasWiki *bool `json:"has_wiki,omitempty"`
  242. // Creating an organization repository. Required for non-owners.
  243. TeamID *int64 `json:"team_id,omitempty"`
  244. AutoInit *bool `json:"auto_init,omitempty"`
  245. GitignoreTemplate *string `json:"gitignore_template,omitempty"`
  246. LicenseTemplate *string `json:"license_template,omitempty"`
  247. AllowSquashMerge *bool `json:"allow_squash_merge,omitempty"`
  248. AllowMergeCommit *bool `json:"allow_merge_commit,omitempty"`
  249. AllowRebaseMerge *bool `json:"allow_rebase_merge,omitempty"`
  250. }
  251. // Create a new repository. If an organization is specified, the new
  252. // repository will be created under that org. If the empty string is
  253. // specified, it will be created for the authenticated user.
  254. //
  255. // Note that only a subset of the repo fields are used and repo must
  256. // not be nil.
  257. //
  258. // GitHub API docs: https://developer.github.com/v3/repos/#create
  259. func (s *RepositoriesService) Create(ctx context.Context, org string, repo *Repository) (*Repository, *Response, error) {
  260. var u string
  261. if org != "" {
  262. u = fmt.Sprintf("orgs/%v/repos", org)
  263. } else {
  264. u = "user/repos"
  265. }
  266. repoReq := &createRepoRequest{
  267. Name: repo.Name,
  268. Description: repo.Description,
  269. Homepage: repo.Homepage,
  270. Private: repo.Private,
  271. HasIssues: repo.HasIssues,
  272. HasProjects: repo.HasProjects,
  273. HasWiki: repo.HasWiki,
  274. TeamID: repo.TeamID,
  275. AutoInit: repo.AutoInit,
  276. GitignoreTemplate: repo.GitignoreTemplate,
  277. LicenseTemplate: repo.LicenseTemplate,
  278. AllowSquashMerge: repo.AllowSquashMerge,
  279. AllowMergeCommit: repo.AllowMergeCommit,
  280. AllowRebaseMerge: repo.AllowRebaseMerge,
  281. }
  282. req, err := s.client.NewRequest("POST", u, repoReq)
  283. if err != nil {
  284. return nil, nil, err
  285. }
  286. r := new(Repository)
  287. resp, err := s.client.Do(ctx, req, r)
  288. if err != nil {
  289. return nil, resp, err
  290. }
  291. return r, resp, nil
  292. }
  293. // Get fetches a repository.
  294. //
  295. // GitHub API docs: https://developer.github.com/v3/repos/#get
  296. func (s *RepositoriesService) Get(ctx context.Context, owner, repo string) (*Repository, *Response, error) {
  297. u := fmt.Sprintf("repos/%v/%v", owner, repo)
  298. req, err := s.client.NewRequest("GET", u, nil)
  299. if err != nil {
  300. return nil, nil, err
  301. }
  302. // TODO: remove custom Accept header when the license support fully launches
  303. // https://developer.github.com/v3/licenses/#get-a-repositorys-license
  304. acceptHeaders := []string{mediaTypeCodesOfConductPreview, mediaTypeTopicsPreview}
  305. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  306. repository := new(Repository)
  307. resp, err := s.client.Do(ctx, req, repository)
  308. if err != nil {
  309. return nil, resp, err
  310. }
  311. return repository, resp, nil
  312. }
  313. // GetCodeOfConduct gets the contents of a repository's code of conduct.
  314. //
  315. // GitHub API docs: https://developer.github.com/v3/codes_of_conduct/#get-the-contents-of-a-repositorys-code-of-conduct
  316. func (s *RepositoriesService) GetCodeOfConduct(ctx context.Context, owner, repo string) (*CodeOfConduct, *Response, error) {
  317. u := fmt.Sprintf("repos/%v/%v/community/code_of_conduct", owner, repo)
  318. req, err := s.client.NewRequest("GET", u, nil)
  319. if err != nil {
  320. return nil, nil, err
  321. }
  322. // TODO: remove custom Accept header when this API fully launches.
  323. req.Header.Set("Accept", mediaTypeCodesOfConductPreview)
  324. coc := new(CodeOfConduct)
  325. resp, err := s.client.Do(ctx, req, coc)
  326. if err != nil {
  327. return nil, resp, err
  328. }
  329. return coc, resp, nil
  330. }
  331. // GetByID fetches a repository.
  332. //
  333. // Note: GetByID uses the undocumented GitHub API endpoint /repositories/:id.
  334. func (s *RepositoriesService) GetByID(ctx context.Context, id int64) (*Repository, *Response, error) {
  335. u := fmt.Sprintf("repositories/%d", id)
  336. req, err := s.client.NewRequest("GET", u, nil)
  337. if err != nil {
  338. return nil, nil, err
  339. }
  340. repository := new(Repository)
  341. resp, err := s.client.Do(ctx, req, repository)
  342. if err != nil {
  343. return nil, resp, err
  344. }
  345. return repository, resp, nil
  346. }
  347. // Edit updates a repository.
  348. //
  349. // GitHub API docs: https://developer.github.com/v3/repos/#edit
  350. func (s *RepositoriesService) Edit(ctx context.Context, owner, repo string, repository *Repository) (*Repository, *Response, error) {
  351. u := fmt.Sprintf("repos/%v/%v", owner, repo)
  352. req, err := s.client.NewRequest("PATCH", u, repository)
  353. if err != nil {
  354. return nil, nil, err
  355. }
  356. r := new(Repository)
  357. resp, err := s.client.Do(ctx, req, r)
  358. if err != nil {
  359. return nil, resp, err
  360. }
  361. return r, resp, nil
  362. }
  363. // Delete a repository.
  364. //
  365. // GitHub API docs: https://developer.github.com/v3/repos/#delete-a-repository
  366. func (s *RepositoriesService) Delete(ctx context.Context, owner, repo string) (*Response, error) {
  367. u := fmt.Sprintf("repos/%v/%v", owner, repo)
  368. req, err := s.client.NewRequest("DELETE", u, nil)
  369. if err != nil {
  370. return nil, err
  371. }
  372. return s.client.Do(ctx, req, nil)
  373. }
  374. // Contributor represents a repository contributor
  375. type Contributor struct {
  376. Login *string `json:"login,omitempty"`
  377. ID *int64 `json:"id,omitempty"`
  378. AvatarURL *string `json:"avatar_url,omitempty"`
  379. GravatarID *string `json:"gravatar_id,omitempty"`
  380. URL *string `json:"url,omitempty"`
  381. HTMLURL *string `json:"html_url,omitempty"`
  382. FollowersURL *string `json:"followers_url,omitempty"`
  383. FollowingURL *string `json:"following_url,omitempty"`
  384. GistsURL *string `json:"gists_url,omitempty"`
  385. StarredURL *string `json:"starred_url,omitempty"`
  386. SubscriptionsURL *string `json:"subscriptions_url,omitempty"`
  387. OrganizationsURL *string `json:"organizations_url,omitempty"`
  388. ReposURL *string `json:"repos_url,omitempty"`
  389. EventsURL *string `json:"events_url,omitempty"`
  390. ReceivedEventsURL *string `json:"received_events_url,omitempty"`
  391. Type *string `json:"type,omitempty"`
  392. SiteAdmin *bool `json:"site_admin,omitempty"`
  393. Contributions *int `json:"contributions,omitempty"`
  394. }
  395. // ListContributorsOptions specifies the optional parameters to the
  396. // RepositoriesService.ListContributors method.
  397. type ListContributorsOptions struct {
  398. // Include anonymous contributors in results or not
  399. Anon string `url:"anon,omitempty"`
  400. ListOptions
  401. }
  402. // ListContributors lists contributors for a repository.
  403. //
  404. // GitHub API docs: https://developer.github.com/v3/repos/#list-contributors
  405. func (s *RepositoriesService) ListContributors(ctx context.Context, owner string, repository string, opt *ListContributorsOptions) ([]*Contributor, *Response, error) {
  406. u := fmt.Sprintf("repos/%v/%v/contributors", owner, repository)
  407. u, err := addOptions(u, opt)
  408. if err != nil {
  409. return nil, nil, err
  410. }
  411. req, err := s.client.NewRequest("GET", u, nil)
  412. if err != nil {
  413. return nil, nil, err
  414. }
  415. var contributor []*Contributor
  416. resp, err := s.client.Do(ctx, req, &contributor)
  417. if err != nil {
  418. return nil, nil, err
  419. }
  420. return contributor, resp, nil
  421. }
  422. // ListLanguages lists languages for the specified repository. The returned map
  423. // specifies the languages and the number of bytes of code written in that
  424. // language. For example:
  425. //
  426. // {
  427. // "C": 78769,
  428. // "Python": 7769
  429. // }
  430. //
  431. // GitHub API docs: https://developer.github.com/v3/repos/#list-languages
  432. func (s *RepositoriesService) ListLanguages(ctx context.Context, owner string, repo string) (map[string]int, *Response, error) {
  433. u := fmt.Sprintf("repos/%v/%v/languages", owner, repo)
  434. req, err := s.client.NewRequest("GET", u, nil)
  435. if err != nil {
  436. return nil, nil, err
  437. }
  438. languages := make(map[string]int)
  439. resp, err := s.client.Do(ctx, req, &languages)
  440. if err != nil {
  441. return nil, resp, err
  442. }
  443. return languages, resp, nil
  444. }
  445. // ListTeams lists the teams for the specified repository.
  446. //
  447. // GitHub API docs: https://developer.github.com/v3/repos/#list-teams
  448. func (s *RepositoriesService) ListTeams(ctx context.Context, owner string, repo string, opt *ListOptions) ([]*Team, *Response, error) {
  449. u := fmt.Sprintf("repos/%v/%v/teams", owner, repo)
  450. u, err := addOptions(u, opt)
  451. if err != nil {
  452. return nil, nil, err
  453. }
  454. req, err := s.client.NewRequest("GET", u, nil)
  455. if err != nil {
  456. return nil, nil, err
  457. }
  458. req.Header.Set("Accept", mediaTypeNestedTeamsPreview)
  459. var teams []*Team
  460. resp, err := s.client.Do(ctx, req, &teams)
  461. if err != nil {
  462. return nil, resp, err
  463. }
  464. return teams, resp, nil
  465. }
  466. // RepositoryTag represents a repository tag.
  467. type RepositoryTag struct {
  468. Name *string `json:"name,omitempty"`
  469. Commit *Commit `json:"commit,omitempty"`
  470. ZipballURL *string `json:"zipball_url,omitempty"`
  471. TarballURL *string `json:"tarball_url,omitempty"`
  472. }
  473. // ListTags lists tags for the specified repository.
  474. //
  475. // GitHub API docs: https://developer.github.com/v3/repos/#list-tags
  476. func (s *RepositoriesService) ListTags(ctx context.Context, owner string, repo string, opt *ListOptions) ([]*RepositoryTag, *Response, error) {
  477. u := fmt.Sprintf("repos/%v/%v/tags", owner, repo)
  478. u, err := addOptions(u, opt)
  479. if err != nil {
  480. return nil, nil, err
  481. }
  482. req, err := s.client.NewRequest("GET", u, nil)
  483. if err != nil {
  484. return nil, nil, err
  485. }
  486. var tags []*RepositoryTag
  487. resp, err := s.client.Do(ctx, req, &tags)
  488. if err != nil {
  489. return nil, resp, err
  490. }
  491. return tags, resp, nil
  492. }
  493. // Branch represents a repository branch
  494. type Branch struct {
  495. Name *string `json:"name,omitempty"`
  496. Commit *RepositoryCommit `json:"commit,omitempty"`
  497. Protected *bool `json:"protected,omitempty"`
  498. }
  499. // Protection represents a repository branch's protection.
  500. type Protection struct {
  501. RequiredStatusChecks *RequiredStatusChecks `json:"required_status_checks"`
  502. RequiredPullRequestReviews *PullRequestReviewsEnforcement `json:"required_pull_request_reviews"`
  503. EnforceAdmins *AdminEnforcement `json:"enforce_admins"`
  504. Restrictions *BranchRestrictions `json:"restrictions"`
  505. }
  506. // ProtectionRequest represents a request to create/edit a branch's protection.
  507. type ProtectionRequest struct {
  508. RequiredStatusChecks *RequiredStatusChecks `json:"required_status_checks"`
  509. RequiredPullRequestReviews *PullRequestReviewsEnforcementRequest `json:"required_pull_request_reviews"`
  510. EnforceAdmins bool `json:"enforce_admins"`
  511. Restrictions *BranchRestrictionsRequest `json:"restrictions"`
  512. }
  513. // RequiredStatusChecks represents the protection status of a individual branch.
  514. type RequiredStatusChecks struct {
  515. // Require branches to be up to date before merging. (Required.)
  516. Strict bool `json:"strict"`
  517. // The list of status checks to require in order to merge into this
  518. // branch. (Required; use []string{} instead of nil for empty list.)
  519. Contexts []string `json:"contexts"`
  520. }
  521. // RequiredStatusChecksRequest represents a request to edit a protected branch's status checks.
  522. type RequiredStatusChecksRequest struct {
  523. Strict *bool `json:"strict,omitempty"`
  524. Contexts []string `json:"contexts,omitempty"`
  525. }
  526. // PullRequestReviewsEnforcement represents the pull request reviews enforcement of a protected branch.
  527. type PullRequestReviewsEnforcement struct {
  528. // Specifies which users and teams can dismiss pull request reviews.
  529. DismissalRestrictions DismissalRestrictions `json:"dismissal_restrictions"`
  530. // Specifies if approved reviews are dismissed automatically, when a new commit is pushed.
  531. DismissStaleReviews bool `json:"dismiss_stale_reviews"`
  532. // RequireCodeOwnerReviews specifies if an approved review is required in pull requests including files with a designated code owner.
  533. RequireCodeOwnerReviews bool `json:"require_code_owner_reviews"`
  534. // RequiredApprovingReviewCount specifies the number of approvals required before the pull request can be merged.
  535. // Valid values are 1-6.
  536. RequiredApprovingReviewCount int `json:"required_approving_review_count"`
  537. }
  538. // PullRequestReviewsEnforcementRequest represents request to set the pull request review
  539. // enforcement of a protected branch. It is separate from PullRequestReviewsEnforcement above
  540. // because the request structure is different from the response structure.
  541. type PullRequestReviewsEnforcementRequest struct {
  542. // Specifies which users and teams should be allowed to dismiss pull request reviews.
  543. // User and team dismissal restrictions are only available for
  544. // organization-owned repositories. Must be nil for personal repositories.
  545. DismissalRestrictionsRequest *DismissalRestrictionsRequest `json:"dismissal_restrictions,omitempty"`
  546. // Specifies if approved reviews can be dismissed automatically, when a new commit is pushed. (Required)
  547. DismissStaleReviews bool `json:"dismiss_stale_reviews"`
  548. // RequireCodeOwnerReviews specifies if an approved review is required in pull requests including files with a designated code owner.
  549. RequireCodeOwnerReviews bool `json:"require_code_owner_reviews"`
  550. // RequiredApprovingReviewCount specifies the number of approvals required before the pull request can be merged.
  551. // Valid values are 1-6.
  552. RequiredApprovingReviewCount int `json:"required_approving_review_count"`
  553. }
  554. // PullRequestReviewsEnforcementUpdate represents request to patch the pull request review
  555. // enforcement of a protected branch. It is separate from PullRequestReviewsEnforcementRequest above
  556. // because the patch request does not require all fields to be initialized.
  557. type PullRequestReviewsEnforcementUpdate struct {
  558. // Specifies which users and teams can dismiss pull request reviews. Can be omitted.
  559. DismissalRestrictionsRequest *DismissalRestrictionsRequest `json:"dismissal_restrictions,omitempty"`
  560. // Specifies if approved reviews can be dismissed automatically, when a new commit is pushed. Can be omitted.
  561. DismissStaleReviews *bool `json:"dismiss_stale_reviews,omitempty"`
  562. // RequireCodeOwnerReviews specifies if an approved review is required in pull requests including files with a designated code owner.
  563. RequireCodeOwnerReviews bool `json:"require_code_owner_reviews,omitempty"`
  564. // RequiredApprovingReviewCount specifies the number of approvals required before the pull request can be merged.
  565. // Valid values are 1 - 6.
  566. RequiredApprovingReviewCount int `json:"required_approving_review_count"`
  567. }
  568. // AdminEnforcement represents the configuration to enforce required status checks for repository administrators.
  569. type AdminEnforcement struct {
  570. URL *string `json:"url,omitempty"`
  571. Enabled bool `json:"enabled"`
  572. }
  573. // BranchRestrictions represents the restriction that only certain users or
  574. // teams may push to a branch.
  575. type BranchRestrictions struct {
  576. // The list of user logins with push access.
  577. Users []*User `json:"users"`
  578. // The list of team slugs with push access.
  579. Teams []*Team `json:"teams"`
  580. }
  581. // BranchRestrictionsRequest represents the request to create/edit the
  582. // restriction that only certain users or teams may push to a branch. It is
  583. // separate from BranchRestrictions above because the request structure is
  584. // different from the response structure.
  585. type BranchRestrictionsRequest struct {
  586. // The list of user logins with push access. (Required; use []string{} instead of nil for empty list.)
  587. Users []string `json:"users"`
  588. // The list of team slugs with push access. (Required; use []string{} instead of nil for empty list.)
  589. Teams []string `json:"teams"`
  590. }
  591. // DismissalRestrictions specifies which users and teams can dismiss pull request reviews.
  592. type DismissalRestrictions struct {
  593. // The list of users who can dimiss pull request reviews.
  594. Users []*User `json:"users"`
  595. // The list of teams which can dismiss pull request reviews.
  596. Teams []*Team `json:"teams"`
  597. }
  598. // DismissalRestrictionsRequest represents the request to create/edit the
  599. // restriction to allows only specific users or teams to dimiss pull request reviews. It is
  600. // separate from DismissalRestrictions above because the request structure is
  601. // different from the response structure.
  602. // Note: Both Users and Teams must be nil, or both must be non-nil.
  603. type DismissalRestrictionsRequest struct {
  604. // The list of user logins who can dismiss pull request reviews. (Required; use nil to disable dismissal_restrictions or &[]string{} otherwise.)
  605. Users *[]string `json:"users,omitempty"`
  606. // The list of team slugs which can dismiss pull request reviews. (Required; use nil to disable dismissal_restrictions or &[]string{} otherwise.)
  607. Teams *[]string `json:"teams,omitempty"`
  608. }
  609. // SignaturesProtectedBranch represents the protection status of an individual branch.
  610. type SignaturesProtectedBranch struct {
  611. URL *string `json:"url,omitempty"`
  612. // Commits pushed to matching branches must have verified signatures.
  613. Enabled *bool `json:"enabled,omitempty"`
  614. }
  615. // ListBranches lists branches for the specified repository.
  616. //
  617. // GitHub API docs: https://developer.github.com/v3/repos/#list-branches
  618. func (s *RepositoriesService) ListBranches(ctx context.Context, owner string, repo string, opt *ListOptions) ([]*Branch, *Response, error) {
  619. u := fmt.Sprintf("repos/%v/%v/branches", owner, repo)
  620. u, err := addOptions(u, opt)
  621. if err != nil {
  622. return nil, nil, err
  623. }
  624. req, err := s.client.NewRequest("GET", u, nil)
  625. if err != nil {
  626. return nil, nil, err
  627. }
  628. // TODO: remove custom Accept header when this API fully launches
  629. req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
  630. var branches []*Branch
  631. resp, err := s.client.Do(ctx, req, &branches)
  632. if err != nil {
  633. return nil, resp, err
  634. }
  635. return branches, resp, nil
  636. }
  637. // GetBranch gets the specified branch for a repository.
  638. //
  639. // GitHub API docs: https://developer.github.com/v3/repos/#get-branch
  640. func (s *RepositoriesService) GetBranch(ctx context.Context, owner, repo, branch string) (*Branch, *Response, error) {
  641. u := fmt.Sprintf("repos/%v/%v/branches/%v", owner, repo, branch)
  642. req, err := s.client.NewRequest("GET", u, nil)
  643. if err != nil {
  644. return nil, nil, err
  645. }
  646. // TODO: remove custom Accept header when this API fully launches
  647. req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
  648. b := new(Branch)
  649. resp, err := s.client.Do(ctx, req, b)
  650. if err != nil {
  651. return nil, resp, err
  652. }
  653. return b, resp, nil
  654. }
  655. // GetBranchProtection gets the protection of a given branch.
  656. //
  657. // GitHub API docs: https://developer.github.com/v3/repos/branches/#get-branch-protection
  658. func (s *RepositoriesService) GetBranchProtection(ctx context.Context, owner, repo, branch string) (*Protection, *Response, error) {
  659. u := fmt.Sprintf("repos/%v/%v/branches/%v/protection", owner, repo, branch)
  660. req, err := s.client.NewRequest("GET", u, nil)
  661. if err != nil {
  662. return nil, nil, err
  663. }
  664. // TODO: remove custom Accept header when this API fully launches
  665. req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
  666. p := new(Protection)
  667. resp, err := s.client.Do(ctx, req, p)
  668. if err != nil {
  669. return nil, resp, err
  670. }
  671. return p, resp, nil
  672. }
  673. // GetRequiredStatusChecks gets the required status checks for a given protected branch.
  674. //
  675. // GitHub API docs: https://developer.github.com/v3/repos/branches/#get-required-status-checks-of-protected-branch
  676. func (s *RepositoriesService) GetRequiredStatusChecks(ctx context.Context, owner, repo, branch string) (*RequiredStatusChecks, *Response, error) {
  677. u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_status_checks", owner, repo, branch)
  678. req, err := s.client.NewRequest("GET", u, nil)
  679. if err != nil {
  680. return nil, nil, err
  681. }
  682. // TODO: remove custom Accept header when this API fully launches
  683. req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
  684. p := new(RequiredStatusChecks)
  685. resp, err := s.client.Do(ctx, req, p)
  686. if err != nil {
  687. return nil, resp, err
  688. }
  689. return p, resp, nil
  690. }
  691. // ListRequiredStatusChecksContexts lists the required status checks contexts for a given protected branch.
  692. //
  693. // GitHub API docs: https://developer.github.com/v3/repos/branches/#list-required-status-checks-contexts-of-protected-branch
  694. func (s *RepositoriesService) ListRequiredStatusChecksContexts(ctx context.Context, owner, repo, branch string) (contexts []string, resp *Response, err error) {
  695. u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_status_checks/contexts", owner, repo, branch)
  696. req, err := s.client.NewRequest("GET", u, nil)
  697. if err != nil {
  698. return nil, nil, err
  699. }
  700. // TODO: remove custom Accept header when this API fully launches
  701. req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
  702. resp, err = s.client.Do(ctx, req, &contexts)
  703. if err != nil {
  704. return nil, resp, err
  705. }
  706. return contexts, resp, nil
  707. }
  708. // UpdateBranchProtection updates the protection of a given branch.
  709. //
  710. // GitHub API docs: https://developer.github.com/v3/repos/branches/#update-branch-protection
  711. func (s *RepositoriesService) UpdateBranchProtection(ctx context.Context, owner, repo, branch string, preq *ProtectionRequest) (*Protection, *Response, error) {
  712. u := fmt.Sprintf("repos/%v/%v/branches/%v/protection", owner, repo, branch)
  713. req, err := s.client.NewRequest("PUT", u, preq)
  714. if err != nil {
  715. return nil, nil, err
  716. }
  717. // TODO: remove custom Accept header when this API fully launches
  718. req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
  719. p := new(Protection)
  720. resp, err := s.client.Do(ctx, req, p)
  721. if err != nil {
  722. return nil, resp, err
  723. }
  724. return p, resp, nil
  725. }
  726. // RemoveBranchProtection removes the protection of a given branch.
  727. //
  728. // GitHub API docs: https://developer.github.com/v3/repos/branches/#remove-branch-protection
  729. func (s *RepositoriesService) RemoveBranchProtection(ctx context.Context, owner, repo, branch string) (*Response, error) {
  730. u := fmt.Sprintf("repos/%v/%v/branches/%v/protection", owner, repo, branch)
  731. req, err := s.client.NewRequest("DELETE", u, nil)
  732. if err != nil {
  733. return nil, err
  734. }
  735. // TODO: remove custom Accept header when this API fully launches
  736. req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
  737. return s.client.Do(ctx, req, nil)
  738. }
  739. // GetSignaturesProtectedBranch gets required signatures of protected branch.
  740. //
  741. // GitHub API docs: https://developer.github.com/v3/repos/branches/#get-required-signatures-of-protected-branch
  742. func (s *RepositoriesService) GetSignaturesProtectedBranch(ctx context.Context, owner, repo, branch string) (*SignaturesProtectedBranch, *Response, error) {
  743. u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_signatures", owner, repo, branch)
  744. req, err := s.client.NewRequest("GET", u, nil)
  745. if err != nil {
  746. return nil, nil, err
  747. }
  748. // TODO: remove custom Accept header when this API fully launches
  749. req.Header.Set("Accept", mediaTypeSignaturePreview)
  750. p := new(SignaturesProtectedBranch)
  751. resp, err := s.client.Do(ctx, req, p)
  752. if err != nil {
  753. return nil, resp, err
  754. }
  755. return p, resp, nil
  756. }
  757. // RequireSignaturesOnProtectedBranch makes signed commits required on a protected branch.
  758. // It requires admin access and branch protection to be enabled.
  759. //
  760. // GitHub API docs: https://developer.github.com/v3/repos/branches/#add-required-signatures-of-protected-branch
  761. func (s *RepositoriesService) RequireSignaturesOnProtectedBranch(ctx context.Context, owner, repo, branch string) (*SignaturesProtectedBranch, *Response, error) {
  762. u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_signatures", owner, repo, branch)
  763. req, err := s.client.NewRequest("POST", u, nil)
  764. if err != nil {
  765. return nil, nil, err
  766. }
  767. // TODO: remove custom Accept header when this API fully launches
  768. req.Header.Set("Accept", mediaTypeSignaturePreview)
  769. r := new(SignaturesProtectedBranch)
  770. resp, err := s.client.Do(ctx, req, r)
  771. if err != nil {
  772. return nil, resp, err
  773. }
  774. return r, resp, err
  775. }
  776. // OptionalSignaturesOnProtectedBranch removes required signed commits on a given branch.
  777. //
  778. // GitHub API docs: https://developer.github.com/v3/repos/branches/#remove-required-signatures-of-protected-branch
  779. func (s *RepositoriesService) OptionalSignaturesOnProtectedBranch(ctx context.Context, owner, repo, branch string) (*Response, error) {
  780. u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_signatures", owner, repo, branch)
  781. req, err := s.client.NewRequest("DELETE", u, nil)
  782. if err != nil {
  783. return nil, err
  784. }
  785. // TODO: remove custom Accept header when this API fully launches
  786. req.Header.Set("Accept", mediaTypeSignaturePreview)
  787. return s.client.Do(ctx, req, nil)
  788. }
  789. // UpdateRequiredStatusChecks updates the required status checks for a given protected branch.
  790. //
  791. // GitHub API docs: https://developer.github.com/v3/repos/branches/#update-required-status-checks-of-protected-branch
  792. func (s *RepositoriesService) UpdateRequiredStatusChecks(ctx context.Context, owner, repo, branch string, sreq *RequiredStatusChecksRequest) (*RequiredStatusChecks, *Response, error) {
  793. u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_status_checks", owner, repo, branch)
  794. req, err := s.client.NewRequest("PATCH", u, sreq)
  795. if err != nil {
  796. return nil, nil, err
  797. }
  798. sc := new(RequiredStatusChecks)
  799. resp, err := s.client.Do(ctx, req, sc)
  800. if err != nil {
  801. return nil, resp, err
  802. }
  803. return sc, resp, nil
  804. }
  805. // License gets the contents of a repository's license if one is detected.
  806. //
  807. // GitHub API docs: https://developer.github.com/v3/licenses/#get-the-contents-of-a-repositorys-license
  808. func (s *RepositoriesService) License(ctx context.Context, owner, repo string) (*RepositoryLicense, *Response, error) {
  809. u := fmt.Sprintf("repos/%v/%v/license", owner, repo)
  810. req, err := s.client.NewRequest("GET", u, nil)
  811. if err != nil {
  812. return nil, nil, err
  813. }
  814. r := &RepositoryLicense{}
  815. resp, err := s.client.Do(ctx, req, r)
  816. if err != nil {
  817. return nil, resp, err
  818. }
  819. return r, resp, nil
  820. }
  821. // GetPullRequestReviewEnforcement gets pull request review enforcement of a protected branch.
  822. //
  823. // GitHub API docs: https://developer.github.com/v3/repos/branches/#get-pull-request-review-enforcement-of-protected-branch
  824. func (s *RepositoriesService) GetPullRequestReviewEnforcement(ctx context.Context, owner, repo, branch string) (*PullRequestReviewsEnforcement, *Response, error) {
  825. u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_pull_request_reviews", owner, repo, branch)
  826. req, err := s.client.NewRequest("GET", u, nil)
  827. if err != nil {
  828. return nil, nil, err
  829. }
  830. // TODO: remove custom Accept header when this API fully launches
  831. req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
  832. r := new(PullRequestReviewsEnforcement)
  833. resp, err := s.client.Do(ctx, req, r)
  834. if err != nil {
  835. return nil, resp, err
  836. }
  837. return r, resp, nil
  838. }
  839. // UpdatePullRequestReviewEnforcement patches pull request review enforcement of a protected branch.
  840. // It requires admin access and branch protection to be enabled.
  841. //
  842. // GitHub API docs: https://developer.github.com/v3/repos/branches/#update-pull-request-review-enforcement-of-protected-branch
  843. func (s *RepositoriesService) UpdatePullRequestReviewEnforcement(ctx context.Context, owner, repo, branch string, patch *PullRequestReviewsEnforcementUpdate) (*PullRequestReviewsEnforcement, *Response, error) {
  844. u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_pull_request_reviews", owner, repo, branch)
  845. req, err := s.client.NewRequest("PATCH", u, patch)
  846. if err != nil {
  847. return nil, nil, err
  848. }
  849. // TODO: remove custom Accept header when this API fully launches
  850. req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
  851. r := new(PullRequestReviewsEnforcement)
  852. resp, err := s.client.Do(ctx, req, r)
  853. if err != nil {
  854. return nil, resp, err
  855. }
  856. return r, resp, err
  857. }
  858. // DisableDismissalRestrictions disables dismissal restrictions of a protected branch.
  859. // It requires admin access and branch protection to be enabled.
  860. //
  861. // GitHub API docs: https://developer.github.com/v3/repos/branches/#update-pull-request-review-enforcement-of-protected-branch
  862. func (s *RepositoriesService) DisableDismissalRestrictions(ctx context.Context, owner, repo, branch string) (*PullRequestReviewsEnforcement, *Response, error) {
  863. u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_pull_request_reviews", owner, repo, branch)
  864. data := struct {
  865. R []interface{} `json:"dismissal_restrictions"`
  866. }{[]interface{}{}}
  867. req, err := s.client.NewRequest("PATCH", u, data)
  868. if err != nil {
  869. return nil, nil, err
  870. }
  871. // TODO: remove custom Accept header when this API fully launches
  872. req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
  873. r := new(PullRequestReviewsEnforcement)
  874. resp, err := s.client.Do(ctx, req, r)
  875. if err != nil {
  876. return nil, resp, err
  877. }
  878. return r, resp, err
  879. }
  880. // RemovePullRequestReviewEnforcement removes pull request enforcement of a protected branch.
  881. //
  882. // GitHub API docs: https://developer.github.com/v3/repos/branches/#remove-pull-request-review-enforcement-of-protected-branch
  883. func (s *RepositoriesService) RemovePullRequestReviewEnforcement(ctx context.Context, owner, repo, branch string) (*Response, error) {
  884. u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_pull_request_reviews", owner, repo, branch)
  885. req, err := s.client.NewRequest("DELETE", u, nil)
  886. if err != nil {
  887. return nil, err
  888. }
  889. // TODO: remove custom Accept header when this API fully launches
  890. req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
  891. return s.client.Do(ctx, req, nil)
  892. }
  893. // GetAdminEnforcement gets admin enforcement information of a protected branch.
  894. //
  895. // GitHub API docs: https://developer.github.com/v3/repos/branches/#get-admin-enforcement-of-protected-branch
  896. func (s *RepositoriesService) GetAdminEnforcement(ctx context.Context, owner, repo, branch string) (*AdminEnforcement, *Response, error) {
  897. u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/enforce_admins", owner, repo, branch)
  898. req, err := s.client.NewRequest("GET", u, nil)
  899. if err != nil {
  900. return nil, nil, err
  901. }
  902. // TODO: remove custom Accept header when this API fully launches
  903. req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
  904. r := new(AdminEnforcement)
  905. resp, err := s.client.Do(ctx, req, r)
  906. if err != nil {
  907. return nil, resp, err
  908. }
  909. return r, resp, nil
  910. }
  911. // AddAdminEnforcement adds admin enforcement to a protected branch.
  912. // It requires admin access and branch protection to be enabled.
  913. //
  914. // GitHub API docs: https://developer.github.com/v3/repos/branches/#add-admin-enforcement-of-protected-branch
  915. func (s *RepositoriesService) AddAdminEnforcement(ctx context.Context, owner, repo, branch string) (*AdminEnforcement, *Response, error) {
  916. u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/enforce_admins", owner, repo, branch)
  917. req, err := s.client.NewRequest("POST", u, nil)
  918. if err != nil {
  919. return nil, nil, err
  920. }
  921. // TODO: remove custom Accept header when this API fully launches
  922. req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
  923. r := new(AdminEnforcement)
  924. resp, err := s.client.Do(ctx, req, r)
  925. if err != nil {
  926. return nil, resp, err
  927. }
  928. return r, resp, err
  929. }
  930. // RemoveAdminEnforcement removes admin enforcement from a protected branch.
  931. //
  932. // GitHub API docs: https://developer.github.com/v3/repos/branches/#remove-admin-enforcement-of-protected-branch
  933. func (s *RepositoriesService) RemoveAdminEnforcement(ctx context.Context, owner, repo, branch string) (*Response, error) {
  934. u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/enforce_admins", owner, repo, branch)
  935. req, err := s.client.NewRequest("DELETE", u, nil)
  936. if err != nil {
  937. return nil, err
  938. }
  939. // TODO: remove custom Accept header when this API fully launches
  940. req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
  941. return s.client.Do(ctx, req, nil)
  942. }
  943. // repositoryTopics represents a collection of repository topics.
  944. type repositoryTopics struct {
  945. Names []string `json:"names"`
  946. }
  947. // ListAllTopics lists topics for a repository.
  948. //
  949. // GitHub API docs: https://developer.github.com/v3/repos/#list-all-topics-for-a-repository
  950. func (s *RepositoriesService) ListAllTopics(ctx context.Context, owner, repo string) ([]string, *Response, error) {
  951. u := fmt.Sprintf("repos/%v/%v/topics", owner, repo)
  952. req, err := s.client.NewRequest("GET", u, nil)
  953. if err != nil {
  954. return nil, nil, err
  955. }
  956. // TODO: remove custom Accept header when this API fully launches.
  957. req.Header.Set("Accept", mediaTypeTopicsPreview)
  958. topics := new(repositoryTopics)
  959. resp, err := s.client.Do(ctx, req, topics)
  960. if err != nil {
  961. return nil, resp, err
  962. }
  963. return topics.Names, resp, nil
  964. }
  965. // ReplaceAllTopics replaces topics for a repository.
  966. //
  967. // GitHub API docs: https://developer.github.com/v3/repos/#replace-all-topics-for-a-repository
  968. func (s *RepositoriesService) ReplaceAllTopics(ctx context.Context, owner, repo string, topics []string) ([]string, *Response, error) {
  969. u := fmt.Sprintf("repos/%v/%v/topics", owner, repo)
  970. t := &repositoryTopics{
  971. Names: topics,
  972. }
  973. if t.Names == nil {
  974. t.Names = []string{}
  975. }
  976. req, err := s.client.NewRequest("PUT", u, t)
  977. if err != nil {
  978. return nil, nil, err
  979. }
  980. // TODO: remove custom Accept header when this API fully launches.
  981. req.Header.Set("Accept", mediaTypeTopicsPreview)
  982. t = new(repositoryTopics)
  983. resp, err := s.client.Do(ctx, req, t)
  984. if err != nil {
  985. return nil, resp, err
  986. }
  987. return t.Names, resp, nil
  988. }
  989. // TransferRequest represents a request to transfer a repository.
  990. type TransferRequest struct {
  991. NewOwner string `json:"new_owner"`
  992. TeamID []int64 `json:"team_ids,omitempty"`
  993. }
  994. // Transfer transfers a repository from one account or organization to another.
  995. //
  996. // This method might return an *AcceptedError and a status code of
  997. // 202. This is because this is the status that GitHub returns to signify that
  998. // it has now scheduled the transfer of the repository in a background task.
  999. // A follow up request, after a delay of a second or so, should result
  1000. // in a successful request.
  1001. //
  1002. // GitHub API docs: https://developer.github.com/v3/repos/#transfer-a-repository
  1003. func (s *RepositoriesService) Transfer(ctx context.Context, owner, repo string, transfer TransferRequest) (*Repository, *Response, error) {
  1004. u := fmt.Sprintf("repos/%v/%v/transfer", owner, repo)
  1005. req, err := s.client.NewRequest("POST", u, &transfer)
  1006. if err != nil {
  1007. return nil, nil, err
  1008. }
  1009. // TODO: remove custom Accept header when this API fully launches.
  1010. req.Header.Set("Accept", mediaTypeRepositoryTransferPreview)
  1011. r := new(Repository)
  1012. resp, err := s.client.Do(ctx, req, r)
  1013. if err != nil {
  1014. return nil, resp, err
  1015. }
  1016. return r, resp, nil
  1017. }
上海开阖软件有限公司 沪ICP备12045867号-1