Slash commands are cool
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.

130 lines
3.5KB

  1. use serde::{Deserialize, Serialize};
  2. use reqwest::blocking;
  3. pub const GITEA_API_URL: &str = "https://git.exmods.org/api/v1";
  4. // API endpoints
  5. pub fn get_releases(owner: &str, repo: &str) -> Result<Vec<Release>, String> {
  6. let client = blocking::Client::new();
  7. let url = format!("{}/repos/{}/{}/releases", GITEA_API_URL, owner, repo);
  8. let result = client.get(&url).send();
  9. if let Ok(resp) = result {
  10. let status = resp.status();
  11. if !status.is_success() {
  12. return Err(format!("{} {}", status.as_str(), status.canonical_reason().unwrap_or("???")));
  13. }
  14. let result = resp.json::<Vec<Release>>();
  15. if let Ok(data) = result {
  16. return Ok(data);
  17. } else {
  18. return Err(format!("Invalid JSON payload {}", result.err().unwrap()));
  19. }
  20. }
  21. Err("Invalid Result".to_string())
  22. }
  23. pub fn get_issue_by_index(owner: &str, repo: &str, index: usize) -> Result<Issue, String> {
  24. let client = blocking::Client::new();
  25. let url = format!("{}/repos/{}/{}/issues/{}", GITEA_API_URL, owner, repo, index);
  26. let result = client.get(&url).send();
  27. if let Ok(resp) = result {
  28. let status = resp.status();
  29. if !status.is_success() {
  30. return Err(format!("{} {}", status.as_str(), status.canonical_reason().unwrap_or("???")));
  31. }
  32. let result = resp.json::<Issue>();
  33. if let Ok(data) = result {
  34. return Ok(data);
  35. } else {
  36. return Err(format!("Invalid JSON payload {}", result.err().unwrap()));
  37. }
  38. }
  39. Err("Invalid Result".to_string())
  40. }
  41. // API structures
  42. #[derive(Serialize, Deserialize, Clone)]
  43. pub struct Release {
  44. pub id: usize,
  45. pub tag_name: String,
  46. pub target_commitish: String,
  47. pub name: String,
  48. pub body: String,
  49. pub url: String,
  50. pub tarball_url: String,
  51. pub zipball_url: String,
  52. pub draft: bool,
  53. pub prerelease: bool,
  54. pub created_at: String,
  55. pub published_at: String,
  56. pub author: Author,
  57. pub assets: Option<Vec<Asset>>,
  58. }
  59. #[derive(Serialize, Deserialize, Clone)]
  60. pub struct Author {
  61. pub id: usize,
  62. pub login: String,
  63. pub full_name: String,
  64. pub email: String,
  65. pub avatar_url: String,
  66. pub language: String,
  67. pub is_admin: bool,
  68. pub last_login: String,
  69. pub created: String,
  70. pub username: String,
  71. }
  72. #[derive(Serialize, Deserialize, Clone)]
  73. pub struct Asset {
  74. pub id: usize,
  75. pub name: String,
  76. pub size: usize,
  77. pub download_count: usize,
  78. pub created_at: String,
  79. pub uuid: String,
  80. pub browser_download_url: String,
  81. }
  82. #[derive(Serialize, Deserialize, Clone)]
  83. pub struct Issue {
  84. pub id: usize,
  85. pub url: String,
  86. pub html_url: String,
  87. pub number: usize,
  88. pub user: Author,
  89. pub original_author: String,
  90. pub original_author_id: usize,
  91. pub title: String,
  92. pub body: String,
  93. pub labels: Vec<IssueTag>,
  94. //pub milestone: Option<???>,
  95. //pub assignee: Option<???>,
  96. //pub assignees: Option<???>,
  97. pub state: String,
  98. pub comments: usize,
  99. pub created_at: String,
  100. pub updated_at: String,
  101. pub closed_at: Option<String>,
  102. pub due_date: Option<String>,
  103. //pub pull_request: Option<???>,
  104. pub repository: IssueRespository,
  105. }
  106. #[derive(Serialize, Deserialize, Clone)]
  107. pub struct IssueTag {
  108. pub id: usize,
  109. pub name: String,
  110. pub color: String,
  111. pub description: String,
  112. pub url: Option<String>,
  113. }
  114. #[derive(Serialize, Deserialize, Clone)]
  115. pub struct IssueRespository {
  116. pub id: usize,
  117. pub name: String,
  118. pub full_name: String,
  119. }