Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redmine: Post attachments using API (or not)

Is there any way to post attachments to issues in Redmine from an outside PHP script? If API doesnt support this (i didnt find anything on the wiki) then is there another way?

So far i have tried only this, but it doesnt work:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://192.168.1.115/redmine/login");
$useragent="Mozilla/5.0 (Windows NT 5.1; rv:8.0a2) Gecko/20110927 Firefox/8.0a2";
curl_setopt($ch, CURLOPT_COOKIEJAR, "C:\\xampp\\htdocs\\redmine\\cookie.txt");
curl_close($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);        
curl_setopt($ch, CURLOPT_REFERER, "http://192.168.1.115/redmine/login");
curl_setopt($ch, curlopt_post, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, "C:\\xampp\\htdocs\\redmine\\cookie.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$token = getToken();
$data2 = array(
    'password' => '1234',
    'back_url' => 'http%3A%2F%2F192.168.1.115%2Fredmine%2F',
    'username' => 'admin',
    'authenticity_token' => $token,
    'login' => 'Login Β»'
);
print_r($data2);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data2);
$out = curl_exec($ch);
echo $out;

curl_exec($ch);
curl_setopt($ch, CURLOPT_COOKIEJAR, "C:\\xampp\\htdocs\\redmine\\cookie2.txt"); 
$useragent="Mozilla/5.0 (Windows NT 5.1; rv:8.0a2) Gecko/20110927 Firefox/8.0a2";
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Redmine-API-Key: 104a2e2b72d4f5d184775d8324c2e0cb6386815e'));
curl_setopt($ch, CURLOPT_URL, "http://192.168.1.115/redmine/projects/lvx/issues/new");
curl_setopt($ch, CURLOPT_COOKIEFILE, "C:\\xampp\\htdocs\\redmine\\cookie2.txt");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = array(
'key' => '104a2e2b72d4f5d184775d8324c2e0cb6386815e',
'is_private' => '0',
'tracker_id' => '1',
'subject' => 'This bug was sent from my API',
'description' => 'this is a description',
'status_id' => '0',
'priority_id' => '4',
'assigned_to_id' => '',
'parent_issue_id' => '' 
);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$out = curl_exec($ch);
echo $out;
curl_close($ch);


    function getToken(){
$url = "http://192.168.1.115/redmine/login"; 
$input = @file_get_contents($url) or die("Could not access file: $url"); 
$regexp = "<input name=\"authenticity_token\" type=\"hidden\" value=\"(.+?)\" />"; 
if(preg_match_all("$regexp", $input, $matches)) 
{ 
    return $matches[1][0];
}
   }
like image 318
Jim Avatar asked Mar 27 '26 15:03

Jim


1 Answers

You can chain method attach_files. There is example from plugin which pastes screenshot to wiki-page/issue.

module RedmineScreenshotPaste
    def self.included(base)
        base.send(:extend, ClassMethods)
        base.class_eval do
            class << self
            alias_method_chain :attach_files, :screenshot
        end
    end
end

module ClassMethods
    def attach_files_with_screenshot(obj, attachments)
        if attachments.is_a?(Hash)
            screenshot = attachments['screenshot']
            if screenshot.is_a?(Hash)
                file = UploadedScreenshot.new(screenshot.delete('content'),
                                    screenshot.delete('name'))
                screenshot['file'] = file
            end
        end
        attach_files_without_screenshot(obj, attachments)
        end
    end
end
like image 141
eliriand Avatar answered Mar 30 '26 04:03

eliriand



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!