projects.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. OwnerURL *string `json:"owner_url,omitempty"`
  20. Name *string `json:"name,omitempty"`
  21. Body *string `json:"body,omitempty"`
  22. Number *int `json:"number,omitempty"`
  23. CreatedAt *Timestamp `json:"created_at,omitempty"`
  24. UpdatedAt *Timestamp `json:"updated_at,omitempty"`
  25. // The User object that generated the project.
  26. Creator *User `json:"creator,omitempty"`
  27. }
  28. func (p Project) String() string {
  29. return Stringify(p)
  30. }
  31. // GetProject gets a GitHub Project for a repo.
  32. //
  33. // GitHub API docs: https://developer.github.com/v3/projects/#get-a-project
  34. func (s *ProjectsService) GetProject(ctx context.Context, id int64) (*Project, *Response, error) {
  35. u := fmt.Sprintf("projects/%v", id)
  36. req, err := s.client.NewRequest("GET", u, nil)
  37. if err != nil {
  38. return nil, nil, err
  39. }
  40. // TODO: remove custom Accept header when this API fully launches.
  41. req.Header.Set("Accept", mediaTypeProjectsPreview)
  42. project := &Project{}
  43. resp, err := s.client.Do(ctx, req, project)
  44. if err != nil {
  45. return nil, resp, err
  46. }
  47. return project, resp, nil
  48. }
  49. // ProjectOptions specifies the parameters to the
  50. // RepositoriesService.CreateProject and
  51. // ProjectsService.UpdateProject methods.
  52. type ProjectOptions struct {
  53. // The name of the project. (Required for creation; optional for update.)
  54. Name string `json:"name,omitempty"`
  55. // The body of the project. (Optional.)
  56. Body string `json:"body,omitempty"`
  57. // The following field(s) are only applicable for update.
  58. // They should be left with zero values for creation.
  59. // State of the project. Either "open" or "closed". (Optional.)
  60. State string `json:"state,omitempty"`
  61. }
  62. // UpdateProject updates a repository project.
  63. //
  64. // GitHub API docs: https://developer.github.com/v3/projects/#update-a-project
  65. func (s *ProjectsService) UpdateProject(ctx context.Context, id int64, opt *ProjectOptions) (*Project, *Response, error) {
  66. u := fmt.Sprintf("projects/%v", id)
  67. req, err := s.client.NewRequest("PATCH", u, opt)
  68. if err != nil {
  69. return nil, nil, err
  70. }
  71. // TODO: remove custom Accept header when this API fully launches.
  72. req.Header.Set("Accept", mediaTypeProjectsPreview)
  73. project := &Project{}
  74. resp, err := s.client.Do(ctx, req, project)
  75. if err != nil {
  76. return nil, resp, err
  77. }
  78. return project, resp, nil
  79. }
  80. // DeleteProject deletes a GitHub Project from a repository.
  81. //
  82. // GitHub API docs: https://developer.github.com/v3/projects/#delete-a-project
  83. func (s *ProjectsService) DeleteProject(ctx context.Context, id int64) (*Response, error) {
  84. u := fmt.Sprintf("projects/%v", id)
  85. req, err := s.client.NewRequest("DELETE", u, nil)
  86. if err != nil {
  87. return nil, err
  88. }
  89. // TODO: remove custom Accept header when this API fully launches.
  90. req.Header.Set("Accept", mediaTypeProjectsPreview)
  91. return s.client.Do(ctx, req, nil)
  92. }
  93. // ProjectColumn represents a column of a GitHub Project.
  94. //
  95. // GitHub API docs: https://developer.github.com/v3/repos/projects/
  96. type ProjectColumn struct {
  97. ID *int64 `json:"id,omitempty"`
  98. Name *string `json:"name,omitempty"`
  99. ProjectURL *string `json:"project_url,omitempty"`
  100. CreatedAt *Timestamp `json:"created_at,omitempty"`
  101. UpdatedAt *Timestamp `json:"updated_at,omitempty"`
  102. }
  103. // ListProjectColumns lists the columns of a GitHub Project for a repo.
  104. //
  105. // GitHub API docs: https://developer.github.com/v3/projects/columns/#list-project-columns
  106. func (s *ProjectsService) ListProjectColumns(ctx context.Context, projectID int64, opt *ListOptions) ([]*ProjectColumn, *Response, error) {
  107. u := fmt.Sprintf("projects/%v/columns", projectID)
  108. u, err := addOptions(u, opt)
  109. if err != nil {
  110. return nil, nil, err
  111. }
  112. req, err := s.client.NewRequest("GET", u, nil)
  113. if err != nil {
  114. return nil, nil, err
  115. }
  116. // TODO: remove custom Accept header when this API fully launches.
  117. req.Header.Set("Accept", mediaTypeProjectsPreview)
  118. columns := []*ProjectColumn{}
  119. resp, err := s.client.Do(ctx, req, &columns)
  120. if err != nil {
  121. return nil, resp, err
  122. }
  123. return columns, resp, nil
  124. }
  125. // GetProjectColumn gets a column of a GitHub Project for a repo.
  126. //
  127. // GitHub API docs: https://developer.github.com/v3/projects/columns/#get-a-project-column
  128. func (s *ProjectsService) GetProjectColumn(ctx context.Context, id int64) (*ProjectColumn, *Response, error) {
  129. u := fmt.Sprintf("projects/columns/%v", id)
  130. req, err := s.client.NewRequest("GET", u, nil)
  131. if err != nil {
  132. return nil, nil, err
  133. }
  134. // TODO: remove custom Accept header when this API fully launches.
  135. req.Header.Set("Accept", mediaTypeProjectsPreview)
  136. column := &ProjectColumn{}
  137. resp, err := s.client.Do(ctx, req, column)
  138. if err != nil {
  139. return nil, resp, err
  140. }
  141. return column, resp, nil
  142. }
  143. // ProjectColumnOptions specifies the parameters to the
  144. // ProjectsService.CreateProjectColumn and
  145. // ProjectsService.UpdateProjectColumn methods.
  146. type ProjectColumnOptions struct {
  147. // The name of the project column. (Required for creation and update.)
  148. Name string `json:"name"`
  149. }
  150. // CreateProjectColumn creates a column for the specified (by number) project.
  151. //
  152. // GitHub API docs: https://developer.github.com/v3/projects/columns/#create-a-project-column
  153. func (s *ProjectsService) CreateProjectColumn(ctx context.Context, projectID int64, opt *ProjectColumnOptions) (*ProjectColumn, *Response, error) {
  154. u := fmt.Sprintf("projects/%v/columns", projectID)
  155. req, err := s.client.NewRequest("POST", u, opt)
  156. if err != nil {
  157. return nil, nil, err
  158. }
  159. // TODO: remove custom Accept header when this API fully launches.
  160. req.Header.Set("Accept", mediaTypeProjectsPreview)
  161. column := &ProjectColumn{}
  162. resp, err := s.client.Do(ctx, req, column)
  163. if err != nil {
  164. return nil, resp, err
  165. }
  166. return column, resp, nil
  167. }
  168. // UpdateProjectColumn updates a column of a GitHub Project.
  169. //
  170. // GitHub API docs: https://developer.github.com/v3/projects/columns/#update-a-project-column
  171. func (s *ProjectsService) UpdateProjectColumn(ctx context.Context, columnID int64, opt *ProjectColumnOptions) (*ProjectColumn, *Response, error) {
  172. u := fmt.Sprintf("projects/columns/%v", columnID)
  173. req, err := s.client.NewRequest("PATCH", u, opt)
  174. if err != nil {
  175. return nil, nil, err
  176. }
  177. // TODO: remove custom Accept header when this API fully launches.
  178. req.Header.Set("Accept", mediaTypeProjectsPreview)
  179. column := &ProjectColumn{}
  180. resp, err := s.client.Do(ctx, req, column)
  181. if err != nil {
  182. return nil, resp, err
  183. }
  184. return column, resp, nil
  185. }
  186. // DeleteProjectColumn deletes a column from a GitHub Project.
  187. //
  188. // GitHub API docs: https://developer.github.com/v3/projects/columns/#delete-a-project-column
  189. func (s *ProjectsService) DeleteProjectColumn(ctx context.Context, columnID int64) (*Response, error) {
  190. u := fmt.Sprintf("projects/columns/%v", columnID)
  191. req, err := s.client.NewRequest("DELETE", u, nil)
  192. if err != nil {
  193. return nil, err
  194. }
  195. // TODO: remove custom Accept header when this API fully launches.
  196. req.Header.Set("Accept", mediaTypeProjectsPreview)
  197. return s.client.Do(ctx, req, nil)
  198. }
  199. // ProjectColumnMoveOptions specifies the parameters to the
  200. // ProjectsService.MoveProjectColumn method.
  201. type ProjectColumnMoveOptions struct {
  202. // Position can be one of "first", "last", or "after:<column-id>", where
  203. // <column-id> is the ID of a column in the same project. (Required.)
  204. Position string `json:"position"`
  205. }
  206. // MoveProjectColumn moves a column within a GitHub Project.
  207. //
  208. // GitHub API docs: https://developer.github.com/v3/projects/columns/#move-a-project-column
  209. func (s *ProjectsService) MoveProjectColumn(ctx context.Context, columnID int64, opt *ProjectColumnMoveOptions) (*Response, error) {
  210. u := fmt.Sprintf("projects/columns/%v/moves", columnID)
  211. req, err := s.client.NewRequest("POST", u, opt)
  212. if err != nil {
  213. return nil, err
  214. }
  215. // TODO: remove custom Accept header when this API fully launches.
  216. req.Header.Set("Accept", mediaTypeProjectsPreview)
  217. return s.client.Do(ctx, req, nil)
  218. }
  219. // ProjectCard represents a card in a column of a GitHub Project.
  220. //
  221. // GitHub API docs: https://developer.github.com/v3/projects/cards/#get-a-project-card
  222. type ProjectCard struct {
  223. URL *string `json:"url,omitempty"`
  224. ColumnURL *string `json:"column_url,omitempty"`
  225. ContentURL *string `json:"content_url,omitempty"`
  226. ID *int64 `json:"id,omitempty"`
  227. Note *string `json:"note,omitempty"`
  228. Creator *User `json:"creator,omitempty"`
  229. CreatedAt *Timestamp `json:"created_at,omitempty"`
  230. UpdatedAt *Timestamp `json:"updated_at,omitempty"`
  231. // The following fields are only populated by Webhook events.
  232. ColumnID *int64 `json:"column_id,omitempty"`
  233. }
  234. // ListProjectCards lists the cards in a column of a GitHub Project.
  235. //
  236. // GitHub API docs: https://developer.github.com/v3/projects/cards/#list-project-cards
  237. func (s *ProjectsService) ListProjectCards(ctx context.Context, columnID int64, opt *ListOptions) ([]*ProjectCard, *Response, error) {
  238. u := fmt.Sprintf("projects/columns/%v/cards", columnID)
  239. u, err := addOptions(u, opt)
  240. if err != nil {
  241. return nil, nil, err
  242. }
  243. req, err := s.client.NewRequest("GET", u, nil)
  244. if err != nil {
  245. return nil, nil, err
  246. }
  247. // TODO: remove custom Accept header when this API fully launches.
  248. req.Header.Set("Accept", mediaTypeProjectsPreview)
  249. cards := []*ProjectCard{}
  250. resp, err := s.client.Do(ctx, req, &cards)
  251. if err != nil {
  252. return nil, resp, err
  253. }
  254. return cards, resp, nil
  255. }
  256. // GetProjectCard gets a card in a column of a GitHub Project.
  257. //
  258. // GitHub API docs: https://developer.github.com/v3/projects/cards/#get-a-project-card
  259. func (s *ProjectsService) GetProjectCard(ctx context.Context, columnID int64) (*ProjectCard, *Response, error) {
  260. u := fmt.Sprintf("projects/columns/cards/%v", columnID)
  261. req, err := s.client.NewRequest("GET", u, nil)
  262. if err != nil {
  263. return nil, nil, err
  264. }
  265. // TODO: remove custom Accept header when this API fully launches.
  266. req.Header.Set("Accept", mediaTypeProjectsPreview)
  267. card := &ProjectCard{}
  268. resp, err := s.client.Do(ctx, req, card)
  269. if err != nil {
  270. return nil, resp, err
  271. }
  272. return card, resp, nil
  273. }
  274. // ProjectCardOptions specifies the parameters to the
  275. // ProjectsService.CreateProjectCard and
  276. // ProjectsService.UpdateProjectCard methods.
  277. type ProjectCardOptions struct {
  278. // The note of the card. Note and ContentID are mutually exclusive.
  279. Note string `json:"note,omitempty"`
  280. // The ID (not Number) of the Issue to associate with this card.
  281. // Note and ContentID are mutually exclusive.
  282. ContentID int64 `json:"content_id,omitempty"`
  283. // The type of content to associate with this card. Possible values are: "Issue".
  284. ContentType string `json:"content_type,omitempty"`
  285. }
  286. // CreateProjectCard creates a card in the specified column of a GitHub Project.
  287. //
  288. // GitHub API docs: https://developer.github.com/v3/projects/cards/#create-a-project-card
  289. func (s *ProjectsService) CreateProjectCard(ctx context.Context, columnID int64, opt *ProjectCardOptions) (*ProjectCard, *Response, error) {
  290. u := fmt.Sprintf("projects/columns/%v/cards", columnID)
  291. req, err := s.client.NewRequest("POST", u, opt)
  292. if err != nil {
  293. return nil, nil, err
  294. }
  295. // TODO: remove custom Accept header when this API fully launches.
  296. req.Header.Set("Accept", mediaTypeProjectsPreview)
  297. card := &ProjectCard{}
  298. resp, err := s.client.Do(ctx, req, card)
  299. if err != nil {
  300. return nil, resp, err
  301. }
  302. return card, resp, nil
  303. }
  304. // UpdateProjectCard updates a card of a GitHub Project.
  305. //
  306. // GitHub API docs: https://developer.github.com/v3/projects/cards/#update-a-project-card
  307. func (s *ProjectsService) UpdateProjectCard(ctx context.Context, cardID int64, opt *ProjectCardOptions) (*ProjectCard, *Response, error) {
  308. u := fmt.Sprintf("projects/columns/cards/%v", cardID)
  309. req, err := s.client.NewRequest("PATCH", u, opt)
  310. if err != nil {
  311. return nil, nil, err
  312. }
  313. // TODO: remove custom Accept header when this API fully launches.
  314. req.Header.Set("Accept", mediaTypeProjectsPreview)
  315. card := &ProjectCard{}
  316. resp, err := s.client.Do(ctx, req, card)
  317. if err != nil {
  318. return nil, resp, err
  319. }
  320. return card, resp, nil
  321. }
  322. // DeleteProjectCard deletes a card from a GitHub Project.
  323. //
  324. // GitHub API docs: https://developer.github.com/v3/projects/cards/#delete-a-project-card
  325. func (s *ProjectsService) DeleteProjectCard(ctx context.Context, cardID int64) (*Response, error) {
  326. u := fmt.Sprintf("projects/columns/cards/%v", cardID)
  327. req, err := s.client.NewRequest("DELETE", u, nil)
  328. if err != nil {
  329. return nil, err
  330. }
  331. // TODO: remove custom Accept header when this API fully launches.
  332. req.Header.Set("Accept", mediaTypeProjectsPreview)
  333. return s.client.Do(ctx, req, nil)
  334. }
  335. // ProjectCardMoveOptions specifies the parameters to the
  336. // ProjectsService.MoveProjectCard method.
  337. type ProjectCardMoveOptions struct {
  338. // Position can be one of "top", "bottom", or "after:<card-id>", where
  339. // <card-id> is the ID of a card in the same project.
  340. Position string `json:"position"`
  341. // ColumnID is the ID of a column in the same project. Note that ColumnID
  342. // is required when using Position "after:<card-id>" when that card is in
  343. // another column; otherwise it is optional.
  344. ColumnID int64 `json:"column_id,omitempty"`
  345. }
  346. // MoveProjectCard moves a card within a GitHub Project.
  347. //
  348. // GitHub API docs: https://developer.github.com/v3/projects/cards/#move-a-project-card
  349. func (s *ProjectsService) MoveProjectCard(ctx context.Context, cardID int64, opt *ProjectCardMoveOptions) (*Response, error) {
  350. u := fmt.Sprintf("projects/columns/cards/%v/moves", cardID)
  351. req, err := s.client.NewRequest("POST", u, opt)
  352. if err != nil {
  353. return nil, err
  354. }
  355. // TODO: remove custom Accept header when this API fully launches.
  356. req.Header.Set("Accept", mediaTypeProjectsPreview)
  357. return s.client.Do(ctx, req, nil)
  358. }