Skip to content

Commit

Permalink
Don't write by yourself
Browse files Browse the repository at this point in the history
  • Loading branch information
dkrizic committed Mar 2, 2024
1 parent a0e9167 commit 8f7aeb7
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 158 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea/
.env
test.md
deployment-overview.md
deployment-overview.json
25 changes: 12 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ go buid main.go -o deployment-overview

## Parrameters

| Paramter | Environment | Required | Default | Example | Description |
| --- | --- |-------|---------|-----------------------------------------------------------------------------|-------------------------------------------------------------------------------|
| --github-token | GITHUB_TOKEN | true | - | - | The GitHub Personal Access Token (PAT) |
| --environments | ENVIRONMENTS | true | - | dev,staging,prod | Environments to query. Comma separated list. |
| --organization | ORGANIZATION | true | - | myorga | The GitHub Organization to query for repositories. |
| --repositories | REPOSITORIES | true | - | frontend,backend | Repositories to query. Comma separated list. |
| --target-repository | TARGET_REPOSITORY | true | - | .github | The target repository to commit the result to. |
| --target-repository-file | TARGET_REPOSITORY_FILE | true | - | profile/README.md | The target repository file to commit the result to. |
| --verbose | VERBOSE | false | 1 | 0 | Verbosity level, 0=info, 1=debug. Overrides the environment variable VERBOSE. |
| --environment-links | ENVIRONMENT_LINKS | false | - | https://dev.example.com,https://staging.example.com,https://www.example.com | Links to the environments. Comma separated list. |
| --template-file | TEMPLATE_FILE | false | - | template/default.md | The template file to use. |
| Paramter | Environment | Required | Default | Example | Description |
| --- | --- |-------|--------------------------|-----------------------------------------------------------------------------|-------------------------------------------------------------------------------|
| --github-token | GITHUB_TOKEN | true | - | - | The GitHub Personal Access Token (PAT) |
| --environments | ENVIRONMENTS | true | - | dev,staging,prod | Environments to query. Comma separated list. |
| --organization | ORGANIZATION | true | - | myorga | The GitHub Organization to query for repositories. |
| --repositories | REPOSITORIES | true | - | frontend,backend | Repositories to query. Comma separated list. |
| --verbose | VERBOSE | false | 1 | 0 | Verbosity level, 0=info, 1=debug. Overrides the environment variable VERBOSE. |
| --environment-links | ENVIRONMENT_LINKS | false | - | https://dev.example.com,https://staging.example.com,https://www.example.com | Links to the environments. Comma separated list. |
| --template-file | TEMPLATE_FILE | false | - | template/default.md | The template file to use. |
| --target-json-file | TARGET_JSON_FILE | false | deployment-overview.json | - | The target file to write the result to as JSON. |
| --target-md-file | TARGET_MD_FILE | false | deployment-overview.md | - | The target file to write the result to as Markdown. |

## Use as GitHub Action

Expand All @@ -65,7 +65,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Deployment overview
uses: prodyna/deployment-overview@v0.1
uses: prodyna/deployment-overview@v0.2
with:
organization: myorg
target-repository: .github
Expand All @@ -86,7 +86,6 @@ The PAT should be a Fine Grain PAT that belongs to the target organization and h
| Permission | Access |
| --- | --- |
| Actions | Read |
| Contents | Read & Write |
| Deployments | Read |
| Environments | Read |
| Metadata | Read |
Expand Down
24 changes: 10 additions & 14 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,21 @@ inputs:
description: 'The verbosity level'
required: true
default: 1
target-repository:
description: 'The repository where the overview will be written to'
required: true
default: '.github'
target-repository-file:
description: 'The file where the overview will be written to'
required: true
default: 'profile/README.md'
github-token:
description: 'The token to use for the GitHub API'
required: true
title:
description: 'The title of the overview'
required: true
default: 'Deployment overview'
template-file:
description: 'The template file to use for the overview'
required: true
default: '/template/default.tpl'
targetJsonFile:
description: 'The file to write the JSON to'
required: false
default: 'deployment-overview.json'
targetMdFile:
description: 'The file to write the MD to'
required: false
default: 'deployment-overview.md'
runs:
using: 'docker'
image: 'Dockerfile'
Expand All @@ -48,8 +44,8 @@ runs:
ENVIRONMENTS: ${{ inputs.environments }}
ENVIRONMENT_LINKS: ${{ inputs.environment-links }}
VERBOSE: ${{ inputs.verbose }}
TARGET_REPOSITORY: ${{ inputs.target-repository }}
TARGET_REPOSITORY_FILE: ${{ inputs.target-repository-file }}
GITHUB_TOKEN: ${{ inputs.github-token }}
TITLE: ${{ inputs.title }}
TEMPLATE_FILE: ${{ inputs.template-file }}
TARGET_JSON_FILE: ${{ inputs.targetJsonFile }}
TARGET_MD_FILE: ${{ inputs.targetMdFile }}
54 changes: 23 additions & 31 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,35 @@ import (
)

const (
keyOrganization = "ORGANIZATION"
keyTargetRepository = "TARGET_REPOSITORY"
keyTargetRepositoryFile = "TARGET_REPOSITORY_FILE"
keyRepositories = "REPOSITORIES"
keyGithubToken = "GITHUB_TOKEN"
keyEnvironments = "ENVIRONMENTS"
keyEnvironmentLinks = "ENVIRONMENT_LINKS"
keyVerbose = "VERBOSE"
keyTemplateFile = "TEMPLATE_FILE"
keyTitle = "TITLE"
keyOrganization = "ORGANIZATION"
keyRepositories = "REPOSITORIES"
keyGithubToken = "GITHUB_TOKEN"
keyEnvironments = "ENVIRONMENTS"
keyEnvironmentLinks = "ENVIRONMENT_LINKS"
keyVerbose = "VERBOSE"
keyTemplateFile = "TEMPLATE_FILE"
keyTitle = "TITLE"
keyTargetJsonFile = "TARGET_JSON_FILE"
keyTargetMdFile = "TARGET_MD_FILE"
)

