Skip to content

Commit

Permalink
optionally create missing project labels on task push
Browse files Browse the repository at this point in the history
  • Loading branch information
jhspetersson committed Dec 24, 2024
1 parent e1d236a commit cb0e441
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
24 changes: 19 additions & 5 deletions src/connectors/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ impl RemoteConnector for GithubRemoteConnector {
user: &String,
repo: &String,
task: &Task,
labels: Option<&Vec<Label>>,
state: RemoteTaskState
) -> Result<(), String> {
match get_token_from_env() {
Expand All @@ -122,6 +123,7 @@ impl RemoteConnector for GithubRemoteConnector {
task.get_id().unwrap().parse().unwrap(),
task.get_property("name").unwrap(),
task.get_property("description").unwrap(),
labels,
state
))
},
Expand Down Expand Up @@ -333,9 +335,11 @@ async fn create_issue(user: &String, repo: &String, task: &Task) -> Result<Strin
create_builder = create_builder.body(description);
}
if let Some(labels) = task.get_labels() {
let _ = prepare_labels(user, repo, labels, &crab).await;
let labels = labels.iter().map(|l| l.get_name()).collect::<Vec<_>>();
create_builder = create_builder.labels(labels);
if !labels.is_empty() {
let _ = prepare_labels(user, repo, labels, &crab).await;
let labels = labels.iter().map(|l| l.get_name()).collect::<Vec<_>>();
create_builder = create_builder.labels(labels);
}
}
match create_builder.send().await {
Ok(issue) => Ok(issue.number.to_string()),
Expand Down Expand Up @@ -400,9 +404,19 @@ async fn prepare_labels(
}
}

async fn update_issue(user: &String, repo: &String, n: u64, title: &String, body: &String, state: IssueState) -> Result<(), String> {
async fn update_issue(user: &String, repo: &String, n: u64, title: &String, body: &String, labels: Option<&Vec<Label>>, state: IssueState) -> Result<(), String> {
let crab = get_octocrab_instance().await;
match crab.issues(user, repo).update(n).title(title).body(body).state(state).send().await {
let crab_issues = crab.issues(user, repo);
let mut update_builder = crab_issues.update(n).title(title).body(body).state(state);
let label_list;
if let Some(labels) = labels {
if !labels.is_empty() {
prepare_labels(user, repo, labels, &crab).await;
}
label_list = labels.iter().map(|l| l.get_name()).collect::<Vec<_>>();
update_builder = update_builder.labels(&label_list);
}
match update_builder.send().await {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string())
}
Expand Down
6 changes: 6 additions & 0 deletions src/connectors/gitlab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,19 @@ impl RemoteConnector for GitlabRemoteConnector {
user: &String,
repo: &String,
task: &Task,
labels: Option<&Vec<Label>>,
state: RemoteTaskState
) -> Result<(), String> {
let client = get_client(get_token_from_env().unwrap().as_str());
let mut endpoint = gitlab::api::projects::issues::EditIssue::builder();
let endpoint = endpoint.project(user.to_string() + "/" + repo).issue(task.get_id().unwrap().parse().unwrap());
endpoint.title(task.get_property("name").unwrap());
endpoint.description(task.get_property("description").unwrap());
if let Some(labels) = labels {
prepare_labels(&client, &user, &repo, &labels);
let labels = labels.iter().map(|l| l.get_name()).collect::<Vec<_>>();
endpoint.labels(labels);
}
endpoint.state_event(if state == RemoteTaskState::Open { IssueStateEvent::Reopen } else { IssueStateEvent::Close });
let endpoint = endpoint.build().unwrap();
match endpoint.query(&client) {
Expand Down
2 changes: 1 addition & 1 deletion src/connectors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub trait RemoteConnector {
fn create_remote_task(&self, user: &String, repo: &String, task: &Task) -> Result<String, String>;
fn create_remote_comment(&self, user: &String, repo: &String, task_id: &String, comment: &Comment) -> Result<String, String>;
fn create_remote_label(&self, user: &String, repo: &String, task_id: &String, label: &Label) -> Result<(), String>;
fn update_remote_task(&self, user: &String, repo: &String, task: &Task, state: RemoteTaskState) -> Result<(), String>;
fn update_remote_task(&self, user: &String, repo: &String, task: &Task, labels: Option<&Vec<Label>>, state: RemoteTaskState) -> Result<(), String>;
fn update_remote_comment(&self, user: &String, repo: &String, task_id: &String, comment_id: &String, text: &String) -> Result<(), String>;
fn delete_remote_task(&self, user: &String, repo: &String, task_id: &String) -> Result<(), String>;
fn delete_remote_comment(&self, user: &String, repo: &String, task_id: &String, comment_id: &String) -> Result<(), String>;
Expand Down
8 changes: 7 additions & 1 deletion src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,13 @@ pub(crate) fn task_push(ids: String, remote: &Option<String>, no_comments: bool,
}
let state = if status_manager.is_done(local_status) { RemoteTaskState::Closed } else { RemoteTaskState::Open };

match connector.update_remote_task(&user, &repo, &local_task, state) {
match connector.update_remote_task(
&user,
&repo,
&local_task,
if !no_labels { local_task.get_labels().into() } else { None },
state
) {
Ok(_) => {
println!("Sync: REMOTE task ID {id} has been updated");
},
Expand Down

0 comments on commit cb0e441

Please sign in to comment.