1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-20 21:59:42 +02:00

remove redundant code

This commit is contained in:
Elliott Stoneham 2016-08-26 14:14:00 +01:00
parent cf6410cfe8
commit 87be56bc4b
9 changed files with 39 additions and 105 deletions

View file

@ -21,11 +21,6 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
authenticated: false, authenticated: false,
config: {}, config: {},
owners: null, owners: null,
repos: null,
noRepos: false,
showCommits: false,
showIssueNum: false,
showLabels: false,
didReceiveAttrs() { didReceiveAttrs() {
let self = this; let self = this;
@ -41,16 +36,9 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
callbackUrl: cfg.authorizationCallbackURL, callbackUrl: cfg.authorizationCallbackURL,
owner: null, owner: null,
owner_name: "", owner_name: "",
repo: null,
repo_name: "",
report: null,
lists: [], lists: [],
branch: "",
branchURL: "",
branchSince: "", branchSince: "",
branchLines: "30", branchLines: "100",
state: null,
issues: "",
userId: "", userId: "",
pageId: page.get('id'), pageId: page.get('id'),
}; };
@ -58,13 +46,8 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
try { try {
let metaConfig = JSON.parse(self.get('meta.config')); let metaConfig = JSON.parse(self.get('meta.config'));
config.owner = metaConfig.owner; config.owner = metaConfig.owner;
config.repo = metaConfig.repo;
config.report = metaConfig.report;
config.lists = metaConfig.lists; config.lists = metaConfig.lists;
config.branchSince = metaConfig.branchSince; config.branchSince = metaConfig.branchSince;
config.branchLines = metaConfig.branchLines;
config.state = metaConfig.state;
config.issues = metaConfig.issues;
config.userId = metaConfig.userId; config.userId = metaConfig.userId;
config.pageId = metaConfig.pageId; config.pageId = metaConfig.pageId;
} catch (e) {} } catch (e) {}
@ -175,7 +158,7 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
}, function (error) { //jshint ignore: line }, function (error) { //jshint ignore: line
self.set('busy', false); self.set('busy', false);
self.set('authenticated', false); self.set('authenticated', false);
self.showNotification("Unable to fetch repository branches"); self.showNotification("Unable to fetch repositories");
console.log(error); console.log(error);
}); });
}, },
@ -229,7 +212,6 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
onOwnerChange(thisOwner) { onOwnerChange(thisOwner) {
this.set('isDirty', true); this.set('isDirty', true);
this.set('config.owner', thisOwner); this.set('config.owner', thisOwner);
this.set('config.repos', []);
this.set('config.lists', []); this.set('config.lists', []);
this.getOwnerLists(); this.getOwnerLists();
}, },
@ -245,15 +227,6 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
onAction(title) { onAction(title) {
this.set('busy', true); this.set('busy', true);
let thisLines = this.get('config.branchLines');
if (is.undefined(thisLines) || thisLines === "") {
this.set('config.branchLines', 30);
} else if (thisLines < 1) {
this.set('config.branchLines', 1);
} else if (thisLines > 100) {
this.set('config.branchLines', 100);
}
let self = this; let self = this;
let page = this.get('page'); let page = this.get('page');
let meta = this.get('meta'); let meta = this.get('meta');
@ -262,8 +235,7 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
meta.set('config', JSON.stringify(this.get('config'))); meta.set('config', JSON.stringify(this.get('config')));
meta.set('externalSource', true); meta.set('externalSource', true);
let thisReport = this.get('config.report'); this.get('sectionService').fetch(page, 'content', this.get('config'))
this.get('sectionService').fetch(page, thisReport.id, this.get('config'))
.then(function (response) { .then(function (response) {
meta.set('rawBody', JSON.stringify(response)); meta.set('rawBody', JSON.stringify(response));
self.set('busy', false); self.set('busy', false);

View file

@ -15,16 +15,10 @@
<div class="tip">Select organization or username whose repositories you want to show</div> <div class="tip">Select organization or username whose repositories you want to show</div>
{{ui-select id="owners-dropdown" content=owners action=(action 'onOwnerChange') optionValuePath="id" optionLabelPath="name" selection=config.owner}} {{ui-select id="owners-dropdown" content=owners action=(action 'onOwnerChange') optionValuePath="id" optionLabelPath="name" selection=config.owner}}
</div> </div>
{{#if noRepos}}
<div class="input-control">
<div class="color-error">You have no repositories.</div>
</div>
{{else}}
<div class="input-control"> <div class="input-control">
<label>Show items since (default 7 days ago)</label> <label>Show items since (default 7 days ago)</label>
{{input id="branch-since" value=config.branchSince type="text" }}<br> {{input id="branch-since" value=config.branchSince type="text" }}<br>
</div> </div>
{{/if}}
</div> </div>
</div> </div>

View file

@ -258,7 +258,7 @@ func getCommits(client *gogithub.Client, config *githubConfig) ([]githubCommit,
sort.Strings(v.Repos) sort.Strings(v.Repos)
retStats = append(retStats, v) retStats = append(retStats, v)
} }
sort.Stable(asToSort(retStats)) sort.Sort(asToSort(retStats))
return overall, retStats, nil return overall, retStats, nil
@ -302,7 +302,7 @@ func renderCommits(payload *githubRender, c *githubConfig) error {
} }
} }
payload.HasAuthorStats = len(payload.AuthorStats) > 0 payload.HasAuthorStats = len(payload.AuthorStats) > 0
sort.Stable(asToSort(payload.AuthorStats)) sort.Sort(asToSort(payload.AuthorStats))
return nil return nil
} }

View file

@ -18,9 +18,12 @@ import (
"html/template" "html/template"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strings"
"github.com/documize/community/core/log" "github.com/documize/community/core/log"
"github.com/documize/community/core/section/provider" "github.com/documize/community/core/section/provider"
gogithub "github.com/google/go-github/github"
) )
// TODO find a smaller image than the one below // TODO find a smaller image than the one below
@ -157,13 +160,9 @@ func (p *Provider) Refresh(ctx *provider.Context, configJSON, data string) strin
c.Clean() c.Clean()
c.Token = ctx.GetSecrets("token") c.Token = ctx.GetSecrets("token")
var gr = githubRender{}
client := p.githubClient(&c) client := p.githubClient(&c)
for _, rep := range reports {
log.IfErr(rep.refresh(&gr, &c, client))
}
byts, err := json.Marshal(&gr) byts, err := json.Marshal(refreshReportData(&c, client))
if err != nil { if err != nil {
log.Error("unable to marshall github data", err) log.Error("unable to marshall github data", err)
return "internal configuration error '" + err.Error() + "'" return "internal configuration error '" + err.Error() + "'"
@ -173,6 +172,14 @@ func (p *Provider) Refresh(ctx *provider.Context, configJSON, data string) strin
} }
func refreshReportData(c *githubConfig, client *gogithub.Client) *githubRender {
var gr = githubRender{}
for _, rep := range reports {
log.IfErr(rep.refresh(&gr, c, client))
}
return &gr
}
// Render ... just returns the data given, suitably formatted // Render ... just returns the data given, suitably formatted
func (p *Provider) Render(ctx *provider.Context, config, data string) string { func (p *Provider) Render(ctx *provider.Context, config, data string) string {
var err error var err error
@ -190,15 +197,20 @@ func (p *Provider) Render(ctx *provider.Context, config, data string) string {
c.Clean() c.Clean()
c.Token = ctx.GetSecrets("token") c.Token = ctx.GetSecrets("token")
err = json.Unmarshal([]byte(data), &payload) data = strings.TrimSpace(data)
if len(data) == 0 {
// TODO review why this error occurs & if it should be reported - seems to occur for new sections
// log.ErrorString(fmt.Sprintf("Rendered empty github JSON payload as '' for owner %s repos %#v", c.Owner, c.Lists))
return ""
}
err = json.Unmarshal([]byte(data), &payload)
if err != nil { if err != nil {
log.Error("unable to unmarshall github data", err) log.Error("unable to unmarshall github data", err)
return "Please delete and recreate this Github section." return "Please delete and recreate this Github section."
} }
payload.Config = c payload.Config = c
payload.Repo = c.RepoInfo
payload.Limit = c.BranchLines payload.Limit = c.BranchLines
payload.List = c.Lists payload.List = c.Lists

View file

@ -168,7 +168,7 @@ func getIssues(client *gogithub.Client, config *githubConfig) ([]githubIssue, er
} }
sort.Stable(issuesToSort(ret)) sort.Sort(issuesToSort(ret))
return ret, nil return ret, nil
@ -212,7 +212,7 @@ func refreshIssues(gr *githubRender, config *githubConfig, client *gogithub.Clie
gr.SharedLabels = append(gr.SharedLabels, thisLab) gr.SharedLabels = append(gr.SharedLabels, thisLab)
} }
} }
sort.Stable(sharedLabelsSort(gr.SharedLabels)) sort.Sort(sharedLabelsSort(gr.SharedLabels))
gr.HasSharedLabels = len(gr.SharedLabels) > 0 gr.HasSharedLabels = len(gr.SharedLabels) > 0
return nil return nil

