diff --git a/.github/workflows/docs.yaml b/.github/workflows/docs.yaml index be653401a4..2cf95a2ddc 100644 --- a/.github/workflows/docs.yaml +++ b/.github/workflows/docs.yaml @@ -3,13 +3,13 @@ name: Documentation on: push: branches: - - master + - v5 paths: - "docs/**" - ".github/workflows/docs.yaml" pull_request: branches: - - master + - v5 paths: - "docs/**" - ".github/workflows/docs.yaml" diff --git a/.github/workflows/e2e-tests.yaml b/.github/workflows/e2e-tests.yaml index 7b70bfedf0..e70ac511a9 100644 --- a/.github/workflows/e2e-tests.yaml +++ b/.github/workflows/e2e-tests.yaml @@ -5,7 +5,7 @@ on: types: [created] pull_request: branches: - - master + - v5 paths: - "Dockerfile" - "**.go" diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index 1dd47162a4..a1a73b6949 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -5,7 +5,7 @@ on: types: [created] pull_request: branches: - - master + - v5 paths: - "**.go" - ".github/workflows/lint.yaml" @@ -16,11 +16,13 @@ jobs: name: lint runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - name: golangci-lint - uses: golangci/golangci-lint-action@v2 + - uses: actions/setup-go@v3 + with: + go-version: 1.18 + - uses: actions/checkout@v3 + - name: Run golangci-lint + uses: golangci/golangci-lint-action@v3.2.0 with: - version: v1.29 args: -v --config=.golangci.yml diff --git a/.github/workflows/npm.yaml b/.github/workflows/npm.yaml index 27f73fc31e..ce5b1d8006 100644 --- a/.github/workflows/npm.yaml +++ b/.github/workflows/npm.yaml @@ -3,7 +3,7 @@ name: Test NPM Installer on: pull_request: branches: - - master + - v5 paths: - "dist/npm/**" - ".github/workflows/npm.yaml" diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index c8d4b0ddeb..e3e4427726 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -5,7 +5,7 @@ on: types: [created] push: branches: - - master + - v5 paths: - "Dockerfile" - "**.go" @@ -13,7 +13,7 @@ on: - ".github/workflows/release.yaml" pull_request: branches: - - master + - v5 paths: - "Dockerfile" - "**.go" diff --git a/.github/workflows/ui.yaml b/.github/workflows/ui.yaml index 312e6d489a..d8026f5c07 100644 --- a/.github/workflows/ui.yaml +++ b/.github/workflows/ui.yaml @@ -3,7 +3,7 @@ name: Test UI on: pull_request: branches: - - master + - v5 paths: - "ui/**" - "hack/build-ui.bash" diff --git a/.github/workflows/unit-tests.yaml b/.github/workflows/unit-tests.yaml index f537847496..efe953f6e8 100644 --- a/.github/workflows/unit-tests.yaml +++ b/.github/workflows/unit-tests.yaml @@ -5,7 +5,7 @@ on: types: [created] pull_request: branches: - - master + - v5 paths: - "**.go" - "!e2e/**" # exclude files from e2e tests diff --git a/dist/npm/index.js b/dist/npm/index.js index 104dddf3a2..bb330aa824 100644 --- a/dist/npm/index.js +++ b/dist/npm/index.js @@ -8,7 +8,7 @@ const inquirer = require('inquirer'); const findProcess = require('find-process'); const downloadPathTemplate = - "https://github.com/loft-sh/devspace/releases/download/{{version}}/devspace-{{platform}}-{{arch}}"; + "https://github.com/loft-sh/devspace/releases/download/v{{version}}/devspace-{{platform}}-{{arch}}"; const ARCH_MAPPING = { ia32: "386", x64: "amd64", @@ -460,4 +460,4 @@ if (process.ppid > 1) { }) } else { continueProcess(true); -} \ No newline at end of file +} diff --git a/pkg/devspace/server/download.go b/pkg/devspace/server/download.go index 829c1becc7..7d65999a92 100644 --- a/pkg/devspace/server/download.go +++ b/pkg/devspace/server/download.go @@ -6,11 +6,9 @@ import ( "compress/gzip" "fmt" "io" - "io/ioutil" "net/http" "os" "path/filepath" - "regexp" "strings" "time" @@ -18,16 +16,17 @@ import ( "github.com/loft-sh/devspace/pkg/devspace/config/constants" "github.com/loft-sh/devspace/pkg/devspace/upgrade" + "github.com/loft-sh/devspace/pkg/util/git" homedir "github.com/mitchellh/go-homedir" "github.com/pkg/errors" ) -// UIDownloadBaseURL is the base url where to look for the ui -const UIDownloadBaseURL = "https://github.com/loft-sh/devspace/releases" +// UIRepository is the repository containing the devspace UI +const UIRepository = "https://github.com/loft-sh/devspace" -// UIDownloadRegEx is the regexp that finds the correct download link for the ui -var UIDownloadRegEx = regexp.MustCompile(`href="(\/loft-sh\/devspace\/releases\/download\/[^\/]*\/ui.tar.gz)"`) +// UIDownloadBaseURL is the base url where to look for the ui +const UIDownloadBaseURL = UIRepository + "/releases/download" // UITempFolder is the temp folder to cache the ui in const UITempFolder = "ui" @@ -77,30 +76,15 @@ func downloadFile(version string, folder string) error { } // Create download url - url := "" if version == "latest" { - url = fmt.Sprintf("%s/%s", UIDownloadBaseURL, version) - } else { - url = fmt.Sprintf("%s/tag/%s", UIDownloadBaseURL, version) + version, err = git.GetLatestVersion(UIRepository) + if err != nil { + return errors.Wrap(err, "get latest version") + } } - // Download html + url := fmt.Sprintf("%s/%s/%s", UIDownloadBaseURL, version, "ui.tar.gz") resp, err := http.Get(url) - if err != nil { - return errors.Wrap(err, "get url") - } - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return errors.Wrap(err, "read body") - } - - matches := UIDownloadRegEx.FindStringSubmatch(string(body)) - if len(matches) != 2 { - return errors.Errorf("Couldn't find ui in github release %s at url %s", version, url) - } - - resp, err = http.Get("https://github.com" + matches[1]) if err != nil { return errors.Wrap(err, "download ui archive") } diff --git a/pkg/devspace/services/inject/inject.go b/pkg/devspace/services/inject/inject.go index e7d75868d1..215ee605fb 100644 --- a/pkg/devspace/services/inject/inject.go +++ b/pkg/devspace/services/inject/inject.go @@ -5,22 +5,22 @@ import ( "bytes" "compress/gzip" "fmt" - "github.com/loft-sh/devspace/assets" "io" "io/fs" "io/ioutil" "net/http" "os" "path/filepath" - "regexp" "strings" "sync" "time" + "github.com/loft-sh/devspace/assets" "github.com/loft-sh/devspace/pkg/devspace/config/constants" "github.com/loft-sh/devspace/pkg/devspace/config/versions/latest" "github.com/loft-sh/devspace/pkg/devspace/kubectl" "github.com/loft-sh/devspace/pkg/devspace/upgrade" + "github.com/loft-sh/devspace/pkg/util/git" "github.com/loft-sh/devspace/pkg/util/hash" logpkg "github.com/loft-sh/devspace/pkg/util/log" "github.com/mitchellh/go-homedir" @@ -28,15 +28,15 @@ import ( v1 "k8s.io/api/core/v1" ) +// DevSpaceHelperRepository is the repository containing the devspace helper +const DevSpaceHelperRepository = "https://github.com/loft-sh/devspace" + // DevSpaceHelperBaseURL is the base url where to look for the sync helper -const DevSpaceHelperBaseURL = "https://github.com/loft-sh/devspace/releases" +const DevSpaceHelperBaseURL = DevSpaceHelperRepository + "/releases/download" // DevSpaceHelperTempFolder is the local folder where we store the sync helper const DevSpaceHelperTempFolder = "devspacehelper" -// helperBinaryRegEx is the regexp that finds the correct download link for the sync helper binary -var helperBinaryRegEx = `href="(\/loft-sh\/devspace\/releases\/download\/[^\/]*\/%s)"` - // DevSpaceHelperContainerPath is the path of the devspace helper in the container const DevSpaceHelperContainerPath = "/tmp/devspacehelper" @@ -143,34 +143,15 @@ func installDevSpaceHelperInContainer(client kubectl.Client, pod *v1.Pod, contai // getDownloadURL func devSpaceHelperDownloadURL(version, filename string) (string, error) { - url := "" if version == "latest" { - url = fmt.Sprintf("%s/%s", DevSpaceHelperBaseURL, version) - } else { - url = fmt.Sprintf("%s/tag/%s", DevSpaceHelperBaseURL, version) - } - - // Download html - resp, err := http.Get(url) - if err != nil { - return "", errors.Wrap(err, "get url") - } - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return "", errors.Wrap(err, "read body") - } - - regEx, err := regexp.Compile(fmt.Sprintf(helperBinaryRegEx, filename)) - if err != nil { - return "", err + var err error + version, err = git.GetLatestVersion(DevSpaceHelperRepository) + if err != nil { + return "", errors.Wrap(err, "get latest version") + } } - matches := regEx.FindStringSubmatch(string(body)) - if len(matches) != 2 { - return "", errors.Errorf("couldn't find %s in github release %s at url %s", filename, version, url) - } - return "https://github.com" + matches[1], nil + return fmt.Sprintf("%s/%s/%s", DevSpaceHelperBaseURL, version, filename), nil } func downloadSyncHelper(helperName, syncBinaryFolder, version string, log logpkg.Logger) error { diff --git a/pkg/util/git/helper.go b/pkg/util/git/helper.go index 5d63d1ac48..0299f72a2c 100644 --- a/pkg/util/git/helper.go +++ b/pkg/util/git/helper.go @@ -1,14 +1,19 @@ package git import ( + "fmt" + "net/http" "os" "os/exec" + "regexp" "strings" "github.com/pkg/errors" "gopkg.in/src-d/go-git.v4" ) +var LatestTagRegEx = regexp.MustCompile(`\/tag\/(.*)$`) + // GetBranch retrieves the current HEADs name func GetBranch(localPath string) (string, error) { repo, err := git.PlainOpen(localPath) @@ -77,3 +82,28 @@ func GetRemote(localPath string) (string, error) { return urls[0], nil } + +func GetLatestVersion(repository string) (string, error) { + client := &http.Client{ + CheckRedirect: func(req *http.Request, via []*http.Request) error { + return http.ErrUseLastResponse + }, + } + + resp, err := client.Get(repository + "/releases/latest") + if err != nil { + return "", err + } + + redirect := resp.Header.Get("location") + if redirect == "" { + return "", fmt.Errorf("redirect URL not found") + } + + matches := LatestTagRegEx.FindStringSubmatch(redirect) + if len(matches) != 2 { + return "", errors.Errorf("Couldn't find latest release version") + } + + return matches[1], nil +}