git-task 0.2.4

Local-first task manager/bug tracker within your git repository which can sync issues with remote sources.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
use std::collections::HashMap;
use std::sync::{Arc, LazyLock};

use futures_util::{StreamExt, TryStreamExt};
use graphql_client::{reqwest::post_graphql_blocking as post_graphql, GraphQLQuery};
use octocrab::Octocrab;
use octocrab::models::{CommentId, IssueState};
use octocrab::params::State;
use regex::Regex;
use tokio::pin;
use tokio::runtime::Runtime;

use gittask::{Comment, Label, Task};
use crate::connectors::{RemoteConnector, RemoteTaskState};
use crate::util::color_str_to_rgb_str;

pub struct GithubRemoteConnector;

static RUNTIME: LazyLock<Runtime> = LazyLock::new(|| {
    Runtime::new().unwrap()
});

impl RemoteConnector for GithubRemoteConnector {
    fn type_name(&self) -> &str {
        "github"
    }

    fn supports_remote(&self, url: &str) -> Option<(String, String)> {
        match Regex::new("((https://)|(git@))github.com[/:](?P<user>[a-zA-Z0-9-]+)/(?P<repo>[a-zA-Z0-9-]+)(\\.git)?").unwrap().captures(url) {
            Some(caps) if caps.len() >= 3 => {
                let user = caps.name("user")?.as_str().to_string();
                let repo = caps.name("repo")?.as_str().to_string();
                Some((user, repo))
            },
            _ => None,
        }
    }

    fn list_remote_tasks(
        &self,
        user: &String,
        repo: &String,
        with_comments: bool,
        with_labels: bool,
        limit: Option<usize>,
        state: RemoteTaskState,
        task_statuses: &Vec<String>
    ) -> Result<Vec<Task>, String> {
        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,
                with_labels,
                limit,
                state,
                task_statuses
            ))
    }

    fn get_remote_task(
        &self,
        user: &String,
        repo: &String,
        task_id: &String,
        with_comments: bool,
        with_labels: bool,
        task_statuses: &Vec<String>
    ) -> Result<Task, String> {
        RUNTIME.block_on(
            get_issue(
                &user, &repo, task_id.parse().unwrap(), with_comments, with_labels, task_statuses
            )
        )
    }

    fn create_remote_task(&self, user: &String, repo: &String, task: &Task) -> Result<String, String> {
        match get_token_from_env() {
            Some(_) => RUNTIME.block_on(create_issue(user, repo, task)),
            None => Err("Could not find GITHUB_TOKEN environment variable.".to_string())
        }
    }

    fn create_remote_comment(&self, user: &String, repo: &String, task_id: &String, comment: &Comment) -> Result<String, String> {
        match get_token_from_env() {
            Some(_) => RUNTIME.block_on(create_comment(user, repo, task_id, comment)),
            None => Err("Could not find GITHUB_TOKEN environment variable.".to_string())
        }
    }

    fn create_remote_label(&self, user: &String, repo: &String, task_id: &String, label: &Label) -> Result<(), String> {
        match get_token_from_env() {
            Some(_) => RUNTIME.block_on(
                add_label(
                    user,
                    repo,
                    task_id.parse().unwrap(),
                    label
                )),
            None => Err("Could not find GITHUB_TOKEN environment variable.".to_string())
        }
    }

    fn update_remote_task(
        &self,
        user: &String,
        repo: &String,
        task: &Task,
        labels: Option<&Vec<Label>>,
        state: RemoteTaskState
    ) -> Result<(), String> {
        match get_token_from_env() {
            Some(_) => {
                let state = match state {
                    RemoteTaskState::Closed(_, _) => IssueState::Closed,
                    _ => IssueState::Open,
                };
                RUNTIME.block_on(
                    update_issue(
                        user,
                        repo,
                        task.get_id().unwrap().parse().unwrap(),
                        task.get_property("name").unwrap(),
                        task.get_property("description").unwrap(),
                        labels,
                        state
                    ))
            },
            None => Err("Could not find GITHUB_TOKEN environment variable.".to_string())
        }
    }

    fn update_remote_comment(&self, user: &String, repo: &String, _task_id: &String, comment_id: &String, text: &String) -> Result<(), String> {
        match get_token_from_env() {
            Some(_) => RUNTIME.block_on(update_comment(user, repo, comment_id.parse().unwrap(), text)),
            None => Err("Could not find GITHUB_TOKEN environment variable.".to_string())
        }
    }

    fn delete_remote_task(&self, user: &String, repo: &String, task_id: &String) -> Result<(), String> {
        match get_token_from_env() {
            Some(token) => {
                let issue_id = RUNTIME.block_on(get_issue_id(user, repo, task_id.parse().unwrap()));
                if issue_id.is_err() {
                    return Err("Could not match task ID with GitHub internal issue ID.".to_string());
                }
                let issue_id = issue_id?;
                let variables = delete_issue::Variables {
                    issue_id,
                };

                let client = reqwest::blocking::Client::builder()
                    .user_agent("git-task/".to_owned() + env!("CARGO_PKG_VERSION"))
                    .default_headers(
                        std::iter::once((
                            reqwest::header::AUTHORIZATION,
                            reqwest::header::HeaderValue::from_str(&format!("Bearer {}", token)).unwrap(),
                        )).collect(),
                    )
                    .build().unwrap();

                let response_body = post_graphql::<DeleteIssue, _>(&client, "https://api.github.com/graphql", variables).expect("Failed to make GraphQL request");

                if let Some(errors) = response_body.errors {
                    if !errors.is_empty() {
                        return Err(errors.first().unwrap().message.clone());
                    }
                }

                let response_data: Option<delete_issue::ResponseData> = response_body.data;

                if response_data.is_none() {
                    return Err("Missing response data.".to_string());
                }

                match response_data {
                    Some(_) => Ok(()),
                    None => Err("Response data not found".to_string())
                }
            },
            None => Err("Could not find GITHUB_TOKEN environment variable.".to_string()),
        }
    }

    fn delete_remote_comment(&self, user: &String, repo: &String, _task_id: &String, comment_id: &String) -> Result<(), String> {
        match get_token_from_env() {
            Some(_) => RUNTIME.block_on(delete_comment(user, repo, comment_id.parse().unwrap())),
            None => Err("Could not find GITHUB_TOKEN environment variable.".to_string())
        }
    }

    fn delete_remote_label(&self, user: &String, repo: &String, task_id: &String, name: &String) -> Result<(), String> {
        match get_token_from_env() {
            Some(_) => RUNTIME.block_on(delete_label(user, repo, task_id.parse().unwrap(), name)),
            None => Err("Could not find GITHUB_TOKEN environment variable.".to_string())
        }
    }
}

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "resources/github/schema.graphql",
    query_path = "resources/github/delete_issue.graphql",
    response_derives = "Debug"
)]
struct DeleteIssue;

