From 7b178a76b28aab9ed89e3f621edf11825fca1ff9 Mon Sep 17 00:00:00 2001 From: HarveyKandola Date: Tue, 7 Aug 2018 19:43:25 +0100 Subject: [PATCH] Render Jira issues --- domain/section/gemini/gemini.go | 1 - domain/section/jira/jira.go | 157 ++++++++++-------- .../components/section/jira/type-editor.js | 49 ++++-- 3 files changed, 122 insertions(+), 85 deletions(-) diff --git a/domain/section/gemini/gemini.go b/domain/section/gemini/gemini.go index 018f3e72..5700513f 100644 --- a/domain/section/gemini/gemini.go +++ b/domain/section/gemini/gemini.go @@ -145,7 +145,6 @@ func (p *Provider) Refresh(ctx *provider.Context, config, data string) (newData } j, err := json.Marshal(items) - if err != nil { p.Runtime.Log.Error("unable to marshal gemini items", err) return diff --git a/domain/section/jira/jira.go b/domain/section/jira/jira.go index 38cc8d74..f725cf82 100644 --- a/domain/section/jira/jira.go +++ b/domain/section/jira/jira.go @@ -49,73 +49,68 @@ func (*Provider) Meta() provider.TypeMeta { } // Render converts Jira data into HTML suitable for browser rendering. -func (*Provider) Render(ctx *provider.Context, config, data string) string { - return "

Something

" +func (p *Provider) Render(ctx *provider.Context, config, data string) string { + // issues := []jira.Issue{} + // var c = jiraConfig{} + + // json.Unmarshal([]byte(data), &issues) + // json.Unmarshal([]byte(config), &c) + + var c = jiraConfig{} + err := json.Unmarshal([]byte(config), &c) + if err != nil { + p.Runtime.Log.Error("Unable to read Jira config", err) + return "" + } + + creds, err := getCredentials(ctx, p.Store) + if err != nil { + p.Runtime.Log.Error("unable to fetch Jira connector configuration", err) + return "" + } + + client, _, err := authenticate(creds) + if err != nil { + p.Runtime.Log.Error("unable to authenticate with Jira", err) + return "" + } + + issues, err := getIssues(c, client) + + return generateGrid(issues) } // Refresh fetches latest issues list. func (p *Provider) Refresh(ctx *provider.Context, config, data string) (newData string) { - // var c = geminiConfig{} - // err := json.Unmarshal([]byte(config), &c) + var c = jiraConfig{} + err := json.Unmarshal([]byte(config), &c) + if err != nil { + p.Runtime.Log.Error("Unable to read Jira config", err) + return + } - // if err != nil { - // p.Runtime.Log.Error("Unable to read Gemini config", err) - // return - // } + creds, err := getCredentials(ctx, p.Store) + if err != nil { + p.Runtime.Log.Error("unable to fetch Jira connector configuration", err) + return + } - // c.Clean(ctx, p.Store) + client, _, err := authenticate(creds) + if err != nil { + p.Runtime.Log.Error("unable to authenticate with Jira", err) + return + } - // if len(c.URL) == 0 { - // p.Runtime.Log.Info("Gemini.Refresh received empty URL") - // return - // } + issues, err := getIssues(c, client) - // if len(c.Username) == 0 { - // p.Runtime.Log.Info("Gemini.Refresh received empty username") - // return - // } + j, err := json.Marshal(issues) + if err != nil { + p.Runtime.Log.Error("unable to marshal Jira items", err) + return + } - // if len(c.APIKey) == 0 { - // p.Runtime.Log.Info("Gemini.Refresh received empty API key") - // return - // } + newData = string(j) - // req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/items/card/%d", c.URL, c.WorkspaceID), nil) - // // req.Header.Set("Content-Type", "application/json") - - // creds := []byte(fmt.Sprintf("%s:%s", c.Username, c.APIKey)) - // req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString(creds)) - - // client := &http.Client{} - // res, err := client.Do(req) - - // if err != nil { - // fmt.Println(err) - // return - // } - - // if res.StatusCode != http.StatusOK { - // return - // } - - // defer res.Body.Close() - // var items []geminiItem - - // dec := json.NewDecoder(res.Body) - // err = dec.Decode(&items) - // if err != nil { - // p.Runtime.Log.Error("unable to Decode gemini items", err) - // return - // } - - // j, err := json.Marshal(items) - - // if err != nil { - // p.Runtime.Log.Error("unable to marshal gemini items", err) - // return - // } - - // newData = string(j) return } @@ -130,8 +125,10 @@ func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http } switch method { - case "preview": - preview(ctx, p.Store, w, r) + case "previewIssues": + previewIssues(ctx, p.Store, w, r) + case "previewGrid": + previewGrid(ctx, p.Store, w, r) case "auth": auth(ctx, p.Store, w, r) } @@ -155,7 +152,33 @@ func auth(ctx *provider.Context, store *domain.Store, w http.ResponseWriter, r * provider.WriteJSON(w, "OK") } -func preview(ctx *provider.Context, store *domain.Store, w http.ResponseWriter, r *http.Request) { +func previewIssues(ctx *provider.Context, store *domain.Store, w http.ResponseWriter, r *http.Request) { + creds, err := getCredentials(ctx, store) + if err != nil { + provider.WriteForbidden(w) + return + } + + client, _, err := authenticate(creds) + if err != nil { + fmt.Println(err) + provider.WriteError(w, logID, err) + return + } + + config, err := readConfig(ctx, store, w, r) + if err != nil { + fmt.Println(err) + provider.WriteError(w, logID, err) + return + } + + issues, err := getIssues(config, client) + + provider.WriteJSON(w, issues) +} + +func previewGrid(ctx *provider.Context, store *domain.Store, w http.ResponseWriter, r *http.Request) { creds, err := getCredentials(ctx, store) if err != nil { provider.WriteForbidden(w) @@ -245,16 +268,18 @@ func generateGrid(issues []jira.Issue) string { payload.Issues = issues buffer := new(bytes.Buffer) - t.Execute(buffer, payload) + err := t.Execute(buffer, payload) + + if err != nil { + fmt.Println("Jira render error", err) + } return buffer.String() - } type jiraConfig struct { - JQL string `json:"jql"` - ItemCount int `json:"itemCount"` - Filter map[string]interface{} `json:"filter"` + JQL string `json:"jql"` + ItemCount int `json:"itemCount"` } type jiraLogin struct { @@ -270,7 +295,7 @@ type jiraGrid struct { // the HTML that is rendered by this section. const renderTemplate = ` -

