Skip to content
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

How to upload file on GitHub using the module ? #155

Closed
damienvanrobaeys opened this issue Apr 17, 2020 · 8 comments
Closed

How to upload file on GitHub using the module ? #155

damienvanrobaeys opened this issue Apr 17, 2020 · 8 comments

Comments

@damienvanrobaeys
Copy link

damienvanrobaeys commented Apr 17, 2020

The module is pretty awesome.
Just starting some tests with it but don't find how to upload file on github ?
I can create a readme using the autoinit parameter.
For now I'm searching how to add my own content into this readme then upload for instance an exe or script in the repository

@HowardWolosky
Copy link
Member

Glad you like the module! Thanks for the feedback.
How did you come to find out about it?

For creating files, we need to use the Contents API.

@Shazwazza just recently added the ability to get contents. Their plan was to add the create/update API next. Until that is done, the workaround would be to add new content via git API's instead.

It looks like #150 is tracking the API's getting done.

@damienvanrobaeys
Copy link
Author

Thanks for the quick answer.
Actually as you may know the Technet gallery will soon retire, so I did a script to backup all contributions locally.
http://www.systanddeploy.com/2020/04/backup-all-your-technet-gallery.html

Now my next step is to backup and migrate to Github.
I can create the readme using the -AutoInit parameter.
Is it possible to add my mardown content to this file ? I saw the convertfrommardown parameter.

@HowardWolosky
Copy link
Member

There's no way to change the content as part of that API.
That's the "create a repository for the authenticated user" API, which has an auto_init option (transposed as an -AutoInit switch) which will create an initial commit with an empty README.

What you're looking for is the create/update contents API which hasn't been added yet, as mentioned.

The workaround would be to make the desired changes, and then just use the normal git commands to push the changes up:

git add *
git commit -m "Updating readme"
git push

@damienvanrobaeys
Copy link
Author

Thanks for answer one more time :-)
I will continue to test the module and will blog about it.
Really awesome module, easy to use and well documented.

@HowardWolosky
Copy link
Member

Thanks for the feedback!

@damienvanrobaeys
Copy link
Author

I have found how to upload my file.

$MyFile = @"
{
"message": "my commit message",
"committer": {
"name": "Damien",
"email": "[email protected]"
},
"content": "bW9uIGZpY2hpZXI="
}
"@

Invoke-GHRestMethod -UriFragment "https://api.github.com/repos/damienvanrobaeys/myrepo/contents/myfile.txt" -Method PUT -Body $MyFile

@damienvanrobaeys
Copy link
Author

I complete my upload script. I proceed as below:

    • Select a file
    • Encode it to base 64
    • Create a json for this one
    • Upload the selected file on the selected repo

The script

$Get_Selected_File = Read-Host "Type the path of the file to upload on your repo"
$Get_File_Name = (Get-ChildItem $Get_Selected_File).name
$File_Content = get-content $Get_Selected_File
$File_Content_Bytes = [System.Text.Encoding]::UTF8.GetBytes($File_Content)
$Encoded_File = [System.Convert]::ToBase64String($File_Content_Bytes)

$MyFile_JSON = @"
{
"message": "",
"content": "$Encoded_File"
}
"@
Invoke-GHRestMethod -UriFragment "https://api.github.com/repos/$OwnerName/$RepositoryName/contents/$Get_File_Name" -Method PUT -Body $MyFile_JSON

If you're interested I can create a function like in your module for this part and submit it to you.

@damienvanrobaeysdev
Copy link

I use another way to encode to base 64 because the previous one does not encode properly file and not encode exe, zip... correctly
Now i'm able to encode zip, txt, pdf, ps1, vbs, rar, exe ... and upload them on github.

$Get_Selected_File = Read-Host "Type the path of the file to upload on your repo"
$Get_File_Name = (Get-ChildItem $Get_Selected_File).name
$Encoded_File = [System.Convert]::ToBase64String([System.IO.File]::ReadAllBytes("$Get_Selected_File"));

$MyFile_JSON = @"
{
"message": "",
"content": "$Encoded_File"
}
"@
Invoke-GHRestMethod -UriFragment "https://api.github.com/repos/$OwnerName/$RepositoryName/contents/$Get_File_Name" -Method PUT -Body $MyFile_JSON

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

No branches or pull requests

3 participants