async fn list_issues(
    user: &String,
    repo: &String,
    with_comments: bool,
    with_labels: bool,
    limit: Option<usize>,
    state: State,
    task_statuses: &Vec<String>
) -> Result<Vec<Task>, String> {
    let mut result = vec![];
    let crab = get_octocrab_instance().await;
    let stream = crab.issues(user, repo)
        .list()
        .state(state)
        .per_page(100)
        .send()
        .await.map_err(|e| e.to_string())?
        .into_stream(&crab);
    pin!(stream);
    let mut count = 0;
    while let Some(issue) = stream.try_next().await.map_err(|e| e.to_string())? {
        if limit.is_some() && count >= limit.unwrap() {
            break;
        }
        count += 1;
        let mut props = HashMap::new();
        props.insert(String::from("name"), issue.title);
        props.insert(String::from("status"), if issue.state == IssueState::Open { task_statuses.first().unwrap().clone() } else { task_statuses.last().unwrap().clone() } );
        props.insert(String::from("description"), issue.body.unwrap_or(String::new()));
        props.insert(String::from("created"), issue.created_at.timestamp().to_string());
        props.insert(String::from("author"), issue.user.login);

        let mut task = Task::from_properties(issue.number.to_string(), props).unwrap();

        if with_comments {
            let task_comments = list_issue_comments(&user, &repo, issue.number).await;
            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);
    }

    Ok(result)
}

async fn list_issue_comments(user: &String, repo: &String, n: u64) -> Vec<Comment> {
    let mut result = vec![];
    let crab = get_octocrab_instance().await;
    let stream = crab.issues(user, repo)
        .list_comments(n)
        .per_page(100)
        .send()
        .await.unwrap()
        .into_stream(&crab);
    pin!(stream);
    while let Some(comment) = stream.try_next().await.unwrap() {
        let comment = Comment::new(comment.id.to_string(), HashMap::from([
            ("author".to_string(), comment.user.login),
            ("created".to_string(), comment.created_at.timestamp().to_string()),
        ]), comment.body.unwrap());
        result.push(comment);
    }

    result
}

async fn get_issue(
    user: &String,
    repo: &String,
    n: u64,
    with_comments: bool,
    with_labels: bool,
    task_statuses: &Vec<String>
) -> Result<Task, String> {
    let crab = get_octocrab_instance().await;
    let issue = crab.issues(user, repo).get(n).await;
    match issue {
        Ok(issue) => {
            let mut props = HashMap::new();
            props.insert(String::from("name"), issue.title);
            props.insert(String::from("status"), if issue.state == IssueState::Open { task_statuses.first().unwrap().clone() } else { task_statuses.last().unwrap().clone() } );
            props.insert(String::from("description"), issue.body.unwrap_or(String::new()));
            props.insert(String::from("created"), issue.created_at.timestamp().to_string());
            props.insert(String::from("author"), issue.user.login);

            let mut task = Task::from_properties(n.to_string(), props).unwrap();

            if with_comments {
                let task_comments = list_issue_comments(user, repo, issue.number).await;
                task.set_comments(task_comments);
            }

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

            Ok(task)
        },
        Err(e) => Err(e.to_string())
    }
}

