pcu 0.6.14

A CI tool to update change log in a PR
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#![allow(dead_code)]
use std::{future::Future, pin::Pin, time::Duration};

use serde::{Deserialize, Serialize};

use crate::{Error, GraphQLWrapper};

/// Default number of retry attempts when `associatedPullRequests` returns empty.
const DEFAULT_RETRY_ATTEMPTS: u32 = 3;

/// Default base delay between retry attempts (seconds).
const DEFAULT_BASE_DELAY_SECS: u64 = 5;

/// Configuration for retry-with-exponential-backoff behaviour.
#[derive(Debug, Clone)]
struct RetryConfig {
    /// Maximum number of retry attempts (not counting the initial try).
    max_retries: u32,
    /// Base delay for the first retry.  Subsequent retries double this value.
    base_delay: Duration,
}

impl Default for RetryConfig {
    fn default() -> Self {
        Self {
            max_retries: DEFAULT_RETRY_ATTEMPTS,
            base_delay: Duration::from_secs(DEFAULT_BASE_DELAY_SECS),
        }
    }
}

#[derive(Deserialize, Debug, Clone)]
struct Data {
    repository: Repository,
}

#[derive(Deserialize, Debug, Clone)]
struct Repository {
    object: Option<Commit>,
}

#[derive(Deserialize, Debug, Clone)]
struct Commit {
    #[serde(rename = "associatedPullRequests")]
    associated_pull_requests: AssociatedPullRequests,
}

#[derive(Deserialize, Debug, Clone)]
struct AssociatedPullRequests {
    nodes: Vec<PullRequest>,
}

#[derive(Deserialize, Debug, Clone)]
struct PullRequest {
    number: i64,
    title: String,
    body: String,
    url: String,
    #[serde(rename = "mergedAt")]
    merged_at: Option<String>,
}

#[derive(Serialize, Debug, Clone)]
struct Vars {
    owner: String,
    name: String,
    oid: String,
}

/// The outcome of a single query attempt, used by the retry loop.
#[derive(Debug)]
enum QueryOutcome {
    /// A PR was found.
    Found(i64, String, String, String),
    /// The query succeeded but `associatedPullRequests` returned no nodes, or
    /// the commit object was not yet indexed.  Retrying may help.
    TransientEmpty,
    /// A hard error occurred (auth failure, network error, …).  Do not retry.
    HardError(Error),
}

/// Select the best PR from a non-empty list and return the tuple fields.
fn select_pr(prs: &[PullRequest]) -> Option<(i64, String, String, String)> {
    let pr = prs
        .iter()
        .filter(|pr| pr.merged_at.is_some())
        .max_by_key(|pr| pr.merged_at.as_ref())
        .or_else(|| prs.first())?;
    Some((pr.number, pr.title.clone(), pr.url.clone(), pr.body.clone()))
}

/// Classify a single query result as found, transient-empty, or a hard error.
fn classify_query_result(result: Result<Data, Error>, attempt: u32) -> QueryOutcome {
    match result {
        Err(e) => QueryOutcome::HardError(e),
        Ok(data) => classify_data(data, attempt),
    }
}

fn classify_data(data: Data, attempt: u32) -> QueryOutcome {
    let Some(commit) = data.repository.object else {
        log::debug!("Commit object not found in GitHub index (attempt {attempt}), will retry");
        return QueryOutcome::TransientEmpty;
    };
    let prs = commit.associated_pull_requests.nodes;
    if prs.is_empty() {
        log::debug!("associatedPullRequests returned empty (attempt {attempt}), will retry");
        return QueryOutcome::TransientEmpty;
    }
    match select_pr(&prs) {
        Some(result) => QueryOutcome::Found(result.0, result.1, result.2, result.3),
        None => QueryOutcome::HardError(Error::InvalidMergeCommitMessage),
    }
}

