fixbug.pl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #!/usr/bin/perl
  2. use XML::Simple;
  3. use Data::Dumper;
  4. use Getopt::Long qw(GetOptions);
  5. use Term::ReadKey;
  6. use JIRA::REST;
  7. my %opts;
  8. sub getpass {
  9. ReadMode( "noecho");
  10. print "Password: ";
  11. chomp (my $pwd = <STDIN>);
  12. ReadMode ("original");
  13. return $pwd;
  14. }
  15. sub getfield {
  16. my $prompt = shift;
  17. my $default = shift;
  18. print $prompt . ($default ? "[$default]: " : "");
  19. chomp (my $data = <STDIN>);
  20. if (!$data) {
  21. $data = $default;
  22. }
  23. return $data;
  24. }
  25. GetOptions(
  26. 'bug=s' => \$opts{bug},
  27. 'msg=s' => \$opts{msg},
  28. 'user=s' => \$opts{user},
  29. 'pass=s' => \$opts{pass},
  30. 'debug' => \$opts{debug},
  31. 'noresolve' => \$opts{noresolve},
  32. 'append=s' => \$opts{append},
  33. 'comment=s' => \$opts{comment},
  34. 'versions=s' => \$opts{versions},
  35. 'author=s' => \$opts{author},
  36. 'auth' => \$opts{auth}
  37. ) or die "Usage: $0 -bug <bug-id> [--auth] [-m [edit|<msg>]] [--append <msg>] [--debug] <files>\n";
  38. $opts{bug} or $opts{bug} = shift;
  39. if ($opts{versions}) {
  40. $opts{auth} = 1;
  41. $opts{versions_array} = [map {{name => $_}} split(" ", $opts{versions})];
  42. }
  43. my $url = "https://freeswitch.org/jira/si/jira.issueviews:issue-xml/$opts{bug}/$opts{bug}.xml";
  44. my $cmd;
  45. my $prog = `which curl` || `which wget`;
  46. my $auto = 1;
  47. my $post = " \#resolve";
  48. my $component;
  49. my $summary;
  50. chomp $prog;
  51. if ($opts{auth}) {
  52. if (!$opts{user}) {
  53. $opts{user} = getfield("User: ");
  54. }
  55. if (!$opts{pass}) {
  56. $opts{pass} = getpass();
  57. print "\n";
  58. }
  59. $jira = JIRA::REST->new('https://freeswitch.org/jira', $opts{user}, $opts{pass}) or die "login incorrect:";
  60. $issue = $jira->GET("/issue/FS-7985") or die "login incorrect:";
  61. my $issue = $jira->GET("/issue/" . $opts{bug});
  62. $component = join(",", map {$_->{name}} @{$issue->{fields}->{components}});
  63. $summary = $issue->{fields}->{summary};
  64. if ($opts{versions_array}) {
  65. $input = {
  66. update => {
  67. fixVersions => [
  68. {set => $opts{versions_array}}
  69. ]
  70. }
  71. };
  72. $jira->PUT("/issue/" . $opts{bug}, undef, $input);
  73. }
  74. } else {
  75. $prog || die "missing url fetch program, install curl or wget";
  76. if ($prog =~ /wget/) {
  77. $cmd = "$prog -O -";
  78. } else {
  79. $cmd = $prog;
  80. }
  81. my $xml = `$cmd $url 2>/dev/null`;
  82. if ($opts{debug}) {
  83. print "URL $url\n";
  84. print $xml;
  85. }
  86. my $xs= new XML::Simple;
  87. my $r = $xs->XMLin($xml);
  88. $summary = $r->{channel}->{item}->{summary};
  89. $summary =~ s/\"/\\"/g;
  90. $component = $r->{channel}->{item}->{component};
  91. if(ref($component) eq 'ARRAY') {
  92. $component = join(",", @{$component});
  93. }
  94. $component =~ s/\"/\\"/g;
  95. }
  96. if ($opts{noresolve}) {
  97. $post = "";
  98. }
  99. if ($opts{msg} eq "edit") {
  100. $auto = 0;
  101. $opts{msg} = undef;
  102. open T, ">/tmp/$opts{bug}.tmp";
  103. print T "$opts{bug}${post} [$summary]\n\n---Cut this line to confirm commit.....";
  104. close T;
  105. }
  106. my $args = join(" ", @ARGV);
  107. my $gitcmd;
  108. if ($opts{append}) {
  109. $opts{append} = " -- " . $opts{append};
  110. }
  111. if ($opts{comment}) {
  112. $opts{append} .= " #comment " . $opts{comment};
  113. }
  114. if ($auto) {
  115. if ($opts{msg}) {
  116. $opts{msg} =~ s/%s/$summary/;
  117. $opts{msg} =~ s/%b/$opts{bug}/;
  118. $opts{msg} =~ s/%c/$component/;
  119. $gitcmd = "git commit $args -m \"$opts{msg}$opts{append}\"";
  120. } else {
  121. $gitcmd = "git commit $args -m \"$opts{bug}: [$component] ${summary}$opts{append}${post}\"";
  122. }
  123. } else {
  124. $gitcmd = "git commit $args -t /tmp/$opts{bug}.tmp";
  125. }
  126. if ($opts{author}) {
  127. $gitcmd .= " --author \"$opts{author}\"";
  128. }
  129. if ($opts{debug}) {
  130. print "CMD: $gitcmd\n";
  131. } else {
  132. system $gitcmd;
  133. }
  134. unlink("/tmp/$opts{bug}.tmp");