mirror of
https://github.com/documize/community.git
synced 2025-08-10 07:55:25 +02:00
commit
ba0ae04df0
3 changed files with 124 additions and 83 deletions
|
@ -24,44 +24,53 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
|
||||||
noRepos: false,
|
noRepos: false,
|
||||||
|
|
||||||
didReceiveAttrs() {
|
didReceiveAttrs() {
|
||||||
let config = {};
|
|
||||||
|
|
||||||
try {
|
let self = this;
|
||||||
config = JSON.parse(this.get('meta.config'));
|
let page = this.get('page');
|
||||||
} catch (e) {}
|
console.log(this.get('config.clientId'));
|
||||||
|
|
||||||
if (is.empty(config)) {
|
if (is.undefined(this.get('config.clientId')) || is.undefined(this.get('config.callbackUrl'))) {
|
||||||
config = {
|
self.get('sectionService').fetch(page, "config", {})
|
||||||
clientId: "8574d0c34142dcdc53f6",
|
.then(function(cfg) {
|
||||||
callbackUrl: "https://localhost:5001/api/public/validate?section=github",
|
let config = {};
|
||||||
token: "",
|
|
||||||
repo: null,
|
try {
|
||||||
lists: [],
|
config = JSON.parse(self.get('meta.config'));
|
||||||
owner: "",
|
} catch (e) {}
|
||||||
repo_name: "",
|
|
||||||
branch: ""
|
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() {
|
willDestroyElement() {
|
||||||
|
@ -160,8 +169,6 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
|
||||||
auth() {
|
auth() {
|
||||||
|
|
||||||
let self = this;
|
let self = this;
|
||||||
|
|
||||||
|
|
||||||
self.set('busy', true);
|
self.set('busy', true);
|
||||||
self.set('authenticated', false);
|
self.set('authenticated', false);
|
||||||
let target = "https://github.com/login/oauth/authorize?client_id=" + self.get('config.clientId') +
|
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);
|
"&state=" + encodeURIComponent(window.location.href);
|
||||||
window.location.href = target;
|
window.location.href = target;
|
||||||
|
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
onRepoChange(thisRepo) {
|
onRepoChange(thisRepo) {
|
||||||
|
|
|
@ -33,9 +33,4 @@ func (*github) Meta() TypeMeta {
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
sectionsMap["github"] = &github{}
|
sectionsMap["github"] = &github{}
|
||||||
|
|
||||||
// TODO make both soft and alterable at runtime
|
|
||||||
gh.ClientID = "8574d0c34142dcdc53f6"
|
|
||||||
gh.ClientSecret = "628e04d1575e234fbf477ff7b5f7dbfc95074c9e"
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,15 +21,27 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/documize/community/documize/api/request"
|
||||||
"github.com/documize/community/wordsmith/log"
|
"github.com/documize/community/wordsmith/log"
|
||||||
|
|
||||||
// vendored locally
|
|
||||||
gogithub "github.com/google/go-github/github"
|
gogithub "github.com/google/go-github/github"
|
||||||
"golang.org/x/oauth2"
|
"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 {
|
type GithubT struct {
|
||||||
|
|
||||||
|
@ -68,7 +80,20 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) {
|
||||||
method := query.Get("method")
|
method := query.Get("method")
|
||||||
|
|
||||||
if len(method) == 0 {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,7 +101,9 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) {
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeMessage(w, "github", "Bad body")
|
msg := "Bad body"
|
||||||
|
log.ErrorString("github: " + msg)
|
||||||
|
writeMessage(w, "gitub", msg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,6 +111,7 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) {
|
||||||
err = json.Unmarshal(body, &config)
|
err = json.Unmarshal(body, &config)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Error("github Command Unmarshal", err)
|
||||||
writeError(w, "github", err)
|
writeError(w, "github", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -91,7 +119,9 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) {
|
||||||
config.Clean()
|
config.Clean()
|
||||||
|
|
||||||
if len(config.Token) == 0 {
|
if len(config.Token) == 0 {
|
||||||
writeMessage(w, "github", "Missing token")
|
msg := "Missing token"
|
||||||
|
log.ErrorString("github: " + msg)
|
||||||
|
writeMessage(w, "gitub", msg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,9 +133,8 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
render, err := t.getCommits(client, config)
|
render, err := t.getCommits(client, config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//fmt.Println("Error:", err.Error())
|
log.Error("github getCommits:", err)
|
||||||
writeError(w, "github", err)
|
writeError(w, "github", err)
|
||||||
// TODO log error
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,16 +144,14 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
me, _, err := client.Users.Get("")
|
me, _, err := client.Users.Get("")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//fmt.Println(err)
|
log.Error("github get user details:", err)
|
||||||
// TODO log error
|
|
||||||
writeError(w, "github", err)
|
writeError(w, "github", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
orgs, _, err := client.Organizations.List("", nil)
|
orgs, _, err := client.Organizations.List("", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//fmt.Println(err)
|
log.Error("github get user's organisations:", err)
|
||||||
// TODO log error
|
|
||||||
writeError(w, "github", err)
|
writeError(w, "github", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -141,11 +168,13 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) {
|
||||||
if vo == *me.Login {
|
if vo == *me.Login {
|
||||||
repos, _, err = client.Repositories.List(vo, nil)
|
repos, _, err = client.Repositories.List(vo, nil)
|
||||||
} else {
|
} 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 {
|
if err != nil {
|
||||||
//fmt.Println(err)
|
log.Error("github get user/org repositories:", err)
|
||||||
// TODO log error
|
|
||||||
writeError(w, "github", err)
|
writeError(w, "github", err)
|
||||||
return
|
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)
|
render = sortRepos(render)
|
||||||
|
|
||||||
writeJSON(w, 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
|
writeJSON(w, []githubBranch{}) // we have nothing to return
|
||||||
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 {
|
if err != nil {
|
||||||
//fmt.Println(err)
|
log.Error("github get branch details:", err)
|
||||||
// TODO log error
|
|
||||||
writeError(w, "github", err)
|
writeError(w, "github", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -195,6 +217,7 @@ func (t *GithubT) Command(w http.ResponseWriter, r *http.Request) {
|
||||||
Name: *vb.Name,
|
Name: *vb.Name,
|
||||||
ID: fmt.Sprintf("%s:%s:%s:%d", config.Owner, config.Repo, *vb.Name, kc),
|
ID: fmt.Sprintf("%s:%s:%s:%d", config.Owner, config.Repo, *vb.Name, kc),
|
||||||
Included: false,
|
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) {
|
func (*GithubT) getCommits(client *gogithub.Client, config githubConfig) ([]githubBranchCommits, error) {
|
||||||
|
|
||||||
guff, _, err := client.Repositories.ListCommits(config.Owner, config.Repo,
|
opts := &gogithub.CommitsListOptions{
|
||||||
&gogithub.CommitsListOptions{SHA: config.Branch})
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -232,6 +266,7 @@ func (*GithubT) getCommits(client *gogithub.Client, config githubConfig) ([]gith
|
||||||
ret := []githubBranchCommits{}
|
ret := []githubBranchCommits{}
|
||||||
|
|
||||||
for k, v := range guff {
|
for k, v := range guff {
|
||||||
|
|
||||||
if guff[k].Commit != nil {
|
if guff[k].Commit != nil {
|
||||||
if guff[k].Commit.Committer.Date != nil {
|
if guff[k].Commit.Committer.Date != nil {
|
||||||
y, m, d := (*guff[k].Commit.Committer.Date).Date()
|
y, m, d := (*guff[k].Commit.Committer.Date).Date()
|
||||||
|
@ -337,7 +372,7 @@ func (*GithubT) Render(config, data string) string {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
t, err = t.Parse(`
|
t, err = t.Parse(`
|
||||||
<p>There are {{ .CommitCount }} commits for branch <span class="bold">{{.Config.Branch}}</span> of repository <a href="{{ .Repo.URL }}">{{.Repo.Name}}.</a></p>
|
<p>There are {{ .CommitCount }} commits for branch <a href="{{.Config.BranchURL}}">{{.Config.Branch}}</a> of repository <a href="{{ .Repo.URL }}">{{.Repo.Name}}.</a></p>
|
||||||
<div class="github-board">
|
<div class="github-board">
|
||||||
{{range $data := .Data}}
|
{{range $data := .Data}}
|
||||||
<div class="github-group-title">
|
<div class="github-group-title">
|
||||||
|
@ -364,14 +399,14 @@ func (*GithubT) Render(config, data string) string {
|
||||||
`)
|
`)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO log?
|
log.Error("github render template.Parse error:", err)
|
||||||
return "Documize internal github template.Parse error: " + err.Error()
|
return "Documize internal github template.Parse error: " + err.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer := new(bytes.Buffer)
|
buffer := new(bytes.Buffer)
|
||||||
err = t.Execute(buffer, payload)
|
err = t.Execute(buffer, payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO log?
|
log.Error("github render template.Execute error:", err)
|
||||||
return "Documize internal github template.Execute error: " + err.Error()
|
return "Documize internal github template.Execute error: " + err.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -407,6 +442,7 @@ type githubBranch struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Included bool `json:"included"`
|
Included bool `json:"included"`
|
||||||
|
URL string `json:"url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type githubBranchCommits struct {
|
type githubBranchCommits struct {
|
||||||
|
@ -424,15 +460,18 @@ type githubCommit struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type githubConfig struct {
|
type githubConfig struct {
|
||||||
AppKey string `json:"appKey"` // TODO keep?
|
AppKey string `json:"appKey"` // TODO keep?
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
Owner string `json:"owner"`
|
Owner string `json:"owner"`
|
||||||
Repo string `json:"repo_name"`
|
Repo string `json:"repo_name"`
|
||||||
Branch string `json:"branch"`
|
Branch string `json:"branch"`
|
||||||
RepoInfo githubRepo `json:"repo"`
|
BranchURL string `json:"branchURL"`
|
||||||
ClientID string `json:"clientId"`
|
BranchSince string `json:"branchSince"`
|
||||||
CallbackURL string `json:"callbackUrl"`
|
BranchLines int `json:"branchLines"`
|
||||||
Lists []githubRepo `json:"lists"`
|
RepoInfo githubRepo `json:"repo"`
|
||||||
|
ClientID string `json:"clientId"`
|
||||||
|
CallbackURL string `json:"callbackUrl"`
|
||||||
|
Lists []githubBranch `json:"lists"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *githubConfig) Clean() {
|
func (c *githubConfig) Clean() {
|
||||||
|
@ -443,6 +482,7 @@ func (c *githubConfig) Clean() {
|
||||||
for _, l := range c.Lists {
|
for _, l := range c.Lists {
|
||||||
if l.Included {
|
if l.Included {
|
||||||
c.Branch = l.Name
|
c.Branch = l.Name
|
||||||
|
c.BranchURL = l.URL
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -459,8 +499,8 @@ func Callback(res http.ResponseWriter, req *http.Request) error {
|
||||||
state := req.URL.Query().Get("state")
|
state := req.URL.Query().Get("state")
|
||||||
|
|
||||||
ghurl := "https://github.com/login/oauth/access_token"
|
ghurl := "https://github.com/login/oauth/access_token"
|
||||||
vals := "client_id=" + ClientID
|
vals := "client_id=" + clientID()
|
||||||
vals += "&client_secret=" + ClientSecret
|
vals += "&client_secret=" + clientSecret()
|
||||||
vals += "&code=" + code
|
vals += "&code=" + code
|
||||||
vals += "&state=" + state
|
vals += "&state=" + state
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue