From 9412a295364eadf5ecfd9f368d76db3c6ac6e933 Mon Sep 17 00:00:00 2001 From: oliverpool Date: Wed, 23 Mar 2022 17:32:29 +0100 Subject: [PATCH] github: check new kernel commit --- .github/workflows/cron.yml | 12 ++++---- cmd/check-update/main.go | 61 +++++++++++++++++++++++++++----------- 2 files changed, 48 insertions(+), 25 deletions(-) diff --git a/.github/workflows/cron.yml b/.github/workflows/cron.yml index 63aa708..68f7f68 100644 --- a/.github/workflows/cron.yml +++ b/.github/workflows/cron.yml @@ -1,4 +1,4 @@ -name: Auto-update firmware +name: Auto-update kernel on: push: @@ -13,8 +13,6 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - with: - submodules: true - name: Set up Go uses: actions/setup-go@v2 @@ -23,13 +21,13 @@ jobs: - name: Check latest kernel version from https://archive.raspberrypi.org/debian/ id: check - run: echo "::set-output name=version::$(go run ./cmd/check-update/main.go)" + run: echo "::set-output name=sha::$(go run ./cmd/check-update/main.go)" - name: Fetch latest kernel - if: steps.check.outputs.version != '' - run: echo "checkout ${{steps.check.outputs.version}}" + if: steps.check.outputs.sha != '' + run: echo "checkout ${{steps.check.outputs.sha}}" - name: Commit the new kernel uses: stefanzweifel/git-auto-commit-action@v4 with: - commit_message: kernel ${{steps.check.outputs.version}} + commit_message: kernel ${{steps.check.outputs.sha}} diff --git a/cmd/check-update/main.go b/cmd/check-update/main.go index c0a7631..49d9f6d 100644 --- a/cmd/check-update/main.go +++ b/cmd/check-update/main.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "encoding/json" "errors" "fmt" "log" @@ -51,40 +52,64 @@ func run() error { log.Println("latest version:", tagName) - tags, err := gitTags("linux-sources") + latestSha, err := githubCommitSha(tagName) if err != nil { return err } - for _, tag := range tags { - if tagName == tag { - log.Println("already up to date") - return nil - } + log.Println("latest commit:", latestSha) + + currentSha, err := submoduleSha("linux-sources") + if err != nil { + return err } - log.Println("outdated tag", tags) - fmt.Println(tagName) + log.Println("submodule commit:", currentSha) + + if latestSha == currentSha { + log.Println("already up to date") + return nil + } + fmt.Println(latestSha) return nil } -func gitTags(folder string) ([]string, error) { - current, err := os.Getwd() +func githubCommitSha(tagName string) (string, error) { + req, err := http.NewRequest("GET", "https://api.github.com/repos/raspberrypi/linux/git/ref/tags/"+tagName, nil) if err != nil { - return nil, err + return "", err } - defer os.Chdir(current) + req.Header.Add("Accept", "application/vnd.github.v3+json") + resp, err := http.DefaultClient.Do(req) + if err != nil { + return "", err + } + defer resp.Body.Close() - err = os.Chdir(folder) - if err != nil { - return nil, err + type githubResponse struct { + Message string `json:"message"` + Object struct { + Sha string `json:"sha"` + } `json:"object"` } - cmd := exec.Command("git", "tag", "--points-at", "HEAD") + var gr githubResponse + err = json.NewDecoder(resp.Body).Decode(&gr) + if err != nil { + return "", err + } + if gr.Object.Sha == "" { + return "", errors.New("could not get sha: " + gr.Message) + } + return gr.Object.Sha, nil +} + +func submoduleSha(submodule string) (string, error) { + cmd := exec.Command("git", "rev-parse", "HEAD:"+submodule) cmd.Stderr = os.Stderr out, err := cmd.Output() if err != nil { - return nil, err + return "", err } - return strings.Split(strings.TrimSpace(string(out)), "\n"), nil + return strings.TrimSpace(string(out)), nil } func scanOnlineTextFile(url string, stopScanning func(string) bool) error {