Skip to content

Commit

Permalink
Merge pull request #12 from k1LoW/badge-url
Browse files Browse the repository at this point in the history
Show badge markdown link
  • Loading branch information
k1LoW authored May 11, 2021
2 parents fb030e6 + 5b0b844 commit 4a0e833
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 35 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Run octocov
uses: k1LoW/octocov-action@v0

- name: Commit badge
- name: Commit badge and example
uses: EndBug/add-and-commit@v7
with:
add: 'docs/coverage.svg'
add: 'docs/coverage.svg example/**'
40 changes: 30 additions & 10 deletions central/central.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package central

import (
"context"
_ "embed"
"encoding/json"
"fmt"
Expand All @@ -13,12 +14,11 @@ import (
"text/template"

"github.com/k1LoW/octocov/config"
"github.com/k1LoW/octocov/datastore"
"github.com/k1LoW/octocov/pkg/badge"
"github.com/k1LoW/octocov/report"
)

const defaultHost = "https://github.com"

//go:embed index.md.tmpl
var indexTmpl []byte

Expand Down Expand Up @@ -126,23 +126,43 @@ func (c *Central) renderIndex(wr io.Writer) error {
tmpl := template.Must(template.New("index").Funcs(funcs()).Parse(string(indexTmpl)))
host := os.Getenv("GITHUB_SERVER_URL")
if host == "" {
host = defaultHost
host = datastore.DefaultGithubServerURL
}

root := c.config.Central.Root
if strings.HasSuffix(root, ".md") {
root = filepath.Dir(c.config.Central.Root)
ctx := context.Background()
g, err := datastore.NewGithub(c.config)
if err != nil {
return err
}
rawRootURL, err := g.GetRawRootURL(ctx)
if err != nil {
return err
}

// Get project root dir
proot := c.config.Getwd()

croot := c.config.Central.Root
if strings.HasSuffix(croot, ".md") {
croot = filepath.Dir(c.config.Central.Root)
}

badgesLinkRel, err := filepath.Rel(croot, c.config.Central.Badges)
if err != nil {
return err
}

badgesRel, err := filepath.Rel(root, c.config.Central.Badges)
badgesURLRel, err := filepath.Rel(proot, c.config.Central.Badges)
if err != nil {
return err
}

d := map[string]interface{}{
"Host": host,
"Reports": c.reports,
"BadgesRel": badgesRel,
"Host": host,
"Reports": c.reports,
"BadgesLinkRel": badgesLinkRel,
"BadgesURLRel": badgesURLRel,
"RawRootURL": rawRootURL,
}
if err := tmpl.Execute(wr, d); err != nil {
return err
Expand Down
29 changes: 16 additions & 13 deletions central/central_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package central

import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -65,10 +66,20 @@ func TestGenerateBadges(t *testing.T) {
}

func TestRenderIndex(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
c := config.New()
c.Setwd(filepath.Dir(wd))
c.Repository = "k1LoW/octocov"
c.Central = &config.ConfigCentral{
Enable: true,
Reports: filepath.Join(testdataDir(t), "reports"),
Badges: "badges",
}
if err := c.BuildCentralConfig(); err != nil {
t.Fatal(err)
}

ctr := New(c)
Expand All @@ -82,19 +93,11 @@ func TestRenderIndex(t *testing.T) {
}

got := buf.String()
want := `## Repositories
| Repository | Coverage | Badge |
| --- | --- | --- |
| [k1LoW/awspec](https://github.com/k1LoW/awspec) | 38.8% | ![k1LoW/awspec](./k1LoW/awspec/coverage.svg) |
| [k1LoW/tbls](https://github.com/k1LoW/tbls) | 68.0% | ![k1LoW/tbls](./k1LoW/tbls/coverage.svg) |
| [sebastianbergmann/phpunit](https://github.com/sebastianbergmann/phpunit) | 80.6% | ![sebastianbergmann/phpunit](./sebastianbergmann/phpunit/coverage.svg) |
| [winebarrel/ridgepole](https://github.com/winebarrel/ridgepole) | 95.4% | ![winebarrel/ridgepole](./winebarrel/ridgepole/coverage.svg) |
---
> Generated by [octocov](https://github.com/k1LoW/octocov)
`
b, err := ioutil.ReadFile(filepath.Join(testdataDir(t), "central_README.md.golden"))
if err != nil {
t.Fatal(err)
}
want := string(b)

if got != want {
t.Errorf("got %v\nwant %v", got, want)
Expand Down
2 changes: 1 addition & 1 deletion central/index.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
| Repository | Coverage | Badge |
| --- | --- | --- |
{{- range $r := .Reports }}
| [{{ $r.Repository }}]({{ $.Host }}/{{ $r.Repository }}) | {{ $r | coverage }} | ![{{ $r.Repository }}]({{ $.BadgesRel }}/{{ $r.Repository}}/coverage.svg) |
| [{{ $r.Repository }}]({{ $.Host }}/{{ $r.Repository }}) | {{ $r | coverage }} | ![{{ $r.Repository }}]({{ $.BadgesLinkRel }}/{{ $r.Repository}}/coverage.svg)<br>```![Coverage]({{ $.RawRootURL }}/{{ $.BadgesURLRel }}/{{ $r.Repository}}/coverage.svg)``` |
{{- end }}

---
Expand Down
16 changes: 13 additions & 3 deletions config/central.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,38 @@ package config
import (
"errors"
"path/filepath"
"strings"
)

func (c *Config) CentralConfigReady() bool {
return (c.Central != nil && c.Central.Enable)
}

func (c *Config) BuildCentralConfig() error {
if c.Repository == "" {
return errors.New("repository: not set (or env GITHUB_REPOSITORY is not set)")
}
if c.Central == nil {
return errors.New("central: not set")
}
if c.Central.Root == "" {
c.Central.Root = "."
}
c.Central.Root = filepath.Clean(filepath.Join(c.Root(), c.Central.Root))
if !strings.HasPrefix(c.Central.Root, "/") {
c.Central.Root = filepath.Clean(filepath.Join(c.Root(), c.Central.Root))
}
if c.Central.Reports == "" {
c.Central.Reports = defaultReportsDir
}
c.Central.Reports = filepath.Clean(filepath.Join(c.Root(), c.Central.Reports))
if !strings.HasPrefix(c.Central.Reports, "/") {
c.Central.Reports = filepath.Clean(filepath.Join(c.Root(), c.Central.Reports))
}
if c.Central.Badges == "" {
c.Central.Badges = defaultBadgesDir
}
c.Central.Badges = filepath.Clean(filepath.Join(c.Root(), c.Central.Badges))
if !strings.HasPrefix(c.Central.Badges, "/") {
c.Central.Badges = filepath.Clean(filepath.Join(c.Root(), c.Central.Badges))
}

return nil
}
8 changes: 8 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ func New() *Config {
}
}

func (c *Config) Getwd() string {
return c.wd
}

func (c *Config) Setwd(path string) {
c.wd = path
}

func (c *Config) Load(path string) error {
if path == "" {
for _, p := range DefaultConfigFilePaths {
Expand Down
40 changes: 40 additions & 0 deletions datastore/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/k1LoW/octocov/report"
)

const DefaultGithubServerURL = "https://github.com"

type Github struct {
config *config.Config
client *github.Client
Expand Down Expand Up @@ -127,6 +129,44 @@ func (g *Github) Store(ctx context.Context, r *report.Report) error {
return nil
}

func (g *Github) GetRawRootURL(ctx context.Context) (string, error) {
splitted := strings.Split(g.config.Repository, "/")
owner := splitted[0]
repo := splitted[1]
r, _, err := g.client.Repositories.Get(ctx, owner, repo)
if err != nil {
return "", err
}
b := r.GetDefaultBranch()

if os.Getenv("GITHUB_SERVER_URL") != "" && os.Getenv("GITHUB_SERVER_URL") != DefaultGithubServerURL {
// GitHub Enterprise Server
return fmt.Sprintf("%s/%s/%s/raw/%s", os.Getenv("GITHUB_SERVER_URL"), owner, repo, b), nil
}

baseRef := fmt.Sprintf("refs/heads/%s", b)
ref, _, err := g.client.Git.GetRef(ctx, owner, repo, baseRef)
if err != nil {
return "", err
}
tree, _, err := g.client.Git.GetTree(ctx, owner, repo, ref.GetObject().GetSHA(), false)
if err != nil {
return "", err
}
for _, e := range tree.Entries {
if e.GetType() != "blob" {
continue
}
path := e.GetPath()
fc, _, _, err := g.client.Repositories.GetContents(ctx, owner, repo, path, &github.RepositoryContentGetOptions{})
if err != nil {
return "", err
}
return strings.TrimSuffix(strings.TrimSuffix(fc.GetDownloadURL(), path), "/"), nil
}
return "", fmt.Errorf("not found files. please commit file to root directory and push: %s", g.config.Repository)
}

type roundTripper struct {
transport *http.Transport
accessToken string
Expand Down
4 changes: 2 additions & 2 deletions docs/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions example/central/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

| Repository | Coverage | Badge |
| --- | --- | --- |
| [k1LoW/awspec](https://github.com/k1LoW/awspec) | 38.8% | ![k1LoW/awspec](badges/k1LoW/awspec/coverage.svg) |
| [k1LoW/tbls](https://github.com/k1LoW/tbls) | 68.0% | ![k1LoW/tbls](badges/k1LoW/tbls/coverage.svg) |
| [sebastianbergmann/phpunit](https://github.com/sebastianbergmann/phpunit) | 80.6% | ![sebastianbergmann/phpunit](badges/sebastianbergmann/phpunit/coverage.svg) |
| [winebarrel/ridgepole](https://github.com/winebarrel/ridgepole) | 95.4% | ![winebarrel/ridgepole](badges/winebarrel/ridgepole/coverage.svg) |
| [k1LoW/awspec](https://github.com/k1LoW/awspec) | 38.8% | ![k1LoW/awspec](badges/k1LoW/awspec/coverage.svg)<br>```![Coverage](https://raw.githubusercontent.com/k1LoW/octocov/main/example/central/badges/k1LoW/awspec/coverage.svg)``` |
| [k1LoW/tbls](https://github.com/k1LoW/tbls) | 68.0% | ![k1LoW/tbls](badges/k1LoW/tbls/coverage.svg)<br>```![Coverage](https://raw.githubusercontent.com/k1LoW/octocov/main/example/central/badges/k1LoW/tbls/coverage.svg)``` |
| [sebastianbergmann/phpunit](https://github.com/sebastianbergmann/phpunit) | 80.6% | ![sebastianbergmann/phpunit](badges/sebastianbergmann/phpunit/coverage.svg)<br>```![Coverage](https://raw.githubusercontent.com/k1LoW/octocov/main/example/central/badges/sebastianbergmann/phpunit/coverage.svg)``` |
| [winebarrel/ridgepole](https://github.com/winebarrel/ridgepole) | 95.4% | ![winebarrel/ridgepole](badges/winebarrel/ridgepole/coverage.svg)<br>```![Coverage](https://raw.githubusercontent.com/k1LoW/octocov/main/example/central/badges/winebarrel/ridgepole/coverage.svg)``` |

---

Expand Down
12 changes: 12 additions & 0 deletions testdata/central_README.md.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## Repositories

| Repository | Coverage | Badge |
| --- | --- | --- |
| [k1LoW/awspec](https://github.com/k1LoW/awspec) | 38.8% | ![k1LoW/awspec](badges/k1LoW/awspec/coverage.svg)<br>```![Coverage](https://raw.githubusercontent.com/k1LoW/octocov/main/badges/k1LoW/awspec/coverage.svg)``` |
| [k1LoW/tbls](https://github.com/k1LoW/tbls) | 68.0% | ![k1LoW/tbls](badges/k1LoW/tbls/coverage.svg)<br>```![Coverage](https://raw.githubusercontent.com/k1LoW/octocov/main/badges/k1LoW/tbls/coverage.svg)``` |
| [sebastianbergmann/phpunit](https://github.com/sebastianbergmann/phpunit) | 80.6% | ![sebastianbergmann/phpunit](badges/sebastianbergmann/phpunit/coverage.svg)<br>```![Coverage](https://raw.githubusercontent.com/k1LoW/octocov/main/badges/sebastianbergmann/phpunit/coverage.svg)``` |
| [winebarrel/ridgepole](https://github.com/winebarrel/ridgepole) | 95.4% | ![winebarrel/ridgepole](badges/winebarrel/ridgepole/coverage.svg)<br>```![Coverage](https://raw.githubusercontent.com/k1LoW/octocov/main/badges/winebarrel/ridgepole/coverage.svg)``` |

---

> Generated by [octocov](https://github.com/k1LoW/octocov)

0 comments on commit 4a0e833

Please sign in to comment.