Skip to content

Commit

Permalink
Write MVP version
Browse files Browse the repository at this point in the history
  • Loading branch information
VOID404 committed Sep 11, 2024
0 parents commit de3a524
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 0 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/gha-summarizer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: GHA Summarizer
on:
push:
branches:
- main
paths-ignore:
- "README.md"
- "LICENSE"
env:
image: ghcr.io/${{ github.repository }}/gha-summarizer

jobs:
test:
permissions:
contents: read
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}

- name: Render template
uses: ./
with:
template: example/template.md
out-file: out.md

- name: Check if the rendered file is different
run: |
diff --unified ./example/result.md ./out.md | tee diff.txt
if [ -s diff.txt ]; then
echo "The rendered file is different from the template"
exit 1
fi
- name: Upload diff
if: success() || failure()
uses: actions/upload-artifact@v4
with:
name: diff.txt
path: diff.txt
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM golang:1.23 AS builder

WORKDIR /workdir

COPY go.mod ./
COPY ./main.go ./

RUN go build -o gha-summarizer github.com/VOID404/gha-summarizer

FROM scratch

WORKDIR /github/workspace

COPY --from=builder /workdir/gha-summarizer /gha-summarizer
ENTRYPOINT [ "/gha-summarizer" ]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Wojciech Nawa

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# GHA Summarizer
15 changes: 15 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: "GHA Summarizer"
description: "Simple templating for Github Actions"
inputs:
template:
description: "Template to render"
required: true
out-file:
description: "File to write output to"
required: true
runs:
using: docker
image: Dockerfile
args:
- ${{ inputs.template }}
- ${{ inputs.out-file }}
7 changes: 7 additions & 0 deletions example/code.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "fmt"

func main() {
fmt.Println("Hello, World!")
}
14 changes: 14 additions & 0 deletions example/result.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Sample template

Generated from `VOID404/gha-summarizer`

```go
package main

import "fmt"

func main() {
fmt.Println("Hello, World!")
}

```
7 changes: 7 additions & 0 deletions example/template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Sample template

Generated from `{{ env "$GITHUB_REPOSITORY" }}`

```go
{{ file "./example/code.go"}}
```
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/VOID404/gha-summarizer

go 1.23.0
45 changes: 45 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"fmt"
"os"
"path"
"text/template"
)

func main() {
if len(os.Args) < 3 {
fmt.Printf("Usage: %s <template file> <output file>\n", os.Args[0])
os.Exit(1)
}

tplFile := os.Args[1]
outFile, err := os.OpenFile(os.Args[2], os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}

funcMap := make(map[string]any)
funcMap["file"] = func(name string) string {
data, err := os.ReadFile(name)
if err != nil {
panic(err)
}
return string(data)
}

funcMap["env"] = func(text string) string {
return os.ExpandEnv(text)
}

tpl := template.Must(
template.New(path.Base(tplFile)). //
Funcs(funcMap).
ParseFiles(tplFile),
)

err = tpl.Execute(outFile, nil)
if err != nil {
panic(err)
}
}

0 comments on commit de3a524

Please sign in to comment.