Module: OmniFocus::Github

Defined in:
lib/omnifocus/github.rb

Constant Summary collapse

VERSION =
"1.5.0"
PREFIX =
"GH"
GH_API_DEFAULT =
"https://api.github.com"

Instance Method Summary collapse

Instance Method Details

#fetch(api, auth, page) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/omnifocus/github.rb', line 42

def fetch api, auth, page
  uri = URI.parse "#{api}/issues?page=#{page}"

  headers = {
    "User-Agent"=>"omnifocus-github/#{VERSION}"
  }

  if auth[:user] && auth[:password]
    headers[:http_basic_authentication] = [auth[:user], auth[:password]]
  else
    raise ArgumentError, "Missing authentication"
  end

  uri.read headers
end

#get_last(body) ⇒ Object



58
59
60
61
# File 'lib/omnifocus/github.rb', line 58

def get_last body
  link, last = body.meta["link"], nil
  link and link[/page=(\d+).. rel=.last/, 1].to_i or 0
end

#omnifocus_git_param(name, default = nil, prefix = "omnifocus-github") ⇒ Object



37
38
39
40
# File 'lib/omnifocus/github.rb', line 37

def omnifocus_git_param name, default = nil, prefix = "omnifocus-github"
  param = `git config --global #{prefix}.#{name}`.chomp
  param.empty? ? default : param
end

#populate_github_tasksObject



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
# File 'lib/omnifocus/github.rb', line 10

def populate_github_tasks
  omnifocus_git_param(:accounts, "github").split(/\s+/).each do ||
    api = omnifocus_git_param(:api, GH_API_DEFAULT, )
    # User + password is required
    auth = {
      :user => omnifocus_git_param(:user, nil, ),
      :password => omnifocus_git_param(:password, nil, ),
      :token => omnifocus_git_param(:token, nil, ),
    }
    unless (auth[:user] && auth[:password])
      warn "Missing authentication parameters for account #{}."
      next
    end

    # process will omit the account label if nil.
    # Supply nil for the default "github" account for compatibility
    # with previous versions.
     =  == "github" ? nil : 

    body = fetch(api, auth, 1)
    process(, body)
    (2..get_last(body)).each do |page|
      process(, fetch(api, auth, page))
    end
  end
end

#process(account, body) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/omnifocus/github.rb', line 63

def process , body
  JSON.parse(body).each do |issue|
    pr        = issue["pull_request"] && !issue["pull_request"]["diff_url"].nil?
    number    = issue["number"]
    url       = issue["html_url"]
    project   = [, url.split(/\//)[-3]].compact.join("-")
    ticket_id = "#{PREFIX}-#{project}##{number}"
    title     = "#{ticket_id}: #{pr ? "[PR] " : ""}#{issue["title"]}"
    note      = "#{url}\n\n#{issue["body"]}"

    if existing[ticket_id] then
      bug_db[existing[ticket_id]][ticket_id] = true
      next
    end

    bug_db[project][ticket_id] = [title, note]
  end
end