agent_hub_runtime/core/task_browser/
mutation.rs1use super::{
2 adapter, Binding, CliReader, IssueAction, IssueStateChange, Provider, TaskClient, TaskComment,
3 TaskFailure, TaskKind, TaskSummary,
4};
5use std::sync::atomic::AtomicBool;
6
7impl<R: CliReader> TaskClient<R> {
8 pub fn update_issue(
10 &self,
11 binding: &Binding,
12 number: u64,
13 change: &IssueStateChange,
14 ) -> Result<TaskSummary, TaskFailure> {
15 binding.validate()?;
16 if number == 0
17 || (matches!(change.action, IssueAction::Duplicate)
18 && (change.duplicate_of.is_none()
19 || change.duplicate_of == Some(0)
20 || change.duplicate_of == Some(number)))
21 {
22 return Err(TaskFailure::new(
23 "invalid_input",
24 "Choose a different issue number in this repository",
25 ));
26 }
27 if binding.provider == Provider::Gitlab
28 && !matches!(change.action, IssueAction::Open | IssueAction::Closed)
29 {
30 return Err(TaskFailure::new(
31 "invalid_input",
32 "This provider supports opening and closing issues without a close reason",
33 ));
34 }
35 let cancel = AtomicBool::new(false);
36 let endpoint = adapter::object_endpoint(binding, TaskKind::Issue, number);
37 let current = self.api(binding, endpoint.clone(), &cancel)?;
38 if current.get("pull_request").is_some() {
39 return Err(TaskFailure::new(
40 "invalid_input",
41 "Issue actions cannot change a pull request",
42 ));
43 }
44 let item = match binding.provider {
45 #[cfg(feature = "github")]
46 Provider::Github => crate::core::platforms::github::tasks::update_issue(
47 self, binding, number, change, current, endpoint, &cancel,
48 )?,
49 #[cfg(feature = "gitlab")]
50 Provider::Gitlab => crate::core::platforms::gitlab::tasks::update_issue(
51 self, binding, number, change, current, endpoint, &cancel,
52 )?,
53 #[cfg(not(feature = "github"))]
54 Provider::Github => {
55 binding.provider.require()?;
56 current
57 }
58 #[cfg(not(feature = "gitlab"))]
59 Provider::Gitlab => {
60 binding.provider.require()?;
61 current
62 }
63 };
64 let summary = adapter::summary(binding, TaskKind::Issue, &item)?;
65 let expected_open = matches!(change.action, IssueAction::Open);
66 if summary.number != number
67 || (expected_open && !matches!(summary.state.as_str(), "open" | "opened"))
68 || (!expected_open && summary.state != "closed")
69 {
70 return Err(TaskFailure::new(
71 "invalid_response",
72 "Platform did not confirm the requested issue state",
73 ));
74 }
75 Ok(summary)
76 }
77
78 pub fn add_comment(
79 &self,
80 binding: &Binding,
81 kind: TaskKind,
82 number: u64,
83 body: &str,
84 ) -> Result<TaskComment, TaskFailure> {
85 binding.validate()?;
86 if number == 0 || body.trim().is_empty() || body.len() > 65536 || body.contains('\0') {
87 return Err(TaskFailure::new(
88 "invalid_input",
89 "Enter a non-empty comment within the platform size limit",
90 ));
91 }
92 let response = self.request(
93 binding,
94 "POST",
95 adapter::comments_endpoint(binding, kind, number),
96 &[("body", body.into())],
97 &AtomicBool::new(false),
98 )?;
99 let comment = adapter::comment(binding.provider, &response);
100 if comment.id == 0 {
101 return Err(TaskFailure::new(
102 "invalid_response",
103 "Platform did not confirm comment creation; refresh before retrying",
104 ));
105 }
106 Ok(comment)
107 }
108}