Load Template from Gitlab Functionality Added#14
Conversation
|
Hi @cophilot , I have completed all the changes along with necessary tests for this PR. Please check and let me know if you want any changes. Also as soon as you read this, please assign me with some other task / bug to contribute on. |
src/commands/load.rs
Outdated
|
|
||
| let url_type = if url.starts_with("https://github.com") { | ||
| URLType::GitHub | ||
| } else if url.starts_with("https://gitlab.com") { |
There was a problem hiding this comment.
Also costum giotlab isntrances should be supported. E.g. then you can have an URL like "https://my.gitlab.com". I think here a a pattern check would be better (like "https://*gitlab.com")
There was a problem hiding this comment.
Hi @cophilot , Thankyou so much for pointing this out , now I'm checking the gitlab url with RegEx, for a thorough pattern check
src/utils/template_handler.rs
Outdated
| path: &str, | ||
| url: &str, | ||
| force: bool, | ||
| url_type: &URLType, |
There was a problem hiding this comment.
Each time this function is called the URL has to be checked to determine the right type. This adds code repetition. Therefore I would suggest to handle the URL type within this function and also check for if the URL is not supported. In this case return a status with an error and the caller then has to check that status as well.
There was a problem hiding this comment.
Thats an amazing point @cophilot , added the url check inside this function, so that this function can become more resuable with lesser code repition. Also, added the check inside for an invalid url and added the test for the same.
| let response: serde_json::Value = response.unwrap(); | ||
|
|
||
| let items = response["payload"]["tree"]["items"].as_array().unwrap(); | ||
| let items = match url_type { |
src/utils/template_handler.rs
Outdated
| )); | ||
| } | ||
| /// Load a template from a gitlab repository | ||
| pub(crate) fn load_gitlab_template( |
There was a problem hiding this comment.
This should not be public because the function is only used here
There was a problem hiding this comment.
Got it, Made the change
src/utils/template_handler.rs
Outdated
| } | ||
|
|
||
| /// Load a template from a github repository | ||
| pub(crate) fn load_github_template( |
| } | ||
|
|
||
| /// Load a template from a remote repository | ||
| pub(crate) fn load_remote_template( |
There was a problem hiding this comment.
Move that function above load_gitlab_template and load_github_templatebecause this function should be used and therefore should be seen first
There was a problem hiding this comment.
For sure, changed the position of function, for better readablity and maintainablity as you suggested
src/utils/template_handler.rs
Outdated
| path: &str, | ||
| url: &str, | ||
| force: bool, | ||
| url_type: &URLType, |
There was a problem hiding this comment.
Same here with the type. Maybe add a internal function that is checking the type to avoid repetition of the check
src/utils/template_handler.rs
Outdated
| for item in items { | ||
| if item["type"] == "tree" { | ||
| let st = load_remote_gitlab_template_dir( | ||
| format!("{}/{}", path, item["name"]) |
There was a problem hiding this comment.
Scince this formatting is done here twice maybe add a lambda just before and then u just have to apply the lambda to the path and the url.
There was a problem hiding this comment.
Sure @cophilot , Instead of Lambda function, I extracted a whole new function for this fomatting purpose, and put those at the last of template_handler.rs only. As it is using in many other places other than this, as pointed by you.
src/utils/template_handler.rs
Outdated
| let response = rest::json_call(url); | ||
| if response.is_err() { | ||
| return Status::error(format!( | ||
| "Failed to get template from {}: : Request failed", |
src/utils/template_handler.rs
Outdated
| for item in items { | ||
| if item["type"] == "tree" { | ||
| let st = load_remote_gitlab_template_dir( | ||
| format!("{}/{}", path, item["name"]) |
There was a problem hiding this comment.
Same here with the lambdas. Maybe then it make sense to add a function for this...
src/utils/template_handler.rs
Outdated
| } | ||
|
|
||
| load_remote_gitlab_template_file( | ||
| format!("{}/{}", path, item["name"]) |
There was a problem hiding this comment.
Yeah add a small internal function for it :D
| pub fn check_gitlab_load() { | ||
| utils::init_tpy(); | ||
|
|
||
| utils::run_successfully("tpy load https://gitlab.com/api/v4/projects/cophilot%2Ftemplify-vault/repository/tree?path=Test"); |
There was a problem hiding this comment.
I see that only direct api calls are supported. Would it be possible to do it as well with the "normal url" of gitlab (e.g. "https://gitlab.com/cophilot/templify-vault/-/tree/main/Test")? Maybe parse the "normal url" to the api url? TBH not sure how easy that is...
There was a problem hiding this comment.
Thats a wonderful idea, @cophilot and I am more than happy to implement this no matter how easy or difficult it will be, as it will give more flexibility to the user while loading templates from gitlab. But I am thinking to take this as an enhancement in a separate PR. Will that be fine with you ? If yes,, Please rasie a separte issue of that , and assign it to me.
|
Hi @RounakJoshi09 , sorry for the late reply, I was pretty busy... |
| path: &str, | ||
| url: &str, | ||
| force: bool, | ||
| url_type: Option<&URLType>, |
There was a problem hiding this comment.
I kept url_type as optional here because at some places this function is called where url_type is already calculated, but in some url_type is not there, so in order to provide more flexiibilty, I made this as optional, so if url_type is calculated before and is passed into this function we will take thaaat otherwise will recalculate later in this code at line 177,
a713c2e to
19d19b2
Compare
|
I greatly appreciate your patience and thorough review of this PR, @cophilot . I'll certainly keep these principles of code reusability and maintainability in mind for future contributions. I've implemented all the changes you suggested. Could you please take another look at the updated code? For your reference, I've also attached the latest test result screenshot. Thank you again for your guidance and support. |
src/commands/load.rs
Outdated
|
|
||
| /// This enum is used to define type of URL (.i.e.. GitHub, GitLab) | ||
| #[derive(Clone)] | ||
| pub(crate) enum URLType { |
There was a problem hiding this comment.
Please move that Type to a own file under src/types.
Otherwise the PR looks good and can be merged after that small change :)
|
@RounakJoshi09 You don`t have to run the test manually and show them, the pipeline is triggered automatically :) |
|
Well done! Will merge now |
|
Thanks for merging this @cophilot , At last I would request to change readme.md of this gitlab repository |
Good Idea @RounakJoshi09 ! |



Added the feature to load the template from GitLab repository.
Type of change
Please select the relevant option:
Description
To imlement this functionality, I researched that the url needed to fetch files from gitlab is little different than github, also gitlab have one api to give the folder/dir of the template and another api to give the content of the blob/file.
The content of the file which we recieve is encoded in base64 format, which needs to be decoded in order to get the content of the file.
Also to make the things resuable , I have added a parameter, named urrlType to the existing function and based on this I am driving the code to the specific function fot gitlab and github.
Changes
Things I did for this feature:-