From 768bedb701c42b8e1fd73c2dc5c2902030d2dee2 Mon Sep 17 00:00:00 2001 From: Elliott Stoneham Date: Thu, 2 Jun 2016 07:46:30 +0100 Subject: [PATCH 1/7] github config from table --- documize/section/github.go | 5 ----- documize/section/github/github.go | 18 ++++++++++++++---- 2 files changed, 14 insertions(+), 9 deletions(-) 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 106f5d74..f85a3bcb 100644 --- a/documize/section/github/github.go +++ b/documize/section/github/github.go @@ -23,13 +23,23 @@ import ( "strings" "github.com/documize/community/wordsmith/log" + "github.com/documize/community/documize/api/request" - // 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{ + return request.ConfigString(configKey,"authorizationCallbackURL") +} type GithubT struct { @@ -458,8 +468,8 @@ func Callback(res http.ResponseWriter, req *http.Request) error { state := req.URL.Query().Get("state") ghurl := "https://github.com/login/oauth/access_token" - vals := "client_id=" + ClientID - vals += "&client_secret=" + ClientSecret + vals := "client_id=" + clientID() + vals += "&client_secret=" + clientSecret() vals += "&code=" + code vals += "&state=" + state From b246ddfb6adce4b4b320c18aa9e147dd89e42b78 Mon Sep 17 00:00:00 2001 From: Elliott Stoneham Date: Thu, 2 Jun 2016 07:52:39 +0100 Subject: [PATCH 2/7] github repo limit 100 (up from 30) --- documize/section/github/github.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/documize/section/github/github.go b/documize/section/github/github.go index f85a3bcb..fe4b5572 100644 --- a/documize/section/github/github.go +++ b/documize/section/github/github.go @@ -22,8 +22,8 @@ import ( "sort" "strings" - "github.com/documize/community/wordsmith/log" "github.com/documize/community/documize/api/request" + "github.com/documize/community/wordsmith/log" gogithub "github.com/google/go-github/github" "golang.org/x/oauth2" @@ -31,14 +31,14 @@ import ( const configKey = "SECTION-GITHUB" -func clientID() string{ - return request.ConfigString(configKey,"clientID") -} -func clientSecret() string{ - return request.ConfigString(configKey,"clientSecret") +func clientID() string { + return request.ConfigString(configKey, "clientID") } -func authorizationCallbackURL() string{ - return request.ConfigString(configKey,"authorizationCallbackURL") +func clientSecret() string { + return request.ConfigString(configKey, "clientSecret") +} +func authorizationCallbackURL() string { + return request.ConfigString(configKey, "authorizationCallbackURL") } type GithubT struct { @@ -151,7 +151,10 @@ 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) From 873509257d2e340e132386a3442ea11c4ebad4d0 Mon Sep 17 00:00:00 2001 From: Elliott Stoneham Date: Thu, 2 Jun 2016 09:00:07 +0100 Subject: [PATCH 3/7] Clarify URL format, up to 100 branches & commits --- documize/section/github/github.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/documize/section/github/github.go b/documize/section/github/github.go index fe4b5572..0b31a44b 100644 --- a/documize/section/github/github.go +++ b/documize/section/github/github.go @@ -38,6 +38,7 @@ 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") } @@ -195,7 +196,8 @@ 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 @@ -231,7 +233,7 @@ 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}) + &gogithub.CommitsListOptions{SHA: config.Branch, ListOptions: gogithub.ListOptions{PerPage: 100}}) if err != nil { return nil, err } From 59e40f34c152dc6454886f8d2c34fef763caa493 Mon Sep 17 00:00:00 2001 From: Elliott Stoneham Date: Thu, 2 Jun 2016 10:40:06 +0100 Subject: [PATCH 4/7] log github section errors --- documize/section/github/github.go | 39 ++++++++++++++----------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/documize/section/github/github.go b/documize/section/github/github.go index d63d77f1..cff8757d 100644 --- a/documize/section/github/github.go +++ b/documize/section/github/github.go @@ -79,7 +79,9 @@ 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 } @@ -87,7 +89,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 } @@ -95,6 +99,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 } @@ -102,7 +107,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 } @@ -114,9 +121,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 } @@ -126,16 +132,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 } @@ -158,8 +162,7 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) { 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 } @@ -180,13 +183,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) @@ -199,8 +195,7 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) { 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 } @@ -379,14 +374,14 @@ func (*GithubT) Render(config, data string) string { `) if err != nil { - // TODO log? + log.Error("github render template.Parse error:", err) return "Documize internal github template.Parse error: " + err.Error() } buffer := new(bytes.Buffer) err = t.Execute(buffer, payload) if err != nil { - // TODO log? + log.Error("github render template.Execute error:", err) return "Documize internal github template.Execute error: " + err.Error() } From 9cebfbe2190d7108714af00927062825ff015ffc Mon Sep 17 00:00:00 2001 From: Elliott Stoneham Date: Thu, 2 Jun 2016 14:56:50 +0100 Subject: [PATCH 5/7] remove secrets from JS, make branch name clickable --- .../components/section/github/type-editor.js | 81 ++++++++++--------- documize/section/github/github.go | 52 +++++++++--- 2 files changed, 83 insertions(+), 50 deletions(-) diff --git a/app/app/components/section/github/type-editor.js b/app/app/components/section/github/type-editor.js index 17330ef9..1badf76b 100644 --- a/app/app/components/section/github/type-editor.js +++ b/app/app/components/section/github/type-editor.js @@ -24,44 +24,52 @@ 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: "" + }; + 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 +168,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 +175,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/github.go b/documize/section/github/github.go index cff8757d..21ea8ca3 100644 --- a/documize/section/github/github.go +++ b/documize/section/github/github.go @@ -21,6 +21,7 @@ import ( "net/url" "sort" "strings" + "time" "github.com/documize/community/documize/api/request" "github.com/documize/community/wordsmith/log" @@ -85,6 +86,17 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) { 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 + } + defer r.Body.Close() body, err := ioutil.ReadAll(r.Body) @@ -205,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, } } @@ -227,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, ListOptions: gogithub.ListOptions{PerPage: 100}}) + opts := &gogithub.CommitsListOptions{ + SHA: config.Branch, + ListOptions: gogithub.ListOptions{PerPage: 100}} + + var since time.Time + + err := since.UnmarshalText([]byte(config.BranchSince)) + if err == nil { + opts.Since = since + } + + guff, _, err := client.Repositories.ListCommits(config.Owner, config.Repo, opts) + if err != nil { return nil, err } @@ -347,7 +371,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}}.

{{range $data := .Data}}
@@ -417,6 +441,7 @@ type githubBranch struct { ID string `json:"id"` Name string `json:"name"` Included bool `json:"included"` + URL string `json:"url"` } type githubBranchCommits struct { @@ -434,15 +459,17 @@ type githubCommit struct { } type githubConfig struct { - AppKey string `json:"appKey"` // TODO keep? - Token string `json:"token"` - Owner string `json:"owner"` - Repo string `json:"repo_name"` - Branch string `json:"branch"` - RepoInfo githubRepo `json:"repo"` - ClientID string `json:"clientId"` - CallbackURL string `json:"callbackUrl"` - Lists []githubRepo `json:"lists"` + AppKey string `json:"appKey"` // TODO keep? + Token string `json:"token"` + Owner string `json:"owner"` + Repo string `json:"repo_name"` + Branch string `json:"branch"` + BranchURL string `json:"branchURL"` + BranchSince string `json:"branchSince"` + RepoInfo githubRepo `json:"repo"` + ClientID string `json:"clientId"` + CallbackURL string `json:"callbackUrl"` + Lists []githubBranch `json:"lists"` } func (c *githubConfig) Clean() { @@ -453,6 +480,7 @@ func (c *githubConfig) Clean() { for _, l := range c.Lists { if l.Included { c.Branch = l.Name + c.BranchURL = l.URL break } } From 1318f424db773ffa319b4c6a07f5e4ae62e3ac96 Mon Sep 17 00:00:00 2001 From: Elliott Stoneham Date: Thu, 2 Jun 2016 15:02:46 +0100 Subject: [PATCH 6/7] add config.BranchLines --- app/app/components/section/github/type-editor.js | 3 ++- documize/section/github/github.go | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/app/components/section/github/type-editor.js b/app/app/components/section/github/type-editor.js index 1badf76b..4638fb3e 100644 --- a/app/app/components/section/github/type-editor.js +++ b/app/app/components/section/github/type-editor.js @@ -48,7 +48,8 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin, repo_name: "", branch: "", branchURL: "", - branchSince: "" + branchSince: "", + branchLines: 30 }; self.set('config', config); diff --git a/documize/section/github/github.go b/documize/section/github/github.go index 21ea8ca3..438490c2 100644 --- a/documize/section/github/github.go +++ b/documize/section/github/github.go @@ -242,7 +242,7 @@ func (*GithubT) getCommits(client *gogithub.Client, config githubConfig) ([]gith opts := &gogithub.CommitsListOptions{ SHA: config.Branch, - ListOptions: gogithub.ListOptions{PerPage: 100}} + ListOptions: gogithub.ListOptions{PerPage: config.BranchLines}} var since time.Time @@ -466,6 +466,7 @@ type githubConfig struct { Branch string `json:"branch"` BranchURL string `json:"branchURL"` BranchSince string `json:"branchSince"` + BranchLines int `json:"branchLines"` RepoInfo githubRepo `json:"repo"` ClientID string `json:"clientId"` CallbackURL string `json:"callbackUrl"` From c620d965fe08d429917085da7eb342ef8e6f9144 Mon Sep 17 00:00:00 2001 From: Elliott Stoneham Date: Thu, 2 Jun 2016 15:52:35 +0100 Subject: [PATCH 7/7] date picker comment --- documize/section/github/github.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/documize/section/github/github.go b/documize/section/github/github.go index 438490c2..68125077 100644 --- a/documize/section/github/github.go +++ b/documize/section/github/github.go @@ -246,7 +246,7 @@ func (*GithubT) getCommits(client *gogithub.Client, config githubConfig) ([]gith var since time.Time - err := since.UnmarshalText([]byte(config.BranchSince)) + err := since.UnmarshalText([]byte(config.BranchSince)) // TODO date Picker format if err == nil { opts.Since = since } @@ -266,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()