Class: Cyclid::API::Plugins::Github

Inherits:
Api
  • Object
show all
Defined in:
app/cyclid/plugins/api/github.rb

Overview

API extension for Github hooks

Class Method Summary collapse

Methods inherited from Api

human_name

Methods inherited from Base

author, get_config, homepage, human_name, license, register_plugin, set_config, version

Class Method Details

.config?Boolean

This plugin has configuration data

Returns:

  • (Boolean)


45
46
47
# File 'app/cyclid/plugins/api/github.rb', line 45

def config?
  true
end

.config_schemaObject

Github plugin configuration schema



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/cyclid/plugins/api/github.rb', line 109

def config_schema
  schema = []
  schema << { name: 'repository_tokens',
              type: 'hash-list',
              description: 'Individual repository personal OAuth tokens',
              default: [] }
  schema << { name: 'oauth_token',
              type: 'password',
              description: 'Organization Github OAuth token',
              default: nil }
  schema << { name: 'oauth_request',
              type: 'link-relative',
              description: 'Authorize with Github',
              default: '/oauth/request' }
  return schema
end

.controllerObject

Return an instance of the Github API controller



29
30
31
32
33
34
# File 'app/cyclid/plugins/api/github.rb', line 29

def controller
  routes = [{ verb: :get, path: '/oauth/request', func: 'oauth_request' },
            { verb: :get, path: '/oauth/callback', func: 'oauth_callback' }]

  return ApiExtension::Controller.new(ApiExtension::GithubMethods, routes)
end

.default_configObject

Default configuration



100
101
102
103
104
105
106
# File 'app/cyclid/plugins/api/github.rb', line 100

def default_config
  config = {}
  config['repository_tokens'] = []
  config['oauth_token'] = nil

  return config
end

.metadataObject

Plugin metadata



37
38
39
40
41
42
# File 'app/cyclid/plugins/api/github.rb', line 37

def 
  super.merge!(version: Cyclid::Api::VERSION,
               license: 'Apache-2.0',
               author: 'Liqwyd Ltd.',
               homepage: 'http://docs.cyclid.io')
end

.update_config(config, new) ⇒ Object

Merge the given config into the current config & validate



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
# File 'app/cyclid/plugins/api/github.rb', line 50

def update_config(config, new)
  Cyclid.logger.debug "config=#{config} new=#{new}"

  if new.key? 'repository_tokens'
    Cyclid.logger.debug 'updating repository tokens'

    new_tokens = new['repository_tokens']
    current_tokens = config['repository_tokens']

    raise 'repository_tokens must be an array' \
      unless new_tokens.is_a? Array

    # Merge the current list of tokens with the new list of tokens;
    # we have to do this in a 'roundabout fashion:
    #
    # 1. Convert both into a hash, with the url as the key and
    #    the original hash itself as the value. E.g.
    #    {url: 'example.com', token: 'abcdef'} becomes
    #    {'exmaple.com': {url: 'example.com', token: 'abcdef'}}
    # 2. Merge the new hash into the current hash; this will
    #    over-write any existing entries.
    # 3. Obtain the values of the the resulting merged object, which
    #    is an array of the original hashes.
    #
    # Thanks, Stackoverflow!
    new_hash = Hash[new_tokens.map{ |h| [h['url'], h] }]
    current_hash = Hash[current_tokens.map{ |h| [h['url'], h] }]

    merged = current_hash.merge(new_hash).values

    # Delete any entries where the token value is nil
    merged.delete_if do |entry|
      entry['token'].nil?
    end

    config['repository_tokens'] = merged
  end

  if new.key? 'oauth_token'
    Cyclid.logger.debug 'updating OAuth token'
    config['oauth_token'] = new['oauth_token']
  end

  # Remove any old keys
  config.delete 'hmac_secret' if config.key? 'hmac_secret'

  return config
end