hubcaps icon indicating copy to clipboard operation
hubcaps copied to clipboard

File creation/update errors

Open pmcgrath opened this issue 4 years ago • 0 comments

🐛 Bug description

When using file creation and file update I have observed a failure due to the comment_count not being returned in the response, this is the error I get

Codec(Error("missing field `comment_count`", line: 1, column: 2082))

NOTE It does create and update files, it fails deserializing the reponse content form the API call, see the suggested solution at the bottom

🤔 Expected Behavior

Cater for the comment_count field being optional, I checked this GitHub API doc and it does not include it in the response

👟 Steps to reproduce

Some code to illustrate - apologies its a bit long but wanted to show both cases (create and update)

use chrono::Utc;
use hubcaps::{Credentials, Github};
use std::env;

#[tokio::main]
async fn main() -> Result<(), String> {
    let pat = env::var("GITHUB_PAT").expect("GITHUB_PAT env var expected");
    let owner = env::var("GITHUB_OWNER").expect("GITHUB_OWNER env var required");
    let repo = env::var("GITHUB_REPO").expect("GITHUB_REPO env var required");

    let credentials = Credentials::Token(pat);
    let github = Github::new("client/0.1.0", credentials).unwrap();

    let repository = github.repo(&owner, &repo);

    let now = Utc::now();
    let test_file_path = format!("/test-{}", now.format("%F-%H-%M-%S"));

    // CREATE FILE
    println!("About to create file [{}]", test_file_path);
    match repository
        .content()
        .create(
            &test_file_path,
            &format!("Creation content @ {}", now),
            "Updated via rust",
        )
        .await
    {
        Ok(r) => {
            println!(
                "Creation info {} {}",
                r.commit.tree.sha,
                r.commit.comment_count // .unwrap()
            );
        }
        Err(err) => {
            println!("Creation err: {}", err);
        }
    }

    println!(
        "\nAbout to get file [{}], so we have the sha for updating",
        test_file_path
    );
    let mut file_sha = String::new();
    match repository.content().file(&test_file_path).await {
        Ok(f) => file_sha = f.sha,
        Err(err) => {
            println!("File get err: {}", err);
        }
    }

    // UPDATE FILE
    println!(
        "\nAbout to update file [{}], existing sha is {}",
        test_file_path, file_sha
    );
    let now = Utc::now();
    match repository
        .content()
        .update(
            &test_file_path,
            &format!("Updated content @ {}", &now),
            "Updated via rust",
            &file_sha,
        )
        .await
    {
        Ok(r) => {
            println!("Update info {}", r.commit.tree.sha);
        }
        Err(err) => {
            println!("Update err: {}", err);
        }
    }

    println!("\nAbout to get file [{}]", test_file_path);
    match repository.content().file(&test_file_path).await {
        Ok(f) => file_sha = f.sha,
        Err(err) => {
            println!("File get err: {}", err);
        }
    }

    println!(
        "\nUpdated file [{}], sha is now {}",
        test_file_path, file_sha
    );

    // CREATE FILE - Let it crash with unwrap
    let now = Utc::now();
    let test_file_path = format!("/test-{}", now.format("%F-%H-%M-%S"));

    println!("About to create file [{}] - WILL CRASH", test_file_path);
    repository
        .content()
        .create(
            &test_file_path,
            &format!("Creation content @ {}", now),
            "Updated via rust",
        )
        .await
        .unwrap();

    Ok(())
}

Log with backtrace

About to create file [/test-2020-09-25-00-37-53]
Creation err: missing field `comment_count` at line 1 column 2082

About to get file [/test-2020-09-25-00-37-53], so we have the sha for updating

About to update file [/test-2020-09-25-00-37-53], existing sha is b25d3b2bca17ede736e5fa74b54b35f9078a6d3a
Update err: missing field `comment_count` at line 1 column 2082

About to get file [/test-2020-09-25-00-37-53]

