mirror of
https://github.com/documize/community.git
synced 2025-08-08 23:15:29 +02:00
refactor existing github functionality
This commit is contained in:
parent
a815d27993
commit
af8e5c138f
5 changed files with 127 additions and 24 deletions
|
@ -20,6 +20,8 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
|
|||
busy: false,
|
||||
authenticated: false,
|
||||
config: {},
|
||||
owners: null,
|
||||
noOwners: false, // TODO required?
|
||||
repos: null,
|
||||
noRepos: false,
|
||||
|
||||
|
@ -38,7 +40,8 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
|
|||
token: "",
|
||||
repo: null,
|
||||
lists: [],
|
||||
owner: "",
|
||||
owner: null,
|
||||
owner_name: "",
|
||||
repo_name: "",
|
||||
branch: "",
|
||||
branchURL: "",
|
||||
|
@ -48,6 +51,7 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
|
|||
|
||||
try {
|
||||
let metaConfig = JSON.parse(self.get('meta.config'));
|
||||
config.owner = metaConfig.owner;
|
||||
config.repo = metaConfig.repo;
|
||||
config.lists = metaConfig.lists;
|
||||
} catch (e) {}
|
||||
|
@ -78,6 +82,46 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
|
|||
this.destroyTooltips();
|
||||
},
|
||||
|
||||
|
||||
getOwnerLists() {
|
||||
this.set('busy', true);
|
||||
|
||||
let self = this;
|
||||
let owners = this.get('owners');
|
||||
let thisOwner = this.get('config.owner');
|
||||
let page = this.get('page');
|
||||
|
||||
console.log("owner", thisOwner);
|
||||
|
||||
if (is.null(owners) || is.undefined(owners) || owners.length === 0) {
|
||||
this.set('noOwners', true);
|
||||
return;
|
||||
}
|
||||
|
||||
this.set('noOwners', false);
|
||||
|
||||
if (is.null(thisOwner) || is.undefined(thisOwner)) {
|
||||
if (owners.length) {
|
||||
thisOwner = owners[0];
|
||||
this.set('config.owner', thisOwner);
|
||||
}
|
||||
} else {
|
||||
this.set('config.owner', owners.findBy('id', thisOwner.id));
|
||||
}
|
||||
|
||||
this.get('sectionService').fetch(page, "repos", self.get('config'))
|
||||
.then(function(lists) {
|
||||
self.set('busy', false);
|
||||
self.set('repos', lists);
|
||||
self.getRepoLists();
|
||||
}, function(error) { //jshint ignore: line
|
||||
self.set('busy', false);
|
||||
self.set('authenticated', false);
|
||||
self.showNotification("Unable to fetch repositories");
|
||||
console.log(error);
|
||||
});
|
||||
},
|
||||
|
||||
getRepoLists() {
|
||||
this.set('busy', true);
|
||||
|
||||
|
@ -86,6 +130,8 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
|
|||
let thisRepo = this.get('config.repo');
|
||||
let page = this.get('page');
|
||||
|
||||
console.log("repo", thisRepo);
|
||||
|
||||
if (is.null(repos) || is.undefined(repos) || repos.length === 0) {
|
||||
this.set('noRepos', true);
|
||||
return;
|
||||
|
@ -153,15 +199,15 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
|
|||
self.set('busy', true);
|
||||
let page = this.get('page');
|
||||
|
||||
self.get('sectionService').fetch(page, "repos", self.get('config'))
|
||||
.then(function(repos) {
|
||||
self.get('sectionService').fetch(page, "owners", self.get('config'))
|
||||
.then(function(owners) {
|
||||
self.set('busy', false);
|
||||
self.set('repos', repos);
|
||||
self.getRepoLists();
|
||||
self.set('owners', owners);
|
||||
self.getOwnerLists();
|
||||
}, function(error) { //jshint ignore: line
|
||||
self.set('busy', false);
|
||||
self.set('authenticated', false);
|
||||
self.showNotification("Unable to fetch repos");
|
||||
self.showNotification("Unable to fetch owners");
|
||||
console.log(error);
|
||||
});
|
||||
|
||||
|
@ -179,6 +225,14 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
|
|||
|
||||
},
|
||||
|
||||
onOwnerChange(thisOwner) {
|
||||
console.log(thisOwner);
|
||||
this.set('isDirty', true);
|
||||
this.set('config.owner', thisOwner);
|
||||
this.set('config.repos', []);
|
||||
this.getOwnerLists();
|
||||
},
|
||||
|
||||
onRepoChange(thisRepo) {
|
||||
this.set('isDirty', true);
|
||||
this.set('config.repo', thisRepo);
|
||||
|
@ -212,4 +266,4 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
|
|||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
|
@ -11,8 +11,13 @@
|
|||
<div class="pull-left width-50">
|
||||
<div class="input-form">
|
||||
<div class="heading">
|
||||
<div class="title">Select Repository & Branch</div>
|
||||
<div class="tip">Choose source of code commits to be displayed</div>
|
||||
<div class="title">Select Repository</div>
|
||||
<div class="tip">Choose source of code information to be displayed</div>
|
||||
</div>
|
||||
<div class="input-control">
|
||||
<label>Organizations and User</label>
|
||||
<div class="tip">Select organization or username</div>
|
||||
{{ui-select id="owners-dropdown" content=owners action=(action 'onOwnerChange') optionValuePath="id" optionLabelPath="name" selection=config.owner}}
|
||||
</div>
|
||||
{{#if noRepos}}
|
||||
<div class="input-control">
|
||||
|
@ -22,7 +27,7 @@
|
|||
<div class="input-control">
|
||||
<label>Repositories</label>
|
||||
<div class="tip">Select repository</div>
|
||||
{{ui-select id="boards-dropdown" content=repos action=(action 'onRepoChange') optionValuePath="id" optionLabelPath="name" selection=config.repo}}
|
||||
{{ui-select id="repos-dropdown" content=repos action=(action 'onRepoChange') optionValuePath="id" optionLabelPath="name" selection=config.repo}}
|
||||
</div>
|
||||
<div class="input-control">
|
||||
<label>Branches</label>
|
||||
|
|
|
@ -129,7 +129,7 @@ func (t *Provider) Command(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
provider.WriteJSON(w, render)
|
||||
|
||||
case "repos":
|
||||
case "owners":
|
||||
|
||||
me, _, err := client.Users.Get("")
|
||||
if err != nil {
|
||||
|
@ -145,22 +145,40 @@ func (t *Provider) Command(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
owners := make([]string, 1+len(orgs))
|
||||
owners[0] = *me.Login
|
||||
owners := make([]githubOwner, 1+len(orgs))
|
||||
owners[0] = githubOwner{ID: *me.Login, Name: *me.Login}
|
||||
for ko, vo := range orgs {
|
||||
owners[1+ko] = *vo.Login
|
||||
id := 1 + ko
|
||||
owners[id].ID = *vo.Login
|
||||
owners[id].Name = *vo.Login
|
||||
}
|
||||
|
||||
owners = sortOwners(owners)
|
||||
|
||||
//fmt.Printf("DEBUG owners %#v\n", owners)
|
||||
|
||||
provider.WriteJSON(w, owners)
|
||||
|
||||
case "repos":
|
||||
|
||||
var render []githubRepo
|
||||
for ko, vo := range owners {
|
||||
if config.Owner != "" {
|
||||
|
||||
me, _, err := client.Users.Get("")
|
||||
if err != nil {
|
||||
log.Error("github get user details:", err)
|
||||
provider.WriteError(w, "github", err)
|
||||
return
|
||||
}
|
||||
|
||||
var repos []gogithub.Repository
|
||||
if vo == *me.Login {
|
||||
repos, _, err = client.Repositories.List(vo, nil)
|
||||
if config.Owner == *me.Login {
|
||||
repos, _, err = client.Repositories.List(config.Owner, nil)
|
||||
} else {
|
||||
opt := &gogithub.RepositoryListByOrgOptions{
|
||||
ListOptions: gogithub.ListOptions{PerPage: 100},
|
||||
}
|
||||
repos, _, err = client.Repositories.ListByOrg(vo, opt)
|
||||
repos, _, err = client.Repositories.ListByOrg(config.Owner, opt)
|
||||
}
|
||||
if err != nil {
|
||||
log.Error("github get user/org repositories:", err)
|
||||
|
@ -174,16 +192,15 @@ func (t *Provider) Command(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
render = append(render,
|
||||
githubRepo{
|
||||
Name: vo + "/" + *vr.Name + private,
|
||||
ID: fmt.Sprintf("%s:%d:%s:%d", vo, ko, *vr.Name, kr),
|
||||
Owner: vo,
|
||||
Name: config.Owner + "/" + *vr.Name + private,
|
||||
ID: fmt.Sprintf("%s:%s:%d", config.Owner, *vr.Name, kr),
|
||||
Owner: config.Owner,
|
||||
Repo: *vr.Name,
|
||||
Private: *vr.Private,
|
||||
URL: *vr.HTMLURL,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
render = sortRepos(render)
|
||||
|
||||
provider.WriteJSON(w, render)
|
||||
|
@ -213,6 +230,7 @@ func (t *Provider) Command(w http.ResponseWriter, r *http.Request) {
|
|||
provider.WriteJSON(w, render)
|
||||
|
||||
default:
|
||||
log.ErrorString("Github connector unknown method: " + method)
|
||||
provider.WriteEmpty(w)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,6 +42,16 @@ const renderTemplate = `
|
|||
</div>
|
||||
`
|
||||
|
||||
type githubOwner 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"` // TODO review field use
|
||||
//URL string `json:"url"`
|
||||
}
|
||||
|
||||
type githubRepo struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
|
@ -76,12 +86,13 @@ type githubCommit struct {
|
|||
type githubConfig struct {
|
||||
AppKey string `json:"appKey"` // TODO keep?
|
||||
Token string `json:"token"`
|
||||
Owner string `json:"owner"`
|
||||
Owner string `json:"owner_name"`
|
||||
Repo string `json:"repo_name"`
|
||||
Branch string `json:"branch"`
|
||||
BranchURL string `json:"branchURL"`
|
||||
BranchSince string `json:"branchSince"`
|
||||
BranchLines int `json:"branchLines"`
|
||||
OwnerInfo githubOwner `json:"owner"`
|
||||
RepoInfo githubRepo `json:"repo"`
|
||||
ClientID string `json:"clientId"`
|
||||
CallbackURL string `json:"callbackUrl"`
|
||||
|
@ -91,7 +102,7 @@ type githubConfig struct {
|
|||
func (c *githubConfig) Clean() {
|
||||
c.AppKey = strings.TrimSpace(c.AppKey) // TODO keep?
|
||||
c.Token = strings.TrimSpace(c.Token)
|
||||
c.Owner = c.RepoInfo.Owner
|
||||
c.Owner = c.OwnerInfo.Name
|
||||
c.Repo = c.RepoInfo.Repo
|
||||
for _, l := range c.Lists {
|
||||
if l.Included {
|
||||
|
|
|
@ -27,3 +27,18 @@ func sortRepos(in []githubRepo) []githubRepo {
|
|||
sort.Sort(sts)
|
||||
return []githubRepo(sts)
|
||||
}
|
||||
|
||||
// sort owners in order that that should be presented.
|
||||
type ownersToSort []githubOwner
|
||||
|
||||
func (s ownersToSort) Len() int { return len(s) }
|
||||
func (s ownersToSort) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||
func (s ownersToSort) Less(i, j int) bool {
|
||||
return s[i].Name < s[j].Name
|
||||
}
|
||||
|
||||
func sortOwners(in []githubOwner) []githubOwner {
|
||||
sts := ownersToSort(in)
|
||||
sort.Sort(sts)
|
||||
return []githubOwner(sts)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue