sumoheavy / jira-ruby

A Ruby gem for the JIRA REST API
MIT License
656 stars 411 forks source link

How can I upload attachments in bulk #371

Closed JackDan9 closed 4 years ago

JackDan9 commented 4 years ago
        options = {
          :username => 'username',
          :password => 'password',
          :site => 'https://example.atlassian.net',
          :context_path => '',
          :auth_type => :basic
        }

        # jira connect client
        client = JIRA::Client.new(options)

        # project type is CustomerEnvironment or CSE
        issue = client.Issue.find('CSE-2061')
        attachment = issue.attachments.build
        attachments_path.each do|attachment_path|
          attachment.save('file': attachment_path)
        end

attachment_path like this "/opt/nginx/html/redmine-3.4.0/files/2020/09/200912205657_511bf29db480696c91e678996626f5c6.png"

But i just upload a single file, my attachment information like this:

=> #<JIRA::Resource::Attachment:0x0000000003c29cc0
 @attrs=
  {:file=>"/opt/nginx/html/redmine-3.4.0/files/2020/09/200911200343_640.png",
   "self"=>"https://easystack.atlassian.net/rest/api/2/attachment/100229",
   "id"=>"100229",
   "filename"=>"200911200343_640.png",
   "author"=>
    {"self"=>"https://easystack.atlassian.net/rest/api/2/user?accountId=5dff08655361330daae9584e",
     "accountId"=>"5dff08655361330daae9584e",
     "emailAddress"=>"esoncall@easystack.cn",
     "avatarUrls"=>
      {"48x48"=>"https://secure.gravatar.com/avatar/bd9fc18965320ebdbf4ab3ecf65efbfe?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FE-4.png",
       "24x24"=>"https://secure.gravatar.com/avatar/bd9fc18965320ebdbf4ab3ecf65efbfe?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FE-4.png",
       "16x16"=>"https://secure.gravatar.com/avatar/bd9fc18965320ebdbf4ab3ecf65efbfe?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FE-4.png",
       "32x32"=>"https://secure.gravatar.com/avatar/bd9fc18965320ebdbf4ab3ecf65efbfe?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FE-4.png"},
     "displayName"=>"esoncall",
     "active"=>true,
     "timeZone"=>"Asia/Shanghai",
     "accountType"=>"atlassian"},
   "created"=>"2020-09-12T20:58:11.094+0800",
   "size"=>174677,
   "mimeType"=>"application/binary",
   "content"=>"https://easystack.atlassian.net/secure/attachment/100229/200911200343_640.png",
   "thumbnail"=>"https://easystack.atlassian.net/secure/thumbnail/100229/200911200343_640.png"},
 @client=
  #<JIRA::Client:0x0000000003ff3c20
   @cache=#<OpenStruct>,
   @http_debug=false,
   @options=
    {:site=>"https://easystack.atlassian.net",
     :context_path=>"",
     :rest_base_path=>"/rest/api/2",
     :ssl_verify_mode=>1,
     :use_ssl=>true,
     :use_client_cert=>false,
     :auth_type=>:basic,
     :http_debug=>false,
     :default_headers=>{},
     :username=>"username",

"/opt/nginx/html/redmine-3.4.0/files/2020/09/200911200343_640.png" is my first file.

Please give me a hand to solve this question! Thanks!

SimonMiaou commented 4 years ago

Shouldn't you move the build part in the loop?

        issue = client.Issue.find('CSE-2061')
        attachments_path.each do|attachment_path|
          attachment = issue.attachments.build
          attachment.save('file': attachment_path)
        end
SimonMiaou commented 4 years ago

I assume my answer solved your issue.