Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add systems table and associate it with projects

Because there are multiple defect systems. Only origin_id won't be
enough to identify a defect. So introduce system to the concept.

Change-Id: Ib2bbcead7828abc4154fb42533bc430a4f8c75fe
  • Loading branch information...
commit 78ba4f32e778d9dd27c927612b8adf975bc05582 1 parent 711cf40
Leo Li authored January 22, 2013
8  conf/config.yml
... ...
@@ -1,9 +1,11 @@
1 1
 ---
2 2
 pivotal:
3  
-- site_url: 'https://www.pivotaltracker.com/'
  3
+  type: pivotaltracker
  4
+  site_url: 'https://www.pivotaltracker.com/'
4 5
   token: token
5 6
 
6  
-jira:
7  
-- site_url: 'https://cloudfoundry.atlassian.net'
  7
+cf_public_jira:
  8
+  type: jira
  9
+  site_url: 'https://cloudfoundry.atlassian.net'
8 10
   username: user
9 11
   password: password
24  db/migrate/002_add_system.rb
... ...
@@ -0,0 +1,24 @@
  1
+class AddSystem < ActiveRecord::Migration
  2
+
  3
+  def self.up
  4
+
  5
+    create_table :systems do |t|
  6
+      t.string :system_type
  7
+      t.string :name, :unique => true
  8
+      t.string :url
  9
+      t.timestamps
  10
+    end
  11
+
  12
+    add_column :projects, :system_id, :int
  13
+    change_column :projects, :origin_id, :int
  14
+
  15
+  end
  16
+
  17
+  def self.down
  18
+    change_column :projects, :origin_id, :int, :unique => true
  19
+    remove_column :projects, :system_id
  20
+    drop_table :systems
  21
+  end
  22
+
  23
+end
  24
+
1  lib/defekts.rb
@@ -14,6 +14,7 @@
14 14
 require File.join( File.dirname(__FILE__), "defekts/models", "defekt" )
15 15
 require File.join( File.dirname(__FILE__), "defekts/models", "project" )
16 16
 require File.join( File.dirname(__FILE__), "defekts/models", "projectdefekt" )
  17
+require File.join( File.dirname(__FILE__), "defekts/models", "system" )
17 18
 
18 19
 module Defekts
19 20
 
28  lib/defekts/defekts_helper.rb
@@ -49,6 +49,34 @@ def self.get_severity
49 49
 
50 50
     end
51 51
 
  52
+    def self.sync_defkets(config, force=false)
  53
+      if Defekts.check_synced && !force
  54
+        return
  55
+      end
  56
+      config.keys.each do |site_name|
  57
+        system_config = config[site_name]
  58
+        system = sync_defekts_system(site_name, system_config)
  59
+        case system.system_type
  60
+        when 'pivotaltracker'
  61
+          PivotalHelper.sync(system, system_config, false)
  62
+        when 'jira'
  63
+          JiraHelper.sync(system, system_config, false)
  64
+        end
  65
+      end
  66
+    end
  67
+
  68
+    def self.sync_defekts_system(site_name, system_config)
  69
+      system =  System.find_by_name(site_name)
  70
+      if system.nil?
  71
+        system = System.create(
  72
+          :system_type => system_config['type'],
  73
+          :name => site_name,
  74
+          :url => system_config['site_url']
  75
+        )
  76
+      end
  77
+      system
  78
+    end
  79
+
52 80
   end
53 81
 
54 82
 end
23  lib/defekts/jira_helper.rb
@@ -6,29 +6,29 @@ module Defekts
6 6
 
7 7
   module JiraHelper
8 8
 
9  
-    def self.sync(force=false, site_url, username, password)
10  
-      if Defekts.check_synced and !force
11  
-        return
12  
-      end
  9
+    def self.sync(system, system_config, force=false)
13 10
       options = {
14  
-                  :site     => site_url,
15  
-                  :username => username,
16  
-                  :password => password,
  11
+                  :site     => system.url,
  12
+                  :username => system_config['username'],
  13
+                  :password => system_config['password'],
17 14
                   :context_path => '',
18 15
                   :auth_type => :basic
19 16
                 }
20 17
       client = JIRA::Client.new(options)
21 18
       @projects = client.Project.all
22 19
       @projects.each do |p|
23  
-        project = Project.find_by_origin_id(p.id)
  20
+        project = Project.find_by_origin_id_and_system_id(p.id, system.id)
24 21
         if project.nil?
25  
-          project = Project.create(:name => p.name, :origin_id => p.id)
  22
+          project = Project.create(
  23
+            :name => p.name,
  24
+            :origin_id => p.id,
  25
+            :system_id => system.id)
26 26
         end
27 27
 
