please-release-please 0.1.0

PRP CLI: automate changelogs, versioning, and publishing
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
use crate::config::{HttpConfig, ResolvedGitHubConfig};
use crate::git::Git;
use crate::http::HttpClient;
use anyhow::{Context, Result, ensure};
use reqwest::header::{ACCEPT, CONTENT_LENGTH, CONTENT_TYPE};
use reqwest::{Method, RequestBuilder, Response, StatusCode, Url};
use serde::Deserialize;
use serde::de::DeserializeOwned;
use serde_json::json;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::time::Duration;

const PUBLIC_API_HOST: &str = "api.github.com";
const PUBLIC_UPLOAD_ORIGIN: &str = "https://uploads.github.com";
const API_VERSION: &str = "2022-11-28";
const PAGE_SIZE: usize = 100;
const UPLOAD_TIMEOUT: Duration = Duration::from_secs(600);

#[derive(Debug, Clone)]
pub struct PullRequest {
  pub base: String,
  pub head: String,
  pub title: String,
  pub body: String,
  pub draft: bool,
  pub labels: Vec<String>,
}

#[derive(Debug, Clone)]
pub struct Release {
  pub tag: String,
  pub title: String,
  pub body: String,
  pub prerelease: bool,
  pub assets: Vec<PathBuf>,
}

#[derive(Debug, Deserialize)]
struct PullResponse {
  number: u64,
  html_url: String,
}

#[derive(Debug, Deserialize)]
struct ReleaseResponse {
  id: u64,
  html_url: String,
  upload_url: String,
}

#[derive(Debug, Deserialize)]
struct AssetResponse {
  id: u64,
  name: String,
  size: u64,
  state: String,
}

/// The repository's GitHub API. Every write is safe to repeat, so running a
/// failed command again finishes the work.
#[derive(Debug)]
pub struct GitHub {
  http: HttpClient,
  api_url: Url,
  owner: String,
  repo: String,
  token: String,
}

impl GitHub {
  pub fn new(
    root: &Path,
    config: &ResolvedGitHubConfig,
    http_config: &HttpConfig,
    token: Option<&str>,
  ) -> Result<Self> {
    let token = token
      .filter(|token| !token.is_empty())
      .context("GitHub requires --github-token or GITHUB_TOKEN")?;
    let (owner, repo) = match (&config.owner, &config.repo) {
      (Some(owner), Some(repo)) => (owner.clone(), repo.clone()),
      _ => {
        let remote = Git::new(root)
          .run(&["remote", "get-url", "origin"])
          .context("Configure github.owner and github.repo, or add a GitHub origin")?;
        repository_from_remote(remote.trim(), &config.api_url)?
      }
    };
    let api_url = Url::parse(&config.api_url)
      .with_context(|| format!("Invalid GitHub API url: {}", config.api_url))?;
    Ok(Self {
      http: HttpClient::new(http_config)?,
      api_url,
      owner,
      repo,
      token: token.to_string(),
    })
  }

  fn url(&self, path: &[&str]) -> Result<Url> {
    let mut url = self.api_url.clone();
    url
      .path_segments_mut()
      .map_err(|_| anyhow::anyhow!("Invalid GitHub API url: {}", self.api_url))?
      .pop_if_empty()
      .extend(["repos", &self.owner, &self.repo])
      .extend(path);
    Ok(url)
  }

  fn request(&self, method: Method, url: Url) -> RequestBuilder {
    self
      .http
      .request(method, url)
      .bearer_auth(&self.token)
      .header(ACCEPT, "application/vnd.github+json")
      .header("X-GitHub-Api-Version", API_VERSION)
  }

  async fn send(&self, request: RequestBuilder) -> Result<Response> {
    let response = self.http.send(request.build()?).await?;
    Ok(response)
  }

  /// Sends a request that has to succeed, and reads its JSON response.
  async fn send_json<T: DeserializeOwned>(&self, request: RequestBuilder) -> Result<T> {
    let response = ensure_success(self.send(request).await?).await?;
    response.json().await.context("Invalid GitHub response")
  }

  /// Opens a pull request, or updates the one open for the same branch.
  pub async fn upsert_pull_request(&self, pr: &PullRequest) -> Result<String> {
    let pull = match self.find_open_pull_request(&pr.head).await? {
      Some(pull) => {
        let number = pull.number.to_string();
        let url = self.url(&["pulls", &number])?;
        let body = json!({ "title": pr.title, "body": pr.body });
        self
          .send_json(self.request(Method::PATCH, url).json(&body))
          .await
          .context("Failed to update the pull request")?
      }
      None => {
        let body = json!({
          "base": pr.base,
          "head": pr.head,
          "title": pr.title,
          "body": pr.body,
          "draft": pr.draft,
        });
        let request = self
          .request(Method::POST, self.url(&["pulls"])?)
          .json(&body);
        self
          .send_json::<PullResponse>(request)
          .await
          .context("Failed to create the pull request")?
      }
    };

    if !pr.labels.is_empty() {
      let number = pull.number.to_string();
      let url = self.url(&["issues", &number, "labels"])?;
      let body = json!({ "labels": pr.labels });
      let response = self
        .send(self.request(Method::POST, url).json(&body))
        .await?;
      ensure_success(response)
        .await
        .with_context(|| format!("Failed to label pull request #{number}"))?;
    }
    Ok(pull.html_url)
  }

