Skip to content

Commit

Permalink
Use git information for AppInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
kaack committed Jul 22, 2023
1 parent b8cadcd commit 9251f16
Show file tree
Hide file tree
Showing 11 changed files with 364 additions and 226 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/windows-amd64.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
dir /a &&
go generate ./... &&
go build -trimpath -tags static -o elrs-joystick-control.exe .\cmd\elrs-joystick-control\. &&
go run scripts\build-release-zip.go --location C:\app --prefix elrs-joystick-control --files *.exe,LICENSE* "
go run scripts\cmd\build-release-zip\build-release-zip.go --location C:\app --prefix elrs-joystick-control --files *.exe,LICENSE* "
- name: Upload binary
uses: actions/upload-artifact@v3
with:
Expand Down
218 changes: 100 additions & 118 deletions pkg/proto/generated/pb/server.pb.go

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions pkg/proto/server.proto
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,9 @@ message BatteryData {


message GetAppInfoRes {
string path = 1;
string version = 2;
string sum = 3;
string vcs = 4;
string vcs_revision = 5;
string release_tag = 1;
string commit_hash = 2;
string branch_name = 3;
}

service JoystickControl {
Expand Down
1 change: 1 addition & 0 deletions pkg/server/generated/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version.json
5 changes: 5 additions & 0 deletions pkg/server/generated/version.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"release_tag": "(generated)",
"commit_hash": "(generated)",
"branch_name": "(generated)"
}
28 changes: 7 additions & 21 deletions pkg/server/server_grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/structpb"
"runtime/debug"
"time"
)

Expand Down Expand Up @@ -298,29 +297,16 @@ func (s *GRPCServer) GetTelemetryStream(_ *pb.Empty, server pb.JoystickControl_G

func (s *GRPCServer) GetAppInfo(_ context.Context, _ *pb.Empty) (*pb.GetAppInfoRes, error) {

var info *debug.BuildInfo
var ok bool

if info, ok = debug.ReadBuildInfo(); !ok {
return nil, status.Error(codes.Internal, "could not read application information")
}

var vcs string
var vcsRevision string
var info *VersionInfo
var err error

for _, setting := range info.Settings {
if setting.Key == "vcs" {
vcs = setting.Value
} else if setting.Key == "vcs.revision" {
vcsRevision = setting.Value
}
if info, err = GetVersionInfo(); err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("could not unmarshal version file. %s", err.Error()))
}

return &pb.GetAppInfoRes{
Path: info.Main.Path,
Version: info.Main.Version,
Sum: info.Main.Sum,
Vcs: vcs,
VcsRevision: vcsRevision,
ReleaseTag: info.ReleaseTag,
CommitHash: info.CommitHash,
BranchName: info.BranchName,
}, nil
}
38 changes: 38 additions & 0 deletions pkg/server/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-FileCopyrightText: © 2023 OneEyeFPV [email protected]
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-License-Identifier: FS-0.9-or-later

package server

import (
_ "embed"
"encoding/json"
"sync"
)

//go:generate go run ..\..\scripts\cmd\generate-version-file\generate-version-file.go --repo ..\.. --location ./generated
//go:embed generated/version.json
var versionJson []byte

type VersionInfo struct {
ReleaseTag string `json:"release_tag"`
CommitHash string `json:"commit_hash"`
BranchName string `json:"branch_name"`
}

var _version *VersionInfo
var _lock = &sync.Mutex{}

func GetVersionInfo() (*VersionInfo, error) {
if _version == nil {
_lock.Lock()
defer _lock.Unlock()
if _version == nil {
_version = &VersionInfo{}
if err := json.Unmarshal(versionJson, _version); err != nil {
return nil, err
}
}
}
return _version, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func createZip(outputDir string, fileName string, filesList []string) error {
return err
}

//goland:noinspection ALL
defer archive.Close()
zipWriter := zip.NewWriter(archive)

Expand Down Expand Up @@ -157,7 +158,7 @@ func createZip(outputDir string, fileName string, filesList []string) error {
}
}

zipWriter.Close()
_ = zipWriter.Close()

return nil
}
Expand Down Expand Up @@ -197,7 +198,9 @@ func main() {
fmt.Printf("release file name: %s\n", zipOutputFile)
fmt.Printf("release file contents: %v", filesList)

createZip(outputZipDir, zipOutputFile, filesList)
if err = createZip(outputZipDir, zipOutputFile, filesList); err != nil {
panic(err)
}

return
}
185 changes: 185 additions & 0 deletions scripts/cmd/generate-version-file/generate-version-file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
// SPDX-FileCopyrightText: © 2023 OneEyeFPV [email protected]
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-License-Identifier: FS-0.9-or-later

