projects.go 15 KB

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