Skip to content
Permalink
Browse files

wip

wip

wip
  • Loading branch information...
lihaoyi committed Jun 15, 2019
1 parent f4faa5c commit 0652ac4691091330c6338a918f7847a2f6eea8a1
@@ -1,5 +1,9 @@
// Load dependencies
import $ivy.{`org.pegdown:pegdown:1.6.0`, `com.lihaoyi::scalatags:0.6.5`}
import $ivy.{
`org.pegdown:pegdown:1.6.0`,
`com.lihaoyi::scalatags:0.7.0`,
`guru.nidi:graphviz-java:0.8.8`
}
import $file.pageStyles, pageStyles._
import $file.pages, pages._
import scalatags.Text.all._
@@ -67,8 +71,8 @@ val posts = {
if(!rendering.text.equals("")){
printAttribute("alt", rendering.text)
}
import collection.JavaConversions._
for (attr <- rendering.attributes) {
import collection.JavaConverters._
for (attr <- rendering.attributes.asScala) {
printAttribute(attr.name, attr.value)
}
printer.print(" style=\"max-width: 100%; max-height: 500px\"")
@@ -102,19 +106,39 @@ val posts = {
}

override def visit(node: VerbatimNode) = {
printer.println().print(
"""<pre style="background-color: #f8f8f8">""" +
s"""<code style="white-space:pre; background-color: #f8f8f8" class="${node.getType()}">"""
)

var text = node.getText()
// print HTML breaks for all initial newlines
while(text.charAt(0) == '\n') {
printer.print("\n")
text = text.substring(1)
node.getType match {
case "graphviz" =>
val widthHeightRegex: scala.util.matching.Regex = """width="([0-9]+)px"\s+height="([0-9]+)px"\s""".r
printer.print(
div(textAlign.center, pageStyles.Styles.svgHoverBold)(
raw(
widthHeightRegex.replaceAllIn(
guru.nidi.graphviz.engine.Graphviz
.fromGraph(guru.nidi.graphviz.parse.Parser.read(node.getText()))
.render(guru.nidi.graphviz.engine.Format.SVG)
.toString
.replace("Times,serif", "Helvetica Neue,Helvetica,Arial,sans-serif"),
m => s"""width="${m.group(1).toInt * 3 / 4}px" +height="${m.group(2).toInt * 3 / 4}px" """
)
)
).render
)

case nodeType =>
printer.println().print(
"""<pre style="background-color: #f8f8f8">""" +
s"""<code style="white-space:pre; background-color: #f8f8f8" class="$nodeType">"""
)

var text = node.getText()
// print HTML breaks for all initial newlines
while(text.charAt(0) == '\n') {
printer.print("\n")
text = text.substring(1)
}
printer.printEncoded(text)
printer.print("</code></pre>")
}
printer.printEncoded(text)
printer.print("</code></pre>")
}
override def visit(node: TableNode) = {
currentTableNode = node
@@ -166,7 +190,7 @@ val posts = {
val rawHtmlContent = rawHtmlSnippet + new Serializer().toHtml(ast) + postlude
// Handle both post/ and posts/ for legacy reasons
val updates = DatesFor(s"post/$index - ").toSeq ++ DatesFor(s"posts/$index - ").toSeq
PostInfo(name, headers, rawHtmlContent, rawHtmlSnippet, updates)
PostInfo(name, headers.toSeq, rawHtmlContent, rawHtmlSnippet, updates)
}
}

@@ -1,4 +1,4 @@
import $ivy.`com.lihaoyi::scalatags:0.6.5`
import $ivy.`com.lihaoyi::scalatags:0.7.0`

import scalatags.stylesheet._
import scalatags.Text.all._
@@ -91,6 +91,16 @@ object Styles extends CascadingStyleSheet{
hoverLink(
opacity := 0.5
)
),
)
def svgHoverBold = cls(
Selector("svg")(
Selector("path")(
&hover(
css("stroke") := "blue",
css("stroke-width") := "5px"
)
)
)
)
def hoverLink = cls(
@@ -1,4 +1,4 @@
import $ivy.`com.lihaoyi::scalatags:0.6.5`
import $ivy.`com.lihaoyi::scalatags:0.7.0`
import scalatags.Text.all._, scalatags.Text.tags2
import java.time.LocalDate
import $file.pageStyles, pageStyles._
@@ -1,8 +1,8 @@
JSON HTTP APIs have become the standard for any organization exposing parts of
their system publicly for those outside the org to work with. This tutotial will
walk you through how to work with JSON HTTP APIs in Scala, building up to a
simple use case: migrating Github issues from one repository to another using
Github's public JSON HTTP API.
their system publicly for external developers to work with. This tutorial will
walk you through how to access JSON HTTP APIs in Scala, building up to a simple
use case: migrating Github issues from one repository to another using Github's
public API.

-------------------------------------------------------------------------------

@@ -79,11 +79,11 @@ tracker off of Github!
For now, let us stick with a simple case: we want to perform a one-off, one-way
migration of Github issues from one existing Github repo:

![ExistingRepo.png](ExistingRepo.png)
![ExistingRepo.png](HttpApis/ExistingRepo.png)

To a brand-new Github repo:

![NewRepo.png](NewRepo.png)
![NewRepo.png](HttpApis/NewRepo.png)

To limit the scope of the tutorial, we will only be copying over issues and
comments, without consideration for other metadata like open/closed status,
@@ -100,7 +100,7 @@ project like this is to use a Personal Access Token, that you can create at:

Make sure you tick "public repositories" when you create your token:

![CreateToken.png](CreateToken.png)
![CreateToken.png](HttpApis/CreateToken.png)

To test out this new token, we can make a simple test request to the create
issue endpoint:
@@ -125,7 +125,7 @@ res56: requests.Response = Response(

Going to the issues page, we can see our new issue has been created:

![NewIssue.png](NewIssue.png)
![NewIssue.png](HttpApis/NewIssue.png)

We can also try creating a comment, using:

@@ -148,10 +148,10 @@ res57: requests.Response = Response(

And open up the issue in the UI to see that the comment has been created:

![NewComment.png](NewComment.png)
![NewComment.png](HttpApis/NewComment.png)

Now that we have basic read/write access working, we can use it to start doing
things that are more intereting in our migration!
Now that we have basic read/write access working, we can use it to start with
the more interesting parts of our migration.

## Step 2: Fetching Existing Issues

@@ -379,7 +379,7 @@ total.

Github by default treats issues and pull requests pretty similarly, but for the
purpose of this exercise, let us assume we only want to copy over the issues.
Let us also assume don't need *all* the information on each issue: just the
We'll also assume we don't need *all* the information on each issue: just the
title, description, original author, and the text/author of each of the
comments. That should be enough to preserve historical context and discussion
around each issue.
@@ -409,13 +409,6 @@ issueData: collection.mutable.Buffer[(Int, String, String, String)] = ArrayBuffe
272,
"Custom pickling for sealed hierarchies",
"""Citing the manual, "sealed hierarchies are serialized as tagged values, the serialized object tagged with the full name of the instance's class". It works like this:
```scala
sealed trait A
case class B(x: Int) extends A
case class C(x: Boolean) extends A
implicit val aW: Writer[A] = Writer.merge(macroW[B], macroW[C])
println(write(s, 2))
...
```
@@ -552,7 +545,7 @@ Creating issue 1
This creates all the issues we want:
![CopiedIssues.png](CopiedIssues.png)
![CopiedIssues.png](HttpApis/CopiedIssues.png)
### One new comment per old comment
@@ -609,7 +602,7 @@ Commenting on issue old_id=272 new_id=194
Now, we can see that all our issues have been populated with their respective
comments:
![CopiedComments.png](CopiedComments.png)
![CopiedComments.png](HttpApis/CopiedComments.png)
And we're done!
@@ -701,6 +694,20 @@ for((issueId, user, body) <- commentData){
```
This tutorial only covers the basics of writing a Github Issue Migrator, and
skips over features like:
- Copying the issue open/closed state using their
[Edit an Issue](https://developer.github.com/v3/issues/#edit-an-issue)
endpoint and `requests.patch`
- Copy other metadata like labels, milestones, or assignees, or lock-status
- Migrating pull requests
Extending the code given in this tutorial to support those use cases is left as
an exercise for the reader.
Working with HTTP JSON APIs in Scala is simple and straightforward; you can
easily use `requests.get` posts to access the data you need, `ujson` to
manipulate the JSON payloads, and `requests.post` to send commands back up to
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 0652ac4

Please sign in to comment.
You can’t perform that action at this time.