refactor code. inject GITHUB_TOKEN for version_ensure_test

This commit is contained in:
zjhe 2022-09-21 22:32:02 +08:00
Родитель cffa1cf1f3
Коммит bea8a24b85
2 изменённых файлов: 66 добавлений и 33 удалений

Просмотреть файл

@ -26,6 +26,51 @@ func main() {
func prepare() {
clean()
latest, err := latestTag()
prepareTerraformAzurermProviderCode(latest, err)
injectProviderCode()
goModEnsure()
}
func goModEnsure() {
if err := exec.Command("go", "mod", "tidy").Run(); err != nil {
panic(err.Error())
}
if err := exec.Command("go", "mod", "vendor").Run(); err != nil {
panic(err.Error())
}
}
func prepareTerraformAzurermProviderCode(latest string, err error) {
link := fmt.Sprintf("https://github.com/hashicorp/terraform-provider-azurerm/archive/refs/tags/%s.zip", latest)
fmt.Printf("Getting %s\n", link)
_, err = getter.Get(context.TODO(), "./", link)
if err != nil {
panic(err.Error())
}
err = os.Rename(fmt.Sprintf("terraform-provider-azurerm-%s", strings.TrimLeft(latest, "v")), "terraform-provider-azurerm")
if err != nil {
panic(err.Error())
}
}
func latestTag() (string, error) {
c := gitClient()
tags, _, err := c.Repositories.ListTags(context.TODO(), "hashicorp", "terraform-provider-azurerm", &github.ListOptions{
Page: 0,
PerPage: 10,
})
if err != nil {
panic(err.Error())
}
if len(tags) == 0 {
panic("no terraform-azurerm-provider tags found")
}
latest := tags[0].GetName()
return latest, err
}
func gitClient() *github.Client {
var client *github.Client
token := os.Getenv("TOKEN")
if token != "" {
@ -37,36 +82,7 @@ func prepare() {
} else {
client = github.NewClient(nil)
}
tags, _, err := client.Repositories.ListTags(context.TODO(), "hashicorp", "terraform-provider-azurerm", &github.ListOptions{
Page: 0,
PerPage: 10,
})
if err != nil {
panic(err.Error())
}
if len(tags) == 0 {
panic("no terraform-azurerm-provider tags found")
}
latest := tags[0].GetName()
link := fmt.Sprintf("https://github.com/hashicorp/terraform-provider-azurerm/archive/refs/tags/%s.zip", latest)
fmt.Printf("Getting %s\n", link)
_, err = getter.Get(context.TODO(), "./", link)
if err != nil {
panic(err.Error())
}
err = os.Rename(fmt.Sprintf("terraform-provider-azurerm-%s", strings.TrimLeft(latest, "v")), "terraform-provider-azurerm")
if err != nil {
panic(err.Error())
}
//os.RemoveAll("./terraform-provider-azurerm/.git")
injectProviderCode()
if err := exec.Command("go", "mod", "tidy").Run(); err != nil {
panic(err.Error())
}
if err := exec.Command("go", "mod", "vendor").Run(); err != nil {
panic(err.Error())
}
return client
}
func injectProviderCode() {

Просмотреть файл

@ -3,18 +3,20 @@ package version_test
import (
"context"
"fmt"
"github.com/terraform-linters/tflint-ruleset-azurerm-ext/project"
"golang.org/x/mod/semver"
"os"
"testing"
"github.com/google/go-github/v47/github"
"github.com/stretchr/testify/assert"
"github.com/terraform-linters/tflint-ruleset-azurerm-ext/project"
"golang.org/x/mod/semver"
"golang.org/x/oauth2"
)
func Test_EnsureVersionHasBeenBumpedUp(t *testing.T) {
currentVersion := fmt.Sprintf("v%s", project.Version)
assert.True(t, semver.IsValid(currentVersion))
client := github.NewClient(nil)
client := gitClient()
tags, _, err := client.Repositories.ListTags(context.TODO(), "Azure", "tflint-ruleset-azurerm-ext", &github.ListOptions{
Page: 0,
PerPage: 10,
@ -28,3 +30,18 @@ func Test_EnsureVersionHasBeenBumpedUp(t *testing.T) {
}
}
}
func gitClient() *github.Client {
var client *github.Client
token := os.Getenv("TOKEN")
if token != "" {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(context.TODO(), ts)
client = github.NewClient(tc)
} else {
client = github.NewClient(nil)
}
return client
}