/// Core retry loop — separated from the GraphQL call so that tests can inject
/// a fake query provider without hitting the network.
///
/// `query_fn` is called once per attempt and must return the raw
/// `Result<Data, Error>` that a single GraphQL call would produce.
///
/// Retries only the transient-empty case (empty `associatedPullRequests` or
/// commit object not yet indexed). Hard errors are propagated immediately.
async fn get_pull_request_by_commit_with_retry<F>(
    query_fn: F,
    config: RetryConfig,
) -> Result<(i64, String, String, String), Error>
where
    F: Fn() -> Pin<Box<dyn Future<Output = Result<Data, Error>> + Send>>,
{
    let mut attempt = 0u32;
    let mut delay = config.base_delay;

    loop {
        let outcome = classify_query_result(query_fn().await, attempt);

        match outcome {
            QueryOutcome::Found(number, title, url, body) => {
                return Ok((number, title, url, body));
            }
            QueryOutcome::HardError(e) => {
                return Err(e);
            }
            QueryOutcome::TransientEmpty => {
                if attempt >= config.max_retries {
                    log::warn!(
                        "Exhausted {max} retries waiting for associatedPullRequests; \
                         giving up",
                        max = config.max_retries
                    );
                    return Err(Error::InvalidMergeCommitMessage);
                }
                log::info!(
                    "Retrying in {secs}s (attempt {attempt}/{max})",
                    secs = delay.as_secs(),
                    max = config.max_retries,
                );
                tokio::time::sleep(delay).await;
                delay *= 2;
                attempt += 1;
            }
        }
    }
}

/// Get pull request information from a commit SHA
///
/// This works for all merge strategies (merge commit, rebase, squash).
///
/// When `associatedPullRequests` returns an empty list (which can happen
/// transiently in the seconds immediately after a PR merge while GitHub's
/// internal index catches up), the function retries up to
/// [`DEFAULT_RETRY_ATTEMPTS`] times with exponential back-off starting at
/// [`DEFAULT_BASE_DELAY_SECS`] seconds (5 s → 10 s → 20 s).
///
/// Hard errors (network failures, authentication errors, repository not
/// found) are **not** retried.
pub(crate) async fn get_pull_request_by_commit(
    github_graphql: &gql_client::Client,
    owner: &str,
    name: &str,
    commit_sha: &str,
) -> Result<(i64, String, String, String), Error> {
    let query = r#"
            query($owner: String!, $name: String!, $oid: GitObjectID!) {
                repository(owner: $owner, name: $name) {
                    object(oid: $oid) {
                        ... on Commit {
                            associatedPullRequests(first: 5) {
                                nodes {
                                    number
                                    title
                                    body
                                    url
                                    mergedAt
                                }
                            }
                        }
                    }
                }
            }
            "#;

    let owner = owner.to_string();
    let name = name.to_string();
    let oid = commit_sha.to_string();
    let github_graphql = github_graphql.clone();
    let query = query.to_string();

    let query_fn = move || -> Pin<Box<dyn Future<Output = Result<Data, Error>> + Send>> {
        let vars = Vars {
            owner: owner.clone(),
            name: name.clone(),
            oid: oid.clone(),
        };
        let github_graphql = github_graphql.clone();
        let query = query.clone();
        Box::pin(async move {
            let data_res = github_graphql
                .query_with_vars_unwrap::<Data, Vars>(&query, vars)
                .await;

            log::trace!("data_res: {data_res:?}");
            let data = data_res.map_err(GraphQLWrapper::from)?;
            log::trace!("data: {data:?}");
            Ok(data)
        })
    };

    get_pull_request_by_commit_with_retry(query_fn, RetryConfig::default()).await
}

#[cfg(test)]
mod tests {
    use std::{
        future::Future,
        pin::Pin,
        sync::{Arc, Mutex},
        time::Duration,
    };

    use super::*;

    // ---------------------------------------------------------------------------
    // Helper: build a Data value with a specific list of PRs (or None for the
    // commit object being absent).
    // ---------------------------------------------------------------------------

    fn data_with_prs(prs: Vec<PullRequest>) -> Data {
        Data {
            repository: Repository {
                object: Some(Commit {
                    associated_pull_requests: AssociatedPullRequests { nodes: prs },
                }),
            },
        }
    }

