There is a prerequisite to dowload an enterprise git repo using code scripting. You have to use a token, like a user and password, to do the request.
TOKEN CREATION
Now we see how to create the token. Follow this steps
Once you connect to bitbucket, go to the contextual menu of the user and select "Manage account" option
you will access to a section and in the left panel you will see different other options and have to select "Personal access tokens"
Now you'll be able to select the "Create a token" button...
You will have to compile a form in which you will have to type a name for the token and select the permissions you need.
For the expire option select "No" and click on create button
It will show you the form that confirm the token creation. You have to copy and save the information that use in the script.
Once you got the token, you'll be able to do the request.
The informations follow this structure: PROJECT - REPOSITORY - BRANCH
The script we are going to create will accept those three parameters plus one that is the output folder where the zip file will download to.
Script Code
'Input Parameters: PROGETTO - REPO - BRANCH - OUT_FOLDER
'The request will be:
'https://bitbucket.....com/rest/api/latest/projects/PROGETTO/repos/REPO/archive?at=refs%2Fheads%2FBRANCH&format=zip
Const GIT_ROOT = "https://bitbucket.....com/rest/api/latest/projects/"
Const GIT_REPO_TAG = "/repos/"
Const GIT_ARCH_REF = "/archive?at=refs%2Fheads%2F"
Const GIT_FORMAT = "&format=zip"
Const GITTKN = "MyU3MDkzOTAxNjBvOhldPJ4zUhLoFp1XG0iSKp5zCBRb"
'Howevere it is possible also to the request through basic authentication with username and password.
'Const GITUSR = "MYUSR"
'Const GITPWD = "MYPWD"
Const RC_OK = 0
Const RC_ERR = 3
Dim StdOut : Set StdOut = WScript.StdOut 'Standart Output
Dim exit_code : exit_code = RC_ERR
if wscript.arguments.count <> 4 then
WriteLine date & " - " & time & " : Arguments number error: "
WriteLine date & " - " & time & " : Arguments expected: PROJECT REPO BRANCH OUT_FOLDER(ex. c:\temp)"
WriteLine date & " - " & time & " : End of Program - Exit Code: " & exit_code
set StdOut = nothing
wscript.quit exit_code
end if
Dim fso : set fso = CreateObject("Scripting.FileSystemObject")
Dim PROJECT : PROJECT = wscript.arguments(0)
Dim REPO : REPO = wscript.arguments(1)
Dim BRANCH : BRANCH = wscript.arguments(2)
Dim OUT_FOLDER : OUT_FOLDER = wscript.arguments(3)
if right(OUT_FOLDER,1) <> "\" then
OUT_FOLDER = OUT_FOLDER & "\"
end if
Dim intRequest : intRequest = JRequest
if intRequest = 200 Or intRequest = 201 then
WriteLine date & " - " & time & " : DownLoad Repo OK "
exit_code = RC_OK
else
WriteLine date & " - " & time & " : Error during download repo - Request Return Code: " & intRequest
end if
WriteLine date & " - " & time & " : End of Program - Exit Code: " & exit_code
DestroyObjects
wscript.quit exit_code
'*************************************************************************************************************************
' The JRequest function is used to do a request and return a string of the response - in this case the status of the request will returns
'*************************************************************************************************************************
Function JRequest()
Dim strRes : strRes = ""
Dim strURL
strURL = GIT_ROOT & PROJECT & GIT_REPO_TAG & REPO & GIT_ARCH_REF & BRANCH & GIT_FORMAT
Dim HttpReq : set HttpReq = CreateObject("Msxml2.ServerXMLHTTP.6.0")
Dim bStrm : set bStrm = CreateObject("Adodb.Stream")
WriteLine date & " - " & time & " : REQUEST: " & strURL
HttpReq.Open "GET", strURL, False
'We use the token to do the request previous created in
'bitbucket in the profile section (Menage Account > Personal Access Token)
'With token - just add Authorization Header and the value: "Bearer " + token.
HttpReq.setRequestHeader "Authorization", "Bearer " & GITTKN
'It is also possible to use the user and password for the basic authentication
'With Basic Authentication - There are two function to do a Base 64 Encoding.
'HttpReq.setRequestHeader "Authorization", "Basic "& Base64Encode(GITUSR &":"& GITPWD)
HttpReq.Send
wscript.sleep 60000 'fix wait for a minute
with bStrm
.type = 1 '//binary
.open
.write HttpReq.responseBody
.savetofile OUT_FOLDER & PROJECT & ".zip", 2
end with
strRes = HttpReq.Status
Set HttpReq = Nothing
Set bStrm = Nothing
JRequest = strRes
End Function
'**********************************************************************************************************
'**********************************************************************************************************
'Function to manage a txt using base64 - It is used only with the basic authentication
'**********************************************************************************************************
Function Base64Encode(sText)
Dim oXML, oNode
Set oXML = CreateObject("Msxml2.DOMDocument.3.0")
Set oNode = oXML.CreateElement("base64")
oNode.dataType = "bin.base64"
oNode.nodeTypedValue = Stream_StringToBinary(sText)
Base64Encode = oNode.text
Set oNode = Nothing
Set oXML = Nothing
End Function
Private Function Stream_StringToBinary(Text)
Const adTypeText = 2
Const adTypeBinary = 1
Dim BinaryStream 'As New Stream
Set BinaryStream = CreateObject("ADODB.Stream")
BinaryStream.Type = adTypeText
BinaryStream.CharSet = "us-ascii"
BinaryStream.Open
BinaryStream.WriteText Text
BinaryStream.Position = 0
BinaryStream.Type = adTypeBinary
BinaryStream.Position = 0
Stream_StringToBinary = BinaryStream.Read
Set BinaryStream = Nothing
End Function
'*********************************************************************************************************************************
'Sub to write online Output (standard output)
'*********************************************************************************************************************************
Sub WriteLine(text)
StdOut.WriteLine(text) 'output writing
End Sub
'*********************************************************************************************************************************
'*********************************************************************************************************************************
'Destroy global objects
'*********************************************************************************************************************************
sub DestroyObjects()
set fso = nothing
set StdOut = nothing
End Sub
'*********************************************************************************************************************************
Questo sito è stato realizzato con Jimdo! Registra il tuo sito gratis su https://it.jimdo.com