gitpoulpi-hosting 1.4.0

Hosting and issue-tracker API clients (GitHub, GitLab, Gitea/Forgejo, Azure DevOps, Bitbucket, Gerrit, Jira, Trello, Linear, YouTrack, Redmine, local Markdown issues): project listing, clone helpers, SSH key generation and upload, remote-URL provider matching.
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
//! Host-driven flows, the shared engine of the CLI, the MCP server and the
//! TUI: create the issue, make the branch on the host, find what the host or a
//! bot made for an issue, wait for it. Everything here is a function over a
//! [`HostingClient`], so a fake transport tests it and the three frontends
//! cannot drift apart. The loops (sleeping, cancelling, drawing a spinner) are
//! the frontend's: it asks [`find_for_issue`] again after [`backoff`].

use std::time::Duration;

use gitpoulpi_core::workflow::{
    branches_for_issue, issue_branch_pattern, slugify, BranchPattern, Fields, IssueDraft,
    IssueFlow, WaitFor, DEFAULT_SLUG_MAX,
};

use crate::client::{CreatedIssue, CreatedPullRequest, HostingClient, NewIssue, NewPullRequest};
use crate::issue_flow::{CreatedBranch, NewIssueBranch};
use crate::model::{PrFilter, PullRequestSummary, StateFilter};
use crate::{Error, Result};

/// What the host (or a bot) made for an issue, as far as it could be told.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Found {
    /// Branch names (without a remote), the host's links first.
    pub branches: Vec<String>,
    /// Merge / pull requests tied to the issue by the host or by name.
    pub requests: Vec<PullRequestSummary>,
    /// Sources that failed (a host without the endpoint, a 403): the rest
    /// still answered.
    pub notes: Vec<String>,
}

impl Found {
    /// Whether what was found is what `wait` asked for.
    pub fn satisfies(&self, wait: WaitFor) -> bool {
        (wait.wants_branch() && !self.branches.is_empty())
            || (wait.wants_request() && !self.requests.is_empty())
    }

    /// The branch to pick up: the one of the newest live request, else the
    /// first branch found.
    pub fn best_branch(&self) -> Option<&str> {
        let live = self.requests.iter().find(|r| {
            matches!(
                r.state,
                crate::model::PullRequestState::Open | crate::model::PullRequestState::Draft
            )
        });
        live.map(|r| r.source_branch.as_str())
            .or_else(|| self.requests.first().map(|r| r.source_branch.as_str()))
            .or_else(|| self.branches.first().map(String::as_str))
            .filter(|b| !b.is_empty())
    }

    /// The request that has `branch` as its source: its target is the
    /// branch's parent.
    pub fn request_of(&self, branch: &str) -> Option<&PullRequestSummary> {
        self.requests.iter().find(|r| r.source_branch == branch)
    }
}

/// How long to wait before asking the host again after `attempt` tries
/// (0-based): 1 s, 2 s, 3 s, 5 s, 8 s, then every 10 s. Bounded so a bot that
/// takes minutes costs a few dozen cheap calls, never a hammered host.
/// `GITPOULPI_FLOW_POLL_MS` fixes the pause (tests and benches).
pub fn backoff(attempt: u32) -> Duration {
    if let Some(ms) = std::env::var("GITPOULPI_FLOW_POLL_MS")
        .ok()
        .and_then(|v| v.parse::<u64>().ok())
    {
        return Duration::from_millis(ms.max(10));
    }
    const STEPS: [u64; 5] = [1, 2, 3, 5, 8];
    Duration::from_secs(STEPS.get(attempt as usize).copied().unwrap_or(10))
}

/// The text a branch search for `number` starts with, for each type's
/// pattern (`feature/12` for `feature/{issue}-{slug}`), without duplicates.
fn searches(flow: &IssueFlow, type_ids: &[String], number: u64) -> Vec<String> {
    let mut out: Vec<String> = Vec::new();
    for t in type_ids {
        if let Ok(pattern) = issue_branch_pattern(flow, t) {
            let s = format!("{}{number}", pattern.prefix());
            if !out.contains(&s) {
                out.push(s);
            }
        }
    }
    if out.is_empty() {
        out.push(number.to_string());
    }
    out
}

