github.com/jancarloviray/community@v0.41.1-0.20170124221257-33a66c87cf2f/core/section/gemini/model.go (about)

     1  // Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
     2  //
     3  // This software (Documize Community Edition) is licensed under
     4  // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
     5  //
     6  // You can operate outside the AGPL restrictions by purchasing
     7  // Documize Enterprise Edition and obtaining a commercial license
     8  // by contacting <sales@documize.com>.
     9  //
    10  // https://documize.com
    11  
    12  package gemini
    13  
    14  import (
    15  	"strings"
    16  
    17  	"github.com/documize/community/core/log"
    18  	"github.com/documize/community/core/section/provider"
    19  )
    20  
    21  // the HTML that is rendered by this section.
    22  const renderTemplate = `
    23  {{if .Authenticated}}
    24  <p class="margin-left-20">The Gemini workspace <a href="{{.Config.URL}}/workspace/{{.Config.WorkspaceID}}/items">{{.Config.WorkspaceName}}</a> contains {{.Config.ItemCount}} items.</p>
    25  <table class="basic-table section-gemini-table">
    26  	<thead>
    27  		<tr>
    28  			<th class="bordered no-width">Item Key</th>
    29  			<th class="bordered">Title</th>
    30  			<th class="bordered no-width">Type</th>
    31  			<th class="bordered no-width">Status</th>
    32  		</tr>
    33  	</thead>
    34  	<tbody>
    35  		{{$wid := .Config.WorkspaceID}}
    36  		{{$app := .Config.URL}}
    37  		{{range $item := .Items}}
    38  		<tr>
    39  			<td class="bordered no-width"><a href="{{ $app }}/workspace/{{ $wid }}/item/{{ $item.ID }}">{{ $item.IssueKey }}</a></td>
    40  			<td class="bordered">{{ $item.Title }}</td>
    41  			<td class="bordered no-width"><img src='{{ $item.TypeImage }}' />&nbsp;{{ $item.Type }}</td>
    42  			<td class="bordered no-width"><img src='{{ $item.StatusImage }}' />&nbsp;{{ $item.Status }}</td>
    43  		</tr>
    44  		{{end}}
    45  	</tbody>
    46  </table>
    47  {{else}}
    48  <p>Authenticate with Gemini to see items.</p>
    49  {{end}}
    50  `
    51  
    52  // Gemini helpers
    53  type geminiRender struct {
    54  	Config        geminiConfig
    55  	Items         []geminiItem
    56  	Authenticated bool
    57  }
    58  
    59  type geminiItem struct {
    60  	ID          int64
    61  	IssueKey    string
    62  	Title       string
    63  	Type        string
    64  	TypeImage   string
    65  	Status      string
    66  	StatusImage string
    67  }
    68  
    69  type geminiUser struct {
    70  	BaseEntity struct {
    71  		ID        int    `json:"id"`
    72  		Username  string `json:"username"`
    73  		Firstname string `json:"firstname"`
    74  		Surname   string `json:"surname"`
    75  		Email     string `json:"email"`
    76  	}
    77  }
    78  
    79  type geminiConfig struct {
    80  	URL           string                 `json:"url"`
    81  	Username      string                 `json:"username"`
    82  	APIKey        string                 `json:"apikey"`
    83  	UserID        int64                  `json:"userId"`
    84  	WorkspaceID   int64                  `json:"workspaceId"`
    85  	WorkspaceName string                 `json:"workspaceName"`
    86  	ItemCount     int                    `json:"itemCount"`
    87  	Filter        map[string]interface{} `json:"filter"`
    88  }
    89  
    90  func (c *geminiConfig) Clean(ctx *provider.Context) {
    91  	if ctx != nil {
    92  		sec, err := getSecrets(ctx)
    93  		if err == nil {
    94  			if len(sec.APIKey) > 0 && len(sec.Username) > 0 && len(sec.URL) > 0 {
    95  				c.APIKey = strings.TrimSpace(sec.APIKey)
    96  				c.Username = strings.TrimSpace(sec.Username)
    97  				c.URL = strings.TrimSpace(sec.URL)
    98  			}
    99  		}
   100  	}
   101  	c.APIKey = strings.TrimSpace(c.APIKey)
   102  	c.Username = strings.TrimSpace(c.Username)
   103  	c.URL = strings.TrimSpace(c.URL)
   104  }
   105  
   106  func (c *geminiConfig) SaveSecrets(ctx *provider.Context) {
   107  	var sec secrets
   108  	sec.APIKey = strings.TrimSpace(c.APIKey)
   109  	sec.Username = strings.TrimSpace(c.Username)
   110  	sec.URL = strings.TrimSpace(c.URL)
   111  	log.IfErr(ctx.MarshalSecrets(sec))
   112  }
   113  
   114  type secrets struct {
   115  	URL      string `json:"url"`
   116  	Username string `json:"username"`
   117  	APIKey   string `json:"apikey"`
   118  }
   119  
   120  func getSecrets(ctx *provider.Context) (sec secrets, err error) {
   121  	err = ctx.UnmarshalSecrets(&sec)
   122  	return
   123  }