  async fn find_open_pull_request(&self, head: &str) -> Result<Option<PullResponse>> {
    let mut url = self.url(&["pulls"])?;
    url
      .query_pairs_mut()
      .append_pair("state", "open")
      .append_pair("head", &format!("{}:{head}", self.owner));
    let pulls: Vec<PullResponse> = self
      .send_json(self.request(Method::GET, url))
      .await
      .context("Failed to list pull requests")?;
    Ok(pulls.into_iter().next())
  }

  /// Creates a release for an already pushed tag, or reuses the one a previous
  /// attempt left, and uploads the assets it is missing.
  pub async fn ensure_release(&self, release: &Release) -> Result<String> {
    let existing = self.find_release(&release.tag).await?;
    let reused = existing.is_some();
    let found = match existing {
      Some(found) => found,
      None => {
        let body = json!({
          "tag_name": release.tag,
          "name": release.title,
          "body": release.body,
          "prerelease": release.prerelease,
        });
        let request = self
          .request(Method::POST, self.url(&["releases"])?)
          .json(&body);
        self
          .send_json(request)
          .await
          .with_context(|| format!("Failed to create GitHub release {}", release.tag))?
      }
    };

    let uploaded = match reused {
      true => self.list_assets(found.id).await?,
      false => HashMap::new(),
    };
    for path in &release.assets {
      self
        .upload_asset(&found, &uploaded, path)
        .await
        .with_context(|| format!("Failed to upload {}", path.display()))?;
    }
    Ok(found.html_url)
  }

  async fn find_release(&self, tag: &str) -> Result<Option<ReleaseResponse>> {
    let url = self.url(&["releases", "tags", tag])?;
    let response = self.send(self.request(Method::GET, url)).await?;
    if response.status() == StatusCode::NOT_FOUND {
      return Ok(None);
    }
    let response = ensure_success(response)
      .await
      .with_context(|| format!("Failed to look up GitHub release {tag}"))?;
    let release = response.json().await.context("Invalid GitHub response")?;
    Ok(Some(release))
  }

  async fn list_assets(&self, release_id: u64) -> Result<HashMap<String, AssetResponse>> {
    let release_id = release_id.to_string();
    let mut assets = HashMap::new();
    for page in 1.. {
      let mut url = self.url(&["releases", &release_id, "assets"])?;
      url
        .query_pairs_mut()
        .append_pair("per_page", &PAGE_SIZE.to_string())
        .append_pair("page", &page.to_string());
      let batch: Vec<AssetResponse> = self
        .send_json(self.request(Method::GET, url))
        .await
        .context("Failed to list release assets")?;
      let is_last = batch.len() < PAGE_SIZE;
      assets.extend(batch.into_iter().map(|asset| (asset.name.clone(), asset)));
      if is_last {
        break;
      }
    }
    Ok(assets)
  }

  async fn upload_asset(
    &self,
    release: &ReleaseResponse,
    uploaded: &HashMap<String, AssetResponse>,
    path: &Path,
  ) -> Result<()> {
    let name = path
      .file_name()
      .and_then(|name| name.to_str())
      .context("Asset has no valid file name")?;
    let data = tokio::fs::read(path).await?;

    if let Some(previous) = uploaded.get(name) {
      if previous.state == "uploaded" && previous.size == data.len() as u64 {
        return Ok(());
      }
      // A broken upload has to go first, as names are unique within a release.
      let id = previous.id.to_string();
      let url = self.url(&["releases", "assets", &id])?;
      ensure_success(self.send(self.request(Method::DELETE, url)).await?).await?;
    }

    let template = release.upload_url.split('{').next().unwrap_or_default();
    let mut url = Url::parse(template).context("Invalid GitHub upload url")?;
    self.ensure_trusted_upload_url(&url)?;
    url.query_pairs_mut().append_pair("name", name);
    let request = self
      .request(Method::POST, url)
      .header(CONTENT_TYPE, "application/octet-stream")
      .header(CONTENT_LENGTH, data.len())
      .timeout(UPLOAD_TIMEOUT)
      .body(data);
    ensure_success(self.send(request).await?).await?;
    Ok(())
  }

