Class: CIRunner::Configuration::User

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/ci_runner/configuration/user.rb

Overview

Class to interact with the user’s configuration. The configuration is used to store the GitHub token amonst other things.

Constant Summary collapse

USER_CONFIG_PATH =
".ci_runner/config.yml"

Instance Method Summary collapse

Constructor Details

#initializevoid

Singleton class. This should/can’t be called directly.



22
23
24
# File 'lib/ci_runner/configuration/user.rb', line 22

def initialize
  load!
end

Instance Method Details

#buildkite_token(organization) ⇒ String?

Retrieve the stored Buildkite access token of the user that has access to the organization.

Returns:

  • (String, nil)

    Depending if the user ran the ‘ci_runner buildkite TOKEN ORGANIZATION` command.



74
75
76
# File 'lib/ci_runner/configuration/user.rb', line 74

def buildkite_token(organization)
  @yaml_config.dig("buildkite", "tokens", organization.downcase)
end

#circle_ci_tokenString?

Retrieve the stored CircleCI access token of the user.

Returns:

  • (String, nil)

    Depending if the user ran the ‘ci_runner circle_ci_token TOKEN` command.



45
46
47
# File 'lib/ci_runner/configuration/user.rb', line 45

def circle_ci_token
  @yaml_config.dig("circle_ci", "token")
end

#config_directoryPathname

Returns The path of the CI Runner directory configuration.

Examples:

puts config_directory # ~/.ci_runner

Returns:

  • (Pathname)

    The path of the CI Runner directory configuration.



97
98
99
# File 'lib/ci_runner/configuration/user.rb', line 97

def config_directory
  config_file.dirname
end

#config_filePathname

Returns The path of the configuration file.

Examples:

puts config_file # ~/.ci_runner/config.yml

Returns:

  • (Pathname)

    The path of the configuration file.



105
106
107
# File 'lib/ci_runner/configuration/user.rb', line 105

def config_file
  Pathname(File.expand_path(USER_CONFIG_PATH, Dir.home))
end

#github_tokenString?

Retrieve the stored GitHub access token of the user.

Returns:

  • (String, nil)

    Depending if the user ran the ‘ci_runner github_token TOKEN` command.



38
39
40
# File 'lib/ci_runner/configuration/user.rb', line 38

def github_token
  @yaml_config.dig("github", "token")
end

#load!void

This method returns an undefined value.

Load the configuration of the user. If it doesn’t exist, write an empty one.



29
30
31
32
33
# File 'lib/ci_runner/configuration/user.rb', line 29

def load!
  save!({}) unless config_file.exist?

  @yaml_config = YAML.load_file(config_file)
end

#save_buildkite_token(token, organization) ⇒ void

This method returns an undefined value.

Write the Buildkite token to the user configuration file.

Parameters:

  • token (String)

    A valid Buildkite access token.

  • organization (String)

    The name of the organization the token has access to.



84
85
86
87
88
89
90
91
# File 'lib/ci_runner/configuration/user.rb', line 84

def save_buildkite_token(token, organization)
  existing_tokens = @yaml_config.dig("buildkite", "tokens") || {}
  existing_tokens[organization.downcase] = token

  @yaml_config["buildkite"] = { "tokens" => existing_tokens }

  save!(@yaml_config)
end

#save_circle_ci_token(token) ⇒ void

This method returns an undefined value.

Write the Circle CI token to the user configuration file

Parameters:

  • token (String)

    A valid Circle CI access token.



65
66
67
68
69
# File 'lib/ci_runner/configuration/user.rb', line 65

def save_circle_ci_token(token)
  @yaml_config["circle_ci"] = { "token" => token }

  save!(@yaml_config)
end

#save_github_token(token) ⇒ void

This method returns an undefined value.

Write the GitHub token to the user configuration file

Parameters:

  • token (String)

    A valid GitHub access token.



54
55
56
57
58
# File 'lib/ci_runner/configuration/user.rb', line 54

def save_github_token(token)
  @yaml_config["github"] = { "token" => token }

  save!(@yaml_config)
end

#validate_token!void

This method returns an undefined value.

Ensure the user ran the ‘ci_runner github_token TOKEN` command prior to using CI Runner.

Note: Technically, it’s possible to access the GitHub API to retrieve checks and download logs on public repositories, but on private repositories the error GitHub sends back is a 404 which can be confusing, so I’d rather just make sure the token exists either way.

Raises:

  • (Error)

    If the user tries to run ‘ci_runner rerun` before it saved a token in its config file.



118
119
120
121
122
123
124
125
126
# File 'lib/ci_runner/configuration/user.rb', line 118

def validate_token!
  return if github_token

  raise(Error, <<~EOM)
    A GitHub token needs to be saved into your configuration before being able to use CI Runner.

    Have a look at the {{command:ci_runner help github_token}} command.
  EOM
end