Updated file [/test-2020-09-25-00-37-53], sha is now 296efcff4ec00488885fff3858e69e4c840cfdbb
About to create file [/test-2020-09-25-00-37-56] - WILL CRASH
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Codec(Error("missing field `comment_count`", line: 1, column: 2082))', src/main.rs:104:10
stack backtrace:
   0: backtrace::backtrace::libunwind::trace
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.46/src/backtrace/libunwind.rs:86
   1: backtrace::backtrace::trace_unsynchronized
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.46/src/backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print_fmt
             at src/libstd/sys_common/backtrace.rs:78
   3: <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt
             at src/libstd/sys_common/backtrace.rs:59
   4: core::fmt::write
             at src/libcore/fmt/mod.rs:1076
   5: std::io::Write::write_fmt
             at src/libstd/io/mod.rs:1537
   6: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:62
   7: std::sys_common::backtrace::print
             at src/libstd/sys_common/backtrace.rs:49
   8: std::panicking::default_hook::{{closure}}
             at src/libstd/panicking.rs:198
   9: std::panicking::default_hook
             at src/libstd/panicking.rs:217
  10: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:526
  11: rust_begin_unwind
             at src/libstd/panicking.rs:437
  12: core::panicking::panic_fmt
             at src/libcore/panicking.rs:85
  13: core::option::expect_none_failed
             at src/libcore/option.rs:1269
  14: core::result::Result<T,E>::unwrap
             at /home/pmcgrath/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libcore/result.rs:1005
  15: hubcaps_issue::main::{{closure}}
             at src/main.rs:96
  16: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /home/pmcgrath/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libcore/future/mod.rs:78
  17: tokio::runtime::enter::Enter::block_on::{{closure}}
             at /home/pmcgrath/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.22/src/runtime/enter.rs:160
  18: tokio::coop::with_budget::{{closure}}
             at /home/pmcgrath/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.22/src/coop.rs:127
  19: std::thread::local::LocalKey<T>::try_with
             at /home/pmcgrath/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/thread/local.rs:263
  20: std::thread::local::LocalKey<T>::with
             at /home/pmcgrath/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/thread/local.rs:239
  21: tokio::coop::with_budget
             at /home/pmcgrath/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.22/src/coop.rs:120
  22: tokio::coop::budget
             at /home/pmcgrath/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.22/src/coop.rs:96
  23: tokio::runtime::enter::Enter::block_on
             at /home/pmcgrath/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.22/src/runtime/enter.rs:160
  24: tokio::runtime::thread_pool::ThreadPool::block_on
             at /home/pmcgrath/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.22/src/runtime/thread_pool/mod.rs:82
  25: tokio::runtime::Runtime::block_on::{{closure}}
             at /home/pmcgrath/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.22/src/runtime/mod.rs:446
  26: tokio::runtime::context::enter
             at /home/pmcgrath/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.22/src/runtime/context.rs:72
  27: tokio::runtime::handle::Handle::enter
             at /home/pmcgrath/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.22/src/runtime/handle.rs:76
  28: tokio::runtime::Runtime::block_on
             at /home/pmcgrath/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.22/src/runtime/mod.rs:441
  29: hubcaps_issue::main
             at src/main.rs:5
  30: std::rt::lang_start::{{closure}}
             at /home/pmcgrath/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/rt.rs:67
  31: std::rt::lang_start_internal::{{closure}}
             at src/libstd/rt.rs:52
  32: std::panicking::try::do_call
             at src/libstd/panicking.rs:348
  33: std::panicking::try
             at src/libstd/panicking.rs:325
  34: std::panic::catch_unwind
             at src/libstd/panic.rs:394
  35: std::rt::lang_start_internal
             at src/libstd/rt.rs:51
  36: std::rt::lang_start
             at /home/pmcgrath/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/rt.rs:67
  37: main
  38: __libc_start_main
  39: _start
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

🌍 Your environment

os version: Ubuntu 20.04.1 LTS cargo version: cargo 1.46.0 (149022b1d 2020-07-17) hubcaps version: 0.6.2

Possible fix

Can see these are the 2 locations in the source that have this field

I have altered a local copy making this optional and this has fixed the issue for me

Leave me know if this seems reasonable and if you want me to create a PR

pmcgrath avatar Sep 25 '20 00:09 pmcgrath