async fn create_issue(user: &String, repo: &String, task: &Task) -> Result<String, String> {
    let crab = get_octocrab_instance().await;
    let crab_issues = crab.issues(user, repo);
    let mut create_builder = crab_issues.create(task.get_property("name").unwrap());
    if let Some(description) = task.get_property("description") {
        create_builder = create_builder.body(description);
    }
    if let Some(labels) = task.get_labels() {
        if !labels.is_empty() {
            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()),
        Err(e) => Err(e.to_string())
    }
}

async fn create_comment(user: &String, repo: &String, task_id: &String, comment: &Comment) -> Result<String, String> {
    let crab = get_octocrab_instance().await;
    match crab.issues(user, repo).create_comment(task_id.parse().unwrap(), comment.get_text()).await {
        Ok(comment) => Ok(comment.id.to_string()),
        Err(e) => Err(e.to_string())
    }
}

async fn add_label(
    user: &String,
    repo: &String,
    n: u64,
    label: &Label,
) -> Result<(), String> {
    let crab = get_octocrab_instance().await;
    let _ = prepare_labels(user, repo, &vec![label.clone()], &crab).await;
    let add_label_body = vec![label.get_name()];
    crab
        .issues(user, repo)
        .add_labels(n, &add_label_body)
        .await
        .map(|_| ())
        .map_err(|e| e.to_string())
}

async fn prepare_labels(
    user: &String,
    repo: &String,
    labels: &Vec<Label>,
    crab: &Arc<Octocrab>)
{
    let existing_labels_stream = crab
        .issues(user, repo)
        .list_labels_for_repo()
        .per_page(100)
        .send()
        .await.unwrap()
        .into_stream(&crab);
    pin!(existing_labels_stream);
    let mut labels_to_create = labels.clone();
    while let Some(Ok(label)) = existing_labels_stream.next().await {
        if let Some(pos) = labels_to_create.iter().position(|l| l.get_name() == label.name) {
            labels_to_create.remove(pos);
        }
    }
    for l in labels_to_create.iter() {
        let _ = crab
            .issues(user, repo)
            .create_label(
                l.get_name(),
                color_str_to_rgb_str(&l.get_color()),
                l.get_description().unwrap_or_else(|| "".to_string()),
            )
            .await;
    }
}

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;
    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())
    }
}

async fn update_comment(user: &String, repo: &String, n: u64, text: &String) -> Result<(), String> {
    let crab = get_octocrab_instance().await;
    match crab.issues(user, repo).update_comment(CommentId(n), text).await {
        Ok(_) => Ok(()),
        Err(e) => Err(e.to_string())
    }
}

async fn delete_comment(user: &String, repo: &String, n: u64) -> Result<(), String> {
    let crab = get_octocrab_instance().await;
    match crab.issues(user, repo).delete_comment(CommentId(n)).await {
        Ok(_) => Ok(()),
        Err(e) => Err(e.to_string())
    }
}

pub async fn delete_label(
    user: &String,
    repo: &String,
    n: u64,
    label_name: &str,
) -> Result<(), String> {
    let crab = get_octocrab_instance().await;

    crab
        .issues(user, repo)
        .remove_label(n, label_name)
        .await
        .map(|_| ())
        .map_err(|e| e.to_string())
}

async fn get_issue_id(user: &String, repo: &String, n: u64) -> Result<String, String> {
    let crab = get_octocrab_instance().await;
    let issue = crab.issues(user, repo).get(n).await;
    match issue {
        Ok(issue) => Ok(issue.node_id),
        Err(e) => Err(e.to_string()),
    }
}

async fn get_octocrab_instance() -> Arc<Octocrab> {
    match get_token_from_env() {
        Some(token) => Arc::new(Octocrab::builder().personal_token(token).build().unwrap()),
        None => octocrab::instance()
    }
}

fn get_token_from_env() -> Option<String> {
    std::env::var("GITHUB_TOKEN").or_else(|_| std::env::var("GITHUB_API_TOKEN")).ok()
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_remote_url() {
        let connector = GithubRemoteConnector {};

        assert!(connector.supports_remote("git@github.com:VIK-777/java-telegram-meetup-bot.git").is_some());
        assert!(connector.supports_remote("https://github.com/jhspetersson/fselect.git").is_some());
    }
}