28 28
         @defeckts = p.issues
29 29
         @defeckts.each do |d|
30 30
           severity = d.priority.id
31  
-          defekt = Defekt.find_by_origin_id d.id
  31
+          defekt = Defekt.find_by_origin_id_and_project_id(d.id, project.id)
32 32
           if defekt.nil?
33 33
             ndefekt = Defekt.create(
34 34
               :project_id => project.id,
@@ -41,7 +41,8 @@ def self.sync(force=false, site_url, username, password)
41 41
               :state => d.status.name,
42 42
               :severity => severity,
43 43
               :owner => d.assignee.displayName,
44  
-              :reporter => d.reporter.displayName
  44
+              :reporter => d.reporter.displayName,
  45
+              :project_id => project.id
45 46
              )
46 47
           else
47 48
               defekt.title    = d.summary
3  lib/defekts/models/defekt.rb
@@ -8,5 +8,4 @@ def set_day
8 8
     self.day = self.creation.to_date
9 9
   end
10 10
 
11  
-end
12  
-
  11
+end
1  lib/defekts/models/project.rb
... ...
@@ -1,5 +1,6 @@
1 1
 class Project < ActiveRecord::Base
2 2
 
3 3
   has_many :defekts
  4
+  belongs_to :systems
4 5
 
5 6
 end
4  lib/defekts/models/system.rb
... ...
@@ -0,0 +1,4 @@
  1
+class System < ActiveRecord::Base
  2
+  has_many :projects
  3
+end
  4
+
19  lib/defekts/pivotal_helper.rb
@@ -17,25 +17,21 @@ def self.get_severity(labels)
17 17
 
18 18
     end
19 19
 
20  
-    def self.sync(force=false, token)
21  
-
22  
-      if Defekts.check_synced and !force
23  
-        return
24  
-      end
25  
-
26  
-      PivotalTracker::Client.token = token
  20
+    def self.sync(system, system_config, force=false)
  21
+      PivotalTracker::Client.token = system_config['token']
27 22
 
28 23
       @projects = PivotalTracker::Project.all
29 24
 
30 25
       @projects.each do |p|
31 26
 
32  
-        project = Project.find_by_origin_id(p.id)
  27
+        project = Project.find_by_origin_id_and_system_id(p.id, system.id)
33 28
 
34 29
         if project.nil?
35 30
 
36 31
           project = Project.create(
37 32
             :name => p.name,
38  
-            :origin_id => p.id )
  33
+            :origin_id => p.id,
  34
+            :system_id => system.id)
39 35
 
40 36
         end
41 37
 
@@ -45,7 +41,7 @@ def self.sync(force=false, token)
45 41
 
46 42
           severity = get_severity(d.labels)
47 43
 
48  
-          defekt = Defekt.find_by_origin_id(d.id)
  44
+          defekt = Defekt.find_by_origin_id_and_project_id(d.id, project.id)
49 45
 
50 46
           if defekt.nil?
51 47
 
@@ -59,7 +55,8 @@ def self.sync(force=false, token)
59 55
               :state => d.current_state,
60 56
               :severity => severity,
61 57
               :owner => d.owned_by,
62  
-              :reporter => d.requested_by )
  58
+              :reporter => d.requested_by,
  59
+              :project_id => project.id )
63 60
 
64 61
           else
65 62
 
12  lib/defekts/server.rb
@@ -6,17 +6,7 @@ module Defekts
6 6
   get "/" do
7 7
     defekt_config = settings.defekt_config
8 8
 
9  
-    # need to change this to more generic way once class pattern is redefined.
10  
-    if !defekt_config['pivotal'].nil?
11  
-      defekt_config['pivotal'].each do |site|
12  
-        PivotalHelper.sync(false, site['token'])
13  
-      end
14  
-    end
15  
-    if !defekt_config['jira'].nil?
16  
-      defekt_config['jira'].each do |site|
17  
-        JiraHelper.sync(false, site['site_url'], site['username'], site['password'])
18  
-      end
19  
-    end
  9
+    Analysis.sync_defkets defekt_config
20 10
 
21 11
     #p = Project.create( :name => "frameworks", :origin_id => "72352" )
22 12
     #po = Project.find_by_origin_id("72351")

Git Notes

review

Code-Review+2: Jacky Li <jli@rbcon.com>
Verified+1: Leo Li <lileo@rbcon.com>
Submitted-by: Leo Li <lileo@rbcon.com>
Submitted-at: Thu, 24 Jan 2013 02:35:46 +0000
Reviewed-on: http://reviews.cloudfoundry.org/13509
Project: sauron
Branch: refs/heads/master

0 notes on commit 78ba4f3

Please sign in to comment.
Something went wrong with that request. Please try again.