paceflow 0.2.4

Local-first CLI that turns AI coding session history and git metadata into engineering analytics.
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
use anyhow::{Context, Result};
use reqwest::Url;
use reqwest::header::{ACCEPT, AUTHORIZATION, HeaderMap, HeaderValue, USER_AGENT};
use serde::{Deserialize, de::DeserializeOwned};

use crate::github::types::{
    GitHubRepo, IssueRecord, IssueTimelineEvent, PullRequestFileRecord, PullRequestRecord,
};

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum GitHubApiErrorKind {
    RateLimited,
    Other,
}

#[derive(Debug, Clone)]
pub struct GitHubApiError {
    pub kind: GitHubApiErrorKind,
    pub message: String,
}

impl std::fmt::Display for GitHubApiError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.message)
    }
}

impl std::error::Error for GitHubApiError {}

pub trait GitHubApi {
    fn fetch_commit_pulls(
        &self,
        repo: &GitHubRepo,
        commit_sha: &str,
    ) -> impl std::future::Future<
        Output = std::result::Result<Vec<PullRequestRecord>, GitHubApiError>,
    > + Send;

    fn fetch_pull_request(
        &self,
        repo: &GitHubRepo,
        pr_number: i64,
    ) -> impl std::future::Future<Output = std::result::Result<PullRequestRecord, GitHubApiError>> + Send;

    fn fetch_closed_issues(
        &self,
        repo: &GitHubRepo,
        since: Option<&str>,
    ) -> impl std::future::Future<Output = std::result::Result<Vec<IssueRecord>, GitHubApiError>> + Send;

    fn fetch_issue_timeline(
        &self,
        repo: &GitHubRepo,
        issue_number: i64,
    ) -> impl std::future::Future<
        Output = std::result::Result<Vec<IssueTimelineEvent>, GitHubApiError>,
    > + Send;

    fn fetch_pull_request_files(
        &self,
        repo: &GitHubRepo,
        pr_number: i64,
    ) -> impl std::future::Future<
        Output = std::result::Result<Vec<PullRequestFileRecord>, GitHubApiError>,
    > + Send;
}

#[derive(Clone)]
pub struct ReqwestGitHubApi {
    client: reqwest::Client,
    base_url: String,
}

impl ReqwestGitHubApi {
    const PAGE_SIZE: usize = 100;

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

    pub fn with_base_url(token: &str, base_url: &str) -> Result<Self> {
        let mut headers = HeaderMap::new();
        headers.insert(
            ACCEPT,
            HeaderValue::from_static("application/vnd.github+json"),
        );
        headers.insert(USER_AGENT, HeaderValue::from_static("paceflow/0.1.0"));
        headers.insert(
            AUTHORIZATION,
            HeaderValue::from_str(&format!("Bearer {}", token.trim()))
                .context("invalid GitHub token header")?,
        );
        headers.insert(
            "X-GitHub-Api-Version",
            HeaderValue::from_static("2022-11-28"),
        );

        let client = reqwest::Client::builder()
            .default_headers(headers)
            .build()
            .context("build GitHub HTTP client")?;

        Ok(Self {
            client,
            base_url: base_url.trim_end_matches('/').to_string(),
        })
    }

    fn build_url(&self, path: &str) -> std::result::Result<Url, GitHubApiError> {
        Url::parse(&format!(
            "{}/{}",
            self.base_url,
            path.trim_start_matches('/')
        ))
        .map_err(|err| GitHubApiError {
            kind: GitHubApiErrorKind::Other,
            message: err.to_string(),
        })
    }

