github.com/elliott5/community@v0.14.1-0.20160709191136-823126fb026a/documize/section/github/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 github
    13  
    14  import (
    15  	"html/template"
    16  	"time"
    17  
    18  	"github.com/documize/community/wordsmith/log"
    19  )
    20  
    21  const tagIssuesData = "issuesData"
    22  const tagCommitsData = "commitsData"
    23  
    24  type githubRender struct {
    25  	Config           githubConfig
    26  	Repo             githubRepo
    27  	List             []githubBranch
    28  	ShowList         bool
    29  	ShowIssueNumbers bool
    30  	BranchCommits    []githubBranchCommits
    31  	CommitCount      int
    32  	Issues           []githubIssue
    33  	//IssueNum         int
    34  	//IssueNumActivity []githubIssueActivity
    35  	Limit       int
    36  	DateMessage string
    37  }
    38  
    39  var renderTemplates = map[string]string{
    40  	tagCommitsData: `
    41  <div class="section-github-render">
    42  	<p>
    43  		There are {{ .CommitCount }} commits for branch <a href="{{.Config.BranchURL}}">{{.Config.Branch}}</a> of repository <a href="{{ .Repo.URL }}">{{.Repo.Name}}.</a>
    44  		Showing {{ .Limit }} items {{ .DateMessage }}.
    45  	</p>
    46  	<div class="github-board">
    47  		{{range $data := .BranchCommits}}
    48  			<div class="github-group-title">
    49  				Commits on {{ $data.Day }}
    50  			</div>
    51  			<ul class="github-list">
    52  				{{range $commit := $data.Commits}}
    53  					<li class="github-commit-item">
    54  						<a class="link" href="{{$commit.URL}}">
    55  							<div class="github-avatar">
    56  								<img alt="@{{$commit.Name}}" src="{{$commit.Avatar}}" height="36" width="36">
    57  							</div>
    58  							<div class="github-commit-body">
    59  								<div class="github-commit-title">{{$commit.Message}}</div>
    60  								<div class="github-commit-meta">{{$commit.Name}} committed on {{$commit.Date}}</div>
    61  							</div>
    62  						</a>
    63  						<div class="clearfix" />
    64  					</li>
    65  				{{end}}
    66  			</ul>
    67  		{{end}}
    68  	</div>
    69  </div>
    70  `,
    71  	tagIssuesData: `
    72  <div class="section-github-render">
    73  	<p>
    74  		{{if .ShowIssueNumbers}}
    75  			Showing Selected Issues
    76  		{{else}}
    77  			{{ .Config.IssueState.Name }}
    78  		{{end}}
    79  		    for repository <a href="{{ .Repo.URL }}/issues">{{.Repo.Name}}</a>
    80  		{{if .ShowList}}
    81  			labelled
    82  			{{range $label := .List}}
    83  				{{if $label.Included}}
    84  					<span class="github-issue-label" style="background-color:#{{$label.Color}}">{{$label.Name}}</span>
    85  				{{end}}
    86  			{{end}}
    87  		{{end}}
    88  		{{if .ShowIssueNumbers}}
    89  			issue(s) {{ .DateMessage }}.
    90  		{{else}}
    91  			up to {{ .Limit }} items are shown{{ .DateMessage }}.
    92  		{{end}}
    93  	</p>
    94  	<div class="github-board">
    95  	<ul class="github-list">
    96  		{{range $data := .Issues}}
    97  			<li class="github-commit-item">
    98  				<a class="link" href="{{$data.URL}}">
    99  					<div class="issue-avatar">
   100  						{{if $data.IsOpen}}
   101  							<span title="Open issue">
   102  								<svg height="16" version="1.1" viewBox="0 0 14 16" width="14"><path d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
   103  							</span>
   104  						{{else}}
   105  							<span title="Closed issue">
   106  								<svg height="16" version="1.1" viewBox="0 0 16 16" width="16"><path d="M7 10h2v2H7v-2zm2-6H7v5h2V4zm1.5 1.5l-1 1L12 9l4-4.5-1-1L12 7l-1.5-1.5zM8 13.7A5.71 5.71 0 0 1 2.3 8c0-3.14 2.56-5.7 5.7-5.7 1.83 0 3.45.88 4.5 2.2l.92-.92A6.947 6.947 0 0 0 8 1C4.14 1 1 4.14 1 8s3.14 7 7 7 7-3.14 7-7l-1.52 1.52c-.66 2.41-2.86 4.19-5.48 4.19v-.01z"></path></svg>
   107  							</span>
   108  						{{end}}
   109  				  	</div>
   110  					<div class="github-commit-body">
   111  						<div class="github-commit-title"><span class="label-name">{{$data.Message}}</span> {{$data.Labels}}</div>
   112  						<div class="github-commit-meta">
   113  							#{{$data.ID}} opened on {{$data.Date}} by {{$data.Name}}, last updated {{$data.Updated}}
   114  						</div>
   115  					</div>
   116  				</a>
   117  				<div class="clearfix" />
   118  			</li>
   119  		{{end}}
   120  	</ul>
   121  	</div>
   122  </div>
   123  `,
   124  	/* "issuenum_data": `
   125  	   <div class="section-github-render">
   126  	   	<p>
   127  	   		Activity for issue #{{.IssueNum}} in repository <a href="{{ .Repo.URL }}/issues">{{.Repo.Name}}.</a>
   128  	   		Up to {{ .Limit }} items are shown{{ .DateMessage }}.
   129  	   	</p>
   130  	   	<div class="github-board">
   131  	   	<ul class="github-list">
   132  	   		{{range $data := .IssueNumActivity}}
   133  	   			<li class="github-commit-item">
   134  	   				<div class="github-avatar">
   135  	   					<img alt="@{{$data.Name}}" src="{{$data.Avatar}}" height="36" width="36">
   136  	   				</div>
   137  	   				<div class="github-commit-meta">
   138  	   					{{$data.Name}} <a class="link" href="{{$data.URL}}">{{$data.Event}}</a> {{$data.Date}}
   139  	   				</div>
   140  	   				<div class="github-commit-body">
   141  	   					<div class="github-commit-title">
   142  	   						{{$data.Message}}
   143  	   					</div>
   144  	   				</div>
   145  	   				<div class="clearfix" />
   146  	   			</li>
   147  	   		{{end}}
   148  	   	</ul>
   149  	   	</div>
   150  	   </div>
   151  	   `,*/
   152  }
   153  
   154  type githubReport struct {
   155  	ID   string `json:"id"`
   156  	Name string `json:"name"`
   157  }
   158  
   159  type githubOwner struct {
   160  	ID   string `json:"id"`
   161  	Name string `json:"name"`
   162  }
   163  
   164  type githubRepo struct {
   165  	ID       string `json:"id"`
   166  	Name     string `json:"name"`
   167  	Included bool   `json:"included"`
   168  	Owner    string `json:"owner"`
   169  	Repo     string `json:"repo"`
   170  	Private  bool   `json:"private"` // TODO review field use
   171  	URL      string `json:"url"`
   172  }
   173  
   174  type githubBranch struct {
   175  	ID       string `json:"id"`
   176  	Name     string `json:"name"`
   177  	Included bool   `json:"included"`
   178  	URL      string `json:"url"`
   179  	Color    string `json:"color,omitempty"`
   180  }
   181  
   182  type githubBranchCommits struct {
   183  	Name    string `json:"name"`
   184  	Day     string `json:"day"`
   185  	Commits []githubCommit
   186  }
   187  
   188  type githubCommit struct {
   189  	Date    string       `json:"date"`
   190  	Message string       `json:"message"`
   191  	URL     template.URL `json:"url"`
   192  	Name    string       `json:"name"`
   193  	Avatar  string       `json:"avatar"`
   194  }
   195  
   196  type githubIssue struct {
   197  	ID      int           `json:"id"`
   198  	Date    string        `json:"date"`
   199  	Updated string        `json:"dated"`
   200  	Message string        `json:"message"`
   201  	URL     template.URL  `json:"url"`
   202  	Name    string        `json:"name"`
   203  	Avatar  string        `json:"avatar"`
   204  	Labels  template.HTML `json:"labels"`
   205  	IsOpen  bool          `json:"isopen"`
   206  }
   207  
   208  /*
   209  type githubIssueActivity struct {
   210  	Date    string        `json:"date"`
   211  	Event   string        `json:"event"`
   212  	Message template.HTML `json:"message"`
   213  	URL     template.URL  `json:"url"`
   214  	Name    string        `json:"name"`
   215  	Avatar  string        `json:"avatar"`
   216  }
   217  */
   218  
   219  type githubConfig struct {
   220  	Token       string         `json:"-"` // NOTE very important that the secret Token is not leaked to the client side, so "-"
   221  	UserID      string         `json:"userId"`
   222  	PageID      string         `json:"pageId"`
   223  	Owner       string         `json:"owner_name"`
   224  	Repo        string         `json:"repo_name"`
   225  	Branch      string         `json:"branch"`
   226  	BranchURL   string         `json:"branchURL"`
   227  	BranchSince string         `json:"branchSince,omitempty"`
   228  	SincePtr    *time.Time     `json:"-"`
   229  	BranchLines int            `json:"branchLines,omitempty,string"`
   230  	OwnerInfo   githubOwner    `json:"owner"`
   231  	RepoInfo    githubRepo     `json:"repo"`
   232  	ReportInfo  githubReport   `json:"report"`
   233  	ClientID    string         `json:"clientId"`
   234  	CallbackURL string         `json:"callbackUrl"`
   235  	Lists       []githubBranch `json:"lists,omitempty"`
   236  	IssueState  githubReport   `json:"state,omitempty"`
   237  	IssuesText  string         `json:"issues,omitempty"`
   238  	//IssueNum    int            `json:"issueNum,omitempty,string"`
   239  }
   240  
   241  func (c *githubConfig) Clean() {
   242  	c.Owner = c.OwnerInfo.Name
   243  	c.Repo = c.RepoInfo.Repo
   244  	for _, l := range c.Lists {
   245  		if l.Included {
   246  			c.Branch = l.Name
   247  			c.BranchURL = l.URL
   248  			break
   249  		}
   250  	}
   251  	if len(c.BranchSince) >= len("yyyy/mm/dd hh:ss") {
   252  		var since time.Time
   253  		tt := []byte("yyyy-mm-ddThh:mm:00Z")
   254  		for _, i := range []int{0, 1, 2, 3, 5, 6, 8, 9, 11, 12, 14, 15} {
   255  			tt[i] = c.BranchSince[i]
   256  		}
   257  		err := since.UnmarshalText(tt)
   258  		if err != nil {
   259  			log.ErrorString("Date unmarshall '" + c.BranchSince + "'->'" + string(tt) + "' error: " + err.Error())
   260  		} else {
   261  			c.SincePtr = &since
   262  		}
   263  	}
   264  }
   265  
   266  type githubCallbackT struct {
   267  	AccessToken string `json:"access_token"`
   268  }