Skip to content

Commit

Permalink
Change gists so they download gist to a project, fixes #229 and fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
bergie committed Jun 15, 2017
1 parent 1fa7d7b commit 0ef7453
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 89 deletions.
34 changes: 34 additions & 0 deletions components/CheckGistExists.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
noflo = require 'noflo'

exports.getComponent = ->
c = new noflo.Component
c.inPorts.add 'in',
datatype: 'object'
c.outPorts.add 'existing',
datatype: 'array'
c.outPorts.add 'new',
datatype: 'object'

noflo.helpers.WirePattern c,
out: ['existing', 'new']
async: true
, (data, groups, out, callback) ->
unless data.state?.projects?.length
# No local projects, pass
out.new.send data
do callback
return

existing = data.state.projects.filter (project) ->
project.gist is data.payload.graph
unless existing.length
out.new.send data
do callback
return

out.existing.send [
'project'
existing[0].id
existing[0].main
]
do callback
58 changes: 58 additions & 0 deletions components/GistToProject.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
noflo = require 'noflo'
octo = require 'octo'
path = require 'path'

exports.getComponent = ->
c = new noflo.Component
c.inPorts.add 'in',
datatype: 'object'
c.outPorts.add 'graph',
datatype: 'object'
c.outPorts.add 'component',
datatype: 'object'
c.outPorts.add 'project',
datatype: 'object'
c.outPorts.add 'error',
datatype: 'object'

noflo.helpers.WirePattern c,
out: ['graph', 'component', 'project']
async: true
, (data, groups, out, callback) ->
api = octo.api()
api.token data.state.user['github-token'] if data.state.user?['github-token']

project =
id: data.payload.graph
gist: data.payload.graph

request = api.get "/gists/#{data.payload.graph}"
request.on 'success', (res) ->
for name, file of res.body.files
basename = path.basename name, path.extname name
if path.extname(name) is '.json'
# JSON graph
graph = JSON.parse file.content
graph.properties = {} unless graph.properties
graph.properties.project = project.id
graph.properties.id = "#{project.id}_#{basename}"
project.main = graph.properties.id unless project.main
project.name = graph.properties.name unless project.name
if graph.properties?.environment?.type
project.type = graph.properties.environment.type unless project.type
out.graph.send graph
continue
# Component
component =
name: basename
language: file.language
project: project.id
code: file.content
tests: ''
out.component.send component
continue
out.project.send project
do callback
request.on 'error', (err) ->
callback err.error or err.body
do request
48 changes: 0 additions & 48 deletions components/PopulateExampleData.coffee

This file was deleted.

31 changes: 31 additions & 0 deletions graphs/GithubMiddleware.fbp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
INPORT=Dispatch.IN:IN
OUTPORT=Dispatch.PASS:PASS
OUTPORT=NewActions.OUT:NEW

'github:open,github:gist' -> ROUTES Dispatch(ui/DispatchAction)

# New actions generated by this middleware
'application:hash' -> ACTION ApplicationRedirectAction(ui/SetAction)
ApplicationRedirectAction OUT -> IN NewActions(core/Merge)
'project:save:graph' -> ACTION SaveGraphAction(ui/SetAction)
SaveGraphAction OUT -> IN NewActions(core/Merge)
'project:save:component' -> ACTION SaveComponentAction(ui/SetAction)
SaveComponentAction OUT -> IN NewActions(core/Merge)
'project:save:project' -> ACTION SaveProjectAction(ui/SetAction)
SaveProjectAction OUT -> IN NewActions(core/Merge)
'github:error' -> ACTION ErrorAction(ui/SetAction)
ErrorAction OUT -> IN NewActions(core/Merge)

# Downloading a GitHub project
Dispatch HANDLE[0] -> IN Output(core/Output)

# Downloading a gist example
# First check if we already have a local version of the gist
Dispatch HANDLE[1] -> IN CheckGist(ui/CheckGistExists)
# If it already exists, redirect
CheckGist EXISTING -> IN ApplicationRedirectAction
# If not, download as new project
CheckGist NEW -> IN GetGist(ui/GistToProject)
GetGist GRAPH -> IN LoadGraph(graph/LoadJson) OUT -> IN SaveGraphAction
GetGist COMPONENT -> IN SaveComponentAction
GetGist PROJECT -> IN SaveProjectAction
12 changes: 1 addition & 11 deletions graphs/GithubStorage.fbp
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,11 @@ OUTPORT=Sync.CHANGED:OUT
GetToken OUT -> STRING HoldToken(strings/SendString)

# Route opening requests
'gist,sync,fetch' -> ROUTES Dispatch(routers/GroupRouter)
'sync,fetch' -> ROUTES Dispatch(routers/GroupRouter)
Dispatch ROUTE -> START Loading(ui/CreateLoadingContext)
Dispatch MISSED -> IN ShowErrors(core/Output)
Loading OUT -> IN MergeContext(core/Merge)

# Load Gist examples
'graph' -> KEY GistId(objects/GetObjectKey)
Dispatch OUT[0] -> IN GistId
GistId OUT -> IN HoldToken OUT -> TOKEN LoadExample
GistId OUT -> GIST LoadExample(ui/LoadExample)
GistId OBJECT -> CONTEXT ExampleToCtx
LoadExample GRAPH -> GRAPH ExampleToCtx(ui/PopulateExampleData) OUT -> IN MergeContext
ExampleToCtx ERROR -> ERROR ErrorToCtx(ui/ErrorToContext)
LoadExample ERROR -> ERROR ErrorToCtx OUT -> IN MergeContext

# GitHub synchronization
Dispatch OUT[1] -> IN Sync(ui/GithubSynchronization)
GetToken OUT -> TOKEN Sync
Expand Down
29 changes: 0 additions & 29 deletions graphs/LoadExample.fbp

This file was deleted.

4 changes: 3 additions & 1 deletion graphs/main.fbp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ Logger PASS -> IN UrlMiddleware(ui/UrlMiddleware)
UrlMiddleware NEW -> IN Actions
UrlMiddleware PASS -> IN UserMiddleware(ui/UserMiddleware)
UserMiddleware NEW -> IN Actions
UserMiddleware PASS -> IN RegistryMiddleware(ui/RegistryMiddleware)
UserMiddleware PASS -> IN GithubMiddleware(ui/GithubMiddleware)
GithubMiddleware NEW -> IN Actions
GithubMiddleware PASS -> IN RegistryMiddleware(ui/RuntimeMiddleware)
RegistryMiddleware NEW -> IN Actions
RegistryMiddleware PASS -> IN RuntimeMiddleware(ui/RuntimeMiddleware)
RuntimeMiddleware NEW -> IN Actions
Expand Down

0 comments on commit 0ef7453

Please sign in to comment.