Skip to content

Commit

Permalink
fetch labels when pulling many tasks at once
Browse files Browse the repository at this point in the history
  • Loading branch information
jhspetersson committed Dec 23, 2024
1 parent 7465494 commit a6acff1
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
45 changes: 42 additions & 3 deletions src/connectors/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,31 @@ impl RemoteConnector for GithubRemoteConnector {
}
}

fn list_remote_tasks(&self, user: &String, repo: &String, with_comments: bool, limit: Option<usize>, state: RemoteTaskState, task_statuses: &Vec<String>) -> Vec<Task> {
fn list_remote_tasks(
&self,
user: &String,
repo: &String,
with_comments: bool,
with_labels: bool,
limit: Option<usize>,
state: RemoteTaskState,
task_statuses: &Vec<String>
) -> Vec<Task> {
let state = match state {
RemoteTaskState::Open => State::Open,
RemoteTaskState::Closed => State::Closed,
RemoteTaskState::All => State::All,
};
RUNTIME.block_on(list_issues(user, repo, with_comments, limit, state, task_statuses))
RUNTIME.block_on(
list_issues(
user,
repo,
with_comments,
with_labels,
limit,
state,
task_statuses
))
}

fn get_remote_task(
Expand Down Expand Up @@ -166,7 +184,15 @@ impl RemoteConnector for GithubRemoteConnector {
)]
struct DeleteIssue;

async fn list_issues(user: &String, repo: &String, with_comments: bool, limit: Option<usize>, state: State, task_statuses: &Vec<String>) -> Vec<Task> {
async fn list_issues(
user: &String,
repo: &String,
with_comments: bool,
with_labels: bool,
limit: Option<usize>,
state: State,
task_statuses: &Vec<String>
) -> Vec<Task> {
let mut result = vec![];
let crab = get_octocrab_instance().await;
let stream = crab.issues(user, repo)
Expand Down Expand Up @@ -197,6 +223,19 @@ async fn list_issues(user: &String, repo: &String, with_comments: bool, limit: O
task.set_comments(task_comments);
}

if with_labels {
if !issue.labels.is_empty() {
let labels = issue.labels.iter()
.map(|l| Label::new(
l.name.clone(),
Some(l.color.clone()),
l.description.clone()
))
.collect();
task.set_labels(labels);
}
}

result.push(task);
}

Expand Down
30 changes: 30 additions & 0 deletions src/connectors/gitlab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ impl RemoteConnector for GitlabRemoteConnector {
user: &String,
repo: &String,
with_comments: bool,
with_labels: bool,
limit: Option<usize>,
state: RemoteTaskState,
task_statuses: &Vec<String>
Expand All @@ -77,6 +78,25 @@ impl RemoteConnector for GitlabRemoteConnector {
RemoteTaskState::All => None
};
let client = get_client(get_token_from_env().unwrap().as_str());

let labels = match with_labels {
true => {
let mut endpoint = gitlab::api::projects::labels::Labels::builder();
let endpoint = endpoint.project(user.to_string() + "/" + repo);
let endpoint = endpoint.build().unwrap();
let labels: Vec<GitlabLabel> = gitlab::api::paged(endpoint, Pagination::All).query(&client).unwrap();
let labels = labels.iter()
.map(|gl| Label::new(
gl.name.to_string(),
Some(gl.color.to_string()),
Some(gl.description.to_string())
))
.collect::<Vec<_>>();
labels
},
false => vec![]
};

let mut endpoint = gitlab::api::issues::ProjectIssues::builder();
let mut endpoint = endpoint.project(user.to_string() + "/" + repo).scope(IssueScope::All);
endpoint = match state {
Expand Down Expand Up @@ -105,6 +125,16 @@ impl RemoteConnector for GitlabRemoteConnector {
task.set_comments(comments);
}

if with_labels {
let labels = issue.labels.iter()
.filter_map(|l| match labels.iter().find(|label| label.get_name() == l.to_string()) {
Some(label) => Some(label.clone()),
None => None
})
.collect::<Vec<_>>();
task.set_labels(labels);
}

result.push(task);
}

Expand Down
2 changes: 1 addition & 1 deletion src/connectors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub enum RemoteTaskState {

pub trait RemoteConnector {
fn supports_remote(&self, url: &str) -> Option<(String, String)>;
fn list_remote_tasks(&self, user: &String, repo: &String, with_comments: bool, limit: Option<usize>, state: RemoteTaskState, task_statuses: &Vec<String>) -> Vec<Task>;
fn list_remote_tasks(&self, user: &String, repo: &String, with_comments: bool, with_labels: bool, limit: Option<usize>, state: RemoteTaskState, task_statuses: &Vec<String>) -> Vec<Task>;
fn get_remote_task(&self, user: &String, repo: &String, task_id: &String, with_comments: bool, with_labels: bool, task_statuses: &Vec<String>) -> Option<Task>;
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>;
Expand Down
2 changes: 1 addition & 1 deletion src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ pub(crate) fn task_pull(
None => RemoteTaskState::All
};

let tasks = connector.list_remote_tasks(&user, &repo, !no_comments, limit, state, &task_statuses);
let tasks = connector.list_remote_tasks(&user, &repo, !no_comments, !no_labels, limit, state, &task_statuses);

if tasks.is_empty() {
success_message("No tasks found".to_string())
Expand Down

0 comments on commit a6acff1

Please sign in to comment.