forked from agapoff/WikiLumper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
confluence.pm
103 lines (94 loc) · 3.06 KB
/
confluence.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package confluence;
#use strict;
use warnings;
use Data::Dumper;
use LWP::UserAgent;
use JSON;
use MIME::Base64;
use utf8;
my $ua;
sub new {
my $class = shift;
my %arg = @_;
return "No URL defined" unless $arg{Url};
my $basic = encode_base64($arg{Login}.":".$arg{Password});
my $self;
$ua = LWP::UserAgent->new;
$ua->timeout(60);
$ua->ssl_opts(
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE,
verify_hostname => 0
);
print $arg{Url}.'/rest/api/latest/content?spaceKey='.$arg{Space}.'&expand=ancestors'."\n"."Authorization => 'Basic '.$basic\n";
my $response = $ua->get($arg{Url}.'/rest/api/latest/content?spaceKey='.$arg{Space}.'&expand=ancestors', Authorization => 'Basic '.$basic);
if ($response->is_success) {
print "Logged to Confluence successfully\n" if ($arg{Debug});
$self = { basic => $basic, url => $arg{Url}, space => $arg{Space}, debug => $arg{Debug} };
my $answer = decode_json $response->decoded_content;
if (scalar @{$answer->{results}}) {
print "Space ".$arg{Space}." found \n";
foreach ( @{$answer->{results}} ) {
if ( ! scalar @{$_->{ancestors}} ) {
print "The space home is \"".$_->{title}."\" with id ".$_->{id}."\n";
$self->{home} = $_->{id};
last;
}
}
}
else {
print "Space ".$arg{Space}." not found \n";
return;
}
} else {
#print "Login to Confluence was unsuccessfull\n";
print $response->status_line;
return;
}
bless $self, $class;
}
sub createContent {
my $self = shift;
my %arg = @_;
my %data = %{$arg{Content}};
unless (defined $data{ancestors}) {
$data{ancestors} = [ { id => $self->{home} } ];
}
my $content = encode_json \%data;
print $content."\n" if ($self->{debug});
my $basic = ($arg{Login} && $arg{Password}) ? encode_base64($arg{Login}.":".$arg{Password}) : $self->{basic};
my $response = $ua->post($self->{url}.'/rest/api/content', Authorization => 'Basic '.$basic, 'Content-Type' => 'application/json; charset=UTF-8', 'Content' => $content);
if ($response->is_success) {
print $response->status_line."\n" if ($self->{debug});
print $response->decoded_content."\n" if ($self->{debug});
my $answer = decode_json $response->decoded_content;
return $answer->{id};
} else {
print "Got error while creating content\n";
print $response->status_line."\n";
print $response->decoded_content."\n";
}
return;
}
sub addAttachments {
my $self = shift;
my %arg = @_;
foreach my $file (@{$arg{Files}}) {
my $filesize = -s $file;
if ( $filesize > 10485760 ) {
print "The file size exceeds the maximum permitted size of 10485760 bytes";
return 1;
}
my $response = $ua->post($self->{url}.'/rest/api/latest/content/'.$arg{Id}.'/child/attachment', Authorization => 'Basic '.$self->{basic}, 'Content_Type' => 'multipart/form-data', Content => [file => [$file]], 'X-Atlassian-Token' => 'no-check');
if ($response->is_success) {
print $response->status_line."\n";
print $response->decoded_content;
} else {
print "Got error while uploading files\n";
print $response->status_line."\n";
print $response->decoded_content."\n";
return;
}
}
return 1;
}
1;