Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown receiver #415

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion cmd/blog/main.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
Expand All @@ -27,7 +28,8 @@ var localZone *time.Location
var tracer trace.Tracer

var rootCmd = &cobra.Command{
Use: "blog",
Use: "blog",
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
return nil
},
Expand Down Expand Up @@ -143,3 +145,13 @@ func main() {
logger.Fatal().Msg(err.Error())
}
}

func requireStringFlags(cmd *cobra.Command, flags ...string) error {
for _, flag := range flags {
val, err := cmd.Flags().GetString(flag)
if err != nil || val == "" {
return fmt.Errorf("required flag `%s` not set", flag)
}
}
return nil
}
53 changes: 53 additions & 0 deletions cmd/blog/receive.go
@@ -0,0 +1,53 @@
package main

import (
"context"
"fmt"
"net/http"
"time"

"github.com/spf13/cobra"
"gitlab.com/zerok/zerokspot.com/pkg/middlewares"
"gitlab.com/zerok/zerokspot.com/pkg/receiver"
)

var receive = &cobra.Command{
Use: "receive",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
var err error
tz := time.UTC
tz, err = time.LoadLocation(tzName)
if err != nil {
return fmt.Errorf("failed to load timezone %s: %w", tzName, err)
}
if err := requireStringFlags(cmd, "repo-path", "token", "github-token"); err != nil {
return err
}
srv := http.Server{}
srv.Addr = addr
recv := receiver.New(ctx, func(cfg *receiver.Configuration) {
cfg.RepoPath = repoPath
cfg.TimeLocation = tz
cfg.GitHubToken = githubToken
cfg.AccessToken = token
})
srv.Handler = middlewares.InjectLogger(recv, logger)
logger.Info().Msgf("Listening on %s", srv.Addr)
go func() {
<-ctx.Done()
srv.Shutdown(context.Background())
}()
return srv.ListenAndServe()
},
}

func init() {
rootCmd.AddCommand(receive)
receive.Flags().StringVar(&repoPath, "repo-path", "", "Path to the repository that should be updated")
receive.Flags().StringVar(&tzName, "timezone", "Europe/Vienna", "Timezone to be used with in generated markdown files")
receive.Flags().StringVar(&token, "token", "", "Token required for requests")
receive.Flags().StringVar(&addr, "addr", "localhost:37080", "Address to listen on")
receive.Flags().StringVar(&githubUser, "github-user", "", "Username to be used for creating a pull-request")
receive.Flags().StringVar(&githubToken, "github-token", "", "Token to be used for creating a pull-request")
}
201 changes: 201 additions & 0 deletions pkg/receiver/receiver.go
@@ -0,0 +1,201 @@
package receiver

import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"os/exec"
"path/filepath"
"time"

"github.com/go-chi/chi"
"github.com/gohugoio/hugo/parser/pageparser"
"github.com/google/go-github/v52/github"
"github.com/rs/zerolog"
"golang.org/x/oauth2"
"gopkg.in/yaml.v2"
)

type Configuration struct {
SkipPull bool
SkipCommit bool
RepoPath string
GitHubToken string
TimeLocation *time.Location
ForceNow time.Time
AccessToken string
}

type Receiver struct {
cfg Configuration
router chi.Router
ghClient *github.Client
}

type Configurator func(cfg *Configuration)

func New(ctx context.Context, cfgs ...Configurator) *Receiver {
router := chi.NewRouter()
cfg := Configuration{}
for _, c := range cfgs {
c(&cfg)
}
recv := &Receiver{
cfg: cfg,
router: router,
}
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: cfg.GitHubToken},
)
tc := oauth2.NewClient(ctx, ts)
recv.ghClient = github.NewClient(tc)
router.Post("/", recv.handleRecieve)
return recv
}

func (recv *Receiver) ServeHTTP(w http.ResponseWriter, r *http.Request) {
recv.router.ServeHTTP(w, r)
}

type postStatus struct {
Path string `json:"path"`
}

func (recv *Receiver) handleRecieve(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
if recv.cfg.AccessToken != "" && token != recv.cfg.AccessToken {
http.Error(w, "Invalid token", http.StatusUnauthorized)
return
}
switch r.Header.Get("Content-Type") {
case "text/markdown":
if status, err := recv.handleReceiveMarkdown(w, r); err != nil {
fmt.Println(err)
http.Error(w, "Error processing markdown", http.StatusInternalServerError)
} else {
w.Header().Set("Content-Type", "text/json")
json.NewEncoder(w).Encode(status)
}
default:
http.Error(w, "Unsupported content type", http.StatusBadRequest)
}
}

