posthog_cli/proguard/
upload.rs

1use std::path::PathBuf;
2
3use anyhow::{anyhow, Result};
4
5use crate::{
6    api::{self, releases::ReleaseBuilder, symbol_sets::SymbolSetUpload},
7    proguard::ProguardFile,
8    utils::git::get_git_info,
9};
10
11#[derive(clap::Args, Clone)]
12pub struct Args {
13    /// The location of the proguard mapping file to upload.
14    #[arg(short, long)]
15    pub path: PathBuf,
16
17    /// This is the identifier posthog will use to look up with mapping file, when it's processing your
18    /// stack traces. Must match with the identifier provided to the posthog SDK at runtime, for this build.
19    #[arg(short, long)]
20    pub map_id: String,
21
22    /// The project name associated with this build. Required to have the exceptions associated with
23    /// a specific release. We will try to auto-derive this from git information if not provided. Strongly recommended
24    /// to be set explicitly during release CD workflows
25    #[arg(long)]
26    pub project: Option<String>,
27
28    /// The version of the project - this can be a version number, semantic version, or a git commit hash. Required
29    /// to have the uploaded chunks associated with a specific release. We will try to auto-derive this from git information
30    /// if not provided.
31    #[arg(long)]
32    pub version: Option<String>,
33}
34
35pub fn upload(args: &Args) -> Result<()> {
36    let Args {
37        path,
38        map_id,
39        project,
40        version,
41    } = args;
42
43    let path = path
44        .canonicalize()
45        .map_err(|e| anyhow!("Path {} canonicalization failed: {}", path.display(), e))?;
46    let directory = path
47        .parent()
48        .ok_or_else(|| anyhow!("Could not get path parent"))?;
49
50    let mut release_builder = get_git_info(Some(directory.to_path_buf()))?
51        .map(ReleaseBuilder::init_from_git)
52        .unwrap_or_default();
53
54    if let Some(project) = project {
55        release_builder.with_project(project);
56    }
57    if let Some(version) = version {
58        release_builder.with_version(version);
59    }
60
61    let mut file = ProguardFile::new(&path, map_id.clone())?;
62
63    let release = release_builder
64        .can_create()
65        .then(|| release_builder.fetch_or_create())
66        .transpose()?;
67
68    file.release_id = release.map(|r| r.id.to_string());
69
70    let to_upload: SymbolSetUpload = file.try_into()?;
71
72    api::symbol_sets::upload(&[to_upload], 50)?;
73
74    Ok(())
75}