Class: UpstreamWatchr::GitLabWatchr

Inherits:
Object
  • Object
show all
Defined in:
lib/UpstreamWatchr/gitlabcheck.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin_url, branch = 'master') ⇒ GitLabWatchr

Returns a new instance of GitLabWatchr.



7
8
9
10
11
# File 'lib/UpstreamWatchr/gitlabcheck.rb', line 7

def initialize(origin_url, branch = 'master')
  @origin_url = origin_url
  @branch = branch
  @comparator = GitRemotes.new(@origin_url, upstream_url)
end

Instance Attribute Details

#comparatorObject (readonly)

Returns the value of attribute comparator.



5
6
7
# File 'lib/UpstreamWatchr/gitlabcheck.rb', line 5

def comparator
  @comparator
end

#origin_projectObject (readonly)

Returns the value of attribute origin_project.



5
6
7
# File 'lib/UpstreamWatchr/gitlabcheck.rb', line 5

def origin_project
  @origin_project
end

#origin_urlObject (readonly)

Returns the value of attribute origin_url.



5
6
7
# File 'lib/UpstreamWatchr/gitlabcheck.rb', line 5

def origin_url
  @origin_url
end

Instance Method Details

#create_forkObject



21
22
23
24
25
26
# File 'lib/UpstreamWatchr/gitlabcheck.rb', line 21

def create_fork
  puts "Forking #{origin_project.name}..."
  Gitlab.create_fork(@origin_project.id)
rescue Gitlab::Error::Conflict
  puts "Fork of #{origin_project.name} already exists."
end

#create_issueObject



73
74
75
76
77
78
79
# File 'lib/UpstreamWatchr/gitlabcheck.rb', line 73

def create_issue
  puts "Warn: Creating new issue '#{issue_title}'"
  Gitlab.create_issue(@origin_project.id,
    issue_title,
    {:description => issue_description}
  )
end

#create_merge_requestObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/UpstreamWatchr/gitlabcheck.rb', line 32

def create_merge_request
  @comparator.push_to_my_fork(fork.ssh_url_to_repo)

  puts "Creating merge request on #{origin_project.name}..."
  Gitlab.create_merge_request(fork.id,
    'UpstreamWatcher detected new upstream changes!',
    :source_branch => @branch,
    :target_branch => @branch,
    :target_project_id => origin_project.id
    )
rescue Gitlab::Error::Conflict
  puts "Merge request seems to be already there. Good. Previous MR updated with even more upstream changes!"
end

#find_issueObject



69
70
71
# File 'lib/UpstreamWatchr/gitlabcheck.rb', line 69

def find_issue
   Gitlab.issues(@origin_project.id, :per_page => 1000).find {|i| i.state != 'closed' && i.author.id == Gitlab.user.id && i.title =~ /^UpstreamWatchr:/}
end

#forkObject



28
29
30
# File 'lib/UpstreamWatchr/gitlabcheck.rb', line 28

def fork
  @fork ||= (Gitlab.projects(:per_page => 10000).find {|p| p.forked_from_project && p.forked_from_project.id == @origin_project.id } || create_fork)
end

#grumble_in_issueObject



87
88
89
90
91
92
93
94
# File 'lib/UpstreamWatchr/gitlabcheck.rb', line 87

def grumble_in_issue
  changes_in_title = issue.title.match(/(\d+)/)[1].to_i
  puts "Debug: Issue states '#{changes_in_title} changes'" if ENV['DEBUG']
  unless changes_in_title == @comparator.upstream_ahead
    puts "Warn: Going to update issue to '#{issue_title}'"
    update_issue
  end
end

#issueObject



65
66
67
# File 'lib/UpstreamWatchr/gitlabcheck.rb', line 65

def issue
  @issue ||= find_issue || create_issue
end

#issue_descriptionObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/UpstreamWatchr/gitlabcheck.rb', line 50

def issue_description
  "UpstreamWatchr noted that your branch '#{@branch}' is #{@comparator.upstream_ahead} commits behind `#{upstream_url}`'s #{@branch}.

  Whenenver you feel confident pulling the upstream changes onto your branch, you can use these git commands:

  ```
  git checkout #{@branch}
  git pull #{upstream_url} #{@branch}
  git push origin #{@branch}
  ```


  Sincerly, UpstreamWatchr"
end

#issue_titleObject



46
47
48
# File 'lib/UpstreamWatchr/gitlabcheck.rb', line 46

def issue_title
  "UpstreamWatchr: #{@comparator.upstream_ahead} change(s) to fetch from your upstream project!"
end

#update_issueObject



81
82
83
84
85
# File 'lib/UpstreamWatchr/gitlabcheck.rb', line 81

def update_issue
  puts "Debug: Updating issue '#{issue_title}'" if ENV['DEBUG']
  Gitlab.edit_issue(@origin_project.id, issue.id, :title => issue_title)
  Gitlab.create_issue_note(@origin_project.id, issue.id, issue_title)
end

#upstream_urlObject



17
18
19
# File 'lib/UpstreamWatchr/gitlabcheck.rb', line 17

def upstream_url
  @upstream_url ||= origin_project.description.match(/^Upstream: (\S*)$/)[1]
end