diff --git a/app/app/components/section/github/type-editor.js b/app/app/components/section/github/type-editor.js index 17330ef9..4638fb3e 100644 --- a/app/app/components/section/github/type-editor.js +++ b/app/app/components/section/github/type-editor.js @@ -24,44 +24,53 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin, noRepos: false, didReceiveAttrs() { - let config = {}; - try { - config = JSON.parse(this.get('meta.config')); - } catch (e) {} + let self = this; + let page = this.get('page'); + console.log(this.get('config.clientId')); - if (is.empty(config)) { - config = { - clientId: "8574d0c34142dcdc53f6", - callbackUrl: "https://localhost:5001/api/public/validate?section=github", - token: "", - repo: null, - lists: [], - owner: "", - repo_name: "", - branch: "" - }; + if (is.undefined(this.get('config.clientId')) || is.undefined(this.get('config.callbackUrl'))) { + self.get('sectionService').fetch(page, "config", {}) + .then(function(cfg) { + let config = {}; + + try { + config = JSON.parse(self.get('meta.config')); + } catch (e) {} + + config = { + clientId: cfg.clientID, + callbackUrl: cfg.authorizationCallbackURL, + token: "", + repo: null, + lists: [], + owner: "", + repo_name: "", + branch: "", + branchURL: "", + branchSince: "", + branchLines: 30 + }; + self.set('config', config); + + // On auth callback capture code + let code = window.location.search; + + if (is.not.undefined(code) && is.not.null(code)) { + let tok = code.replace("?code=", ""); + if (is.not.empty(code)) { + self.set('config.token', tok); + self.send('authStage2'); + } + } else { + if (self.get('config.token') === "") { + self.send('auth'); + } + } + }, function(error) { //jshint ignore: line + console.log(error); + }); } - - this.set('config', config); - - - // On auth callback capture code - let code = window.location.search; - if (is.not.undefined(code) && is.not.null(code)) { - let tok = code.replace("?code=", ""); - if (is.not.empty(code)) { - this.set('config.token', tok); - this.send('authStage2'); - } - } - - if ((this.get('config.clientId') === "" || this.get('config.token') === "")) { - - this.send('auth'); - - } - }, willDestroyElement() { @@ -160,8 +169,6 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin, auth() { let self = this; - - self.set('busy', true); self.set('authenticated', false); let target = "https://github.com/login/oauth/authorize?client_id=" + self.get('config.clientId') + @@ -169,7 +176,6 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin, "&state=" + encodeURIComponent(window.location.href); window.location.href = target; - }, onRepoChange(thisRepo) { diff --git a/documize/section/github.go b/documize/section/github.go index 00c7fe4d..a52d578c 100644 --- a/documize/section/github.go +++ b/documize/section/github.go @@ -33,9 +33,4 @@ func (*github) Meta() TypeMeta { func init() { sectionsMap["github"] = &github{} - - // TODO make both soft and alterable at runtime - gh.ClientID = "8574d0c34142dcdc53f6" - gh.ClientSecret = "628e04d1575e234fbf477ff7b5f7dbfc95074c9e" - } diff --git a/documize/section/github/github.go b/documize/section/github/github.go index e66b0f56..68125077 100644 --- a/documize/section/github/github.go +++ b/documize/section/github/github.go @@ -21,15 +21,27 @@ import ( "net/url" "sort" "strings" + "time" + "github.com/documize/community/documize/api/request" "github.com/documize/community/wordsmith/log" - // vendored locally gogithub "github.com/google/go-github/github" "golang.org/x/oauth2" ) -var ClientID, ClientSecret string +const configKey = "SECTION-GITHUB" + +func clientID() string { + return request.ConfigString(configKey, "clientID") +} +func clientSecret() string { + return request.ConfigString(configKey, "clientSecret") +} +func authorizationCallbackURL() string { + // NOTE: URL value must have the path and query "/api/public/validate?section=github" + return request.ConfigString(configKey, "authorizationCallbackURL") +} type GithubT struct { @@ -68,7 +80,20 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) { method := query.Get("method") if len(method) == 0 { - writeMessage(w, "gitub", "missing method name") + msg := "missing method name" + log.ErrorString("github: " + msg) + writeMessage(w, "gitub", msg) + return + } + + if method == "config" { + var ret struct { + CID string `json:"clientID"` + URL string `json:"authorizationCallbackURL"` + } + ret.CID = clientID() + ret.URL = authorizationCallbackURL() + writeJSON(w, ret) return } @@ -76,7 +101,9 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) { body, err := ioutil.ReadAll(r.Body) if err != nil { - writeMessage(w, "github", "Bad body") + msg := "Bad body" + log.ErrorString("github: " + msg) + writeMessage(w, "gitub", msg) return } @@ -84,6 +111,7 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) { err = json.Unmarshal(body, &config) if err != nil { + log.Error("github Command Unmarshal", err) writeError(w, "github", err) return } @@ -91,7 +119,9 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) { config.Clean() if len(config.Token) == 0 { - writeMessage(w, "github", "Missing token") + msg := "Missing token" + log.ErrorString("github: " + msg) + writeMessage(w, "gitub", msg) return } @@ -103,9 +133,8 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) { render, err := t.getCommits(client, config) if err != nil { - //fmt.Println("Error:", err.Error()) + log.Error("github getCommits:", err) writeError(w, "github", err) - // TODO log error return } @@ -115,16 +144,14 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) { me, _, err := client.Users.Get("") if err != nil { - //fmt.Println(err) - // TODO log error + log.Error("github get user details:", err) writeError(w, "github", err) return } orgs, _, err := client.Organizations.List("", nil) if err != nil { - //fmt.Println(err) - // TODO log error + log.Error("github get user's organisations:", err) writeError(w, "github", err) return } @@ -141,11 +168,13 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) { if vo == *me.Login { repos, _, err = client.Repositories.List(vo, nil) } else { - repos, _, err = client.Repositories.ListByOrg(vo, nil) + opt := &gogithub.RepositoryListByOrgOptions{ + ListOptions: gogithub.ListOptions{PerPage: 100}, + } + repos, _, err = client.Repositories.ListByOrg(vo, opt) } if err != nil { - //fmt.Println(err) - // TODO log error + log.Error("github get user/org repositories:", err) writeError(w, "github", err) return } @@ -166,13 +195,6 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) { } } - if err != nil { - //fmt.Println(err) - // TODO log error - writeError(w, "github", err) - return - } - render = sortRepos(render) writeJSON(w, render) @@ -182,10 +204,10 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) { writeJSON(w, []githubBranch{}) // we have nothing to return return } - branches, _, err := client.Repositories.ListBranches(config.Owner, config.Repo, nil) + branches, _, err := client.Repositories.ListBranches(config.Owner, config.Repo, + &gogithub.ListOptions{PerPage: 100}) if err != nil { - //fmt.Println(err) - // TODO log error + log.Error("github get branch details:", err) writeError(w, "github", err) return } @@ -195,6 +217,7 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) { Name: *vb.Name, ID: fmt.Sprintf("%s:%s:%s:%d", config.Owner, config.Repo, *vb.Name, kc), Included: false, + URL: "https://github.com/" + config.Owner + "/" + config.Repo + "/tree/" + *vb.Name, } } @@ -217,8 +240,19 @@ func (*GithubT) githubClient(config githubConfig) *gogithub.Client { func (*GithubT) getCommits(client *gogithub.Client, config githubConfig) ([]githubBranchCommits, error) { - guff, _, err := client.Repositories.ListCommits(config.Owner, config.Repo, - &gogithub.CommitsListOptions{SHA: config.Branch}) + opts := &gogithub.CommitsListOptions{ + SHA: config.Branch, + ListOptions: gogithub.ListOptions{PerPage: config.BranchLines}} + + var since time.Time + + err := since.UnmarshalText([]byte(config.BranchSince)) // TODO date Picker format + if err == nil { + opts.Since = since + } + + guff, _, err := client.Repositories.ListCommits(config.Owner, config.Repo, opts) + if err != nil { return nil, err } @@ -232,6 +266,7 @@ func (*GithubT) getCommits(client *gogithub.Client, config githubConfig) ([]gith ret := []githubBranchCommits{} for k, v := range guff { + if guff[k].Commit != nil { if guff[k].Commit.Committer.Date != nil { y, m, d := (*guff[k].Commit.Committer.Date).Date() @@ -337,7 +372,7 @@ func (*GithubT) Render(config, data string) string { var err error t, err = t.Parse(` -
There are {{ .CommitCount }} commits for branch {{.Config.Branch}} of repository {{.Repo.Name}}.
+There are {{ .CommitCount }} commits for branch {{.Config.Branch}} of repository {{.Repo.Name}}.