Skip to content

Commit

Permalink
feat: comment diffs in go (#50)
Browse files Browse the repository at this point in the history
* chore: moving libs around

* chore: moving things around and caching works

* feat: fetch gh api concurrently

* moving github versioning internally

* move diff parsing to golang (easier to have tests here)
  • Loading branch information
stanistan authored May 5, 2023
1 parent 2fe8ca9 commit 47cee31
Show file tree
Hide file tree
Showing 16 changed files with 462 additions and 330 deletions.
39 changes: 1 addition & 38 deletions frontend/components/DiffBlock.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<pre class="bg-gray-100"><code ref="code" :class="language">{{ diff }}</code></pre>
<pre class="bg-gray-100"><code ref="code" :class="language">{{ comment.diff_hunk }}</code></pre>
</template>

<script setup lang="ts">
Expand Down Expand Up @@ -32,43 +32,6 @@ const props = defineProps({
},
});
// drop the first line of the diff since it's a diff hunk header
const diff = computed(() => {
const comment = props.comment;
// let's get the lines first...
const lines = comment.diff_hunk.split("\n");
// the first line has metadata in it from the diff hunk...
// and we can get the starting line that's expected to be there
// from it...
const startCountingAt = parseInt(lines.shift().match(/@@ -(\d+),/)[1], 10);
// desiredRange
const
desiredStartLine = comment.start_line === undefined
? comment.line - 4
: comment.start_line - (!startCountingAt ? 1 : 0),
desiredEndLine = comment.line,
outputDiff = [];
let lineNumber = startCountingAt;
lines.forEach(line => {
if (lineNumber >= desiredStartLine && lineNumber <= desiredEndLine) {
outputDiff.push(line);
}
if (comment.side == "LEFT" && !line.startsWith("+")) {
lineNumber++;
} else if (comment.side == "RIGHT" && !line.startsWith("-")) {
lineNumber++;
}
});
return outputDiff.join("\n");
});
// we grab the file extension and map it to the diff-language
const languageMap = { rs: 'rust' };
const language = computed(() => {
Expand Down
6 changes: 3 additions & 3 deletions server/cmd/server-nuxt/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"github.com/pkg/errors"

pm "github.com/stanistan/present-me"
"github.com/stanistan/present-me/internal/github"
"github.com/stanistan/present-me/internal/http"
)

Expand All @@ -22,7 +22,7 @@ var apiRoutes = http.Routes(
return nil, errors.New("missing github context")
}

params, err := pm.ReviewParamsFromURL(r.URL.Query().Get("search"))
params, err := github.ReviewParamsFromURL(r.URL.Query().Get("search"))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -52,7 +52,7 @@ var apiRoutes = http.Routes(
return nil, errors.New("missing github context")
}

params, err := pm.ReviewParamsFromMap(pm.ReviewParamsMap{
params, err := github.ReviewParamsFromMap(github.ReviewParamsMap{
Owner: values.Get("org"),
Repo: values.Get("repo"),
Number: values.Get("pull"),
Expand Down
32 changes: 18 additions & 14 deletions server/cmd/server-nuxt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ import (

pm "github.com/stanistan/present-me"
"github.com/stanistan/present-me/internal/cache"
"github.com/stanistan/present-me/internal/github"
)

func main() {
var config pm.Config
_ = kong.Parse(&config)

config.Configure()
log := config.Logger()
diskCache := config.Cache(context.TODO())

gh, err := config.GH()
gh, err := config.GithubClient()
if err != nil {
log.Fatal().Err(err).Msg("could not configure GH client")
}
Expand All @@ -32,7 +33,7 @@ func main() {
api.Use(
hlog.NewHandler(log),
githubMiddleware(gh),
cacheMiddleware,
cacheMiddleware(diskCache),
)

for _, r := range apiRoutes {
Expand Down Expand Up @@ -65,16 +66,16 @@ type ghCtxKey int

var ghCtxValue ghCtxKey

func ContextWithGH(ctx context.Context, gh *pm.GH) context.Context {
func ContextWithGH(ctx context.Context, gh *github.Client) context.Context {
return context.WithValue(ctx, ghCtxValue, gh)
}

func GHFromContext(ctx context.Context) (*pm.GH, bool) {
v, ok := ctx.Value(ghCtxValue).(*pm.GH)
func GHFromContext(ctx context.Context) (*github.Client, bool) {
v, ok := ctx.Value(ghCtxValue).(*github.Client)
return v, ok
}

func githubMiddleware(g *pm.GH) func(http.Handler) http.Handler {
func githubMiddleware(g *github.Client) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := ContextWithGH(r.Context(), g)
Expand All @@ -83,12 +84,15 @@ func githubMiddleware(g *pm.GH) func(http.Handler) http.Handler {
}
}

func cacheMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := cache.ContextWithOptions(r.Context(), &cache.Options{
TTL: 10 * time.Minute,
ForceRefresh: r.URL.Query().Get("refresh") == "1",
func cacheMiddleware(c *cache.Cache) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := cache.ContextWithCache(r.Context(), c)
ctx = cache.ContextWithOptions(ctx, &cache.Options{
TTL: 10 * time.Minute,
ForceRefresh: r.URL.Query().Get("refresh") == "1",
})
next.ServeHTTP(w, r.WithContext(ctx))
})
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}
26 changes: 8 additions & 18 deletions server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,26 @@ import (
"github.com/pkg/errors"
"github.com/rs/zerolog"

dc "github.com/stanistan/present-me/internal/cache"
"github.com/stanistan/present-me/internal/cache"
"github.com/stanistan/present-me/internal/github"
"github.com/stanistan/present-me/internal/log"
)

type Config struct {
ServeConfig
DiskCache dc.CacheOpts `embed:"" prefix:"disk-cache-"`
Github GHOpts `embed:"" prefix:"gh-"`
DiskCache cache.CacheOpts `embed:"" prefix:"disk-cache-"`
Github github.ClientOptions `embed:"" prefix:"gh-"`
}

func (c *Config) GH() (*GH, error) {
g, err := NewGH(c.Github)
func (c *Config) GithubClient() (*github.Client, error) {
g, err := github.New(c.Github)
return g, errors.WithStack(err)
}

func (c *Config) Logger() zerolog.Logger {
return log.NewLogger()
}

func (c *Config) Configure() {
configureCache(c.DiskCache)
func (c *Config) Cache(ctx context.Context) *cache.Cache {
return cache.NewCache(ctx, c.DiskCache)
}

func configureCache(opts dc.CacheOpts) {
cache = dc.NewCache(context.TODO(), opts)
}

var (
cache *dc.Cache = dc.NewCache(
context.TODO(),
dc.CacheOpts{Enabled: false},
)
)
Loading

0 comments on commit 47cee31

Please sign in to comment.