repos.go 39 KB

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