type Config struct {
Organization string
TargetRepository string
TargetRepositoryFile string
Repositories string
Environments string
EnvironmentLinks string
GithubToken string
TemplateFile string
Title string
Organization string
TargetJsonFile string
TargetMdFile string
Repositories string
Environments string
EnvironmentLinks string
GithubToken string
TemplateFile string
Title string
}

func CreateConfig(ctx context.Context) (*Config, error) {
c := Config{}
flag.StringVar(&c.Organization, "organization", lookupEnvOrString(keyOrganization, ""), "The GitHub Organization to query for repositories.")
flag.StringVar(&c.TargetRepository, "target-repository", lookupEnvOrString(keyTargetRepository, ""), "The target repository to commit the result to.")
flag.StringVar(&c.TargetRepositoryFile, "target-repository-file", lookupEnvOrString(keyTargetRepositoryFile, ""), "The target repository file to commit the result to.")
flag.StringVar(&c.TargetJsonFile, "target-json-file", lookupEnvOrString(keyTargetJsonFile, "deployment-overview.json"), "The target json file to commit the result to.")
flag.StringVar(&c.TargetMdFile, "target-md-file", lookupEnvOrString(keyTargetMdFile, "deployment-overview.md"), "The target md file to commit the result to.")
flag.StringVar(&c.Repositories, "repositories", lookupEnvOrString(keyRepositories, ""), "Repositories to query. Comma separated list.")
flag.StringVar(&c.GithubToken, "github-token", lookupEnvOrString(keyGithubToken, ""), "The GitHub Token to use for authentication.")
flag.StringVar(&c.Environments, "environments", lookupEnvOrString(keyEnvironments, ""), "Environments to query. Comma separated list.")
Expand All @@ -64,9 +64,9 @@ func CreateConfig(ctx context.Context) (*Config, error) {
"Repositories", c.RepositoriesAsList(),
"Environments", c.EnvironmentsAsList(),
"EnvironmentLinks", c.EnvironmentLinksAsList(),
"TargetRepository", c.TargetRepository,
"TargetRepositoryFile", c.TargetRepositoryFile,
"TemplateFile", c.TemplateFile,
"TargetJsonFile", c.TargetJsonFile,
"TargetMdFile", c.TargetMdFile,
"Title", c.Title)

return &c, nil
Expand All @@ -85,14 +85,6 @@ func (c *Config) Validate() error {
return errors.New("Repositories is required")
}

if c.TargetRepository == "" {
return errors.New("Target Repository is required")
}

if c.TargetRepositoryFile == "" {
return errors.New("Target Repository File is required")
}

return nil

}
Expand Down
16 changes: 12 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
"fmt"
"github.com/google/go-github/v59/github"
_ "github.com/google/go-github/v59/github"
"github.com/prodyna/deployment-overview/config"
Expand Down Expand Up @@ -59,19 +58,28 @@ func do() error {
slog.ErrorContext(ctx, "Unable to render organization", "error", err)
return err
}
fmt.Printf("%s\n", json)

slog.DebugContext(ctx, "Writing json to file", "file", c.TargetJsonFile)
err = publish.WriteFile(c.TargetJsonFile, json)
if err != nil {
slog.ErrorContext(ctx, "Unable to write json to file", "error", err)
return err
}
slog.InfoContext(ctx, "Wrote json to file", "file", c.TargetJsonFile)

md, err := organization.RenderMarkdown(ctx, string(template))
if err != nil {
slog.ErrorContext(ctx, "Unable to render organization", "error", err)
return err
}

err = publish.PublishToGitHub(ctx, c, md, gh)
slog.DebugContext(ctx, "Writing md to file", "file", c.TargetMdFile)
err = publish.WriteFile(c.TargetMdFile, []byte(md))
if err != nil {
slog.ErrorContext(ctx, "Unable to publish to github", "error", err)
slog.ErrorContext(ctx, "Unable to write md to file", "error", err)
return err
}
slog.InfoContext(ctx, "Wrote md to file", "file", c.TargetMdFile)

return nil
}
94 changes: 0 additions & 94 deletions publish/publish.go

This file was deleted.

25 changes: 25 additions & 0 deletions publish/write.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package publish

import "os"

func WriteFile(file string, data []byte) error {
// Open file for writing
f, err := os.Create(file)
if err != nil {
return err
}

// Write data to file
_, err = f.Write(data)
if err != nil {
return err
}

// Close file
err = f.Close()
if err != nil {
return err
}

return nil
}
2 changes: 0 additions & 2 deletions result/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/google/go-github/v59/github"
"github.com/prodyna/deployment-overview/config"
"log/slog"
"os"
"strings"
"text/template"
"time"
Expand Down Expand Up @@ -293,7 +292,6 @@ func (organization *Organization) RenderJson(ctx context.Context) (result []byte
slog.ErrorContext(ctx, "Unable to render organization", "error", err)
return nil, err
}
os.Stdout.Write(output)
return output, nil
}

Expand Down

0 comments on commit 8f7aeb7

Please sign in to comment.