Class: GHTUpdateRepo

Inherits:
GHTorrent::Command show all
Includes:
GHTorrent::Logging, GHTorrent::Persister, GHTorrent::Retriever, GHTorrent::Settings
Defined in:
lib/ghtorrent/commands/ght_update_repo.rb

Constant Summary

Constants included from GHTorrent::Settings

GHTorrent::Settings::CONFIGKEYS, GHTorrent::Settings::DEFAULTS

Constants included from GHTorrent::Persister

GHTorrent::Persister::ADAPTERS

Instance Method Summary collapse

Methods included from GHTorrent::Logging

#debug, #error, #info, #loggerr, #warn

Methods included from GHTorrent::Settings

#config, #merge, #merge_config_values, #override_config, #settings

Methods included from GHTorrent::Utils

included, #read_value, #user_type, #write_value

Methods included from GHTorrent::Persister

#connect, #disconnect

Methods included from GHTorrent::Retriever

#get_event, #get_events, #get_repo_events, #retrieve_commit, #retrieve_commit_comment, #retrieve_commit_comments, #retrieve_commits, #retrieve_fork, #retrieve_forks, #retrieve_issue, #retrieve_issue_comment, #retrieve_issue_comments, #retrieve_issue_event, #retrieve_issue_events, #retrieve_issue_labels, #retrieve_issues, #retrieve_languages, #retrieve_org, #retrieve_org_members, #retrieve_orgs, #retrieve_pull_req_comment, #retrieve_pull_req_comments, #retrieve_pull_req_commits, #retrieve_pull_request, #retrieve_pull_requests, #retrieve_repo, #retrieve_repo_collaborator, #retrieve_repo_collaborators, #retrieve_repo_label, #retrieve_repo_labels, #retrieve_user_byemail, #retrieve_user_byusername, #retrieve_user_follower, #retrieve_user_followers, #retrieve_user_following, #retrieve_watcher, #retrieve_watchers

Methods included from GHTorrent::APIClient

#api_request, #num_pages, #paged_api_request

Methods inherited from GHTorrent::Command

#command_name, #override_config, #process_options, #queue_client, run, #version

Instance Method Details

#date(arg) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/ghtorrent/commands/ght_update_repo.rb', line 35

def date(arg)
  if arg.class != Time
    Time.parse(arg)#.to_i
  else
    arg
  end
end

#dbObject



31
32
33
# File 'lib/ghtorrent/commands/ght_update_repo.rb', line 31

def db
  @db ||= @ght.get_db
end

#goObject



114
115
116
117
118
119
120
121
122
123
# File 'lib/ghtorrent/commands/ght_update_repo.rb', line 114

def go

  @ght ||= GHTorrent::Mirror.new(settings)

  unless ARGV[1].nil?
    process_project(ARGV[0], ARGV[1])
    exit(0)
  end

end

#persisterObject



26
27
28
29
# File 'lib/ghtorrent/commands/ght_update_repo.rb', line 26

def persister
  @persister ||= connect(:mongo, settings)
  @persister
end

#prepare_options(options) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/ghtorrent/commands/ght_update_repo.rb', line 12

def prepare_options(options)
  options.banner "Updates the deleted field in the project table with current data\n\n\#{command_name} owner repo\n\n  BANNER\nend\n"

#process_project(owner, name) ⇒ Object



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
# File 'lib/ghtorrent/commands/ght_update_repo.rb', line 74

def process_project(owner, name)
  @ght.transaction do

    in_mongo = persister.find(:repos, {'owner.login' => owner, 'name' => name })
    on_github = api_request(ghurl ("repos/#{owner}/#{name}"))

    unless in_mongo.empty? and on_github.empty?
      in_mysql = retrieve_repo(owner, name)
    end

    if in_mongo.empty?
      if on_github.empty?
        if in_mysql.nil?
          # Project does not exist anywhere
          warn "Repo #{owner}/#{name} does not exist in MySQL"
        else
          # Project exists in MySQL but not on Github or Mongo
          # Mark it as deleted
          set_deleted(owner, name)
        end
      else
        # Project does not exist in Mongo, but exists in Github
        if in_mysql.nil?
          warn "Repo #{owner}/#{name} does not exist in MySQL"
        else
          # The retrieval process already added it to Mongo, so update MySQL
          update_mysql(owner, name, in_mysql)
        end
      end
    else
      if on_github.empty?
        # Project was deleted on Github. Mark it as deleted.
        set_deleted(owner, name)
      else
        update_mysql(owner, name, in_mysql)
      end
    end
  end
end

#set_deleted(owner, repo) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/ghtorrent/commands/ght_update_repo.rb', line 43

def set_deleted(owner, repo)
  db.from(:projects, :users).\
     where(:projects__owner_id => :users__id).\
     where(:users__login => owner).\
     where(:projects__name => repo).\
     update(:projects__deleted => true)
  info("Project #{owner}/#{repo} marked as deleted")
end

#update_mysql(owner, repo, retrieved) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ghtorrent/commands/ght_update_repo.rb', line 52

def update_mysql(owner, repo, retrieved)

  parent = unless retrieved['parent'].nil?
             @ght.ensure_repo(retrieved['parent']['owner']['login'],
                              retrieved['parent']['name'])
           end

  db.from(:projects, :users).\
     where(:projects__owner_id => :users__id).\
     where(:users__login => owner).\
     where(:projects__name => repo).\
     update(
       :projects__url => retrieved['url'],
       :projects__description => retrieved['description'],
       :projects__language => retrieved['language'],
       :projects__created_at => date(retrieved['created_at']),
       :projects__forked_from => unless parent.nil? then parent[:id] end)
  debug("Repo #{owner}/#{repo} updated")

  @ght.ensure_languages(owner, repo)
end

#validateObject



21
22
23
24
# File 'lib/ghtorrent/commands/ght_update_repo.rb', line 21

def validate
  super
  Trollop::die "Takes two arguments" if ARGV.size == 1
end