/// Asks the host what exists for issue `number`: its own links, the branches
/// matching the workflow's `branch_from_issue` and the requests whose source
/// branch does. A source that fails is noted, not fatal; when every source
/// fails the error of the first is returned.
pub async fn find_for_issue(
    client: &dyn HostingClient,
    project: &str,
    flow: &IssueFlow,
    type_ids: &[String],
    number: u64,
) -> Result<Found> {
    let mut found = Found::default();
    let mut answered = false;
    let mut first_error: Option<Error> = None;
    let note = |what: &str, e: Error, found: &mut Found, first: &mut Option<Error>| {
        found.notes.push(format!("{what}: {e}"));
        if first.is_none() {
            *first = Some(e);
        }
    };
    let ids: Vec<&str> = type_ids.iter().map(String::as_str).collect();

    match client.issue_links(project, number).await {
        Ok(links) => {
            answered = true;
            found.branches.extend(links.branches);
            found.requests.extend(links.requests);
        }
        Err(e) => note("the issue's links", e, &mut found, &mut first_error),
    }
    let mut names: Vec<String> = Vec::new();
    for search in searches(flow, type_ids, number) {
        match client.branch_names(project, &search).await {
            Ok(rows) => {
                answered = true;
                names.extend(rows);
            }
            Err(e) => {
                note("the branch search", e, &mut found, &mut first_error);
                break;
            }
        }
    }
    for b in branches_for_issue(flow, &ids, &names, number) {
        if !found.branches.contains(&b) {
            found.branches.push(b);
        }
    }
    // Requests the host did not link (Gitea, a bot that does not close the
    // issue): their source branch carries the issue number.
    if found.requests.is_empty() {
        let filter = PrFilter {
            state: StateFilter::Open,
            ..PrFilter::default()
        };
        match client.list_pull_requests(project, &filter).await {
            Ok(rows) => {
                answered = true;
                let sources: Vec<String> = rows.iter().map(|r| r.source_branch.clone()).collect();
                let matching = branches_for_issue(flow, &ids, &sources, number);
                found.requests.extend(
                    rows.into_iter()
                        .filter(|r| matching.contains(&r.source_branch)),
                );
            }
            Err(e) => note("the request list", e, &mut found, &mut first_error),
        }
    }
    for r in &found.requests {
        if !r.source_branch.is_empty() && !found.branches.contains(&r.source_branch) {
            found.branches.push(r.source_branch.clone());
        }
    }
    if !answered {
        if let Some(e) = first_error {
            return Err(e);
        }
    }
    Ok(found)
}

/// Creates the issue of `draft` on `project`.
pub async fn create_issue(
    client: &dyn HostingClient,
    project: &str,
    draft: &IssueDraft,
    milestone: Option<u64>,
) -> Result<CreatedIssue> {
    client
        .create_issue(&NewIssue {
            project: project.to_string(),
            title: draft.title.clone(),
            body: draft.body.clone(),
            labels: draft.labels.clone(),
            assignees: draft.assignees.clone(),
            milestone,
            ..NewIssue::default()
        })
        .await
}

/// The name the host-made branch of issue `number` gets: the workflow's
/// `branch_from_issue` with the type, the issue number and the title's slug.
pub fn issue_branch_name(
    flow: &IssueFlow,
    type_id: &str,
    number: u64,
    title: &str,
) -> std::result::Result<String, String> {
    let pattern: BranchPattern =
        issue_branch_pattern(flow, type_id).map_err(|e| format!("branch_from_issue: {e}"))?;
    let mut fields = Fields::new();
    fields.insert("issue".into(), number.to_string());
    fields.insert("issue_key".into(), format!("{type_id}-{number}"));
    let mut slug = slugify(title, DEFAULT_SLUG_MAX);
    if slug.is_empty() {
        slug = "issue".into();
    }
    fields.insert("slug".into(), slug);
    pattern.render(&fields).map_err(|e| e.to_string())
}