Showing {{.ItemCount}} Jira issues

+

{{.ItemCount}} items

diff --git a/gui/app/components/section/jira/type-editor.js b/gui/app/components/section/jira/type-editor.js index f45b6be3..78bf8a83 100644 --- a/gui/app/components/section/jira/type-editor.js +++ b/gui/app/components/section/jira/type-editor.js @@ -20,6 +20,7 @@ export default Component.extend(SectionMixin, TooltipMixin, { waiting: false, authenticated: false, issuesGrid: '', + issuesList: null, init() { this._super(...arguments); @@ -52,48 +53,60 @@ export default Component.extend(SectionMixin, TooltipMixin, { .then((response) => { // eslint-disable-line no-unused-vars this.set('authenticated', true); this.set('waiting', false); + + this.generatePreview(); }, (reason) => { // eslint-disable-line no-unused-vars this.set('authenticated', false); this.set('waiting', false); }); }, + generatePreview() { + this.set('waiting', true); + + this.get('sectionService').fetch(this.get('page'), 'previewIssues', this.get('config')) + .then((response) => { // eslint-disable-line no-unused-vars + this.set('issuesList', response); + this.set('authenticated', true); + this.set('waiting', false); + + this.get('sectionService').fetchText(this.get('page'), 'previewGrid', this.get('config')) + .then((response) => { // eslint-disable-line no-unused-vars + this.set('issuesGrid', response); + }, (reason) => { // eslint-disable-line no-unused-vars + console.log(reason); // eslint-disable-line no-console + this.set('issuesGrid', ''); + }); + }, (reason) => { // eslint-disable-line no-unused-vars + console.log(reason); // eslint-disable-line no-console + this.set('issuesList', []); + this.set('authenticated', false); + this.set('waiting', false); + }); + }, + actions: { isDirty() { return this.get('isDirty'); }, onPreview() { - this.set('waiting', true); - - this.get('sectionService').fetchText(this.get('page'), 'preview', this.get('config')) - .then((response) => { // eslint-disable-line no-unused-vars - this.set('issuesGrid', response); - this.set('authenticated', true); - this.set('waiting', false); - }, (reason) => { // eslint-disable-line no-unused-vars - console.log(reason); - this.set('issuesGrid', ''); - this.set('authenticated', false); - this.set('waiting', false); - }); + this.generatePreview(); }, onCancel() { - let cb = this.get('onCancel'); - cb(); + this.get('onCancel')(); }, onAction(title) { let page = this.get('page'); let meta = this.get('meta'); page.set('title', title); - meta.set('rawBody', JSON.stringify(this.get("items"))); + meta.set('rawBody', JSON.stringify(this.get("issuesList"))); meta.set('config', JSON.stringify(this.get('config'))); meta.set('externalSource', true); - let cb = this.get('onAction'); - cb(page, meta); + this.get('onAction')(page, meta); } } });