    async fn fetch_json<T>(&self, url: Url) -> std::result::Result<T, GitHubApiError>
    where
        T: DeserializeOwned,
    {
        let response = self
            .client
            .get(url)
            .send()
            .await
            .map_err(|err| GitHubApiError {
                kind: GitHubApiErrorKind::Other,
                message: err.to_string(),
            })?;

        if response.status().is_success() {
            return response.json::<T>().await.map_err(|err| GitHubApiError {
                kind: GitHubApiErrorKind::Other,
                message: err.to_string(),
            });
        }

        let status = response.status();
        let remaining = response
            .headers()
            .get("x-ratelimit-remaining")
            .and_then(|value| value.to_str().ok())
            .map(ToOwned::to_owned);
        let body = response.text().await.unwrap_or_else(|_| String::new());
        let message = if body.trim().is_empty() {
            format!("GitHub API request failed with {}", status)
        } else {
            format!("GitHub API request failed with {}: {}", status, body.trim())
        };

        let kind = if status.as_u16() == 429
            || (status.as_u16() == 403 && remaining.as_deref() == Some("0"))
        {
            GitHubApiErrorKind::RateLimited
        } else {
            GitHubApiErrorKind::Other
        };

        Err(GitHubApiError { kind, message })
    }

    async fn fetch_paginated_json<T, F>(
        &self,
        mut build_page_url: F,
    ) -> std::result::Result<Vec<T>, GitHubApiError>
    where
        T: DeserializeOwned,
        F: FnMut(usize) -> std::result::Result<Url, GitHubApiError>,
    {
        let mut out = Vec::new();
        let mut page = 1usize;
        loop {
            let batch = self.fetch_json::<Vec<T>>(build_page_url(page)?).await?;
            let batch_len = batch.len();
            out.extend(batch);
            if batch_len < Self::PAGE_SIZE {
                break;
            }
            page += 1;
        }
        Ok(out)
    }
}

impl GitHubApi for ReqwestGitHubApi {
    async fn fetch_commit_pulls(
        &self,
        repo: &GitHubRepo,
        commit_sha: &str,
    ) -> std::result::Result<Vec<PullRequestRecord>, GitHubApiError> {
        let url = self.build_url(&format!(
            "/repos/{}/{}/commits/{}/pulls",
            repo.owner, repo.name, commit_sha
        ))?;
        let pulls = self.fetch_json::<Vec<PullRequestResponse>>(url).await?;
        Ok(pulls.into_iter().map(Into::into).collect())
    }

    async fn fetch_pull_request(
        &self,
        repo: &GitHubRepo,
        pr_number: i64,
    ) -> std::result::Result<PullRequestRecord, GitHubApiError> {
        let url = self.build_url(&format!(
            "/repos/{}/{}/pulls/{}",
            repo.owner, repo.name, pr_number
        ))?;
        let pull = self.fetch_json::<PullRequestResponse>(url).await?;
        Ok(pull.into())
    }

    async fn fetch_closed_issues(
        &self,
        repo: &GitHubRepo,
        since: Option<&str>,
    ) -> std::result::Result<Vec<IssueRecord>, GitHubApiError> {
        let issues = self
            .fetch_paginated_json::<IssueResponse, _>(|page| {
                let mut url =
                    self.build_url(&format!("/repos/{}/{}/issues", repo.owner, repo.name))?;
                {
                    let mut query = url.query_pairs_mut();
                    query.append_pair("state", "closed");
                    query.append_pair("per_page", &Self::PAGE_SIZE.to_string());
                    query.append_pair("page", &page.to_string());
                    if let Some(since) = since {
                        query.append_pair("since", since);
                    }
                }
                Ok(url)
            })
            .await?;
        Ok(issues.into_iter().map(Into::into).collect())
    }

    async fn fetch_issue_timeline(
        &self,
        repo: &GitHubRepo,
        issue_number: i64,
    ) -> std::result::Result<Vec<IssueTimelineEvent>, GitHubApiError> {
        let events = self
            .fetch_paginated_json::<IssueTimelineEventResponse, _>(|page| {
                let mut url = self.build_url(&format!(
                    "/repos/{}/{}/issues/{}/timeline",
                    repo.owner, repo.name, issue_number
                ))?;
                {
                    let mut query = url.query_pairs_mut();
                    query.append_pair("per_page", &Self::PAGE_SIZE.to_string());
                    query.append_pair("page", &page.to_string());
                }
                Ok(url)
            })
            .await?;
        Ok(events.into_iter().map(Into::into).collect())
    }