View file

@ -52,7 +52,6 @@ func listFailed(method string, config githubConfig, client *gogithub.Client, w h
provider.WriteJSON(w, owners) provider.WriteJSON(w, owners)
case "orgrepos": case "orgrepos":
var render []githubBranch var render []githubBranch
@ -96,6 +95,9 @@ func listFailed(method string, config githubConfig, client *gogithub.Client, w h
provider.WriteJSON(w, render) provider.WriteJSON(w, render)
case "content":
provider.WriteJSON(w, refreshReportData(&config, client))
default: default:
return true // failed to get a list return true // failed to get a list

View file

@ -201,7 +201,7 @@ func renderMilestones(payload *githubRender, c *githubConfig) error {
} }
} }
sort.Stable(milestonesToSort(payload.Milestones)) sort.Sort(milestonesToSort(payload.Milestones))
return nil return nil
} }

View file

@ -23,7 +23,6 @@ import (
type githubRender struct { type githubRender struct {
Config githubConfig `json:"config"` Config githubConfig `json:"config"`
Repo githubRepo `json:"repo"`
List []githubBranch `json:"list"` List []githubBranch `json:"list"`
ShowList bool `json:"showList"` ShowList bool `json:"showList"`
ShowIssueNumbers bool `json:"showIssueNumbers"` ShowIssueNumbers bool `json:"showIssueNumbers"`
@ -55,26 +54,11 @@ type report struct {
var reports = make(map[string]report) var reports = make(map[string]report)
type githubReport struct {
ID string `json:"id"`
Name string `json:"name"`
}
type githubOwner struct { type githubOwner struct {
ID string `json:"id"` ID string `json:"id"`
Name string `json:"name"` Name string `json:"name"`
} }
type githubRepo struct {
ID string `json:"id"`
Name string `json:"name"`
Included bool `json:"included"`
Owner string `json:"owner"`
Repo string `json:"repo"`
Private bool `json:"private"`
URL string `json:"url"`
}
type githubBranch struct { type githubBranch struct {
ID string `json:"id"` ID string `json:"id"`
Owner string `json:"owner"` Owner string `json:"owner"`
@ -102,35 +86,20 @@ type githubConfig struct {
UserID string `json:"userId"` UserID string `json:"userId"`
PageID string `json:"pageId"` PageID string `json:"pageId"`
Owner string `json:"owner_name"` Owner string `json:"owner_name"`
Repo string `json:"repo_name"`
Branch string `json:"branch"`
BranchURL string `json:"branchURL"`
BranchSince string `json:"branchSince,omitempty"` BranchSince string `json:"branchSince,omitempty"`
SincePtr *time.Time `json:"-"` SincePtr *time.Time `json:"-"`
Since string `json:"since"` Since string `json:"-"`
BranchLines int `json:"branchLines,omitempty,string"` BranchLines int `json:"branchLines,omitempty,string"`
OwnerInfo githubOwner `json:"owner"` OwnerInfo githubOwner `json:"owner"`
RepoInfo githubRepo `json:"repo"`
ReportInfo githubReport `json:"report"`
ClientID string `json:"clientId"` ClientID string `json:"clientId"`
CallbackURL string `json:"callbackUrl"` CallbackURL string `json:"callbackUrl"`
Lists []githubBranch `json:"lists,omitempty"` Lists []githubBranch `json:"lists,omitempty"`
IssueState githubReport `json:"state,omitempty"` ReportOrder []string `json:"-"`
IssuesText string `json:"issues,omitempty"` DateMessage string `json:"-"`
ReportOrder []string `json:"reportOrder,omitempty"`
DateMessage string `json:"dateMessage,omitempty"`
} }
func (c *githubConfig) Clean() { func (c *githubConfig) Clean() {
c.Owner = c.OwnerInfo.Name c.Owner = c.OwnerInfo.Name
c.Repo = c.RepoInfo.Repo
for _, l := range c.Lists {
if l.Included {
c.Branch = l.Name
c.BranchURL = l.URL
break
}
}
if len(c.BranchSince) >= len("yyyy/mm/dd hh:ss") { if len(c.BranchSince) >= len("yyyy/mm/dd hh:ss") {
var since time.Time var since time.Time
tt := []byte("yyyy-mm-ddThh:mm:00Z") tt := []byte("yyyy-mm-ddThh:mm:00Z")
@ -154,9 +123,9 @@ func (c *githubConfig) Clean() {
c.Since = (*c.SincePtr).Format(issuesTimeFormat) c.Since = (*c.SincePtr).Format(issuesTimeFormat)
c.ReportOrder = []string{tagSummaryData, tagMilestonesData, tagIssuesData, tagCommitsData} c.ReportOrder = []string{tagSummaryData, tagMilestonesData, tagIssuesData, tagCommitsData}
c.BranchLines = 100 // overide js default of 30 with maximum allowable in one call c.BranchLines = 100 // overide any existing value with maximum allowable in one call
sort.Stable(branchesToSort(c.Lists)) // get the configured branches in a sensible order for printing sort.Sort(branchesToSort(c.Lists)) // get the configured branches in a sensible order for display
lastItem := 0 lastItem := 0
for i := range c.Lists { for i := range c.Lists {

View file

@ -13,21 +13,6 @@ package github
import "sort" import "sort"
// sort repos in order that that should be presented.
type reposToSort []githubRepo
func (s reposToSort) Len() int { return len(s) }
func (s reposToSort) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s reposToSort) Less(i, j int) bool {
return s[i].Name < s[j].Name
}
func sortRepos(in []githubRepo) []githubRepo {
sts := reposToSort(in)
sort.Sort(sts)
return []githubRepo(sts)
}
// sort owners in order that that should be presented. // sort owners in order that that should be presented.
type ownersToSort []githubOwner type ownersToSort []githubOwner