Using VB.net - pull a file from GitLab

99封情书 提交于 2019-12-13 20:22:14

问题


As the title implies, I'm looking for a way to pull specific file(s) from a private GitLab repo using VB.net (2017).

I have an application that I'm writing which will call certain PowerShell scripts. I have a few other users working with me writing the scripts, so we are using GitLab as the repository for these.

We want the application to pull the latest version of the scripts from GitLab when the application opens, then from within the app, call the scripts.

I have everything done, with the exception of downloading the scripts from GitLab.


回答1:


So I'm posting an answer here just in case anyone else has the same question. I was actually able to get this done pretty easily.

First, you have to generate a private token. Plenty of walk-throughs for that, so I won't go into that here.

Next, you have to get the address of raw file that you want to download. You can get this by opening the file in GitLab, then there's a button on the top right of the window to "Open Raw", which opens the raw page.
See Image Here

Grab the url from the address bar. Once you have that, you have all the pieces you need to curl the file down using VB.net.

You have to take the address of the raw file, let's say that was "https://gitlab.com/CompanyName/raw/master/folder/filename.ps1", you then append ?, with your private token so it looks like this: "https://gitlab.com/CompanyName/raw/master/folder/filename.ps1?private_token=MyPrivateToken" and use a curl (through powershell) to get it.

I already had a function in my code to run powershell scripts (with code I believe I got off this site...forgot the exact location), which was like this:

    Private Function RunScript(ByVal scriptText As String) As String
    ' Takes script text as input and runs it, then converts
    ' the results to a string to return to the user
    ' create Powershell runspace
    Dim MyRunSpace As Runspace = RunspaceFactory.CreateRunspace()

    ' open it
    MyRunSpace.Open()

    ' create a pipeline and feed it the script text
    Dim MyPipeline As Pipeline = MyRunSpace.CreatePipeline()

    MyPipeline.Commands.AddScript(scriptText)

    ' add an extra command to transform the script output objects into nicely formatted strings
    ' remove this line to get the actual objects that the script returns. For example, the script
    ' "Get-Process" returns a collection of System.Diagnostics.Process instances.
    MyPipeline.Commands.Add("Out-String")

    ' execute the script
    Dim results As Collection(Of PSObject) = MyPipeline.Invoke()

    ' close the runspace
    MyRunSpace.Close()

    ' convert the script result into a single string
    Dim MyStringBuilder As New StringBuilder()

    For Each obj As PSObject In results
        MyStringBuilder.AppendLine(obj.ToString())
    Next

    ' return the results of the script that has
    ' now been converted to text
    Return MyStringBuilder.ToString()

End Function

Now, I can call a curl command with that function like this:

RunScript("curl https://gitlab.com/CompanyName/raw/master/folder/filename.ps1?private_token=MyPrivateToken -outfile C:\DownloadFolder\FileName.ps1")

That's it! Anytime you need to get a file, you can simply get the location of the raw file and modify the function call to reflect the new address and grab it.



来源:https://stackoverflow.com/questions/50763823/using-vb-net-pull-a-file-from-gitlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!