Skip to content

Commit 9390793

Browse files
authored
feat: add check for newer versions (#1562)
* feat: add check for newer versions * fix: support JSON logger and rever updates to go.mod * fix: keep version updated in source code * fix: lint errors * fix: revert go.*
1 parent 767e6a8 commit 9390793

File tree

6 files changed

+121
-13
lines changed

6 files changed

+121
-13
lines changed

.mega-linter.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ DISABLE_LINTERS:
1414
- MARKDOWN_MARKDOWN_LINK_CHECK
1515
- REPOSITORY_CHECKOV
1616
- REPOSITORY_TRIVY
17-
FILTER_REGEX_EXCLUDE: (.*testdata/*|install.sh|pkg/container/docker_cli.go|pkg/container/DOCKER_LICENSE)
17+
FILTER_REGEX_EXCLUDE: (.*testdata/*|install.sh|pkg/container/docker_cli.go|pkg/container/DOCKER_LICENSE|VERSION)
1818
MARKDOWN_MARKDOWNLINT_CONFIG_FILE: .markdownlint.yml
1919
PARALLEL: false
2020
PRINT_ALPACA: false

Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ ifneq ($(shell git status -s),)
9696
@echo "Unable to promote a dirty workspace"
9797
@exit 1
9898
endif
99+
echo -n $(NEW_VERSION) > VERSION
100+
git add VERSION
101+
git commit -m "chore: bump VERSION"
99102
git tag -a -m "releasing v$(NEW_VERSION)" v$(NEW_VERSION)
100103
git push origin v$(NEW_VERSION)
101104

@@ -105,3 +108,5 @@ snapshot:
105108
--rm-dist \
106109
--single-target \
107110
--snapshot
111+
112+
.PHONY: clean all

VERSION

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.2.35

cmd/notices.go

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package cmd
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"net/url"
8+
"os"
9+
"runtime"
10+
"time"
11+
12+
log "github.com/sirupsen/logrus"
13+
)
14+
15+
type Notice struct {
16+
Level string `json:"level"`
17+
Message string `json:"message"`
18+
}
19+
20+
func displayNotices(input *Input) {
21+
select {
22+
case notices := <-noticesLoaded:
23+
if len(notices) > 0 {
24+
noticeLogger := log.New()
25+
if input.jsonLogger {
26+
noticeLogger.SetFormatter(&log.JSONFormatter{})
27+
} else {
28+
noticeLogger.SetFormatter(&log.TextFormatter{
29+
DisableQuote: true,
30+
DisableTimestamp: true,
31+
PadLevelText: true,
32+
})
33+
}
34+
35+
fmt.Printf("\n")
36+
for _, notice := range notices {
37+
level, err := log.ParseLevel(notice.Level)
38+
if err != nil {
39+
level = log.InfoLevel
40+
}
41+
noticeLogger.Log(level, notice.Message)
42+
}
43+
}
44+
case <-time.After(time.Second * 1):
45+
log.Debugf("Timeout waiting for notices")
46+
}
47+
}
48+
49+
var noticesLoaded = make(chan []Notice)
50+
51+
func loadVersionNotices(version string) {
52+
go func() {
53+
noticesLoaded <- getVersionNotices(version)
54+
}()
55+
}
56+
57+
const NoticeURL = "https://api.nektosact.com/notices"
58+
59+
func getVersionNotices(version string) []Notice {
60+
if os.Getenv("ACT_DISABLE_VERSION_CHECK") == "1" {
61+
return nil
62+
}
63+
64+
noticeURL, err := url.Parse(NoticeURL)
65+
if err != nil {
66+
log.Error(err)
67+
return nil
68+
}
69+
query := noticeURL.Query()
70+
query.Add("os", runtime.GOOS)
71+
query.Add("arch", runtime.GOARCH)
72+
query.Add("version", version)
73+
74+
noticeURL.RawQuery = query.Encode()
75+
76+
resp, err := http.Get(noticeURL.String())
77+
if err != nil {
78+
log.Debug(err)
79+
return nil
80+
}
81+
82+
defer resp.Body.Close()
83+
notices := []Notice{}
84+
if err := json.NewDecoder(resp.Body).Decode(&notices); err != nil {
85+
log.Debug(err)
86+
return nil
87+
}
88+
89+
return notices
90+
}

cmd/root.go

+21-11
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ import (
3030
func Execute(ctx context.Context, version string) {
3131
input := new(Input)
3232
var rootCmd = &cobra.Command{
33-
Use: "act [event name to run] [flags]\n\nIf no event name passed, will default to \"on: push\"\nIf actions handles only one event it will be used as default instead of \"on: push\"",
34-
Short: "Run GitHub actions locally by specifying the event name (e.g. `push`) or an action name directly.",
35-
Args: cobra.MaximumNArgs(1),
36-
RunE: newRunCommand(ctx, input),
37-
PersistentPreRun: setupLogging,
38-
Version: version,
39-
SilenceUsage: true,
33+
Use: "act [event name to run] [flags]\n\nIf no event name passed, will default to \"on: push\"\nIf actions handles only one event it will be used as default instead of \"on: push\"",
34+
Short: "Run GitHub actions locally by specifying the event name (e.g. `push`) or an action name directly.",
35+
Args: cobra.MaximumNArgs(1),
36+
RunE: newRunCommand(ctx, input),
37+
PersistentPreRun: setup(input),
38+
PersistentPostRun: cleanup(input),
39+
Version: version,
40+
SilenceUsage: true,
4041
}
4142
rootCmd.Flags().BoolP("watch", "w", false, "watch the contents of the local repo and run when files change")
4243
rootCmd.Flags().BoolP("list", "l", false, "list workflows")
@@ -244,10 +245,19 @@ func readArgsFile(file string, split bool) []string {
244245
return args
245246
}
246247

247-
func setupLogging(cmd *cobra.Command, _ []string) {
248-
verbose, _ := cmd.Flags().GetBool("verbose")
249-
if verbose {
250-
log.SetLevel(log.DebugLevel)
248+
func setup(inputs *Input) func(*cobra.Command, []string) {
249+
return func(cmd *cobra.Command, _ []string) {
250+
verbose, _ := cmd.Flags().GetBool("verbose")
251+
if verbose {
252+
log.SetLevel(log.DebugLevel)
253+
}
254+
loadVersionNotices(cmd.Version)
255+
}
256+
}
257+
258+
func cleanup(inputs *Input) func(*cobra.Command, []string) {
259+
return func(cmd *cobra.Command, _ []string) {
260+
displayNotices(inputs)
251261
}
252262
}
253263

main.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ package main
22

33
import (
44
"context"
5+
_ "embed"
56
"os"
67
"os/signal"
78
"syscall"
89

910
"github.com/nektos/act/cmd"
1011
)
1112

12-
var version = "v0.2.27-dev" // Manually bump after tagging next release
13+
//go:embed VERSION
14+
var version string
1315

1416
func main() {
1517
ctx := context.Background()

0 commit comments

Comments
 (0)