    fn data_commit_not_found() -> Data {
        Data {
            repository: Repository { object: None },
        }
    }

    fn make_pr(number: i64) -> PullRequest {
        PullRequest {
            number,
            title: format!("PR #{number}"),
            body: format!("Body of PR #{number}"),
            url: format!("https://github.com/owner/repo/pull/{number}"),
            merged_at: Some("2024-01-01T00:00:00Z".to_string()),
        }
    }

    // ---------------------------------------------------------------------------
    // RED: retry-succeeds-on-second-attempt test
    //
    // Scenario: first call returns empty PR list (transient), second call
    // returns one PR.  The function should eventually return Ok with that PR.
    // ---------------------------------------------------------------------------
    #[tokio::test]
    async fn test_retry_succeeds_on_second_attempt() {
        // Shared call counter.
        let call_count = Arc::new(Mutex::new(0u32));

        let call_count_clone = Arc::clone(&call_count);

        // RetryConfig with zero sleep to keep tests fast.
        let config = RetryConfig {
            max_retries: 3,
            base_delay: Duration::from_millis(0),
        };

        let query_fn = move || -> Pin<Box<dyn Future<Output = Result<Data, Error>> + Send>> {
            let call_count = Arc::clone(&call_count_clone);
            Box::pin(async move {
                let mut count = call_count.lock().unwrap();
                *count += 1;
                if *count == 1 {
                    // First attempt: empty PR list — simulates GitHub indexing lag.
                    Ok(data_with_prs(vec![]))
                } else {
                    // Second attempt: PR is now indexed.
                    Ok(data_with_prs(vec![make_pr(42)]))
                }
            })
        };

        let result = get_pull_request_by_commit_with_retry(query_fn, config).await;

        assert!(result.is_ok(), "Expected Ok after retry, got: {result:?}");
        let (number, title, url, body) = result.unwrap();
        assert_eq!(number, 42);
        assert_eq!(title, "PR #42");
        assert_eq!(url, "https://github.com/owner/repo/pull/42");
        assert_eq!(body, "Body of PR #42");

        let final_count = *call_count.lock().unwrap();
        assert_eq!(
            final_count, 2,
            "Expected exactly 2 calls (initial + 1 retry)"
        );
    }

    // ---------------------------------------------------------------------------
    // RED: all-retries-exhausted test
    //
    // Scenario: all attempts return empty PR list.  The function should return
    // Err(InvalidMergeCommitMessage) after exhausting retries.
    // ---------------------------------------------------------------------------
    #[tokio::test]
    async fn test_all_retries_exhausted_returns_error() {
        let call_count = Arc::new(Mutex::new(0u32));
        let call_count_clone = Arc::clone(&call_count);

        let config = RetryConfig {
            max_retries: 3,
            base_delay: Duration::from_millis(0),
        };

        let query_fn = move || -> Pin<Box<dyn Future<Output = Result<Data, Error>> + Send>> {
            let call_count = Arc::clone(&call_count_clone);
            Box::pin(async move {
                let mut count = call_count.lock().unwrap();
                *count += 1;
                // Always return empty — simulates GitHub never indexing the PR.
                Ok(data_with_prs(vec![]))
            })
        };

        let result = get_pull_request_by_commit_with_retry(query_fn, config).await;

        assert!(
            result.is_err(),
            "Expected Err after retries exhausted, got: {result:?}"
        );
        assert!(
            matches!(result.unwrap_err(), Error::InvalidMergeCommitMessage),
            "Expected InvalidMergeCommitMessage"
        );

        // Called 4 times: initial attempt + 3 retries.
        let final_count = *call_count.lock().unwrap();
        assert_eq!(
            final_count, 4,
            "Expected 4 calls (initial + 3 retries), got {final_count}"
        );
    }

