2
0

filebug.pl 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #!/usr/bin/perl
  2. #use strict;
  3. use Getopt::Long qw(GetOptions);
  4. use Term::ReadKey;
  5. use JIRA::REST;
  6. use Data::Dumper;
  7. my $editor = $ENV{"EDITOR"} || $ENV{"VISUAL"} || `which emacs` || `which vi`;
  8. my $default_versions = "1.9 1.8";
  9. my $default_components = "freeswitch-core";
  10. my $desc_head = "; Enter the description lines beginning with a ; will be ignored.\n";
  11. chomp($editor);
  12. sub getpass {
  13. ReadMode( "noecho");
  14. print "Password: ";
  15. chomp (my $pwd = <STDIN>);
  16. ReadMode ("original");
  17. return $pwd;
  18. }
  19. sub getfield {
  20. my $prompt = shift;
  21. my $default = shift;
  22. print $prompt . ($default ? "[$default]: " : "");
  23. chomp (my $data = <STDIN>);
  24. if (!$data) {
  25. $data = $default;
  26. }
  27. return $data;
  28. }
  29. sub get_text {
  30. my $text = shift;
  31. my $notes = shift;
  32. my @chars = ("A".."Z", "a".."z");
  33. my $string;
  34. $string .= $chars[rand @chars] for 1..8;
  35. if ($text || $notes) {
  36. open O, ">/tmp/TEXT.$string";
  37. if ($notes) {
  38. print O $notes;
  39. }
  40. if ($text) {
  41. print O $text;
  42. }
  43. close O;
  44. }
  45. system("$editor /tmp/TEXT.$string");
  46. my $newtext = `cat /tmp/TEXT.$string | grep -v "^\\;"`;
  47. unlink("/tmp/TEXT.$string");
  48. return $newtext;
  49. }
  50. my %opts;
  51. my $hashtxt = `git log -1 --oneline 2>/dev/null`;
  52. my ($hash) = split(" ", $hashtxt);
  53. GetOptions(
  54. 'bug=s' => \$opts{bug},
  55. 'attach' => \$opts{attach},
  56. 'comment=s' => \$opts{comment},
  57. 'project=s' => \$opts{project},
  58. 'summary=s' => \$opts{summary},
  59. 'desc=s' => \$opts{desc},
  60. 'components=s' => \$opts{components},
  61. 'hash=s' => \$opts{hash},
  62. 'user=s' => \$opts{user},
  63. 'pass=s' => \$opts{pass},
  64. 'type=s' => \$opts{type},
  65. 'versions=s' => \$opts{versions},
  66. 'noedit' => \$opts{noedit},
  67. 'terse' => \$opts{terse},
  68. 'debug' => \$opts{debug},
  69. ) or die "Usage: $0 -summary <summary> -desc <desc> [-debug] ....\n";
  70. $opts{project} or $opts{project} = "FS";
  71. if ($opts{versions}) {
  72. $opts{versions_array} = [map {{name => $_}} split(" ", $opts{versions})];
  73. } else {
  74. $opts{versions_array} = [map {{name => $_}} ($default_versions)];
  75. $opts{versions} = $default_versions;;
  76. }
  77. if ($opts{components}) {
  78. if ($opts{components} =~ /,/) {
  79. $opts{components_array} = [map {{name => $_}} split(" ", $opts{components})];
  80. } else {
  81. $opts{components_array} = [map {{name => $_}} split(" ", $opts{components})];
  82. }
  83. } else {
  84. $opts{components_array} = [map {{name => $_}} ($default_components)];
  85. $opts{components} = $default_components;
  86. }
  87. if (!$opts{user}) {
  88. $opts{user} = getfield("User: ");
  89. }
  90. if (!$opts{pass} && !$opts{debug}) {
  91. $opts{pass} = getpass();
  92. print "\n";
  93. }
  94. my $jira;
  95. my $issue;
  96. if (!$opts{debug}) {
  97. $jira = JIRA::REST->new('https://freeswitch.org/jira', $opts{user}, $opts{pass}) or die "login incorrect:";
  98. $issue = $jira->GET("/issue/FS-7985") or die "login incorrect:";
  99. }
  100. if ($opts{bug}) {
  101. if ($opts{comment}) {
  102. if ($opts{comment} eq "edit") {
  103. $opts{comment} = get_text();
  104. }
  105. my $input = {
  106. update => {
  107. comment =>
  108. [{
  109. add => {
  110. body => $opts{comment}
  111. }
  112. }
  113. ]
  114. }
  115. };
  116. $jira->PUT("/issue/" . $opts{bug}, undef, $input);
  117. print "Comment Posted.\n";
  118. }
  119. if ($opts{attach}) {
  120. $jira->attach_files($opts{bug}, @ARGV);
  121. printf "%d file%s attached.\n", scalar @ARGV, scalar @ARGV == 1 ? "" : "s";
  122. }
  123. if ($opts{versions_array}) {
  124. $input = {
  125. update => {
  126. fixVersions => [
  127. {set => $opts{versions_array}}
  128. ]
  129. }
  130. };
  131. $jira->PUT("/issue/" . $opts{bug}, undef, $input);
  132. }
  133. exit;
  134. }
  135. #print $issue->{key};
  136. #exit;
  137. if (!$opts{type}) {
  138. $opts{type} = "Bug";
  139. }
  140. if (!$opts{hash}) {
  141. $opts{hash} = $hash;
  142. if (!$opts{hash}) {
  143. $opts{hash} = "N/A";
  144. }
  145. }
  146. if (!$opts{terse}) {
  147. $opts{project} = getfield("Project: ", $opts{project});
  148. $opts{type} = getfield("Type: ", $opts{type});
  149. $opts{versions} = getfield("Versions: ", $opts{versions});
  150. $opts{versions_array} = [map {{name => $_}} split(" ", $opts{versions})];
  151. $opts{summary} = getfield("Summary: ", $opts{summary});
  152. $opts{components} = getfield("Components: ", $opts{components});
  153. if ($opts{components} =~ /,/) {
  154. $opts{components_array} = [map {{name => $_}} split(",", $opts{components})];
  155. } else {
  156. $opts{components_array} = [map {{name => $_}} split(" ", $opts{components})];
  157. }
  158. $opts{hash} = getfield("GIT Hash: ", $opts{hash});
  159. if ($opts{noedit}) {
  160. $opts{desc} = getfield("Description: ", $opts{desc}, $desc_head);
  161. } else {
  162. $opts{desc} = get_text($opts{desc}, $desc_head);
  163. }
  164. }
  165. if (!$opts{desc}) {
  166. if ($opts{noedit}) {
  167. $opts{desc} = getfield("Description: ", $opts{desc});
  168. } else {
  169. $opts{desc} = get_text($opts{desc}, $desc_head);
  170. }
  171. if (!$opts{desc}) {
  172. die "missing desc:";
  173. }
  174. }
  175. if (!$opts{summary}) {
  176. $opts{summary} = getfield("Summary: ", $opts{summary});
  177. if (!$opts{summary}) {
  178. die "Summary is mandatory.";
  179. }
  180. }
  181. my $input = {
  182. fields => {
  183. project => { key => $opts{project} },
  184. issuetype => { name => $opts{type} },
  185. summary => $opts{summary},
  186. description => $opts{desc},
  187. customfield_10024 => $opts{hash},
  188. customfield_10025 => $opts{hash},
  189. components => $opts{components_array},
  190. versions => $opts{versions_array}
  191. },
  192. };
  193. if ($opts{debug}) {
  194. print Dumper \%opts;
  195. print Dumper $input;
  196. } else {
  197. $issue = $jira->POST('/issue', undef, $input) or die "Issue was not created:";
  198. print "Issue Posted: " . $issue->{key} . "\n";
  199. if ($opts{versions_array}) {
  200. $input = {
  201. update => {
  202. fixVersions => [
  203. {set => $opts{versions_array}}
  204. ]
  205. }
  206. };
  207. $jira->PUT("/issue/" . $issue->{key}, undef, $input);
  208. print "Fix versions updated for issue " . $issue->{key} . "\n";
  209. }
  210. if ($opts{attach}) {
  211. $jira->attach_files($issue->{key}, @ARGV);
  212. printf "%d file%s attached.\n", scalar @ARGV, scalar @ARGV == 1 ? "" : "s";
  213. }
  214. }