Skip to content

Commit

Permalink
introduce getTagObject function
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Hoß committed Aug 10, 2022
1 parent 3d32d54 commit 6628551
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 38 deletions.
11 changes: 7 additions & 4 deletions internal/provider/data_source_git_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package provider

import (
"context"
"github.com/go-git/go-git/v5/plumbing"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
Expand Down Expand Up @@ -111,20 +110,24 @@ func (r *dataSourceGitTag) Read(ctx context.Context, req tfsdk.ReadDataSourceReq
return
}

tagObject, err := getTagObject(ctx, repository, tagReference.Hash(), &resp.Diagnostics)
if err != nil {
return
}

var state dataSourceGitTagSchema
state.Directory = inputs.Directory
state.Id = inputs.Name
state.Name = inputs.Name
state.SHA1 = types.String{Value: tagReference.Hash().String()}
tag, err := repository.TagObject(tagReference.Hash())
if err == plumbing.ErrObjectNotFound {
if tagObject == nil {
state.Annotated = types.Bool{Value: false}
state.Lightweight = types.Bool{Value: true}
state.Message = types.String{Null: true}
} else {
state.Annotated = types.Bool{Value: true}
state.Lightweight = types.Bool{Value: false}
state.Message = types.String{Value: tag.Message}
state.Message = types.String{Value: tagObject.Message}
}

diags = resp.State.Set(ctx, &state)
Expand Down
51 changes: 24 additions & 27 deletions internal/provider/data_source_git_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,36 +146,33 @@ func (r *dataSourceGitTags) Read(ctx context.Context, req tfsdk.ReadDataSourceRe

allTags := make(map[string]attr.Value)
if err := tags.ForEach(func(ref *plumbing.Reference) error {
_, err := repository.TagObject(ref.Hash())

switch err {
case nil:
if inputs.Annotated.Value {
allTags[ref.Name().Short()] = types.Object{
AttrTypes: tagType,
Attrs: map[string]attr.Value{
"annotated": types.Bool{Value: true},
"lightweight": types.Bool{Value: false},
"sha1": types.String{Value: ref.Hash().String()},
},
}
tagObject, err := getTagObject(ctx, repository, ref.Hash(), &resp.Diagnostics)
if err != nil {
return err
}

if inputs.Annotated.Value && tagObject != nil {
allTags[ref.Name().Short()] = types.Object{
AttrTypes: tagType,
Attrs: map[string]attr.Value{
"annotated": types.Bool{Value: true},
"lightweight": types.Bool{Value: false},
"sha1": types.String{Value: ref.Hash().String()},
},
}
return nil
case plumbing.ErrObjectNotFound:
if inputs.Lightweight.Value {
allTags[ref.Name().Short()] = types.Object{
AttrTypes: tagType,
Attrs: map[string]attr.Value{
"annotated": types.Bool{Value: false},
"lightweight": types.Bool{Value: true},
"sha1": types.String{Value: ref.Hash().String()},
},
}
}
if inputs.Lightweight.Value && tagObject == nil {
allTags[ref.Name().Short()] = types.Object{
AttrTypes: tagType,
Attrs: map[string]attr.Value{
"annotated": types.Bool{Value: false},
"lightweight": types.Bool{Value: true},
"sha1": types.String{Value: ref.Hash().String()},
},
}
return nil
default:
return err
}

return nil
}); err != nil {
resp.Diagnostics.AddError(
"Error reading tags",
Expand Down
22 changes: 22 additions & 0 deletions internal/provider/git_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"context"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
Expand Down Expand Up @@ -50,3 +51,24 @@ func getTagReference(ctx context.Context, repository *git.Repository, tagName st
})
return tag
}

func getTagObject(ctx context.Context, repository *git.Repository, hash plumbing.Hash, diag *diag.Diagnostics) (*object.Tag, error) {
tag, err := repository.TagObject(hash)
if err == plumbing.ErrObjectNotFound {
tflog.Trace(ctx, "lightweight tag", map[string]interface{}{
"hash": hash.String(),
})
return nil, nil
} else if err == nil {
tflog.Trace(ctx, "annotated tag", map[string]interface{}{
"hash": hash.String(),
})
return tag, nil
} else {
diag.AddError(
"Cannot read tag",
"Could not read tag at hash ["+hash.String()+"] because of: "+err.Error(),
)
return nil, err
}
}
21 changes: 14 additions & 7 deletions internal/provider/resource_git_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package provider
import (
"context"
"fmt"
"github.com/go-git/go-git/v5/plumbing"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
Expand Down Expand Up @@ -177,16 +176,20 @@ func (r *resourceGitTag) Read(ctx context.Context, req tfsdk.ReadResourceRequest
return
}

tagObject, err := getTagObject(ctx, repository, tagReference.Hash(), &resp.Diagnostics)
if err != nil {
return
}

var newState resourceGitTagSchema
newState.Directory = state.Directory
newState.Id = state.Name
newState.Name = state.Name
newState.SHA1 = types.String{Value: tagReference.Hash().String()}
tag, err := repository.TagObject(tagReference.Hash())
if err == plumbing.ErrObjectNotFound {
if tagObject == nil {
newState.Message = types.String{Null: true}
} else {
newState.Message = types.String{Value: strings.TrimSpace(tag.Message)}
newState.Message = types.String{Value: strings.TrimSpace(tagObject.Message)}
}

diags = resp.State.Set(ctx, &newState)
Expand Down Expand Up @@ -250,16 +253,20 @@ func (r *resourceGitTag) ImportState(ctx context.Context, req tfsdk.ImportResour
return
}

tagObject, err := getTagObject(ctx, repository, tagReference.Hash(), &resp.Diagnostics)
if err != nil {
return
}

var state resourceGitTagSchema
state.Directory = types.String{Value: directory}
state.Id = types.String{Value: tagName}
state.Name = types.String{Value: tagName}
state.SHA1 = types.String{Value: tagReference.Hash().String()}
tag, err := repository.TagObject(tagReference.Hash())
if err == plumbing.ErrObjectNotFound {
if tagObject == nil {
state.Message = types.String{Null: true}
} else {
state.Message = types.String{Value: strings.TrimSpace(tag.Message)}
state.Message = types.String{Value: strings.TrimSpace(tagObject.Message)}
}

diags := resp.State.Set(ctx, &state)
Expand Down

0 comments on commit 6628551

Please sign in to comment.