package main

import (
"bufio"
"encoding/json"
"errors"
"flag"
"fmt"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/storer"
"io"
"os"
"path/filepath"
"regexp"
)

type VersionInfo struct {
ReleaseTag string `json:"release_tag"`
CommitHash string `json:"commit_hash"`
BranchName string `json:"branch_name"`
}

func getCommitTags(r *git.Repository, hash plumbing.Hash) ([]string, error) {
var iter storer.ReferenceIter
var err error

if iter, err = r.Tags(); err != nil {
return nil, err
}

var ref *plumbing.Reference
var tags []string
for {
if ref, err = iter.Next(); errors.Is(err, io.EOF) {
break
} else if err != nil {
return nil, err
}

if ref.Hash() != hash {
continue
}

tags = append(tags, ref.Name().Short())

}
return tags, nil
}

func getHeadTags(repoPath string) ([]string, error) {
var repo *git.Repository
var err error
if repo, err = git.PlainOpen(repoPath); err != nil {
return nil, err
}

var hashCommit *plumbing.Reference
if hashCommit, err = repo.Head(); err != nil {
return nil, err
}

return getCommitTags(repo, hashCommit.Hash())
}

func getReleaseTag(repoPath string, defaultTag string) (string, error) {
var tags []string
var err error

if tags, err = getHeadTags(repoPath); err != nil {
return "", err
}

var ok bool
for _, tag := range tags {
if ok, err = regexp.MatchString(`^v\d+\.\d+\.\d+`, tag); err != nil {
return "", err
} else if ok {
return tag, nil
}
}

return defaultTag, nil
}

func getCommitHash(repoPath string) (string, error) {
var repo *git.Repository
var err error
if repo, err = git.PlainOpen(repoPath); err != nil {
return "", err
}

var hashCommit *plumbing.Reference
if hashCommit, err = repo.Head(); err != nil {
return "", err
}

return hashCommit.Hash().String(), nil
}

func getBranchName(repoPath string) (string, error) {
var repo *git.Repository
var err error
if repo, err = git.PlainOpen(repoPath); err != nil {
return "", err
}

var head *plumbing.Reference
if head, err = repo.Head(); err != nil {
return "", err
}

return head.Name().Short(), nil

}

//goland:noinspection GoUnhandledErrorResult
func createVersionFile(location string, versionInfo VersionInfo) error {
var err error
var file *os.File

if file, err = os.Create(filepath.Join(location, "version.json")); err != nil {
return err
}
defer file.Close()

writer := bufio.NewWriter(file)

var jsonData []byte
if jsonData, err = json.MarshalIndent(versionInfo, "", " "); err != nil {
panic(err)
}

writer.Write(jsonData)
writer.Flush()

return nil
}

func main() {
var outputDir string
var repoPath string

flag.StringVar(&repoPath, "repo", ".", "Path containing .git")
flag.StringVar(&outputDir, "location", ".", "Output file directory")

flag.Parse()

var err error
var info os.FileInfo

if info, err = os.Stat(outputDir); err != nil || !info.IsDir() {
panic(errors.New(fmt.Sprintf("output directory %s does not exist\n", outputDir)))
}

var releaseTag string
var commitHash string
var branchName string

if releaseTag, err = getReleaseTag(repoPath, "(devel)"); err != nil {
panic(err)
}
if commitHash, err = getCommitHash(repoPath); err != nil {
panic(err)
}
if branchName, err = getBranchName(repoPath); err != nil {
panic(err)
}

version := VersionInfo{
ReleaseTag: releaseTag,
CommitHash: commitHash,
BranchName: branchName,
}

if err = createVersionFile(outputDir, version); err != nil {
panic(err)
}

return
}
2 changes: 1 addition & 1 deletion webapp/src/components/pages/AboutPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default function AboutPage({}) {


const getBackendVersionString = function (appInfo) {
return `${appInfo.getVersion()} ${appInfo.getVcs()}-${appInfo.getVcsRevision().substring(0, 7)}`;
return `${appInfo.getReleaseTag()}-${appInfo.getCommitHash().substring(0, 7)}`;
}

const openInNewTab = (url) => {
Expand Down
Loading

0 comments on commit 9251f16

Please sign in to comment.