    // ---------------------------------------------------------------------------
    // RED: commit-not-found also triggers retry
    //
    // Scenario: first attempt returns `object: null` (commit not yet indexed),
    // second attempt returns the commit with a PR.
    // ---------------------------------------------------------------------------
    #[tokio::test]
    async fn test_commit_not_found_retries_and_succeeds() {
        let call_count = Arc::new(Mutex::new(0u32));
        let call_count_clone = Arc::clone(&call_count);

        let config = RetryConfig {
            max_retries: 3,
            base_delay: Duration::from_millis(0),
        };

        let query_fn = move || -> Pin<Box<dyn Future<Output = Result<Data, Error>> + Send>> {
            let call_count = Arc::clone(&call_count_clone);
            Box::pin(async move {
                let mut count = call_count.lock().unwrap();
                *count += 1;
                if *count == 1 {
                    Ok(data_commit_not_found())
                } else {
                    Ok(data_with_prs(vec![make_pr(7)]))
                }
            })
        };

        let result = get_pull_request_by_commit_with_retry(query_fn, config).await;

        assert!(result.is_ok(), "Expected Ok, got: {result:?}");
        let (number, ..) = result.unwrap();
        assert_eq!(number, 7);
    }

    // ---------------------------------------------------------------------------
    // RED: hard error is not retried
    //
    // Scenario: query returns a hard error (e.g. auth error).  The function
    // should propagate that error immediately without retrying.
    // ---------------------------------------------------------------------------
    #[tokio::test]
    async fn test_hard_error_is_not_retried() {
        let call_count = Arc::new(Mutex::new(0u32));
        let call_count_clone = Arc::clone(&call_count);

        let config = RetryConfig {
            max_retries: 3,
            base_delay: Duration::from_millis(0),
        };

        let query_fn = move || -> Pin<Box<dyn Future<Output = Result<Data, Error>> + Send>> {
            let call_count = Arc::clone(&call_count_clone);
            Box::pin(async move {
                let mut count = call_count.lock().unwrap();
                *count += 1;
                // Return a hard error every time.
                Err(Error::NoGitHubAPIAuth)
            })
        };

        let result = get_pull_request_by_commit_with_retry(query_fn, config).await;

        assert!(result.is_err(), "Expected Err, got: {result:?}");
        assert!(
            matches!(result.unwrap_err(), Error::NoGitHubAPIAuth),
            "Expected NoGitHubAPIAuth"
        );

        let final_count = *call_count.lock().unwrap();
        assert_eq!(
            final_count, 1,
            "Expected only 1 call — no retries for hard errors"
        );
    }

    // ---------------------------------------------------------------------------
    // Deserialization sanity test (mirrors style from get_tag.rs)
    // ---------------------------------------------------------------------------
    #[test]
    fn test_deserialize_response_with_pr() {
        let response = r#"{
            "repository": {
                "object": {
                    "associatedPullRequests": {
                        "nodes": [
                            {
                                "number": 99,
                                "title": "feat: add retry",
                                "body": "Retry logic",
                                "url": "https://github.com/owner/repo/pull/99",
                                "mergedAt": "2024-06-01T12:00:00Z"
                            }
                        ]
                    }
                }
            }
        }"#;

        let data: Data = serde_json::from_str(response).unwrap();
        let prs = &data
            .repository
            .object
            .unwrap()
            .associated_pull_requests
            .nodes;
        assert_eq!(prs.len(), 1);
        assert_eq!(prs[0].number, 99);
        assert_eq!(prs[0].title, "feat: add retry");
    }

    #[test]
    fn test_deserialize_response_empty_nodes() {
        let response = r#"{
            "repository": {
                "object": {
                    "associatedPullRequests": {
                        "nodes": []
                    }
                }
            }
        }"#;

        let data: Data = serde_json::from_str(response).unwrap();
        let prs = &data
            .repository
            .object
            .unwrap()
            .associated_pull_requests
            .nodes;
        assert!(prs.is_empty());
    }

    #[test]
    fn test_deserialize_response_commit_not_found() {
        let response = r#"{
            "repository": {
                "object": null
            }
        }"#;

        let data: Data = serde_json::from_str(response).unwrap();
        assert!(data.repository.object.is_none());
    }
}