Creating issues in a script

Added by Nox Metus over 9 years ago

I am experimenting with a shell script that would allow me to create an issue on certain conditions. For example, if a night build has failed. I tried so far something very simple:

{
cat <<EOF
From: user@server.com
To: user@server.com
Subject: Night build failed

Issue body
EOF
} | rdm-mailhandler.rb --url=https://localhost/redmine --key=secretkey --project=myproject --tracker=Bug --category=some --status=New --priority=Urgent

It does create an issue. But the problem is that “Assigned to” field in the created issue is empty. I tried different combinations of user name, e-mail in the "To:" field of the e-mail, but whatever I try "Assigned to" is still empty. What is wrong?

Replies (12)

RE: Creating issues in a script - Added by Felix Schäfer over 9 years ago

The To: header is typically the address your mail goes to, a mail would land at the assignees address instead of in the redmine if you were to send it ;-) Have a look at the mail cheatsheet http://theadmin.org/articles/2010/06/08/redmine-email-keyword-cheatsheet/ , but you will want the "Assigned To:" keyword in the mail itself.

RE: Creating issues in a script - Added by Nox Metus over 9 years ago

Felix Schäfer wrote:

but you will want the "Assigned To:" keyword in the mail itself.

Hmm... I tried this:

{
cat <<EOF
From: user@server.com
To: user@server.com
Subject: Night build failed

Issue body

Assigned To: user
EOF
} | rdm-mailhandler.rb\
  --url=https://localhost/redmine\
  --allow-override="Assigned To"\
  --key=secretkey\
  --project=myproject\
  --tracker=Bug\
  --category=some\
  --status=New\
  --priority=Urgent

The result is still pretty much the same: the issue is unassigned.

RE: Creating issues in a script - Added by Nox Metus over 9 years ago

Just in case, my environment is:

About your application's environment
Ruby version              1.8.7 (x86_64-linux)
RubyGems version          1.3.5
Rails version             2.2.3
Active Record version     2.2.3
Action Pack version       2.2.3
Active Resource version   2.2.3
Action Mailer version     2.2.3
Active Support version    2.2.3
Edge Rails revision       unknown
Application root          /usr/share/redmine
Environment               production
Database adapter          postgresql
Database schema version   20100221100219

About your Redmine plugins
Redmine Code Review plugin   0.3.1

RE: Creating issues in a script - Added by Felix Schäfer over 9 years ago

I'm not sure when that keyword was added, but it's recentish, I'd say 1.0 at least. I think the assignee needs to be member of the project the issue is created in too. If it helps: that's feature's been added in r3764.

RE: Creating issues in a script - Added by Nox Metus over 9 years ago

Alright, that explains a lot. I am using 0.9.3.stable. Thanks for the help anyway :).
I'll wait for the next issue of Ubuntu, where the new Redmine will be included.

RE: Creating issues in a script - Added by Nox Metus over 9 years ago

Are there any other ways to create an issue from a script? Can I launch some Ruby Redmine's script other than rdm-mailhandler.rb to perform the task? So that it would be with the "Assigned to" field of course.

RE: Creating issues in a script - Added by Felix Schäfer over 9 years ago

If your ruby/rails is up to speed: script/runner and/or script/console, but I'd recommend against it if you haven't studied the IssuesController to enough extent that you know what you are doing.

RE: Creating issues in a script - Added by Nox Metus over 9 years ago

I am not only didn't study IssuesController, I didn't study ruby as well. I tried to decipher MailHandler. Can I just use

user = User.find_by_mail("user@server.com")
issue = Issue.new(:author => user, :project => project, :tracker => tracker, :category => category, :priority => priority, :assigned_to => user)
issue.save!

?

RE: Creating issues in a script - Added by Nox Metus over 9 years ago

I found the solution.

script/console production <<EOF
project = Project.find_by_identifier("myproject")
user = User.find_by_mail("user@server.com")
tracker = project.trackers.find_by_name("Bug")
category = project.issue_categories.find_by_name("some")
priority = IssuePriority.find_by_name("Urgent")
status = IssueStatus.find_by_name("New")
issue = Issue.new(
  :author => user,
  :project => project,
  :tracker => tracker,
  :category => category,
  :priority => priority,
  :assigned_to => user
)
issue.subject = "Night build has failed" 
issue.description = "Issue body" 
issue.save

RE: Creating issues in a script - Added by Felix Schäfer over 9 years ago

You might prefer to use:

#!/path/to/redmine/script/runner

some
commands
and
whatnot

Instead of throwing your script into the console (IIRC command line arguments are ARGV[0], ARGV[1] and so on, see http://linuxtopia.org/online_books/programming_books/ruby_tutorial/Ruby_and_Its_World_ARGV.html and google) :-) Other than that, it looks right, but as I said: not a supported way to enter issues.

RE: Creating issues in a script - Added by Nox Metus over 9 years ago

Thanks for the help :). I'll try out the runner script.

Can you give me a clue how to create an attachment? I tried

attachment = Attachment.create(:container => issue, :file => "/var/log/nightbuild.log", :author => user)

But it failed with a message: "undefined method `original_filename' for "/var/log/syslog":String". What class should I use for file? How to create it?

RE: Creating issues in a script - Added by Felix Schäfer over 9 years ago

You will need at least File.read("/path/to/file"), but it looks like it not only wants an object of type File, but of type UploadedTempFile, for which I couldn't find any satisfying way to instantiate one by just googling around real quick.

(1-12/12)