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

Add --oneshot option #12

Merged
merged 5 commits into from
Feb 24, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ gleam run # Run the project
gleam shell # Run an Erlang shell
```

## Oneshot buildograms

You can use the `--oneshot` command line flag to instead of a server just
render one SVG immediately for a given repository.

```shell session
$ podman run --rm ghcr.io/fabjan/buildogram:latest run --oneshot=fabjan/buildogram 2>/dev/null > test.svg
```

## Deploying

For each release, a container image is pushed to [buildogram packages](https://github.com/fabjan/buildogram/pkgs/container/buildogram).
Expand Down
33 changes: 32 additions & 1 deletion src/buildogram.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ import gleam/erlang
import gleam/erlang/os
import gleam/erlang/process
import gleam/http/elli
import buildogram/diagram
import buildogram/github
import buildogram/http_server
import buildogram/http_client
import outil.{CommandError, CommandResult, print_usage_and_exit}
import outil/opt
import snag.{Snag}

fn log(s) {
io.println("[main] " <> s)
io.println_error("[main] " <> s)
}

pub fn get_env(
Expand All @@ -53,6 +55,13 @@ pub fn main_cmd(args: List(String)) -> CommandResult(Nil, Snag) {
let cache_size = get_env("BUILDOGRAM_CACHE_SIZE", int.parse, 100)
use cache_size, cmd <- opt.int(cmd, "cache", "HTTP cache size", cache_size)

use one_shot, cmd <- opt.string(
cmd,
"oneshot",
"Just print the SVG for the given GitHub repo and exit",
"",
)

try port = port(cmd)
try cache_size = cache_size(cmd)

Expand All @@ -62,6 +71,11 @@ pub fn main_cmd(args: List(String)) -> CommandResult(Nil, Snag) {
http_client.start(cache_size)
|> cmd_snag("starting HTTP client")

let _ = case one_shot(cmd) {
Ok(repo) if repo != "" -> print_svg_and_exit(client, repo)
_ -> Nil
}

// Start the web server process
try server =
http_server.stack(client)
Expand Down Expand Up @@ -91,3 +105,20 @@ fn cmd_snag(res: Result(a, b), context: String) -> CommandResult(a, Snag) {
fn cmd_result(res: Result(a, b)) -> CommandResult(a, b) {
result.map_error(res, fn(e) { CommandError(e) })
}

fn print_svg_and_exit(client, repo_path: String) -> Nil {
case github.get_all_runs(client, repo_path) {
Ok(runs) -> {
let svg = diagram.bar_chart(runs, 400, 100)
io.println(svg)
halt(0)
}
Error(Snag(issue, _)) -> {
io.println_error("Error: " <> issue)
halt(1)
}
}
}

external fn halt(Int) -> Nil =
"erlang" "halt"
2 changes: 1 addition & 1 deletion src/buildogram/diagram.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import buildogram/timestamp
import buildogram/util

fn log(s) {
io.println("[diagram] " <> s)
io.println_error("[diagram] " <> s)
}

/// Render an SVG bar chart from the given workflow runs.
Expand Down
2 changes: 1 addition & 1 deletion src/buildogram/github.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import buildogram/timestamp.{Timestamp, decode_timestamp}
import buildogram/util

fn log(s) {
io.println("[github] " <> s)
io.println_error("[github] " <> s)
}

/// WorkflowRun is a full pipeline run.
Expand Down
2 changes: 1 addition & 1 deletion src/buildogram/http_client.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ type HttpClient {
}

fn log(s) {
io.println("[http_client] " <> s)
io.println_error("[http_client] " <> s)
}

fn handle_get(get: HttpGet, state: HttpClient) -> actor.Next(HttpClient) {
Expand Down
2 changes: 1 addition & 1 deletion src/buildogram/http_server.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import buildogram/http_client.{HttpGet}
import buildogram/util

fn log(s) {
io.println("[http_server] " <> s)
io.println_error("[http_server] " <> s)
}

/// Configure middleware etc.
Expand Down
12 changes: 2 additions & 10 deletions test/diagram_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ pub fn one_run_test() {

let diagram = diagram.bar_chart(runs, 1, 1)

io.println(diagram)

// each run yields a rect in the SVG
count_rects(diagram)
|> should.equal(1)
Expand All @@ -29,8 +27,6 @@ pub fn multi_attempt_run_test() {

let diagram = diagram.bar_chart(runs, 1, 1)

io.println(diagram)

// each run yields a rect in the SVG
count_rects(diagram)
|> should.equal(2)
Expand Down Expand Up @@ -65,23 +61,21 @@ pub fn many_runs_test() {

let diagram = diagram.bar_chart(runs, 1, 1)

io.println(diagram)

// each run yields a rect in the SVG
count_rects(diagram)
|> should.equal(15)
}

pub fn unexpected_conclusion_test() {
io.println_error("Heads up: this test will print to stderr!")

let runs = [
[fake_run(0, 5, "unexpected", None)],
[fake_run(0, 5, "wjin beof8y23bu", None)],
]

let diagram = diagram.bar_chart(runs, 1, 1)

io.println(diagram)

// each run yields a rect in the SVG
count_rects(diagram)
|> should.equal(2)
Expand All @@ -95,8 +89,6 @@ pub fn empty_runs_test() {

let diagram = diagram.bar_chart(runs, 1, 1)

io.println(diagram)

// each run yields a rect in the SVG
count_rects(diagram)
|> should.equal(0)
Expand Down