Skip to content

Commit

Permalink
Merge pull request #23 from Adirio/debug
Browse files Browse the repository at this point in the history
🌱 Improve debugging output from the action execution
  • Loading branch information
k8s-ci-robot authored Nov 20, 2020
2 parents 52659fd + 8685728 commit f75088a
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ inputs:
required: true
runs:
using: docker
image: '../Dockerfile'
image: '../../../Dockerfile'
17 changes: 9 additions & 8 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
name: PR Verifier

on:
pull_request_target:
types: [opened, edited, reopened, synchronize]

jobs:
verify:
name: Verify PR contents
runs-on: ubuntu-latest
name: verify PR contents
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Verifier action
id: verifier
uses: ./action-nightly
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
- name: Checkout
uses: actions/checkout@v2
- name: Verifier action
uses: ./.github/actions/verifier
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ The code that actually runs lives in [verify/cmd](/verify/cmd), while
from GitHub actions & uploading the result via the GitHub checks API.
This repo itself uses a "live" version of the action that always rebuilds
from the local code, which lives in [action-nightly](/action-nightly).
from the local code (master branch), which lives in
[.github/actions/verifier](/.github/actions/verifier).
### Updating the action
Expand Down
17 changes: 9 additions & 8 deletions verify/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ import (

"github.com/google/go-github/v32/github"
"golang.org/x/oauth2"

"sigs.k8s.io/kubebuilder-release-tools/verify/pkg/log"
)

var log logger
var l = log.New()

type ActionsEnv struct {
Owner string
Expand Down Expand Up @@ -87,15 +89,13 @@ type ActionsCallback func(*ActionsEnv) error
func ActionsEntrypoint(cb ActionsCallback) {
env, err := setupEnv()
if err != nil {
log.errorf("%v", err)
os.Exit(1)
l.Fatalf(1, "%v", err)
}

if err := cb(env); err != nil {
log.errorf("%v", err)
os.Exit(2)
l.Fatalf(2, "%v", err)
}
fmt.Println("Success!")
l.Info("Success!")
}

func RunPlugins(plugins ...PRPlugin) ActionsCallback {
Expand All @@ -107,6 +107,7 @@ func RunPlugins(plugins ...PRPlugin) ActionsCallback {
done.Add(1)
go func(plugin PRPlugin) {
defer done.Done()
plugin.init()
res <- plugin.entrypoint(env)
}(plugin)
}
Expand All @@ -122,10 +123,10 @@ func RunPlugins(plugins ...PRPlugin) ActionsCallback {
continue
}
errCount++
log.errorf("%v", err)
l.Errorf("%v", err)
}

fmt.Printf("%d plugins ran\n", len(plugins))
l.Infof("%d plugins ran", len(plugins))
if errCount > 0 {
return fmt.Errorf("%d plugins had errors", errCount)
}
Expand Down
30 changes: 30 additions & 0 deletions verify/pkg/log/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package log

type Logger interface {
Debug(content string)
Debugf(format string, args ...interface{})
Info(content string)
Infof(format string, args ...interface{})
Warning(content string)
Warningf(format string, args ...interface{})
Error(content string)
Errorf(format string, args ...interface{})
Fatal(exitCode int, content string)
Fatalf(exitCode int, format string, args ...interface{})
}
36 changes: 21 additions & 15 deletions verify/logger.go → verify/pkg/log/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,28 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package verify
package log

import (
"fmt"
)

type logger struct{}

func (logger) errorf(format string, args ...interface{}) {
fmt.Printf("::error::"+format+"\n", args...)
}
type LoggingLevel uint8

func (logger) debugf(format string, args ...interface{}) {
fmt.Printf("::debug::"+format+"\n", args...)
}
const (
Debug = iota
Info
Warning
Error
)

func (logger) warningf(format string, args ...interface{}) {
fmt.Printf("::warning::"+format+"\n", args...)
func (level LoggingLevel) String() string {
switch level {
case Debug:
return "debug"
case Info:
return "info"
case Warning:
return "warning"
case Error:
return "error"
default:
return "unknown logging level"
}
}
125 changes: 125 additions & 0 deletions verify/pkg/log/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package log

import (
"fmt"
"os"
"strings"
)

// Prefixes used in GitHub actions
const (
ghPrefixDebug = "::debug::"
ghPrefixWarning = "::warning::"
ghPrefixError = "::error::"
)

// Verify that logger implements Logger
var _ Logger = logger{}

// logger provides logging functions for GitHub actions
type logger struct{
// Prefixes for the different logging methods
prefixDebug string
prefixInfo string
prefixWarning string
prefixError string
}

// New returns a basic logger for GitHub actions
func New() Logger {
return &logger{
prefixDebug: ghPrefixDebug,
prefixInfo: "",
prefixWarning: ghPrefixWarning,
prefixError: ghPrefixError,
}
}

// NewFor returns a named logger for GitHub actions
func NewFor(name string) Logger {
return &logger{
prefixDebug: fmt.Sprintf("%s[%s]", ghPrefixDebug, name),
prefixInfo: fmt.Sprintf("[%s]", name),
prefixWarning: fmt.Sprintf("%s[%s]", ghPrefixWarning, name),
prefixError: fmt.Sprintf("%s[%s]", ghPrefixError, name),
}
}

func (l logger) prefixFor(level LoggingLevel) string {
switch level {
case Debug:
return l.prefixDebug
case Info:
return l.prefixInfo
case Warning:
return l.prefixWarning
case Error:
return l.prefixError
default:
panic("invalid logging level")
}
}

func (l logger) log(content string, level LoggingLevel) {
prefix := l.prefixFor(level)
for _, s := range strings.Split(content, "\n") {
fmt.Println(prefix + s)
}
}

func (l logger) Debug(content string) {
l.log(content, Debug)
}

func (l logger) Debugf(format string, args ...interface{}) {
l.Debug(fmt.Sprintf(format, args...))
}

func (l logger) Info(content string) {
l.log(content, Info)
}

func (l logger) Infof(format string, args ...interface{}) {
l.Info(fmt.Sprintf(format, args...))
}

func (l logger) Warning(content string) {
l.log(content, Warning)
}

func (l logger) Warningf(format string, args ...interface{}) {
l.Warning(fmt.Sprintf(format, args...))
}

func (l logger) Error(content string) {
l.log(content, Error)
}

func (l logger) Errorf(format string, args ...interface{}) {
l.Error(fmt.Sprintf(format, args...))
}

func (l logger) Fatal(exitCode int, content string) {
l.log(content, Error)
os.Exit(exitCode)
}

func (l logger) Fatalf(exitCode int, format string, args ...interface{}) {
l.Fatal(exitCode, fmt.Sprintf(format, args...))
}
Loading

0 comments on commit f75088a

Please sign in to comment.