/// What `host-creates-branch` made.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HostMade {
    pub branch: CreatedBranch,
    pub request: Option<CreatedPullRequest>,
    /// The merge request could not be opened (a host refuses one with no
    /// commits of its own); the branch exists.
    pub warning: Option<String>,
}

/// Creates `name` from `from` on the host for the issue and, when `open_request`,
/// a draft merge request into `target` that closes the issue.
#[allow(clippy::too_many_arguments)]
pub async fn host_creates_branch(
    client: &dyn HostingClient,
    project: &str,
    number: u64,
    name: &str,
    from: &str,
    target: &str,
    title: &str,
    open_request: bool,
) -> Result<HostMade> {
    let branch = client
        .create_issue_branch(&NewIssueBranch {
            project: project.to_string(),
            issue_number: number,
            name: name.to_string(),
            from: from.to_string(),
        })
        .await?;
    let mut made = HostMade {
        branch,
        request: None,
        warning: None,
    };
    if open_request {
        let result = client
            .create_pull_request(&NewPullRequest {
                project: project.to_string(),
                source_branch: made.branch.name.clone(),
                target_branch: target.to_string(),
                title: title.to_string(),
                body: format!("Closes #{number}"),
                draft: true,
                ..NewPullRequest::default()
            })
            .await;
        match result {
            Ok(created) => made.request = Some(created),
            Err(e) => {
                made.warning = Some(format!(
                    "the branch exists but the merge request could not be opened: {e}"
                ))
            }
        }
    }
    Ok(made)
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use gitpoulpi_config::IntegrationKind;

    use crate::client::client_for;
    use crate::http::RecordedTransport;
    use crate::integration::Integration;

    use super::*;

    fn block_on<F: std::future::Future>(f: F) -> F::Output {
        tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .unwrap()
            .block_on(f)
    }

    fn gitlab(t: Arc<RecordedTransport>) -> Box<dyn HostingClient> {
        client_for(
            Integration::new(
                "gl",
                IntegrationKind::GitlabSelfManaged,
                "https://gl.example.com",
            ),
            "t".into(),
            t,
            &crate::trust::TrustGate::for_tests(),
        )
        .unwrap()
    }

    fn mr(iid: u64, state: &str, source: &str) -> String {
        format!(
            r#"{{"iid":{iid},"title":"t","state":"{state}","source_branch":"{source}","target_branch":"develop",
            "source_project_id":1,"target_project_id":1,"web_url":"u","updated_at":""}}"#
        )
    }

    #[test]
    fn backoff_grows_then_stays_at_ten_seconds() {
        if std::env::var("GITPOULPI_FLOW_POLL_MS").is_ok() {
            return;
        }
        let secs: Vec<u64> = (0..8).map(|i| backoff(i).as_secs()).collect();
        assert_eq!(secs, [1, 2, 3, 5, 8, 10, 10, 10]);
    }

    #[test]
    fn nothing_yet_is_an_empty_answer_not_an_error() {
        let t = Arc::new(
            RecordedTransport::new()
                .on("/related_merge_requests", 200, "[]")
                .on("/closed_by", 200, "[]")
                .on("/repository/branches", 200, "[]")
                .on("/merge_requests", 200, "[]"),
        );
        let found = block_on(find_for_issue(
            &*gitlab(t),
            "g/p",
            &IssueFlow::default(),
            &["feature".to_string()],
            12,
        ))
        .unwrap();
        assert!(!found.satisfies(WaitFor::BranchOrMergeRequest));
        assert!(found.best_branch().is_none());
    }

    #[test]
    fn a_bot_branch_and_its_request_are_found_by_number_and_by_the_hosts_link() {
        let t = Arc::new(
            RecordedTransport::new()
                .on(
                    "/related_merge_requests",
                    200,
                    &format!("[{}]", mr(5, "opened", "feature/12-login")),
                )
                .on("/closed_by", 200, "[]")
                .on(
                    "/repository/branches",
                    200,
                    r#"[{"name":"feature/12-login"},{"name":"feature/120-x"}]"#,
                ),
        );
        let found = block_on(find_for_issue(
            &*gitlab(t),
            "g/p",
            &IssueFlow::default(),
            &["feature".to_string(), "bugfix".to_string()],
            12,
        ))
        .unwrap();
        assert_eq!(found.branches, ["feature/12-login"]);
        assert_eq!(found.best_branch(), Some("feature/12-login"));
        assert_eq!(
            found.request_of("feature/12-login").unwrap().target_branch,
            "develop"
        );
        assert!(found.satisfies(WaitFor::Branch));
        assert!(found.satisfies(WaitFor::MergeRequest));
    }

    #[test]
    fn a_request_the_host_did_not_link_is_found_by_its_source_branch() {
        let t = Arc::new(
            RecordedTransport::new()
                .on("/related_merge_requests", 200, "[]")
                .on("/closed_by", 200, "[]")
                .on("/repository/branches", 200, "[]")
                .on(
                    "/merge_requests",
                    200,
                    &format!(
                        "[{},{}]",
                        mr(6, "opened", "feature/12-a"),
                        mr(7, "opened", "feature/13-b")
                    ),
                ),
        );
        let found = block_on(find_for_issue(
            &*gitlab(t),
            "g/p",
            &IssueFlow::default(),
            &["feature".to_string()],
            12,
        ))
        .unwrap();
        assert_eq!(found.requests.len(), 1);
        assert_eq!(found.requests[0].number, 6);
        // The request's branch is a branch of the issue too.
        assert_eq!(found.branches, ["feature/12-a"]);
    }

    #[test]
    fn when_every_source_fails_the_error_comes_back() {
        let t = Arc::new(RecordedTransport::new().on("/projects/", 500, "{}"));
        let r = block_on(find_for_issue(
            &*gitlab(t),
            "g/p",
            &IssueFlow::default(),
            &["feature".to_string()],
            1,
        ));
        assert!(r.is_err());
    }

    #[test]
    fn the_branch_name_follows_the_pattern() {
        let flow = IssueFlow::default();
        assert_eq!(
            issue_branch_name(&flow, "feature", 12, "Login page!").unwrap(),
            "feature/12-login-page"
        );
        assert_eq!(
            issue_branch_name(&flow, "bugfix", 3, "???").unwrap(),
            "bugfix/3-issue"
        );
    }

    #[test]
    fn host_creates_the_branch_then_a_draft_request_that_closes_the_issue() {
        let t = Arc::new(
            RecordedTransport::new()
                .on("/repository/branches", 201, r#"{"name":"feature/12-x"}"#)
                .on(
                    "/merge_requests",
                    201,
                    r#"{"iid":9,"web_url":"https://gl.example.com/mr/9"}"#,
                ),
        );
        let made = block_on(host_creates_branch(
            &*gitlab(t.clone()),
            "g/p",
            12,
            "feature/12-x",
            "develop",
            "develop",
            "Login",
            true,
        ))
        .unwrap();
        assert_eq!(made.request.unwrap().number, 9);
        let mr_request = &t.requests()[1];
        let body: serde_json::Value =
            serde_json::from_str(mr_request.body.as_deref().unwrap()).unwrap();
        assert_eq!(body["description"], "Closes #12");
        assert_eq!(body["source_branch"], "feature/12-x");
        assert!(body["title"].as_str().unwrap().starts_with("Draft: "));
    }

    #[test]
    fn a_refused_request_leaves_the_branch_and_a_warning() {
        let t = Arc::new(
            RecordedTransport::new()
                .on("/repository/branches", 201, r#"{"name":"b"}"#)
                .on("/merge_requests", 422, r#"{"message":"no commits"}"#),
        );
        let made = block_on(host_creates_branch(
            &*gitlab(t),
            "g/p",
            1,
            "b",
            "main",
            "main",
            "t",
            true,
        ))
        .unwrap();
        assert!(made.request.is_none());
        assert!(made.warning.unwrap().contains("could not be opened"));
    }
}