communique 1.2.4

Editorialized release notes powered by AI
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
use serde::{Deserialize, Serialize};

use crate::error::{Error, Result};

pub struct GitHubClient {
    client: reqwest::Client,
    token: String,
    owner: String,
    repo: String,
    base_url: String,
}

#[derive(Debug, Deserialize)]
pub struct Release {
    pub id: u64,
    #[allow(dead_code)]
    pub tag_name: String,
    #[allow(dead_code)]
    pub name: Option<String>,
    pub body: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct PullRequest {
    pub number: u64,
    pub title: String,
    pub body: Option<String>,
    pub user: User,
    pub labels: Vec<Label>,
}

#[derive(Debug, Deserialize)]
pub struct Issue {
    pub number: u64,
    pub title: String,
    pub body: Option<String>,
    pub state: String,
    pub user: User,
    pub labels: Vec<Label>,
}

#[derive(Debug, Deserialize)]
pub struct User {
    pub login: String,
}

#[derive(Debug, Deserialize)]
pub struct Label {
    pub name: String,
}

#[derive(Debug, Serialize)]
struct UpdateRelease {
    /// Always sent, never skipped.
    ///
    /// GitHub regenerates a *draft* release's `tag_name` as `untagged-<hash>`
    /// when a PATCH omits it. The draft then no longer answers to its tag, and
    /// anything downstream that addresses it by tag — `gh release upload`, for
    /// one — fails with "release not found". Re-sending the tag we already know
    /// keeps the association intact and is a no-op for published releases.
    tag_name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    body: Option<String>,
}

impl GitHubClient {
    pub fn new(token: String, owner_repo: &str) -> Result<Self> {
        Self::with_base_url(token, owner_repo, "https://api.github.com".into())
    }

    pub(crate) fn with_base_url(token: String, owner_repo: &str, base_url: String) -> Result<Self> {
        let (owner, repo) = owner_repo
            .split_once('/')
            .ok_or_else(|| Error::GitHub(format!("invalid owner/repo: {owner_repo}")))?;
        let client = reqwest::Client::builder()
            .user_agent("communique/0.1")
            .build()?;
        Ok(Self {
            client,
            token,
            owner: owner.to_string(),
            repo: repo.to_string(),
            base_url,
        })
    }

    fn api_url(&self, path: &str) -> String {
        format!("{}/repos/{}/{}{path}", self.base_url, self.owner, self.repo)
    }

    pub async fn get_release_by_tag(&self, tag: &str) -> Result<Option<Release>> {
        // Try the direct endpoint first (works for published releases)
        let url = self.api_url(&format!("/releases/tags/{tag}"));
        let resp = crate::retry::retry_request("GitHub API", || {
            self.client
                .get(&url)
                .bearer_auth(&self.token)
                .header("Accept", "application/vnd.github+json")
                .send()
        })
        .await?;
        if resp.status().is_success() {
            return Ok(Some(resp.json().await?));
        }
        if resp.status() != reqwest::StatusCode::NOT_FOUND {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(Error::GitHub(format!("GET release {tag}: {status} {body}")));
        }

        // Fall back to listing releases — the /releases/tags endpoint
        // doesn't return draft releases, but the list endpoint does.
        let releases = self.list_recent_releases(10).await?;
        Ok(releases.into_iter().find(|r| r.tag_name == tag))
    }

    /// Update a release's title and body, preserving its tag.
    ///
    /// `tag` is required rather than optional because omitting it is precisely
    /// the bug this signature exists to prevent — see [`UpdateRelease::tag_name`].
    pub async fn update_release(
        &self,
        release_id: u64,
        tag: &str,
        title: Option<&str>,
        body: Option<&str>,
    ) -> Result<()> {
        let url = self.api_url(&format!("/releases/{release_id}"));
        let payload = UpdateRelease {
            tag_name: tag.to_string(),
            name: title.map(String::from),
            body: body.map(String::from),
        };
        let resp = crate::retry::retry_request("GitHub API", || {
            self.client
                .patch(&url)
                .bearer_auth(&self.token)
                .header("Accept", "application/vnd.github+json")
                .json(&payload)
                .send()
        })
        .await?;
        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(Error::GitHub(format!(
                "PATCH release {release_id}: {status} {body}"
            )));
        }
        Ok(())
    }

