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

595 lines
21KB

  1. // Copyright 2016 The go-github AUTHORS. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. package github
  6. import (
  7. "context"
  8. "fmt"
  9. )
  10. // ProjectsService provides access to the projects functions in the
  11. // GitHub API.
  12. //
  13. // GitHub API docs: https://developer.github.com/v3/projects/
  14. type ProjectsService service
  15. // Project represents a GitHub Project.
  16. type Project struct {
  17. ID *int64 `json:"id,omitempty"`
  18. URL *string `json:"url,omitempty"`
  19. HTMLURL *string `json:"html_url,omitempty"`
  20. ColumnsURL *string `json:"columns_url,omitempty"`
  21. OwnerURL *string `json:"owner_url,omitempty"`
  22. Name *string `json:"name,omitempty"`
  23. Body *string `json:"body,omitempty"`
  24. Number *int `json:"number,omitempty"`
  25. State *string `json:"state,omitempty"`
  26. CreatedAt *Timestamp `json:"created_at,omitempty"`
  27. UpdatedAt *Timestamp `json:"updated_at,omitempty"`
  28. NodeID *string `json:"node_id,omitempty"`
  29. // The User object that generated the project.
  30. Creator *User `json:"creator,omitempty"`
  31. }
  32. func (p Project) String() string {
  33. return Stringify(p)
  34. }
  35. // GetProject gets a GitHub Project for a repo.
  36. //
  37. // GitHub API docs: https://developer.github.com/v3/projects/#get-a-project
  38. func (s *ProjectsService) GetProject(ctx context.Context, id int64) (*Project, *Response, error) {
  39. u := fmt.Sprintf("projects/%v", id)
  40. req, err := s.client.NewRequest("GET", u, nil)
  41. if err != nil {
  42. return nil, nil, err
  43. }
  44. // TODO: remove custom Accept headers when APIs fully launch.
  45. req.Header.Set("Accept", mediaTypeProjectsPreview)
  46. project := &Project{}
  47. resp, err := s.client.Do(ctx, req, project)
  48. if err != nil {
  49. return nil, resp, err
  50. }
  51. return project, resp, nil
  52. }
  53. // ProjectOptions specifies the parameters to the
  54. // RepositoriesService.CreateProject and
  55. // ProjectsService.UpdateProject methods.
  56. type ProjectOptions struct {
  57. // The name of the project. (Required for creation; optional for update.)
  58. Name *string `json:"name,omitempty"`
  59. // The body of the project. (Optional.)
  60. Body *string `json:"body,omitempty"`
  61. // The following field(s) are only applicable for update.
  62. // They should be left with zero values for creation.
  63. // State of the project. Either "open" or "closed". (Optional.)
  64. State *string `json:"state,omitempty"`
  65. // The permission level that all members of the project's organization
  66. // will have on this project.
  67. // Setting the organization permission is only available
  68. // for organization projects. (Optional.)
  69. OrganizationPermission *string `json:"organization_permission,omitempty"`
  70. // Sets visibility of the project within the organization.
  71. // Setting visibility is only available
  72. // for organization projects.(Optional.)
  73. Public *bool `json:"public,omitempty"`
  74. }
  75. // UpdateProject updates a repository project.
  76. //
  77. // GitHub API docs: https://developer.github.com/v3/projects/#update-a-project
  78. func (s *ProjectsService) UpdateProject(ctx context.Context, id int64, opt *ProjectOptions) (*Project, *Response, error) {
  79. u := fmt.Sprintf("projects/%v", id)
  80. req, err := s.client.NewRequest("PATCH", u, opt)
  81. if err != nil {
  82. return nil, nil, err
  83. }
  84. // TODO: remove custom Accept headers when APIs fully launch.
  85. req.Header.Set("Accept", mediaTypeProjectsPreview)
  86. project := &Project{}
  87. resp, err := s.client.Do(ctx, req, project)
  88. if err != nil {
  89. return nil, resp, err
  90. }
  91. return project, resp, nil
  92. }
  93. // DeleteProject deletes a GitHub Project from a repository.
  94. //
  95. // GitHub API docs: https://developer.github.com/v3/projects/#delete-a-project
  96. func (s *ProjectsService) DeleteProject(ctx context.Context, id int64) (*Response, error) {
  97. u := fmt.Sprintf("projects/%v", id)
  98. req, err := s.client.NewRequest("DELETE", u, nil)
  99. if err != nil {
  100. return nil, err
  101. }
  102. // TODO: remove custom Accept header when this API fully launches.
  103. req.Header.Set("Accept", mediaTypeProjectsPreview)
  104. return s.client.Do(ctx, req, nil)
  105. }
  106. // ProjectColumn represents a column of a GitHub Project.
  107. //
  108. // GitHub API docs: https://developer.github.com/v3/repos/projects/
  109. type ProjectColumn struct {
  110. ID *int64 `json:"id,omitempty"`
  111. Name *string `json:"name,omitempty"`
  112. URL *string `json:"url,omitempty"`
  113. ProjectURL *string `json:"project_url,omitempty"`
  114. CardsURL *string `json:"cards_url,omitempty"`
  115. CreatedAt *Timestamp `json:"created_at,omitempty"`
  116. UpdatedAt *Timestamp `json:"updated_at,omitempty"`
  117. NodeID *string `json:"node_id,omitempty"`
  118. }
  119. // ListProjectColumns lists the columns of a GitHub Project for a repo.
  120. //
  121. // GitHub API docs: https://developer.github.com/v3/projects/columns/#list-project-columns
  122. func (s *ProjectsService) ListProjectColumns(ctx context.Context, projectID int64, opt *ListOptions) ([]*ProjectColumn, *Response, error) {
  123. u := fmt.Sprintf("projects/%v/columns", projectID)
  124. u, err := addOptions(u, opt)
  125. if err != nil {
  126. return nil, nil, err
  127. }
  128. req, err := s.client.NewRequest("GET", u, nil)
  129. if err != nil {
  130. return nil, nil, err
  131. }
  132. // TODO: remove custom Accept headers when APIs fully launch.
  133. req.Header.Set("Accept", mediaTypeProjectsPreview)
  134. columns := []*ProjectColumn{}
  135. resp, err := s.client.Do(ctx, req, &columns)
  136. if err != nil {
  137. return nil, resp, err
  138. }
  139. return columns, resp, nil
  140. }
  141. // GetProjectColumn gets a column of a GitHub Project for a repo.
  142. //
  143. // GitHub API docs: https://developer.github.com/v3/projects/columns/#get-a-project-column
  144. func (s *ProjectsService) GetProjectColumn(ctx context.Context, id int64) (*ProjectColumn, *Response, error) {
  145. u := fmt.Sprintf("projects/columns/%v", id)
  146. req, err := s.client.NewRequest("GET", u, nil)
  147. if err != nil {
  148. return nil, nil, err
  149. }
  150. // TODO: remove custom Accept headers when APIs fully launch.
  151. req.Header.Set("Accept", mediaTypeProjectsPreview)
  152. column := &ProjectColumn{}
  153. resp, err := s.client.Do(ctx, req, column)
  154. if err != nil {
  155. return nil, resp, err
  156. }
  157. return column, resp, nil
  158. }
  159. // ProjectColumnOptions specifies the parameters to the
  160. // ProjectsService.CreateProjectColumn and
  161. // ProjectsService.UpdateProjectColumn methods.
  162. type ProjectColumnOptions struct {
  163. // The name of the project column. (Required for creation and update.)
  164. Name string `json:"name"`
  165. }
  166. // CreateProjectColumn creates a column for the specified (by number) project.
  167. //
  168. // GitHub API docs: https://developer.github.com/v3/projects/columns/#create-a-project-column
  169. func (s *ProjectsService) CreateProjectColumn(ctx context.Context, projectID int64, opt *ProjectColumnOptions) (*ProjectColumn, *Response, error) {
  170. u := fmt.Sprintf("projects/%v/columns", projectID)
  171. req, err := s.client.NewRequest("POST", u, opt)
  172. if err != nil {
  173. return nil, nil, err
  174. }
  175. // TODO: remove custom Accept headers when APIs fully launch.
  176. req.Header.Set("Accept", mediaTypeProjectsPreview)
  177. column := &ProjectColumn{}
  178. resp, err := s.client.Do(ctx, req, column)
  179. if err != nil {
  180. return nil, resp, err
  181. }
  182. return column, resp, nil
  183. }
  184. // UpdateProjectColumn updates a column of a GitHub Project.
  185. //
  186. // GitHub API docs: https://developer.github.com/v3/projects/columns/#update-a-project-column
  187. func (s *ProjectsService) UpdateProjectColumn(ctx context.Context, columnID int64, opt *ProjectColumnOptions) (*ProjectColumn, *Response, error) {
  188. u := fmt.Sprintf("projects/columns/%v", columnID)
  189. req, err := s.client.NewRequest("PATCH", u, opt)
  190. if err != nil {
  191. return nil, nil, err
  192. }
  193. // TODO: remove custom Accept headers when APIs fully launch.
  194. req.Header.Set("Accept", mediaTypeProjectsPreview)
  195. column := &ProjectColumn{}
  196. resp, err := s.client.Do(ctx, req, column)
  197. if err != nil {
  198. return nil, resp, err
  199. }
  200. return column, resp, nil
  201. }
  202. // DeleteProjectColumn deletes a column from a GitHub Project.
  203. //
  204. // GitHub API docs: https://developer.github.com/v3/projects/columns/#delete-a-project-column
  205. func (s *ProjectsService) DeleteProjectColumn(ctx context.Context, columnID int64) (*Response, error) {
  206. u := fmt.Sprintf("projects/columns/%v", columnID)
  207. req, err := s.client.NewRequest("DELETE", u, nil)
  208. if err != nil {
  209. return nil, err
  210. }
  211. // TODO: remove custom Accept header when this API fully launches.
  212. req.Header.Set("Accept", mediaTypeProjectsPreview)
  213. return s.client.Do(ctx, req, nil)
  214. }
  215. // ProjectColumnMoveOptions specifies the parameters to the
  216. // ProjectsService.MoveProjectColumn method.
  217. type ProjectColumnMoveOptions struct {
  218. // Position can be one of "first", "last", or "after:<column-id>", where
  219. // <column-id> is the ID of a column in the same project. (Required.)
  220. Position string `json:"position"`
  221. }
  222. // MoveProjectColumn moves a column within a GitHub Project.
  223. //
  224. // GitHub API docs: https://developer.github.com/v3/projects/columns/#move-a-project-column
  225. func (s *ProjectsService) MoveProjectColumn(ctx context.Context, columnID int64, opt *ProjectColumnMoveOptions) (*Response, error) {
  226. u := fmt.Sprintf("projects/columns/%v/moves", columnID)
  227. req, err := s.client.NewRequest("POST", u, opt)
  228. if err != nil {
  229. return nil, err
  230. }
  231. // TODO: remove custom Accept header when this API fully launches.
  232. req.Header.Set("Accept", mediaTypeProjectsPreview)
  233. return s.client.Do(ctx, req, nil)
  234. }
  235. // ProjectCard represents a card in a column of a GitHub Project.
  236. //
  237. // GitHub API docs: https://developer.github.com/v3/projects/cards/#get-a-project-card
  238. type ProjectCard struct {
  239. URL *string `json:"url,omitempty"`
  240. ColumnURL *string `json:"column_url,omitempty"`
  241. ContentURL *string `json:"content_url,omitempty"`
  242. ID *int64 `json:"id,omitempty"`
  243. Note *string `json:"note,omitempty"`
  244. Creator *User `json:"creator,omitempty"`
  245. CreatedAt *Timestamp `json:"created_at,omitempty"`
  246. UpdatedAt *Timestamp `json:"updated_at,omitempty"`
  247. NodeID *string `json:"node_id,omitempty"`
  248. Archived *bool `json:"archived,omitempty"`
  249. // The following fields are only populated by Webhook events.
  250. ColumnID *int64 `json:"column_id,omitempty"`
  251. // The following fields are only populated by Events API.
  252. ProjectID *int64 `json:"project_id,omitempty"`
  253. ProjectURL *string `json:"project_url,omitempty"`
  254. ColumnName *string `json:"column_name,omitempty"`
  255. PreviousColumnName *string `json:"previous_column_name,omitempty"` // Populated in "moved_columns_in_project" event deliveries.
  256. }
  257. // ProjectCardListOptions specifies the optional parameters to the
  258. // ProjectsService.ListProjectCards method.
  259. type ProjectCardListOptions struct {
  260. // ArchivedState is used to list all, archived, or not_archived project cards.
  261. // Defaults to not_archived when you omit this parameter.
  262. ArchivedState *string `url:"archived_state,omitempty"`
  263. ListOptions
  264. }
  265. // ListProjectCards lists the cards in a column of a GitHub Project.
  266. //
  267. // GitHub API docs: https://developer.github.com/v3/projects/cards/#list-project-cards
  268. func (s *ProjectsService) ListProjectCards(ctx context.Context, columnID int64, opt *ProjectCardListOptions) ([]*ProjectCard, *Response, error) {
  269. u := fmt.Sprintf("projects/columns/%v/cards", columnID)
  270. u, err := addOptions(u, opt)
  271. if err != nil {
  272. return nil, nil, err
  273. }
  274. req, err := s.client.NewRequest("GET", u, nil)
  275. if err != nil {
  276. return nil, nil, err
  277. }
  278. // TODO: remove custom Accept headers when APIs fully launch.
  279. req.Header.Set("Accept", mediaTypeProjectsPreview)
  280. cards := []*ProjectCard{}
  281. resp, err := s.client.Do(ctx, req, &cards)
  282. if err != nil {
  283. return nil, resp, err
  284. }
  285. return cards, resp, nil
  286. }
  287. // GetProjectCard gets a card in a column of a GitHub Project.
  288. //
  289. // GitHub API docs: https://developer.github.com/v3/projects/cards/#get-a-project-card
  290. func (s *ProjectsService) GetProjectCard(ctx context.Context, columnID int64) (*ProjectCard, *Response, error) {
  291. u := fmt.Sprintf("projects/columns/cards/%v", columnID)
  292. req, err := s.client.NewRequest("GET", u, nil)
  293. if err != nil {
  294. return nil, nil, err
  295. }
  296. // TODO: remove custom Accept headers when APIs fully launch.
  297. req.Header.Set("Accept", mediaTypeProjectsPreview)
  298. card := &ProjectCard{}
  299. resp, err := s.client.Do(ctx, req, card)
  300. if err != nil {
  301. return nil, resp, err
  302. }
  303. return card, resp, nil
  304. }
  305. // ProjectCardOptions specifies the parameters to the
  306. // ProjectsService.CreateProjectCard and
  307. // ProjectsService.UpdateProjectCard methods.
  308. type ProjectCardOptions struct {
  309. // The note of the card. Note and ContentID are mutually exclusive.
  310. Note string `json:"note,omitempty"`
  311. // The ID (not Number) of the Issue to associate with this card.
  312. // Note and ContentID are mutually exclusive.
  313. ContentID int64 `json:"content_id,omitempty"`
  314. // The type of content to associate with this card. Possible values are: "Issue" and "PullRequest".
  315. ContentType string `json:"content_type,omitempty"`
  316. // Use true to archive a project card.
  317. // Specify false if you need to restore a previously archived project card.
  318. Archived *bool `json:"archived,omitempty"`
  319. }
  320. // CreateProjectCard creates a card in the specified column of a GitHub Project.
  321. //
  322. // GitHub API docs: https://developer.github.com/v3/projects/cards/#create-a-project-card
  323. func (s *ProjectsService) CreateProjectCard(ctx context.Context, columnID int64, opt *ProjectCardOptions) (*ProjectCard, *Response, error) {
  324. u := fmt.Sprintf("projects/columns/%v/cards", columnID)
  325. req, err := s.client.NewRequest("POST", u, opt)
  326. if err != nil {
  327. return nil, nil, err
  328. }
  329. // TODO: remove custom Accept headers when APIs fully launch.
  330. req.Header.Set("Accept", mediaTypeProjectsPreview)
  331. card := &ProjectCard{}
  332. resp, err := s.client.Do(ctx, req, card)
  333. if err != nil {
  334. return nil, resp, err
  335. }
  336. return card, resp, nil
  337. }
  338. // UpdateProjectCard updates a card of a GitHub Project.
  339. //
  340. // GitHub API docs: https://developer.github.com/v3/projects/cards/#update-a-project-card
  341. func (s *ProjectsService) UpdateProjectCard(ctx context.Context, cardID int64, opt *ProjectCardOptions) (*ProjectCard, *Response, error) {
  342. u := fmt.Sprintf("projects/columns/cards/%v", cardID)
  343. req, err := s.client.NewRequest("PATCH", u, opt)
  344. if err != nil {
  345. return nil, nil, err
  346. }
  347. // TODO: remove custom Accept headers when APIs fully launch.
  348. req.Header.Set("Accept", mediaTypeProjectsPreview)
  349. card := &ProjectCard{}
  350. resp, err := s.client.Do(ctx, req, card)
  351. if err != nil {
  352. return nil, resp, err
  353. }
  354. return card, resp, nil
  355. }
  356. // DeleteProjectCard deletes a card from a GitHub Project.
  357. //
  358. // GitHub API docs: https://developer.github.com/v3/projects/cards/#delete-a-project-card
  359. func (s *ProjectsService) DeleteProjectCard(ctx context.Context, cardID int64) (*Response, error) {
  360. u := fmt.Sprintf("projects/columns/cards/%v", cardID)
  361. req, err := s.client.NewRequest("DELETE", u, nil)
  362. if err != nil {
  363. return nil, err
  364. }
  365. // TODO: remove custom Accept header when this API fully launches.
  366. req.Header.Set("Accept", mediaTypeProjectsPreview)
  367. return s.client.Do(ctx, req, nil)
  368. }
  369. // ProjectCardMoveOptions specifies the parameters to the
  370. // ProjectsService.MoveProjectCard method.
  371. type ProjectCardMoveOptions struct {
  372. // Position can be one of "top", "bottom", or "after:<card-id>", where
  373. // <card-id> is the ID of a card in the same project.
  374. Position string `json:"position"`
  375. // ColumnID is the ID of a column in the same project. Note that ColumnID
  376. // is required when using Position "after:<card-id>" when that card is in
  377. // another column; otherwise it is optional.
  378. ColumnID int64 `json:"column_id,omitempty"`
  379. }
  380. // MoveProjectCard moves a card within a GitHub Project.
  381. //
  382. // GitHub API docs: https://developer.github.com/v3/projects/cards/#move-a-project-card
  383. func (s *ProjectsService) MoveProjectCard(ctx context.Context, cardID int64, opt *ProjectCardMoveOptions) (*Response, error) {
  384. u := fmt.Sprintf("projects/columns/cards/%v/moves", cardID)
  385. req, err := s.client.NewRequest("POST", u, opt)
  386. if err != nil {
  387. return nil, err
  388. }
  389. // TODO: remove custom Accept header when this API fully launches.
  390. req.Header.Set("Accept", mediaTypeProjectsPreview)
  391. return s.client.Do(ctx, req, nil)
  392. }
  393. // ProjectCollaboratorOptions specifies the optional parameters to the
  394. // ProjectsService.AddProjectCollaborator method.
  395. type ProjectCollaboratorOptions struct {
  396. // Permission specifies the permission to grant to the collaborator.
  397. // Possible values are:
  398. // "read" - can read, but not write to or administer this project.
  399. // "write" - can read and write, but not administer this project.
  400. // "admin" - can read, write and administer this project.
  401. //
  402. // Default value is "write"
  403. Permission *string `json:"permission,omitempty"`
  404. }
  405. // AddProjectCollaborator adds a collaborator to an organization project and sets
  406. // their permission level. You must be an organization owner or a project admin to add a collaborator.
  407. //
  408. // GitHub API docs: https://developer.github.com/v3/projects/collaborators/#add-user-as-a-collaborator
  409. func (s *ProjectsService) AddProjectCollaborator(ctx context.Context, id int64, username string, opt *ProjectCollaboratorOptions) (*Response, error) {
  410. u := fmt.Sprintf("projects/%v/collaborators/%v", id, username)
  411. req, err := s.client.NewRequest("PUT", u, opt)
  412. if err != nil {
  413. return nil, err
  414. }
  415. // TODO: remove custom Accept header when this API fully launches.
  416. req.Header.Set("Accept", mediaTypeProjectsPreview)
  417. return s.client.Do(ctx, req, nil)
  418. }
  419. // RemoveProjectCollaborator removes a collaborator from an organization project.
  420. // You must be an organization owner or a project admin to remove a collaborator.
  421. //
  422. // GitHub API docs: https://developer.github.com/v3/projects/collaborators/#remove-user-as-a-collaborator
  423. func (s *ProjectsService) RemoveProjectCollaborator(ctx context.Context, id int64, username string) (*Response, error) {
  424. u := fmt.Sprintf("projects/%v/collaborators/%v", id, username)
  425. req, err := s.client.NewRequest("DELETE", u, nil)
  426. if err != nil {
  427. return nil, err
  428. }
  429. // TODO: remove custom Accept header when this API fully launches.
  430. req.Header.Set("Accept", mediaTypeProjectsPreview)
  431. return s.client.Do(ctx, req, nil)
  432. }
  433. // ListCollaboratorOptions specifies the optional parameters to the
  434. // ProjectsService.ListProjectCollaborators method.
  435. type ListCollaboratorOptions struct {
  436. // Affiliation specifies how collaborators should be filtered by their affiliation.
  437. // Possible values are:
  438. // "outside" - All outside collaborators of an organization-owned repository
  439. // "direct" - All collaborators with permissions to an organization-owned repository,
  440. // regardless of organization membership status
  441. // "all" - All collaborators the authenticated user can see
  442. //
  443. // Default value is "all".
  444. Affiliation *string `url:"affiliation,omitempty"`
  445. ListOptions
  446. }
  447. // ListProjectCollaborators lists the collaborators for an organization project. For a project,
  448. // the list of collaborators includes outside collaborators, organization members that are direct
  449. // collaborators, organization members with access through team memberships, organization members
  450. // with access through default organization permissions, and organization owners. You must be an
  451. // organization owner or a project admin to list collaborators.
  452. //
  453. // GitHub API docs: https://developer.github.com/v3/projects/collaborators/#list-collaborators
  454. func (s *ProjectsService) ListProjectCollaborators(ctx context.Context, id int64, opt *ListCollaboratorOptions) ([]*User, *Response, error) {
  455. u := fmt.Sprintf("projects/%v/collaborators", id)
  456. u, err := addOptions(u, opt)
  457. if err != nil {
  458. return nil, nil, err
  459. }
  460. req, err := s.client.NewRequest("GET", u, nil)
  461. if err != nil {
  462. return nil, nil, err
  463. }
  464. // TODO: remove custom Accept header when this API fully launches.
  465. req.Header.Set("Accept", mediaTypeProjectsPreview)
  466. var users []*User
  467. resp, err := s.client.Do(ctx, req, &users)
  468. if err != nil {
  469. return nil, resp, err
  470. }
  471. return users, resp, nil
  472. }
  473. // ProjectPermissionLevel represents the permission level an organization
  474. // member has for a given project.
  475. type ProjectPermissionLevel struct {
  476. // Possible values: "admin", "write", "read", "none"
  477. Permission *string `json:"permission,omitempty"`
  478. User *User `json:"user,omitempty"`
  479. }
  480. // ReviewProjectCollaboratorPermission returns the collaborator's permission level for an organization
  481. // project. Possible values for the permission key: "admin", "write", "read", "none".
  482. // You must be an organization owner or a project admin to review a user's permission level.
  483. //
  484. // GitHub API docs: https://developer.github.com/v3/projects/collaborators/#review-a-users-permission-level
  485. func (s *ProjectsService) ReviewProjectCollaboratorPermission(ctx context.Context, id int64, username string) (*ProjectPermissionLevel, *Response, error) {
  486. u := fmt.Sprintf("projects/%v/collaborators/%v/permission", id, username)
  487. req, err := s.client.NewRequest("GET", u, nil)
  488. if err != nil {
  489. return nil, nil, err
  490. }
  491. // TODO: remove custom Accept header when this API fully launches.
  492. req.Header.Set("Accept", mediaTypeProjectsPreview)
  493. ppl := new(ProjectPermissionLevel)
  494. resp, err := s.client.Do(ctx, req, ppl)
  495. if err != nil {
  496. return nil, resp, err
  497. }
  498. return ppl, resp, nil
  499. }
上海开阖软件有限公司 沪ICP备12045867号-1