Rest API to add a submodule to a repository

Hi,

I have a requirement of adding a submodule to a repository.

 I was following the below link:

https://stackoverflow.com/questions/35765445/how-to-create-submodule-in-github-using-github-rest-api/ in which the first call is to create a tree.

So I followed the below docs:"

https://developer.github.com/v3/git/trees/#create-a-tree

So I am trying to make a POST request to the below URL:

https://github.com/repos/owner/repo/git/trees

Request Body:

{
“base_tree”: “519e1a67a6f4cad20225ae7d80a2fef879771e83”,
“tree”: [
{
“path”: “.gitmodules”,
“mode”: “100644”,
“type”: “blob”,
“content”: “[submodule “test”]\n\tpath = test\n\turl = https://github.com/owner/newgit
},
{
“path”: “test”,
“mode”: “160000”,
“type”: “commit”,
“sha”: “84e84960afaf58a667a81d33023e93be5b28b894”
}
]
}

while making the POST request I am getting “422 Unprocessable Entity” error code.

When will I be getting this error?? Is there a chance of  getting this error if the “sha” values that I have provided are wrong values??

Thanks and Regards,

Lahari G

This post was moved to a different board that fits your topic of discussion a bit better. This means you’ll get better engagement on your post, and it keeps our Community organized so users can more easily find information.

As you’ll notice, your Topic is now in the How to use Git and GitHub board. No action is needed on your part; you can continue the conversation as normal here.

From MDN:

The HyperText Transfer Protocol (HTTP) 422 Unprocessable Entity response status code indicates that the server understands the content type of the request entity, and the syntax of the request entity is correct, but it was unable to process the contained instructions.

So yes, it entirely could be that if you included the incorrect SHA it would return this error.

Hi,

May I know the How to find “SHA1 checksum ID of the object in the tree”  ???

Do we have any command???Can we get the value with git log command???

Personally, I wouldn’t do any of this via the API and do it all via Git commands on a local filesystem and push the changes to the repository on GitHub. It would be much, much simpler. But if having Git installed wherever you have available to do this work isn’t a possibility, then I can understand needing to go through this.

In order to get the SHA of a tree object, you have to first get the commit object that the tree object belongs to. For that you need the SHA of the commit object. To get the SHA of the commit object, you should probably get it from the ref belonging to the branch you want to base your change upon. As you can hopefully see, the Git Database API is designed to allow you to do really low-level things to the underlying Git database but it also means that you have to have a lot of knowledge about how the Git database is constructed. Additionally, even the simplest of things is a very involved process because you’re essentially re-implementing Git by hand.

Also, a cultural note … repeated question marks (such as “???” in your message) is often seen as demanding an answer. This can come across as being very rude. A single question mark is all that is necessary for us to do our best to help :grinning:

I hope that helps!

Hi,

Thank you for the reply. We have a requirement to implement it via REST API only so I was just searching the ways to do it. I have tested creation of submodule using git commands. But I was not able to achieve it through REST API.So I just posted my query. 

Sorry for the multiple question marks. It was not intentionally used. I was just wondering what was the reason for my Issue.whether it was because of incorrect sha value or is it because of some other things.

Thank you for the support.

Thanks and Regards,

Lahari G