summaryrefslogtreecommitdiff
blob: 49df77baf5e362fb9228c1d3f8fb401a1fc494dc (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# ===GLSAMaker v2
#  Copyright (C) 2009 Alex Legler <a3li@gentoo.org>
#  Copyright (C) 2009 Pierre-Yves Rofes <py@gentoo.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# For more information, see the LICENSE file.

require 'nokogiri'
require 'fastercsv'
require 'fileutils'

module Bugzilla ; end

%w[ comment history bug ].each {|lib| require File.join(File.dirname(__FILE__), "bugzilla/#{lib}")}

# Bugzilla module
module Bugzilla
  # Looks on bugs.gentoo.org for all bugs that are in the [glsa] state
  module_function
  def find_glsa_bugs
    url="http://bugs.gentoo.org/buglist.cgi?bug_file_loc=&bug_file_loc_type=allwordssubstr&bug_id=&bug_status=UNCONFIRMED&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&bugidtype=include&chfieldfrom=&chfieldto=Now&chfieldvalue=&component=Vulnerabilities&email1=&email2=&emailassigned_to1=1&emailassigned_to2=1&emailcc2=1&emailreporter2=1&emailtype1=substring&emailtype2=substring&field-1-0-0=product&field-1-1-0=component&field-1-2-0=bug_status&field-1-3-0=status_whiteboard&field0-0-0=noop&keywords=&keywords_type=allwords&long_desc=&long_desc_type=substring&product=Gentoo%20Security&query_format=advanced&remaction=&short_desc=&short_desc_type=allwordssubstr&status_whiteboard=%5Bglsa%5D&status_whiteboard_type=substring&type-1-0-0=anyexact&type-1-1-0=anyexact&type-1-2-0=anyexact&type-1-3-0=substring&type0-0-0=noop&value-1-0-0=Gentoo%20Security&value-1-1-0=Vulnerabilities&value-1-2-0=UNCONFIRMED%2CNEW%2CASSIGNED%2CREOPENED&value-1-3-0=%5Bglsa%5D&value0-0-0=&votes=&ctype=csv"
    bugs = []

    FasterCSV.parse(Glsamaker::HTTP.get(url)) do |row|
      bugs << { "bug_id" => row.shift,
        "severity" => row.shift,
        "priority" => row.shift,
        "os" => row.shift,
        "assignee" => row.shift,
        "status" => row.shift,
        "resolution" => row.shift,
        "summary" => row.shift
      }
    end

    bugs.shift
    bugs
  end

  def add_comment(bug, comment)
    Float(bug)
    cookie_file = File.join(RAILS_ROOT, 'tmp', 'bugzie-cookies.yaml')

    a = Mechanize.new { |agent|
      agent.user_agent = "GLSAMaker/#{GLSAMAKER_VERSION} (http://security.gentoo.org/)"
    }

    a.cookie_jar.load(cookie_file)

    a.get("https://bugs.gentoo.org/show_bug.cgi?id=%s" % bug) do |page|
      unless page.body.include? '*+*GLSAMAKER-LOGGEDIN*+*'
        log_in()
        a.cookie_jar.load(cookie_file)
      end

      post_result = page.form_with(:name => 'changeform') do |form|
        form.comment = comment
      end.submit

      raise unless post_result.body.include? "Changes submitted for bug"
    end
  end

  def update_bug(bug, changes = {})
    Rails.logger.debug "Called Bugzilla.update_bug"
    Float(bug)
    cookie_file = File.join(RAILS_ROOT, 'tmp', 'bugzie-cookies.yaml')

    a = Mechanize.new { |agent|
      agent.user_agent = "GLSAMaker/#{GLSAMAKER_VERSION} (http://security.gentoo.org/)"
    }

    log_in unless File.exist? cookie_file
    a.cookie_jar.load(cookie_file)

    a.get("https://bugs.gentoo.org/show_bug.cgi?id=%s" % bug) do |page|
      unless page.body.include? '*+*GLSAMAKER-LOGGEDIN*+*'
        Rails.logger.debug "Not logged in, doing that now."
        log_in()
        a.cookie_jar.load(cookie_file)
      end

      post_result = page.form_with(:name => 'changeform') do |form|
        form.alias = changes[:alias] if changes.has_key?(:alias)
        form.newcc = changes[:newcc] if changes.has_key?(:newcc)
        form.bug_file_log = changes[:url] if changes.has_key?(:url)
        form.short_desc = changes[:summary] if changes.has_key?(:summary)
        form.status_whiteboard = changes[:whiteboard] if changes.has_key?(:whiteboard)
        form.keywords = changes[:keywords] if changes.has_key?(:keywords)
        form.dependson = changes[:depends] if changes.has_key?(:depends)
        form.blocked = changes[:blocks] if changes.has_key?(:blocks)
        form.comment = changes[:comment] if changes.has_key?(:comment)
        form.knob = changes[:knob] if changes.has_key?(:knob)
        form.assigned_to = changes[:assignee] if changes.has_key?(:assignee)
      end.submit

      raise unless post_result.body.include? "Changes submitted for bug"
    end
  end
  
  # Files a bug, and returns the id of the filed bug
  def file_bug(data)
    Rails.logger.debug "Called Bugzilla.file_bug"
    cookie_file = File.join(RAILS_ROOT, 'tmp', 'bugzie-cookies.yaml')

    a = Mechanize.new { |agent|
      agent.user_agent = "GLSAMaker/#{GLSAMAKER_VERSION} (http://security.gentoo.org/)"
    }

    log_in unless File.exist? cookie_file
    a.cookie_jar.load(cookie_file)

    Rails.logger.debug "URL: https://bugs.gentoo.org/enter_bug.cgi?product=%s" % data[:product]
    a.get("https://bugs.gentoo.org/enter_bug.cgi?product=%s" % data[:product]) do |page|
      unless page.body.include? '*+*GLSAMAKER-LOGGEDIN*+*'
        Rails.logger.debug "Not logged in, doing that now."
        log_in()
        a.cookie_jar.load(cookie_file)
      end

      post_result = page.form_with(:name => 'Create') do |form|
        form.component = data[:component] if data.has_key?(:component)
        form.assigned_to = data[:assignee] if data.has_key?(:assignee)
        form.cc = data[:cc] if data.has_key?(:cc)
        form.alias = data[:alias] if data.has_key?(:alias)
        form.bug_file_loc = data[:url] if data.has_key?(:url)
        form.short_desc = data[:summary] if data.has_key?(:summary)
        form.comment = data[:comment] if data.has_key?(:comment)
        form.keywords = data[:keywords] if data.has_key?(:keywords)
        form.dependson = data[:depends] if data.has_key?(:depends)
        form.blocked = data[:blocks] if data.has_key?(:blocks)
        form.bug_severity = data[:severity] if data.has_key?(:severity)
      end.submit

      raise unless post_result.body.include? "has been added to the database"
      
      post_result.body =~ /(\d+) has been added to the database/
      return $1.to_i
    end
  end  

  def log_in
    Rails.logger.debug "Called Bugzilla.log_in"
    raise unless GLSAMAKER_BUGZIE_USER and GLSAMAKER_BUGZIE_PW
    a = Mechanize.new { |agent|
      agent.user_agent = "GLSAMaker/#{GLSAMAKER_VERSION} (http://security.gentoo.org/)"
    }

    a.get("https://bugs.gentoo.org/index.cgi?GoAheadAndLogIn=1") do |page|
      login_result = page.form_with(:name => 'login') do |form|
        form['Bugzilla_login'] = GLSAMAKER_BUGZIE_USER
        form['Bugzilla_password'] = GLSAMAKER_BUGZIE_PW
      end.submit

      if login_result.body.include? '*+*GLSAMAKER-LOGGEDIN*+*'
        Rails.logger.debug "Successfully logged in."
        cookie_file = File.join(RAILS_ROOT, 'tmp', 'bugzie-cookies.yaml')
        FileUtils.touch(cookie_file)
        File.chmod(0600, cookie_file)
        a.cookie_jar.save_as(cookie_file)
      else
        Rails.logger.warn "Failure logging in."
      end
    end

  end
end