Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Exportando issues e anexos com Jira Cloud Rest API

De tempos em tempos, preciso exportar issues de um projeto no Jira, incluindo todos os anexos. Pode ser para ter um backup dos registros para montar uma base de conhecimento, ou para transferir os registros para alguém que não possui acesso ao Jira. 

A funcionalidade de exportação do Jira para Word funciona super bem, porém, não exporta os anexos. Para contornar essa limitação, poderia recorrer a aplicativos (ou plugins para os mais velhos/experientes) que permitem essa exportação, mas alguns são pagos ou só funcionam na versão server.

A solução que adotei foi utilizar a interface REST para baixar os anexos. Para isso, fiz um script em Powershell (testado em um Windows 10 - PSVersion 5.1.17134.407) que deixou o processo mais simples.

Passo a passo:

1) Crie um filtro com as issues que você deseja exportar

2) Clique no ícone de export e copie o link do XML export; O script irá usar a lista em XML para selecionar todos os anexos presentes na lista de issues

3) Execute o script .\export-attachments.ps1 e siga as instruções:

3.1) Forneça as credenciais de acesso ao Jira, o script irá converter usuário em senha em um basic authentication e gerar o hash de autorização

3.2) Forneça o diretório onde será criado um diretório chamado attachments e dentro deste diretório serão criados um diretório para cada issue e dentro destes diretórios serão armazenados cada um dos anexos

3.3) Forneça o link para o "XML Export" (copiado no passo 2)

3.4) Sente-se e aguarde o processo terminar

 

Script export-attachments.ps1

# Export all attachments from XML export of Jira

Function Get-Folder($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null

$foldername = New-Object System.Windows.Forms.FolderBrowserDialog
$foldername.Description = "Select a folder to store exported attachments"
$foldername.rootfolder = "MyComputer"

if($foldername.ShowDialog() -eq "OK")
{
$folder += $foldername.SelectedPath
}
return $folder
}

# You will need authorization string to access Jira
$cred=get-credential -Message "Enter you username and password to access Jira Cloud"
$user = $cred.username
$pass = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($cred.password))
$pair = "$($user):$($pass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$authorization = "Basic $encodedCreds"
$directory = Get-Folder

# You must create a filter and copy "export to XML" URL
$uri=read-host "Paste 'XML export' URL (Jira)"
$temp=$uri.Split("/")
$base_uri=$temp[0]+'//'+$temp[2]
$outfile="lista.xml"

# This folder will be used to store all attachments, for each record (jira issue) will be created a folder
# with the issue key and inside it will be stored all attachments
$directory="$directory\attachments"

Invoke-WebRequest -Headers @{"Authorization"=$authorization} -uri $uri -outfile $outfile

[xml]$Content = Get-Content .\lista.xml
$Feed = $Content.rss.channel

ForEach ($msg in $Feed.Item){
$idjira=$msg.key.'#text'
if (Test-path "$directory\$idjira") {
write-host "Folder $directory\$idjira exists"
} else {
write-host "Folder $directory\$idjira don´t exist, creating..."
mkdir "$directory\$idjira"
}
ForEach ($attachment in $msg.attachments.attachment) {
$idattachment=$attachment.id
$nameattachment=$attachment.name
$uri="$base_uri/secure/attachment/$idattachment/"
if (test-path "$directory\$idjira\$nameattachment") {
$outfile="$directory\$idjira\$idattachment.$nameattachment"
$outfile=$outfile.Replace('[','').replace(']','')
write-host "File [$directory\$idjira\$nameattachment] already exists, creating other version $outfile"
} else {
$outfile="$directory\$idjira\$nameattachment"
$outfile=$outfile.Replace('[','').replace(']','')
write-host "Creating file $outfile"
}
write-host "Downloading $uri to $outfile..."
Invoke-WebRequest -Headers @{"Authorization"=$authorization} -uri $uri -outfile $outfile
write-host "done"
}
}
Remove-item ".\lista.xml"

 

 

1 comment

Fala João, blz?

Excelente post meu caro, me poupou um grande trabalho!

Muito obrigado!

Comment

Log in or Sign up to comment
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you