func (recv *Receiver) handleReceiveMarkdown(w http.ResponseWriter, r *http.Request) (*postStatus, error) {
ctx := r.Context()
logger := zerolog.Ctx(ctx)
defer r.Body.Close()
data, err := pageparser.ParseFrontMatterAndContent(r.Body)
if err != nil {
return nil, err
}
slug := data.FrontMatter["slug"].(string)
if slug == "" {
return nil, fmt.Errorf("no slug specified")
}
delete(data.FrontMatter, "slug")
now := recv.cfg.ForceNow
if now.IsZero() {
now = time.Now()
}
now = now.In(recv.cfg.TimeLocation)

if !recv.cfg.SkipPull {
logger.Debug().Msg("Switching to main and pulling latest changes")
if err := recv.callGit(ctx, "switch", "main"); err != nil {
return nil, err
}
if err := recv.callGit(ctx, "pull", "origin", "main"); err != nil {
return nil, err
}
}

relDestDir := filepath.Join("content", "weblog", fmt.Sprintf("%d", now.Year()))
relDestPath := filepath.Join(relDestDir, fmt.Sprintf("%s.md", slug))
destDir := filepath.Join(recv.cfg.RepoPath, "content", "weblog", fmt.Sprintf("%d", now.Year()))
if err := os.MkdirAll(destDir, 0700); err != nil {
return nil, fmt.Errorf("failed to create destination directory: %w", err)
}
destPath := filepath.Join(destDir, fmt.Sprintf("%s.md", slug))
data.FrontMatter["date"] = now.Format(time.RFC3339)
logger.Info().Msgf("Writing output to %s", destPath)
fp, err := os.OpenFile(destPath, os.O_CREATE|os.O_RDWR|os.O_EXCL, 0600)
if err != nil {
return nil, fmt.Errorf("failed to open destination file `%s`: %w", destPath, err)
}
fp.WriteString("---\n")
yaml.NewEncoder(fp).Encode(data.FrontMatter)
fp.WriteString("---\n\n")
fp.Write(data.Content)
if err := fp.Close(); err != nil {
return nil, fmt.Errorf("failed to close target file: %w", err)
}

if !recv.cfg.SkipCommit {
branchname := fmt.Sprintf("articles/%s", slug)
if err := recv.callGit(ctx, "switch", "-C", branchname); err != nil {
return nil, err
}
if err := recv.callGit(ctx, "add", relDestPath); err != nil {
recv.cleanupBranch(ctx, branchname)
return nil, err
}
if err := recv.callGit(ctx, "commit", "-m", "Add "+slug); err != nil {
recv.cleanupBranch(ctx, branchname)
return nil, err
}
if err := recv.callGit(ctx, "push", "origin", branchname); err != nil {
recv.cleanupBranch(ctx, branchname)
return nil, err
}
if err := recv.cleanupBranch(ctx, branchname); err != nil {
return nil, err
}
logger.Debug().Msg("Creating pull-request")
if err := recv.createPR(ctx, slug, branchname); err != nil {
return nil, err
}
}
return &postStatus{
Path: relDestPath,
}, nil
}

func (recv *Receiver) cleanupBranch(ctx context.Context, branchname string) error {
if err := recv.callGit(ctx, "switch", "main"); err != nil {
return err
}
if err := recv.callGit(ctx, "branch", "-D", branchname); err != nil {
return err
}
return nil
}

func (recv *Receiver) callGit(ctx context.Context, args ...string) error {
logger := zerolog.Ctx(ctx)
logger.Debug().Msgf("Executing git-command: %s", args)
cmd := exec.CommandContext(ctx, "git", args...)
cmd.Dir = recv.cfg.RepoPath
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}

func (recv *Receiver) createPR(ctx context.Context, slug string, branchname string) error {
logger := zerolog.Ctx(ctx)
title := fmt.Sprintf("Article: %s", slug)
base := "main"
pull := github.NewPullRequest{
Title: &title,
Head: &branchname,
Base: &base,
}
pr, _, err := recv.ghClient.PullRequests.Create(ctx, "zerok", "zerokspot.com", &pull)
if err != nil {
return fmt.Errorf("failed to create pull-request: %w", err)
}
logger.Info().Msgf("Pull-request created: %s", pr.GetHTMLURL())
return nil
}
52 changes: 52 additions & 0 deletions pkg/receiver/receiver_test.go
@@ -0,0 +1,52 @@
package receiver_test

import (
"bytes"
"context"
"io/ioutil"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/require"
"gitlab.com/zerok/zerokspot.com/pkg/receiver"
)

func TestReceiverMarkdownHandling(t *testing.T) {
t.Run("markdown-file-without-path", func(t *testing.T) {
ctx := context.Background()
tmpDir := t.TempDir()
now := time.Date(2023, 8, 8, 12, 0, 0, 0, time.UTC)
loc, _ := time.LoadLocation("Europe/Vienna")
recv := receiver.New(ctx, func(cfg *receiver.Configuration) {
cfg.SkipCommit = true
cfg.SkipPull = true
cfg.RepoPath = tmpDir
cfg.ForceNow = now
cfg.TimeLocation = loc
})
testSrv := httptest.NewServer(recv)
content := `---
title: "something"
slug: "something"
---

Some content`
expectedContent := `---
date: "2023-08-08T14:00:00+02:00"
title: something
---

Some content`
resp, err := http.Post(testSrv.URL, "text/markdown", bytes.NewBufferString(content))
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
require.FileExists(t, filepath.Join(tmpDir, "content/weblog/2023/something.md"))
rawFileContent, err := ioutil.ReadFile(filepath.Join(tmpDir, "content/weblog/2023/something.md"))
require.NoError(t, err)
fileContent := string(rawFileContent)
require.Equal(t, expectedContent, fileContent)
})
}