    pub async fn list_recent_releases(&self, count: u8) -> Result<Vec<Release>> {
        let url = self.api_url(&format!("/releases?per_page={count}"));
        let resp = crate::retry::retry_request("GitHub API", || {
            self.client
                .get(&url)
                .bearer_auth(&self.token)
                .header("Accept", "application/vnd.github+json")
                .send()
        })
        .await?;
        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(Error::GitHub(format!("GET releases: {status} {body}")));
        }
        Ok(resp.json().await?)
    }

    pub async fn get_issue(&self, number: u64) -> Result<Issue> {
        let url = self.api_url(&format!("/issues/{number}"));
        let resp = crate::retry::retry_request("GitHub API", || {
            self.client
                .get(&url)
                .bearer_auth(&self.token)
                .header("Accept", "application/vnd.github+json")
                .send()
        })
        .await?;
        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(Error::GitHub(format!(
                "GET issue #{number}: {status} {body}"
            )));
        }
        Ok(resp.json().await?)
    }

    pub async fn get_pr(&self, number: u64) -> Result<PullRequest> {
        let url = self.api_url(&format!("/pulls/{number}"));
        let resp = crate::retry::retry_request("GitHub API", || {
            self.client
                .get(&url)
                .bearer_auth(&self.token)
                .header("Accept", "application/vnd.github+json")
                .send()
        })
        .await?;
        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(Error::GitHub(format!("GET PR #{number}: {status} {body}")));
        }
        Ok(resp.json().await?)
    }

    pub async fn get_pr_diff(&self, number: u64) -> Result<String> {
        let url = self.api_url(&format!("/pulls/{number}"));
        let resp = crate::retry::retry_request("GitHub API", || {
            self.client
                .get(&url)
                .bearer_auth(&self.token)
                .header("Accept", "application/vnd.github.v3.diff")
                .send()
        })
        .await?;
        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(Error::GitHub(format!(
                "GET PR #{number} diff: {status} {body}"
            )));
        }
        let diff = resp.text().await?;
        // Truncate very large diffs to avoid blowing up context
        if diff.len() > 50_000 {
            Ok(format!(
                "{}...\n\n[diff truncated at 50KB]",
                &diff[..50_000]
            ))
        } else {
            Ok(diff)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use wiremock::matchers::{body_partial_json, method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    async fn setup() -> (MockServer, GitHubClient) {
        let server = MockServer::start().await;
        let client =
            GitHubClient::with_base_url("test-token".into(), "owner/repo", server.uri()).unwrap();
        (server, client)
    }

    #[test]
    fn test_new_invalid_owner_repo() {
        let result = GitHubClient::new("token".into(), "invalid");
        assert!(result.is_err());
        assert!(
            result
                .err()
                .unwrap()
                .to_string()
                .contains("invalid owner/repo")
        );
    }

    #[tokio::test]
    async fn test_get_release_by_tag() {
        let (server, client) = setup().await;
        Mock::given(method("GET"))
            .and(path("/repos/owner/repo/releases/tags/v1.0.0"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "id": 123,
                "tag_name": "v1.0.0",
                "name": "Version 1.0.0",
                "body": "Release notes"
            })))
            .mount(&server)
            .await;

        let release = client.get_release_by_tag("v1.0.0").await.unwrap().unwrap();
        assert_eq!(release.id, 123);
        assert_eq!(release.body.as_deref(), Some("Release notes"));
    }

    #[tokio::test]
    async fn test_get_release_by_tag_not_found() {
        let (server, client) = setup().await;
        Mock::given(method("GET"))
            .and(path("/repos/owner/repo/releases/tags/v9.9.9"))
            .respond_with(ResponseTemplate::new(404))
            .mount(&server)
            .await;
        // Fallback list endpoint also returns nothing matching
        Mock::given(method("GET"))
            .and(path("/repos/owner/repo/releases"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!([])))
            .mount(&server)
            .await;

        let result = client.get_release_by_tag("v9.9.9").await.unwrap();
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn test_update_release() {
        let (server, client) = setup().await;
        Mock::given(method("PATCH"))
            .and(path("/repos/owner/repo/releases/123"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({"id": 123})))
            .mount(&server)
            .await;

        client
            .update_release(123, "v1.0.0", Some("Title"), Some("Body"))
            .await
            .unwrap();
    }

    /// The PATCH must carry `tag_name`.
    ///
    /// GitHub regenerates a draft release's tag as `untagged-<hash>` when a
    /// PATCH omits it, and the draft stops answering to its own tag. That broke
    /// a real release: `gh release upload v0.0.3` failed with "release not
    /// found" after this call had rewritten the notes, leaving seven built
    /// binaries with nowhere to go.
    #[tokio::test]
    async fn update_release_preserves_the_tag() {
        let (server, client) = setup().await;
        Mock::given(method("PATCH"))
            .and(path("/repos/owner/repo/releases/42"))
            .and(body_partial_json(json!({"tag_name": "v0.0.3"})))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({"id": 42})))
            .expect(1)
            .mount(&server)
            .await;

        client
            .update_release(42, "v0.0.3", Some("Title"), Some("Body"))
            .await
            .unwrap();
    }

    #[tokio::test]
    async fn test_list_recent_releases() {
        let (server, client) = setup().await;
        Mock::given(method("GET"))
            .and(path("/repos/owner/repo/releases"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!([
                {"id": 1, "tag_name": "v2.0.0", "name": "v2", "body": "Notes 2"},
                {"id": 2, "tag_name": "v1.0.0", "name": "v1", "body": "Notes 1"},
            ])))
            .mount(&server)
            .await;

        let releases = client.list_recent_releases(3).await.unwrap();
        assert_eq!(releases.len(), 2);
        assert_eq!(releases[0].tag_name, "v2.0.0");
    }

    #[tokio::test]
    async fn test_get_pr() {
        let (server, client) = setup().await;
        Mock::given(method("GET"))
            .and(path("/repos/owner/repo/pulls/42"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "number": 42,
                "title": "Add feature",
                "body": "Description",
                "user": {"login": "testuser"},
                "labels": [{"name": "enhancement"}]
            })))
            .mount(&server)
            .await;

        let pr = client.get_pr(42).await.unwrap();
        assert_eq!(pr.number, 42);
        assert_eq!(pr.title, "Add feature");
        assert_eq!(pr.user.login, "testuser");
        assert_eq!(pr.labels[0].name, "enhancement");
    }

    #[tokio::test]
    async fn test_get_pr_diff() {
        let (server, client) = setup().await;
        Mock::given(method("GET"))
            .and(path("/repos/owner/repo/pulls/42"))
            .respond_with(ResponseTemplate::new(200).set_body_string("diff --git a/file.rs"))
            .mount(&server)
            .await;

        let diff = client.get_pr_diff(42).await.unwrap();
        assert!(diff.contains("diff --git"));
    }

    #[tokio::test]
    async fn test_get_pr_diff_truncation() {
        let (server, client) = setup().await;
        let large_diff = "x".repeat(100_000);
        Mock::given(method("GET"))
            .and(path("/repos/owner/repo/pulls/42"))
            .respond_with(ResponseTemplate::new(200).set_body_string(&large_diff))
            .mount(&server)
            .await;

        let diff = client.get_pr_diff(42).await.unwrap();
        assert!(diff.contains("[diff truncated at 50KB]"));
        assert!(diff.len() < 100_000);
    }

    #[tokio::test]
    async fn test_get_issue() {
        let (server, client) = setup().await;
        Mock::given(method("GET"))
            .and(path("/repos/owner/repo/issues/7"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "number": 7,
                "title": "Bug report",
                "body": "Something is broken",
                "state": "open",
                "user": {"login": "reporter"},
                "labels": [{"name": "bug"}]
            })))
            .mount(&server)
            .await;

        let issue = client.get_issue(7).await.unwrap();
        assert_eq!(issue.number, 7);
        assert_eq!(issue.title, "Bug report");
        assert_eq!(issue.state, "open");
        assert_eq!(issue.user.login, "reporter");
        assert_eq!(issue.labels[0].name, "bug");
    }
}