Skip to content

Load Template from Gitlab Functionality Added#14

Merged
cophilot merged 8 commits intocophilot:devfrom
RounakJoshi09:rounakj/gitlab
Nov 3, 2024
Merged

Load Template from Gitlab Functionality Added#14
cophilot merged 8 commits intocophilot:devfrom
RounakJoshi09:rounakj/gitlab

Conversation

@RounakJoshi09
Copy link
Copy Markdown
Collaborator

@RounakJoshi09 RounakJoshi09 commented Sep 10, 2024

Added the feature to load the template from GitLab repository.

Type of change

Please select the relevant option:

  • Bug fix
  • New feature
  • New test
  • Refactoring / code cleanup
  • Documentation
  • Other (please describe here: ...)

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:-

  1. Added a package to decode base64 string, because gitlab gives the content of file in base64.
  2. Added a new method to load template/dir from gitlab
  3. Added tests to make sure everything works as expected.
  4. Added required changes in the help description.

{BE6D8152-8835-4961-B18F-A56F6E436F55}

@RounakJoshi09 RounakJoshi09 mentioned this pull request Sep 10, 2024
@cophilot cophilot marked this pull request as ready for review September 22, 2024 06:49
@RounakJoshi09
Copy link
Copy Markdown
Collaborator Author

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.

@cophilot cophilot linked an issue Oct 28, 2024 that may be closed by this pull request

let url_type = if url.starts_with("https://github.com") {
URLType::GitHub
} else if url.starts_with("https://gitlab.com") {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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")

Copy link
Copy Markdown
Collaborator Author

@RounakJoshi09 RounakJoshi09 Nov 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @cophilot , Thankyou so much for pointing this out , now I'm checking the gitlab url with RegEx, for a thorough pattern check

path: &str,
url: &str,
force: bool,
url_type: &URLType,
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice!

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thankyou 😊

));
}
/// Load a template from a gitlab repository
pub(crate) fn load_gitlab_template(
Copy link
Copy Markdown
Owner

@cophilot cophilot Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be public because the function is only used here

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, Made the change

}

/// Load a template from a github repository
pub(crate) fn load_github_template(
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also not public

}

/// Load a template from a remote repository
pub(crate) fn load_remote_template(
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move that function above load_gitlab_template and load_github_templatebecause this function should be used and therefore should be seen first

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For sure, changed the position of function, for better readablity and maintainablity as you suggested

path: &str,
url: &str,
force: bool,
url_type: &URLType,
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here with the type. Maybe add a internal function that is checking the type to avoid repetition of the check

for item in items {
if item["type"] == "tree" {
let st = load_remote_gitlab_template_dir(
format!("{}/{}", path, item["name"])
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

let response = rest::json_call(url);
if response.is_err() {
return Status::error(format!(
"Failed to get template from {}: : Request failed",
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove one colon :)

for item in items {
if item["type"] == "tree" {
let st = load_remote_gitlab_template_dir(
format!("{}/{}", path, item["name"])
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here with the lambdas. Maybe then it make sense to add a function for this...

}

load_remote_gitlab_template_file(
format!("{}/{}", path, item["name"])
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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");
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

Copy link
Copy Markdown
Collaborator Author

@RounakJoshi09 RounakJoshi09 Nov 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@cophilot
Copy link
Copy Markdown
Owner

Hi @RounakJoshi09 , sorry for the late reply, I was pretty busy...
Very nice work you did here! I added some comments with small code changes but I think they are quiet easy to fix :)
Also your changes are not passing the comment-check and the lint check in the CI pipeline. Please also fix the issues that occure when running the scripts (are also quiet easy to fix). You can find more details here.

path: &str,
url: &str,
force: bool,
url_type: Option<&URLType>,
Copy link
Copy Markdown
Collaborator Author

@RounakJoshi09 RounakJoshi09 Nov 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,

@RounakJoshi09
Copy link
Copy Markdown
Collaborator Author

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.

image

@cophilot cophilot changed the base branch from master to dev November 2, 2024 16:49

/// This enum is used to define type of URL (.i.e.. GitHub, GitLab)
#[derive(Clone)]
pub(crate) enum URLType {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move that Type to a own file under src/types.
Otherwise the PR looks good and can be merged after that small change :)

Copy link
Copy Markdown
Collaborator Author

@RounakJoshi09 RounakJoshi09 Nov 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made the change here @cophilot 94b7250

Thankyou

@RounakJoshi09
Copy link
Copy Markdown
Collaborator Author

image

@cophilot
Copy link
Copy Markdown
Owner

cophilot commented Nov 3, 2024

image

@RounakJoshi09 You don`t have to run the test manually and show them, the pipeline is triggered automatically :)

@cophilot
Copy link
Copy Markdown
Owner

cophilot commented Nov 3, 2024

Well done! Will merge now

@cophilot cophilot merged commit c0a3b2b into cophilot:dev Nov 3, 2024
@RounakJoshi09
Copy link
Copy Markdown
Collaborator Author

Thanks for merging this @cophilot , At last I would request to change readme.md of this gitlab repository
https://gitlab.com/cophilot/templify-vault , and update all the load urls of the vault to the gitlab urls, for users convenience.

@cophilot
Copy link
Copy Markdown
Owner

cophilot commented Nov 3, 2024

Thanks for merging this @cophilot , At last I would request to change readme.md of this gitlab repository

https://gitlab.com/cophilot/templify-vault , and update all the load urls of the vault to the gitlab urls, for users convenience.

Good Idea @RounakJoshi09 !
I will do that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Load from Gitlab

2 participants