  /// The upload url comes from a response and receives the token, so it must
  /// point at GitHub.
  fn ensure_trusted_upload_url(&self, url: &Url) -> Result<()> {
    let is_public_upload = self.api_url.host_str() == Some(PUBLIC_API_HOST)
      && url.origin() == Url::parse(PUBLIC_UPLOAD_ORIGIN)?.origin();
    ensure!(
      (is_public_upload || url.origin() == self.api_url.origin())
        && url.username().is_empty()
        && url.password().is_none(),
      "GitHub returned an unexpected upload host"
    );
    Ok(())
  }
}

async fn ensure_success(response: Response) -> Result<Response> {
  let status = response.status();
  if status.is_success() {
    return Ok(response);
  }
  let body = response.text().await.unwrap_or_default();
  anyhow::bail!("GitHub responded with {status}: {}", body.trim())
}

fn repository_from_remote(remote: &str, api_url: &str) -> Result<(String, String)> {
  let (host, path) = if remote.contains("://") {
    let url = Url::parse(remote).context("Invalid Git origin URL")?;
    (
      url
        .host_str()
        .context("Missing Git origin host")?
        .to_owned(),
      url.path().trim_matches('/').to_owned(),
    )
  } else {
    let (host, path) = remote
      .split_once(':')
      .context("Origin is not a GitHub URL; configure github.owner and github.repo")?;
    (
      host.rsplit('@').next().unwrap_or(host).to_owned(),
      path.trim_matches('/').to_owned(),
    )
  };
  let api = Url::parse(api_url).with_context(|| format!("Invalid GitHub API url: {api_url}"))?;
  let api_host = api.host_str().context("GitHub API url has no host")?;
  let expected_host = match api_host {
    PUBLIC_API_HOST => "github.com",
    host => host,
  };
  ensure!(
    host == expected_host,
    "Origin is not on {expected_host}; configure github.owner, github.repo and github.api_url"
  );
  let path = path.strip_suffix(".git").unwrap_or(&path);
  let (owner, repo) = path
    .split_once('/')
    .context("Origin must specify owner/repository")?;
  ensure!(
    !owner.is_empty() && !repo.is_empty() && !repo.contains('/'),
    "Invalid GitHub repository path"
  );
  Ok((owner.into(), repo.into()))
}

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

  fn github(api_url: &str) -> GitHub {
    let config = ResolvedGitHubConfig {
      api_url: api_url.to_string(),
      owner: Some("org".to_string()),
      repo: Some("repo".to_string()),
      skip_release: false,
      release_pr_branch_format: String::new(),
      release_pr_title_format: String::new(),
      release_pr_labels: vec![],
    };
    GitHub::new(
      Path::new("."),
      &config,
      &HttpConfig::default(),
      Some("token"),
    )
    .unwrap()
  }

  #[test]
  fn upload_urls_are_limited_to_github() {
    let public = github("https://api.github.com");
    let trusted = Url::parse("https://uploads.github.com/repos/org/repo/releases/1/assets");
    assert!(public.ensure_trusted_upload_url(&trusted.unwrap()).is_ok());
    for url in [
      "https://untrusted.example/assets",
      "http://uploads.github.com/assets",
      "https://uploads.github.com:8443/assets",
      "https://user:pass@uploads.github.com/assets",
    ] {
      let url = Url::parse(url).unwrap();
      assert!(public.ensure_trusted_upload_url(&url).is_err(), "{url}");
    }

    let enterprise = github("https://git.example.com/api/v3");
    let url = Url::parse("https://git.example.com/api/uploads/assets").unwrap();
    assert!(enterprise.ensure_trusted_upload_url(&url).is_ok());
  }

  #[test]
  fn repository_comes_from_https_and_ssh_remotes() {
    for remote in [
      "https://github.com/org/repo.git",
      "git@github.com:org/repo.git",
      "ssh://git@github.com/org/repo.git",
    ] {
      assert_eq!(
        repository_from_remote(remote, "https://api.github.com").unwrap(),
        ("org".to_string(), "repo".to_string())
      );
    }
    assert!(
      repository_from_remote("git@gitlab.com:org/repo.git", "https://api.github.com").is_err()
    );
    assert_eq!(
      repository_from_remote(
        "git@git.example.com:org/repo.git",
        "https://git.example.com/api/v3"
      )
      .unwrap(),
      ("org".to_string(), "repo".to_string())
    );
  }

  #[test]
  fn requires_a_token() {
    let config = ResolvedGitHubConfig {
      api_url: "https://api.github.com".to_string(),
      owner: Some("org".to_string()),
      repo: Some("repo".to_string()),
      skip_release: false,
      release_pr_branch_format: String::new(),
      release_pr_title_format: String::new(),
      release_pr_labels: vec![],
    };
    let error = GitHub::new(Path::new("."), &config, &HttpConfig::default(), None).unwrap_err();
    assert!(error.to_string().contains("GITHUB_TOKEN"), "{error}");
  }
}