forked from diem/move
-
Notifications
You must be signed in to change notification settings - Fork 0
Add command to upload metadata to Movey #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ea-open-source
wants to merge
7
commits into
main
Choose a base branch
from
upload-command
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a1f6664
upload metadata to movey
ea-open-source 594f813
Add api token to upload request, get token from credential file
ea-open-source d204684
Show error message if upload with bad credentials
ea-open-source b9003cd
Refactor move_cli's utils & add cli tests
ea-open-source 00fb5b2
Merge pull request #4 from ea-open-source/upload-with-movey-api-token
ea-open-source ed52cb6
Remove `description, total_size` field from request body, change move…
ea-open-source d169f4d
Change movey domain of release build
ea-open-source File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
| use std::{ | ||
| collections::HashMap, | ||
| fmt, | ||
| fs::{create_dir_all, read_to_string}, | ||
| fs::{self, create_dir_all, read_to_string}, | ||
| io::Write, | ||
| path::{Path, PathBuf}, | ||
| process::ExitStatus, | ||
|
|
@@ -16,6 +16,7 @@ use std::os::windows::process::ExitStatusExt; | |
| // if unix | ||
| #[cfg(any(target_family = "unix"))] | ||
| use std::os::unix::prelude::ExitStatusExt; | ||
| use std::process::Command; | ||
| // if not windows nor unix | ||
| #[cfg(not(any(target_family = "windows", target_family = "unix")))] | ||
| compile_error!("Unsupported OS, currently we only support windows and unix family"); | ||
|
|
@@ -45,7 +46,9 @@ use move_package::{ | |
| }; | ||
| use move_unit_test::UnitTestingConfig; | ||
|
|
||
| use crate::utils::credential; | ||
| use crate::{package::prover::run_move_prover, NativeFunctionRecord}; | ||
| use reqwest::blocking::Client; | ||
|
|
||
| #[derive(Parser)] | ||
| pub enum CoverageSummaryOptions { | ||
|
|
@@ -88,6 +91,7 @@ pub enum PackageCommand { | |
| /// Print address information. | ||
| #[clap(name = "info")] | ||
| Info, | ||
| Upload, | ||
| /// Generate error map for the package and its dependencies at `path` for use by the Move | ||
| /// explanation tool. | ||
| #[clap(name = "errmap")] | ||
|
|
@@ -214,6 +218,14 @@ impl From<UnitTestResult> for ExitStatus { | |
| } | ||
| } | ||
|
|
||
| #[derive(serde::Serialize, Default)] | ||
| pub struct UploadRequest { | ||
| github_repo_url: String, | ||
| rev: String, | ||
| total_files: usize, | ||
| token: String, | ||
| } | ||
|
|
||
| impl CoverageSummaryOptions { | ||
| pub fn handle_command(&self, config: move_package::BuildConfig, path: &Path) -> Result<()> { | ||
| let coverage_map = CoverageMap::from_binary_file(path.join(".coverage_map.mvcov"))?; | ||
|
|
@@ -315,6 +327,89 @@ pub fn handle_package_commands( | |
| .resolution_graph_for_package(&rerooted_path)? | ||
| .print_info()?; | ||
| } | ||
| PackageCommand::Upload => { | ||
| let mut upload_request: UploadRequest = Default::default(); | ||
| let mut output = Command::new("git") | ||
| .current_dir(".") | ||
| .args(&["remote", "-v"]) | ||
| .output() | ||
| .unwrap(); | ||
| if !output.status.success() || output.stdout.len() == 0 { | ||
| bail!("invalid git repository") | ||
| } | ||
|
|
||
| let lines = String::from_utf8_lossy(output.stdout.as_slice()); | ||
| let lines = lines.split("\n"); | ||
| for line in lines { | ||
| if line.contains("github.com") { | ||
| let tokens: Vec<&str> = line.split(&['\t', ' '][..]).collect(); | ||
| if tokens.len() != 3 { | ||
| bail!("invalid remote url") | ||
| } | ||
| // convert ssh url to https | ||
| if tokens[1].starts_with("[email protected]") { | ||
| let https_url = tokens[1] | ||
| .replace(":", "/") | ||
| .replace("git@", "https://") | ||
| .replace(".git", ""); | ||
| upload_request.github_repo_url = https_url; | ||
| break; | ||
| } | ||
| upload_request.github_repo_url = String::from(tokens[1]); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| output = Command::new("git") | ||
| .current_dir(".") | ||
| .args(&["rev-parse", "--short", "HEAD"]) | ||
| .output() | ||
| .unwrap(); | ||
| if !output.status.success() { | ||
| bail!("invalid HEAD commit id") | ||
| } | ||
| let revision_num = String::from_utf8_lossy(output.stdout.as_slice()); | ||
| upload_request.rev = String::from(revision_num.trim()); | ||
|
|
||
| output = Command::new("git") | ||
| .current_dir(".") | ||
| .args(&["ls-files"]) | ||
| .output() | ||
| .unwrap(); | ||
| let tracked_files = String::from_utf8_lossy(output.stdout.as_slice()); | ||
| let tracked_files: Vec<&str> = tracked_files.split("\n").collect(); | ||
| let mut total_files = tracked_files.len(); | ||
| for file_path in tracked_files { | ||
| if file_path.is_empty() { | ||
| total_files -= 1; | ||
| continue; | ||
| } | ||
| } | ||
| upload_request.total_files = total_files; | ||
| upload_request.token = credential::get_registry_api_token(config.test_mode)?; | ||
|
|
||
| if config.test_mode { | ||
| fs::write( | ||
| "./request-body.txt", | ||
| serde_json::to_string(&upload_request).expect("invalid request body"), | ||
| ) | ||
| .expect("unable to write file"); | ||
| } else { | ||
| let url: String; | ||
| if cfg!(debug_assertions) { | ||
| url = String::from("http://staging.movey.net/api/v1/post_package/"); | ||
| } else { | ||
| url = String::from("https://www.movey.net/api/v1/post_package/"); | ||
| } | ||
| let client = Client::new(); | ||
| let response = client.post(&url).json(&upload_request).send().unwrap(); | ||
| if response.status().as_u16() == 200 { | ||
| println!("{}", "Your package has been successfully uploaded to Movey") | ||
| } else { | ||
| println!("{}", "Upload failed") | ||
| } | ||
| } | ||
| } | ||
| PackageCommand::BytecodeView { | ||
| interactive, | ||
| package_name, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| use std::fs; | ||
| use anyhow::{Result, Context, bail}; | ||
| use toml_edit::easy::{Value}; | ||
|
|
||
| pub fn get_move_home_path(is_test_mode: bool) -> String { | ||
| let mut home = std::env::var("MOVE_HOME").unwrap_or_else(|_| { | ||
| format!( | ||
| "{}/.move", | ||
| std::env::var("HOME").expect("env var 'HOME' must be set") | ||
| ) | ||
| }); | ||
| if is_test_mode && !home.contains("/test") { | ||
| home.push_str("/test"); | ||
| } | ||
| home | ||
| } | ||
|
|
||
| pub fn get_credential_path(is_test_mode: bool) -> String { | ||
| get_move_home_path(is_test_mode) + "/credential.toml" | ||
| } | ||
|
|
||
| pub fn get_registry_api_token(is_test_mode: bool) -> Result<String> { | ||
| if let Ok(content) = get_api_token(is_test_mode) { | ||
| Ok(content) | ||
| } else { | ||
| bail!("There seems to be an error with your Movey credential. \ | ||
| Please run `move login` and follow the instructions.") | ||
| } | ||
| } | ||
|
|
||
| fn get_api_token(is_test_mode: bool) -> Result<String> { | ||
| let credential_path = get_credential_path(is_test_mode); | ||
|
|
||
| let contents = fs::read_to_string(&credential_path)?; | ||
| let mut toml: Value = contents.parse()?; | ||
| let registry = toml.as_table_mut() | ||
| .context("Error parsing credential.toml")? | ||
| .get_mut("registry") | ||
| .context("Error parsing credential.toml")?; | ||
| let token = registry.as_table_mut() | ||
| .context("Error parsing token")? | ||
| .get_mut("token") | ||
| .context("Error parsing token")?; | ||
| Ok(token.to_string().replace("\"", "")) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use home::home_dir; | ||
| use std::env; | ||
| use std::fs::File; | ||
|
|
||
| fn setup_move_home() -> (String, String) { | ||
| let mut move_home = env::var("MOVE_HOME").unwrap_or_else(|_| { | ||
| env::var("HOME").unwrap_or_else(|_| { | ||
| let home_dir = home_dir().unwrap().to_string_lossy().to_string(); | ||
| env::set_var("HOME", &home_dir); | ||
| home_dir | ||
| }) | ||
| }); | ||
| move_home.push_str("/.move/test"); | ||
| let credential_path = move_home.clone() + "/credential.toml"; | ||
|
|
||
| return (move_home, credential_path); | ||
| } | ||
|
|
||
| fn clean_up() { | ||
| let (move_home, _) = setup_move_home(); | ||
| let _ = fs::remove_dir_all(move_home); | ||
| } | ||
|
|
||
| #[test] | ||
| fn get_api_token_works() { | ||
| let (move_home, credential_path) = setup_move_home(); | ||
|
|
||
| let _ = fs::create_dir_all(&move_home); | ||
| File::create(&credential_path).unwrap(); | ||
|
|
||
| let content = "[registry]\ntoken = \"a sample token\""; | ||
| fs::write(&credential_path, content).unwrap(); | ||
|
|
||
| let token = get_api_token(true).unwrap(); | ||
| assert!(token.contains("a sample token")); | ||
|
|
||
| clean_up() | ||
| } | ||
|
|
||
| #[test] | ||
| fn get_api_token_fails_if_there_is_no_move_home_directory() { | ||
| let (move_home, _) = setup_move_home(); | ||
| let _ = fs::remove_dir_all(&move_home); | ||
| let token = get_api_token(true); | ||
| assert!(token.is_err()); | ||
|
|
||
| clean_up() | ||
| } | ||
|
|
||
| #[test] | ||
| fn get_api_token_fails_if_there_is_no_credential_file() { | ||
| let (move_home, _) = setup_move_home(); | ||
| let _ = fs::remove_dir_all(&move_home); | ||
| fs::create_dir_all(&move_home).unwrap(); | ||
| let token = get_api_token(true); | ||
| assert!(token.is_err()); | ||
|
|
||
| clean_up() | ||
| } | ||
|
|
||
| #[test] | ||
| fn get_api_token_fails_if_credential_file_is_in_wrong_format() { | ||
| let (move_home, credential_path) = setup_move_home(); | ||
|
|
||
| let _ = fs::remove_dir_all(&move_home); | ||
| fs::create_dir_all(&move_home).unwrap(); | ||
| File::create(&credential_path).unwrap(); | ||
|
|
||
| let content = "[registry]\ntoken = a sample token"; | ||
| fs::write(&credential_path, content).unwrap(); | ||
|
|
||
| let token = get_api_token(true); | ||
| assert!(token.is_err()); | ||
|
|
||
| let content = "[registry]\ntokens = \"a sample token\""; | ||
| fs::write(&credential_path, content).unwrap(); | ||
|
|
||
| let token = get_api_token(true); | ||
| assert!(token.is_err()); | ||
|
|
||
| clean_up() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| pub mod credential; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if this can reuse the package manager's code for downloading from github: https://github.com/move-language/move/blob/main/language/tools/move-package/src/resolution/resolution_graph.rs#L517
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean we should go with
map_errinstead ofunwraplike in the code you linked, OR do you mean we should try to reuse the functiondownload_and_update_if_repo, in which case not necessary because here we are querying info from the local (already fetched) repo.