Skip to content

Commit

Permalink
Merge pull request #105 from bgpat/bgpat/lint
Browse files Browse the repository at this point in the history
Fix lint
  • Loading branch information
bgpat authored Feb 22, 2023
2 parents f0231ef + a4b2dd6 commit 9cf00f8
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 77 deletions.
5 changes: 3 additions & 2 deletions travis/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package travis

import (
"errors"
"net/http"
"sync"

Expand Down Expand Up @@ -40,6 +41,6 @@ func (r *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
}

func isNotFound(err error) bool {
errResp, ok := err.(*travis.ErrorResponse)
return ok && errResp.ErrorType == "not_found"
var errResp *travis.ErrorResponse
return errors.As(err, &errResp) && errResp.ErrorType == "not_found"
}
3 changes: 3 additions & 0 deletions travis/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package travis

var IsNotFound = isNotFound
2 changes: 1 addition & 1 deletion travis/data_source_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func dataSourceTravisRead(ctx context.Context, d *schema.ResourceData, m interfa
ctx = tflog.SetField(ctx, "userID", userID)

if waitSync {
_, _, err := client.User.Sync(ctx, uint(userID))
_, _, err := client.User.Sync(ctx, userID)
if err != nil {
return diag.Errorf("failed to sync user %v: %v", userID, err)
}
Expand Down
3 changes: 1 addition & 2 deletions travis/data_source_user_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package travis
package travis_test

import (
_ "embed"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down
11 changes: 7 additions & 4 deletions travis/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package travis
package travis_test

import (
"os"
"regexp"
"testing"

"github.com/bgpat/terraform-provider-travis/travis"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand All @@ -21,23 +22,25 @@ var (
)

func init() {
testAccProvider = Provider()
testAccProvider = travis.Provider()
testAccProviders = map[string]*schema.Provider{
"travis": testAccProvider,
}
}

func TestProvider(t *testing.T) {
if err := Provider().InternalValidate(); err != nil {
if err := travis.Provider().InternalValidate(); err != nil {
t.Fatal(err)
}
}

func TestProvider_impl(t *testing.T) {
var _ *schema.Provider = Provider()
var _ *schema.Provider = travis.Provider()
}

func testAccPreCheck(t *testing.T) {
t.Helper()

if v := os.Getenv("TRAVIS_TOKEN"); v == "" {
t.Fatal("TRAVIS_TOKEN must be set for acceptance tests")
}
Expand Down
69 changes: 47 additions & 22 deletions travis/resource_cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,58 +21,58 @@ func resourceCron() *schema.Resource {
DeleteContext: resourceCronDelete,

Schema: map[string]*schema.Schema{
"repository_id": &schema.Schema{
"repository_id": {
Type: schema.TypeInt,
Optional: true,
Description: "Value uniquely identifying the repository.",
ForceNew: true,
ExactlyOneOf: []string{"repository_slug"},
},
"repository_slug": &schema.Schema{
"repository_slug": {
Type: schema.TypeString,
Optional: true,
Description: "Same as {repository.owner.name}/{repository.name}.",
ForceNew: true,
ExactlyOneOf: []string{"repository_id"},
},
"branch": &schema.Schema{
"branch": {
Type: schema.TypeString,
Required: true,
Description: "The branch to which this cron job belongs.",
ForceNew: true,
},
"interval": &schema.Schema{
"interval": {
Type: schema.TypeString,
Required: true,
Description: "Interval at which this cron runs. Can be daily, weekly, or monthly.",
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"daily", "weekly", "monthly"}, false),
},
"dont_run_if_recent_build_exists": &schema.Schema{
"dont_run_if_recent_build_exists": {
Type: schema.TypeBool,
Optional: true,
Description: "Whether a cron build should run if there has been a build on this branch in the last 24 hours.",
ForceNew: true,
},
"last_run": &schema.Schema{
"last_run": {
Type: schema.TypeString,
Description: "When the cron ran last.",
Computed: true,
ForceNew: true,
},
"next_run": &schema.Schema{
"next_run": {
Type: schema.TypeString,
Description: "When the cron is scheduled to run next.",
Computed: true,
ForceNew: true,
},
"created_at": &schema.Schema{
"created_at": {
Type: schema.TypeString,
Description: "When the cron was created.",
Computed: true,
ForceNew: true,
},
"active": &schema.Schema{
"active": {
Type: schema.TypeBool,
Description: "Whether the cron is active.",
Computed: true,
Expand Down Expand Up @@ -105,7 +105,9 @@ func resourceCronCreate(ctx context.Context, d *schema.ResourceData, m interface
} else {
return diag.Errorf("one of repository_id or repository_slug must be specified")
}
assignCron(cron, d)
if err := assignCron(cron, d); err != nil {
return diag.Errorf("failed to assign cron: %v", err)
}
return nil
}

Expand Down Expand Up @@ -136,7 +138,9 @@ func resourceCronRead(ctx context.Context, d *schema.ResourceData, m interface{}
} else {
return diag.Errorf("one of repository_id or repository_slug must be specified")
}
assignCron(cron, d)
if err := assignCron(cron, d); err != nil {
return diag.Errorf("failed to assign cron: %v", err)
}
return nil
}

Expand All @@ -162,15 +166,30 @@ func generateCronBody(d *schema.ResourceData) *travis.CronBody {
}
}

func assignCron(cron *travis.Cron, d *schema.ResourceData) {
func assignCron(cron *travis.Cron, d *schema.ResourceData) error {
d.SetId(strconv.FormatUint(uint64(*cron.Id), 10))
d.Set("branch", cron.Branch.Name)
d.Set("interval", cron.Interval)
d.Set("dont_run_if_recent_build_exists", cron.DontRunIfRecentBuildExists)
d.Set("last_run", cron.LastRun)
d.Set("next_run", cron.NextRun)
d.Set("created_at", cron.CreatedAt)
d.Set("active", cron.Active)
if err := d.Set("branch", cron.Branch.Name); err != nil {
return err
}
if err := d.Set("interval", cron.Interval); err != nil {
return err
}
if err := d.Set("dont_run_if_recent_build_exists", cron.DontRunIfRecentBuildExists); err != nil {
return err
}
if err := d.Set("last_run", cron.LastRun); err != nil {
return err
}
if err := d.Set("next_run", cron.NextRun); err != nil {
return err
}
if err := d.Set("created_at", cron.CreatedAt); err != nil {
return err
}
if err := d.Set("active", cron.Active); err != nil {
return err
}
return nil
}

func importCron(ctx context.Context, d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
Expand All @@ -189,16 +208,22 @@ func importCron(ctx context.Context, d *schema.ResourceData, m interface{}) ([]*
if err != nil {
return nil, fmt.Errorf("error getting cron in branch (%q) of repo id (%d): %w", branch, repoID, err)
}
d.Set("repository_id", repoID)
if err := d.Set("repository_id", repoID); err != nil {
return nil, err
}
} else {
cron, _, err = client.Crons.FindByRepoSlug(ctx, repo, branch, nil)
if err != nil {
return nil, fmt.Errorf("error getting cron in branch (%q) of repo slug (%q): %w", branch, repo, err)
}
d.Set("repository_slug", repo)
if err := d.Set("repository_slug", repo); err != nil {
return nil, err
}
}

assignCron(cron, d)
if err := assignCron(cron, d); err != nil {
return nil, err
}

return []*schema.ResourceData{d}, nil
}
21 changes: 12 additions & 9 deletions travis/resource_cron_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package travis
package travis_test

import (
"context"
Expand All @@ -9,6 +9,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/shuheiktgw/go-travis"

tptravis "github.com/bgpat/terraform-provider-travis/travis"
)

func TestAccResourceCron_basic(t *testing.T) {
Expand All @@ -22,7 +24,7 @@ func TestAccResourceCron_basic(t *testing.T) {
{
Config: testAccCronResource(testBranch, "daily", false),
Check: resource.ComposeTestCheckFunc(
testAccCheckCronResourceExists("travis_cron.foo", &cron),
testAccCheckCronResourceExists(&cron),
resource.TestCheckResourceAttr("travis_cron.foo", "repository_slug", testRepoSlug),
resource.TestCheckResourceAttr("travis_cron.foo", "branch", testBranch),
resource.TestCheckResourceAttr("travis_cron.foo", "interval", "daily"),
Expand All @@ -32,7 +34,7 @@ func TestAccResourceCron_basic(t *testing.T) {
{
Config: testAccCronResource(testBranch, "weekly", false),
Check: resource.ComposeTestCheckFunc(
testAccCheckCronResourceExists("travis_cron.foo", &cron),
testAccCheckCronResourceExists(&cron),
resource.TestCheckResourceAttr("travis_cron.foo", "repository_slug", testRepoSlug),
resource.TestCheckResourceAttr("travis_cron.foo", "branch", testBranch),
resource.TestCheckResourceAttr("travis_cron.foo", "interval", "weekly"),
Expand All @@ -42,7 +44,7 @@ func TestAccResourceCron_basic(t *testing.T) {
{
Config: testAccCronResource(testBranch, "monthly", false),
Check: resource.ComposeTestCheckFunc(
testAccCheckCronResourceExists("travis_cron.foo", &cron),
testAccCheckCronResourceExists(&cron),
resource.TestCheckResourceAttr("travis_cron.foo", "repository_slug", testRepoSlug),
resource.TestCheckResourceAttr("travis_cron.foo", "branch", testBranch),
resource.TestCheckResourceAttr("travis_cron.foo", "interval", "monthly"),
Expand All @@ -52,7 +54,7 @@ func TestAccResourceCron_basic(t *testing.T) {
{
Config: testAccCronResource(testBranch, "daily", true),
Check: resource.ComposeTestCheckFunc(
testAccCheckCronResourceExists("travis_cron.foo", &cron),
testAccCheckCronResourceExists(&cron),
resource.TestCheckResourceAttr("travis_cron.foo", "repository_slug", testRepoSlug),
resource.TestCheckResourceAttr("travis_cron.foo", "branch", testBranch),
resource.TestCheckResourceAttr("travis_cron.foo", "interval", "daily"),
Expand All @@ -64,7 +66,7 @@ func TestAccResourceCron_basic(t *testing.T) {
}

func testAccCheckCronResourceDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*Client)
client := testAccProvider.Meta().(*tptravis.Client)
for _, rs := range s.RootModule().Resources {
if rs.Type != "travis_cron" {
continue
Expand All @@ -77,15 +79,16 @@ func testAccCheckCronResourceDestroy(s *terraform.State) error {
if err == nil && cron != nil {
return fmt.Errorf("cron %v still exists", rs.Primary.ID)
}
if err != nil && !isNotFound(err) {
if err != nil && !tptravis.IsNotFound(err) {
return err
}
return nil
}
return nil
}

func testAccCheckCronResourceExists(resourceName string, cron *travis.Cron) resource.TestCheckFunc {
func testAccCheckCronResourceExists(cron *travis.Cron) resource.TestCheckFunc {
resourceName := "travis_cron.foo"
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
Expand All @@ -94,7 +97,7 @@ func testAccCheckCronResourceExists(resourceName string, cron *travis.Cron) reso
if rs.Primary.ID == "" {
return fmt.Errorf("cron ID is not set")
}
client := testAccProvider.Meta().(*Client)
client := testAccProvider.Meta().(*tptravis.Client)
id, err := strconv.ParseUint(rs.Primary.ID, 10, 64)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 9cf00f8

Please sign in to comment.