    async fn fetch_pull_request_files(
        &self,
        repo: &GitHubRepo,
        pr_number: i64,
    ) -> std::result::Result<Vec<PullRequestFileRecord>, GitHubApiError> {
        let files = self
            .fetch_paginated_json::<PullRequestFileResponse, _>(|page| {
                let mut url = self.build_url(&format!(
                    "/repos/{}/{}/pulls/{}/files",
                    repo.owner, repo.name, pr_number
                ))?;
                {
                    let mut query = url.query_pairs_mut();
                    query.append_pair("per_page", &Self::PAGE_SIZE.to_string());
                    query.append_pair("page", &page.to_string());
                }
                Ok(url)
            })
            .await?;
        Ok(files.into_iter().map(Into::into).collect())
    }
}

#[derive(Debug, Deserialize)]
struct PullRequestResponse {
    number: i64,
    state: String,
    draft: Option<bool>,
    created_at: Option<String>,
    updated_at: Option<String>,
    closed_at: Option<String>,
    merged_at: Option<String>,
    html_url: Option<String>,
    base: PullRequestBranchRef,
    head: PullRequestBranchRef,
}

#[derive(Debug, Deserialize)]
struct PullRequestBranchRef {
    #[serde(rename = "ref")]
    git_ref: Option<String>,
}

impl From<PullRequestResponse> for PullRequestRecord {
    fn from(value: PullRequestResponse) -> Self {
        Self {
            number: value.number,
            state: value.state,
            draft: value.draft.unwrap_or(false),
            created_at: value.created_at,
            updated_at: value.updated_at,
            closed_at: value.closed_at,
            merged_at: value.merged_at,
            base_ref: value.base.git_ref,
            head_ref: value.head.git_ref,
            html_url: value.html_url,
        }
    }
}

#[derive(Debug, Deserialize)]
struct IssueResponse {
    number: i64,
    state: String,
    created_at: Option<String>,
    updated_at: Option<String>,
    closed_at: Option<String>,
    pull_request: Option<serde_json::Value>,
    labels: Vec<IssueLabelResponse>,
}

#[derive(Debug, Deserialize)]
struct IssueLabelResponse {
    name: Option<String>,
}

impl From<IssueResponse> for IssueRecord {
    fn from(value: IssueResponse) -> Self {
        Self {
            number: value.number,
            state: value.state,
            created_at: value.created_at,
            updated_at: value.updated_at,
            closed_at: value.closed_at,
            is_pull_request: value.pull_request.is_some(),
            label_names: value
                .labels
                .into_iter()
                .filter_map(|label| label.name)
                .collect(),
        }
    }
}

#[derive(Debug, Deserialize)]
struct IssueTimelineEventResponse {
    event: String,
    created_at: Option<String>,
    source: Option<IssueTimelineSourceResponse>,
}

#[derive(Debug, Deserialize)]
struct IssueTimelineSourceResponse {
    issue: Option<IssueTimelineSourceIssueResponse>,
}

#[derive(Debug, Deserialize)]
struct IssueTimelineSourceIssueResponse {
    number: i64,
    pull_request: Option<serde_json::Value>,
}

impl From<IssueTimelineEventResponse> for IssueTimelineEvent {
    fn from(value: IssueTimelineEventResponse) -> Self {
        let source_pr_number = value
            .source
            .and_then(|source| source.issue)
            .and_then(|issue| issue.pull_request.map(|_| issue.number));
        Self {
            event: value.event,
            created_at: value.created_at,
            source_pr_number,
        }
    }
}

#[derive(Debug, Deserialize)]
struct PullRequestFileResponse {
    filename: String,
    previous_filename: Option<String>,
    status: String,
    patch: Option<String>,
}

impl From<PullRequestFileResponse> for PullRequestFileRecord {
    fn from(value: PullRequestFileResponse) -> Self {
        Self {
            filename: value.filename,
            previous_filename: value.previous_filename,
            status: value.status,
            patch: value.patch,
        }
    }
}

pub fn github_token_from_env() -> Option<String> {
    std::env::var("PACEFLOW_GITHUB_TOKEN")
        .ok()
        .map(|value| value.trim().to_string())
        .filter(|value| !value.is_empty())
}