DEV Community

Evan Lin
Evan Lin

Posted on • Originally published at evanlin.com on

[Learning Notes] [Golang] Migrating Disqus Comments to Github Issues by Writing disqus-importor-go

title: [Learning Experience][Golang] Migrating Disqus Comments to Github Issue by Writing - disqus-importor-go
published: false
date: 2021-05-22 00:00:00 UTC
tags: 
canonical_url: http://www.evanlin.com/go-disqus-github/
---

![](https://github.com/kkdai/disqus-importor-go/raw/master/img/imported.jpg)

## Preface:

In the previous articles [[Jekyll] Removing Disqus and replacing it with utteranc to use github issue as article comments](https://www.evanlin.com/jekyll-remove-disqus/), I mentioned that I moved the Disqus comments from my blog to [https://utteranc.es/](https://utteranc.es/), but what about the nearly two hundred comments in Disqus (a total of 189 comments, in 55 articles)?

Adhering to the principle of creating my own small tools (–too bored to stay at home–), I wrote this small tool to use, hoping to help some people. I also hope that more people will join in the development of related functions.

# TL;DR

This article will introduce some of the following parts, in addition to introducing how to use it.

- [How to export Disqus Comment](#export)
- [Package: Disqus to github issue Go ](#package)
- [What features are included](#features)
- [Disqus XML Format Parsing](#disqus-xml)
- [Github issue created in Go](#github-issue-go)
- [Conclusion](#summary)
- [Results](#result)
- [Reference Articles](#refer)

# How to export Disqus Comment to Utteranc

![](https://avatars3.githubusercontent.com/u/27908738?v=3&s=88)

- Go to the Disqus management interface to export: [http://disqus.com/admin/discussions/export/](http://disqus.com/admin/discussions/export/).
  - It usually takes a day or two to receive the exported file (irregularly).
- Set up the import to Utteranc
- Modify Github Page settings.

You can refer to the article [[Jekyll] Removing Disqus and replacing it with utteranc to use github issue as article comments](https://www.evanlin.com/jekyll-remove-disqus/)

# Package: Disqus to github issues Go

## [https://github.com/kkdai/disqus-to-github-issues-go](https://github.com/kkdai/disqus-to-github-issues-go)

- How to install the package: ``go get github.com/kkdai/disqus-to-github-issues-go`
- And provide a CLI for everyone to use, you can install `go install github.com/kkdai/disqus-to-github-issues-go/cmd/import_disqus_cli`

Related commands:

- `-f`: Import disqus exported xml file. (get from http://disqus.com/admin/discussions/export/)
- `-u`: github user name, you want to use for post github issue.
- `-r`: github repo name, you want to use for post github issue.
- `-t`: github token, you can request your from https://github.com/settings/tokens.

Example of using the command:

`./import_disqus_cli -f="EXPORTED_XML_FILE" -r="BLOG_REPO" -u="GITHUB_USER" -t="YOUR_TOKEN"`

# What features are included

- Read the XML file exported from disqus. (You can refer to the format example on [github](https://github.com/kkdai/disqus-to-github-issues-go/blob/master/example/evanlin_20210517.xml) )
- List all Comments
- List all articles (title, author, time)
- Import to github issues (using [utteranc](https://utteranc.es/) format)

Next, I will put related code for several parts, hoping to share how to write.

# Disqus XML Format Parsing

<script src="https://gist.github.com/kkdai/29bb2fe4805bba524763cb58c4d4ce22.js"></script>

This is a simple version of the XML Parser, you can throw the file to [XML to Go](https://www.onlinetool.io/xmltogo/) after receiving the XML to get the format.

The following are the parts that need to be explained:

- `Articles`: (xml tag: thread) This refers to the original article. It has a one-to-many relationship with the comments. (One article can have a comment)
- `Comments`: (xml tag: post) This is the comment, the data is later than Article, and a separate Mapping Table needs to be made to link.
<script src="https://gist.github.com/kkdai/71aabad734a0ba96b027a93b7296bd3a.js"></script>

The connection relationship between `Article` and `Comments` is: `Comment.ArticleLink.ID` and `Article.AttrID`.

### How to prepare the data for Import github issue

The current method is to use the title in Githib Issue as the key to do the map.

If you use path as the title, ` mapping := make(map[string] Issue)` to manage, you can quickly find the relevant information through Map.

<script src="https://gist.github.com/kkdai/34e778f60b40e83a080ded84aedddfdb.js"></script>
### Comment sorting

Among them, the comments of each article need to be sorted. In order to ensure that the comments can be presented in the annual order. The preparation for related Sort is as follows:

<script src="https://gist.github.com/kkdai/4f08d942bf776035131c4f30e40ca2b8.js"></script>

By preparing [Sort Interfaces](https://golang.org/pkg/sort/#Interface), `[]Comment` supports `Sort()`.

# Github issue created in Go

Regarding the part of sending Github Issue, the golang package open-sourced by Google is used. [https://github.com/google/go-github](https://github.com/google/go-github), remember to use the latest version (the latest version at the time of the author is v35)

Enter fullscreen mode Exit fullscreen mode

import (
...
"github.com/google/go-github/v35/github"
"golang.org/x/oauth2"
)


The relevant code is as follows:

<script src="https://gist.github.com/kkdai/1c628d5e08f540e57cfa6b24cb35f44d.js"></script>

Among them, `gIssue, _, err = client.Issues.Create(ctx, b.User, b.Repo, input)` will get the information related to the created Github Issue, and the relevant ID is needed to add Comment later.

This piece of code can actually be used as a reference for many developers who want to use Github Issue as a data management method (I heard that someone uses Github Issue as a DB www).

# Results

![](https://github.com/kkdai/disqus-importor-go/raw/master/img/blog_result.jpg)

After the final execution, all the comments of Disqus can be integrated into Github Issue, and the relevant time can be preserved. It can also be presented normally in the blog. I hope this tool can help everyone.

## Conclusion:

This article shares the analysis of the output data format of Disqus, and shares the relevant code for creating Issues on Github. It is not difficult to integrate, but many times these applications often become very interesting. You can refer to my previous article: [[TIL] For my own habits, I made a simple service Github issue bookmark](http://www.evanlin.com/til-2017-05-23/).

## Related articles:

- 

[Removing Disqus and adding GitHub Issue Comments](https://dev.to/juergengutsch/removing-disqus-and-adding-github-issue-comments-19h9-temp-slug-2945779)

- 

[Golang time doc](https://golang.org/pkg/time/)

- 

[https://github.com/goreleaser/goreleaser](https://github.com/goreleaser/goreleaser)

- 

[https://github.com/settings/tokens](https://github.com/settings/tokens)

- 

[[TIL] For my own habits, I made a simple service Github issue bookmark](http://www.evanlin.com/til-2017-05-23/)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)