github.com/wtfutil/wtf@v0.43.0/modules/github/settings.go (about)

     1  package github
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/olebedev/config"
     7  	"github.com/wtfutil/wtf/cfg"
     8  )
     9  
    10  const (
    11  	defaultFocusable = true
    12  	defaultTitle     = "GitHub"
    13  )
    14  
    15  // Settings defines the configuration properties for this module
    16  type Settings struct {
    17  	*cfg.Common
    18  
    19  	apiKey                 string        `help:"Your GitHub API token."`
    20  	baseURL                string        `help:"Your GitHub Enterprise API URL." optional:"true"`
    21  	customQueries          []customQuery `help:"Custom queries allow you to filter pull requests and issues however you like. Give the query a title and a filter. Filters can be copied directly from GitHub’s UI." optional:"true"`
    22  	enableStatus           bool          `help:"Display pull request mergeability status (‘dirty’, ‘clean’, ‘unstable’, ‘blocked’)." optional:"true"`
    23  	repositories           []string      `help:"A list of github repositories." values:"Example: wtfutil/wtf"`
    24  	showMyPullRequests     bool          `help:"Show my pull requests section" optional:"true"`
    25  	showOpenReviewRequests bool          `help:"Show open review requests section" optional:"true"`
    26  	showStats              bool          `help:"Show repository stats section" optional:"true"`
    27  	uploadURL              string        `help:"Your GitHub Enterprise upload URL (often the same as API URL)." optional:"true"`
    28  	username               string        `help:"Your GitHub username. Used to figure out which review requests you’ve been added to."`
    29  }
    30  
    31  type customQuery struct {
    32  	title   string `help:"Display title for this query"`
    33  	filter  string `help:"Github query filter"`
    34  	perPage int    `help:"Number of issues to show"`
    35  }
    36  
    37  // NewSettingsFromYAML creates a new settings instance from a YAML config block
    38  func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings {
    39  	settings := Settings{
    40  		Common: cfg.NewCommonSettingsFromModule(name, defaultTitle, defaultFocusable, ymlConfig, globalConfig),
    41  
    42  		apiKey:                 ymlConfig.UString("apiKey", ymlConfig.UString("apikey", os.Getenv("WTF_GITHUB_TOKEN"))),
    43  		baseURL:                ymlConfig.UString("baseURL", os.Getenv("WTF_GITHUB_BASE_URL")),
    44  		enableStatus:           ymlConfig.UBool("enableStatus", false),
    45  		showMyPullRequests:     ymlConfig.UBool("showMyPullRequests", true),
    46  		showOpenReviewRequests: ymlConfig.UBool("showOpenReviewRequests", true),
    47  		showStats:              ymlConfig.UBool("showStats", true),
    48  		uploadURL:              ymlConfig.UString("uploadURL", os.Getenv("WTF_GITHUB_UPLOAD_URL")),
    49  		username:               ymlConfig.UString("username"),
    50  	}
    51  	settings.repositories = cfg.ParseAsMapOrList(ymlConfig, "repositories")
    52  	settings.customQueries = parseCustomQueries(ymlConfig)
    53  
    54  	cfg.ModuleSecret(name, globalConfig, &settings.apiKey).
    55  		Service(settings.baseURL).Load()
    56  
    57  	return &settings
    58  }
    59  
    60  /* -------------------- Unexported Functions -------------------- */
    61  
    62  func parseCustomQueries(ymlConfig *config.Config) []customQuery {
    63  	result := []customQuery{}
    64  	if customQueries, err := ymlConfig.Map("customQueries"); err == nil {
    65  		for _, query := range customQueries {
    66  			c := customQuery{}
    67  			for key, value := range query.(map[string]interface{}) {
    68  				switch key {
    69  				case "title":
    70  					c.title = value.(string)
    71  				case "filter":
    72  					c.filter = value.(string)
    73  				case "perPage":
    74  					c.perPage = value.(int)
    75  				}
    76  			}
    77  
    78  			if c.title != "" && c.filter != "" {
    79  				result = append(result, c)
    80  			}
    81  		}
    82  	}
    83  	return result
    84  }