@@ -780,51 +780,223 @@ func (r *repositoryResource) Create(ctx context.Context, req resource.CreateRequ
780
780
func (r * repositoryResource ) Read (ctx context.Context , req resource.ReadRequest , resp * resource.ReadResponse ) {
781
781
defer un (trace (ctx , "Read repository resource" ))
782
782
783
- // var data repositoryResourceModel
783
+ var (
784
+ data repositoryResourceModel
785
+ owner repositoryResourceUser
786
+ )
784
787
785
- // // Read Terraform configuration data into model
786
- // diags := req.State.Get(ctx, &data)
787
- // resp.Diagnostics.Append(diags...)
788
- // if resp.Diagnostics.HasError() {
789
- // return
790
- // }
788
+ // Read Terraform prior state data into the model
789
+ diags := req .State .Get (ctx , & data )
790
+ resp .Diagnostics .Append (diags ... )
791
+ if resp .Diagnostics .HasError () {
792
+ return
793
+ }
791
794
792
- // tflog.Info(ctx, "Get repository by name", map[string]any{
793
- // "name": data.Name.ValueString(),
794
- // })
795
+ // Read repository owner into model
796
+ diags = data .Owner .As (ctx , & owner , basetypes.ObjectAsOptions {})
797
+ resp .Diagnostics .Append (diags ... )
798
+ if resp .Diagnostics .HasError () {
799
+ return
800
+ }
795
801
796
- // // Use Forgejo client to get repository by name
797
- // o, re, err := r.client.GetOrg(data.Name.ValueString())
798
- // if err != nil {
799
- // tflog.Error(ctx, "Error", map[string]any{
800
- // "status": re.Status,
801
- // })
802
+ tflog .Info (ctx , "Get repository by name" , map [string ]any {
803
+ "owner" : owner .UserName .ValueString (),
804
+ "name" : data .Name .ValueString (),
805
+ })
802
806
803
- // var msg string
804
- // switch re.StatusCode {
805
- // case 404:
806
- // msg = fmt.Sprintf("Repository with name %s not found: %s", data.Name.String(), err)
807
- // default:
808
- // msg = fmt.Sprintf("Unknown error: %s", err)
809
- // }
810
- // resp.Diagnostics.AddError("Unable to get repository by name", msg)
807
+ // Use Forgejo client to get repository by owner and name
808
+ rep , res , err := r .client .GetRepo (
809
+ owner .UserName .ValueString (),
810
+ data .Name .ValueString (),
811
+ )
812
+ if err != nil {
813
+ tflog .Error (ctx , "Error" , map [string ]any {
814
+ "status" : res .Status ,
815
+ })
811
816
812
- // return
813
- // }
817
+ var msg string
818
+ switch res .StatusCode {
819
+ case 404 :
820
+ msg = fmt .Sprintf (
821
+ "Repository with owner %s and name %s not found: %s" ,
822
+ owner .UserName .String (),
823
+ data .Name .String (),
824
+ err ,
825
+ )
826
+ default :
827
+ msg = fmt .Sprintf ("Unknown error: %s" , err )
828
+ }
829
+ resp .Diagnostics .AddError ("Unable to get repository by name" , msg )
814
830
815
- // // Map response body to model
816
- // data.ID = types.Int64Value(o.ID)
817
- // data.Name = types.StringValue(o.UserName)
818
- // data.FullName = types.StringValue(o.FullName)
819
- // data.AvatarURL = types.StringValue(o.AvatarURL)
820
- // data.Description = types.StringValue(o.Description)
821
- // data.Website = types.StringValue(o.Website)
822
- // data.Location = types.StringValue(o.Location)
823
- // data.Visibility = types.StringValue(o.Visibility)
831
+ return
832
+ }
824
833
825
- // // Save data into Terraform state
826
- // diags = resp.State.Set(ctx, &data)
827
- // resp.Diagnostics.Append(diags...)
834
+ // Map response body to model
835
+ data .ID = types .Int64Value (rep .ID )
836
+ data .FullName = types .StringValue (rep .FullName )
837
+ data .Description = types .StringValue (rep .Description )
838
+ data .Empty = types .BoolValue (rep .Empty )
839
+ data .Private = types .BoolValue (rep .Private )
840
+ data .Fork = types .BoolValue (rep .Fork )
841
+ data .Template = types .BoolValue (rep .Template )
842
+ if rep .Parent != nil {
843
+ data .ParentID = types .Int64Value (rep .Parent .ID )
844
+ } else {
845
+ data .ParentID = types .Int64Null ()
846
+ }
847
+ data .Mirror = types .BoolValue (rep .Mirror )
848
+ data .Size = types .Int64Value (int64 (rep .Size ))
849
+ data .HTMLURL = types .StringValue (rep .HTMLURL )
850
+ data .SSHURL = types .StringValue (rep .SSHURL )
851
+ data .CloneURL = types .StringValue (rep .CloneURL )
852
+ data .OriginalURL = types .StringValue (rep .OriginalURL )
853
+ data .Website = types .StringValue (rep .Website )
854
+ data .Stars = types .Int64Value (int64 (rep .Stars ))
855
+ data .Forks = types .Int64Value (int64 (rep .Forks ))
856
+ data .Watchers = types .Int64Value (int64 (rep .Watchers ))
857
+ data .OpenIssues = types .Int64Value (int64 (rep .OpenIssues ))
858
+ data .OpenPulls = types .Int64Value (int64 (rep .OpenPulls ))
859
+ data .Releases = types .Int64Value (int64 (rep .Releases ))
860
+ data .DefaultBranch = types .StringValue (rep .DefaultBranch )
861
+ data .Archived = types .BoolValue (rep .Archived )
862
+ data .Created = types .StringValue (rep .Created .String ())
863
+ data .Updated = types .StringValue (rep .Updated .String ())
864
+ data .HasIssues = types .BoolValue (rep .HasIssues )
865
+ data .HasWiki = types .BoolValue (rep .HasWiki )
866
+ data .HasPullRequests = types .BoolValue (rep .HasPullRequests )
867
+ data .HasProjects = types .BoolValue (rep .HasProjects )
868
+ data .HasReleases = types .BoolValue (rep .HasReleases )
869
+ data .HasPackages = types .BoolValue (rep .HasPackages )
870
+ data .HasActions = types .BoolValue (rep .HasActions )
871
+ data .IgnoreWhitespaceConflicts = types .BoolValue (rep .IgnoreWhitespaceConflicts )
872
+ data .AllowMerge = types .BoolValue (rep .AllowMerge )
873
+ data .AllowRebase = types .BoolValue (rep .AllowRebase )
874
+ data .AllowRebaseMerge = types .BoolValue (rep .AllowRebaseMerge )
875
+ data .AllowSquash = types .BoolValue (rep .AllowSquash )
876
+ data .AvatarURL = types .StringValue (rep .AvatarURL )
877
+ data .Internal = types .BoolValue (rep .Internal )
878
+ data .MirrorInterval = types .StringValue (rep .MirrorInterval )
879
+ data .MirrorUpdated = types .StringValue (rep .MirrorUpdated .String ())
880
+ data .DefaultMergeStyle = types .StringValue (string (rep .DefaultMergeStyle ))
881
+
882
+ // Repository owner
883
+ if rep .Owner != nil {
884
+ ownerElement := repositoryDataSourceUser {
885
+ ID : types .Int64Value (rep .Owner .ID ),
886
+ UserName : types .StringValue (rep .Owner .UserName ),
887
+ LoginName : types .StringValue (rep .Owner .LoginName ),
888
+ FullName : types .StringValue (rep .Owner .FullName ),
889
+ Email : types .StringValue (rep .Owner .Email ),
890
+ }
891
+ ownerValue , diags := types .ObjectValueFrom (
892
+ ctx ,
893
+ ownerElement .AttributeTypes (),
894
+ ownerElement ,
895
+ )
896
+ resp .Diagnostics .Append (diags ... )
897
+ if resp .Diagnostics .HasError () {
898
+ return
899
+ }
900
+ data .Owner = ownerValue
901
+ } else {
902
+ data .Owner = types .ObjectNull (
903
+ repositoryResourceUser {}.AttributeTypes (),
904
+ )
905
+ }
906
+
907
+ // Repository permissions
908
+ if rep .Permissions != nil {
909
+ perms := repositoryDataSourcePermissions {
910
+ Admin : types .BoolValue (rep .Permissions .Admin ),
911
+ Push : types .BoolValue (rep .Permissions .Push ),
912
+ Pull : types .BoolValue (rep .Permissions .Pull ),
913
+ }
914
+ permsValue , diags := types .ObjectValueFrom (
915
+ ctx ,
916
+ perms .AttributeTypes (),
917
+ perms ,
918
+ )
919
+ resp .Diagnostics .Append (diags ... )
920
+ if resp .Diagnostics .HasError () {
921
+ return
922
+ }
923
+ data .Permissions = permsValue
924
+ } else {
925
+ data .Permissions = types .ObjectNull (
926
+ repositoryResourcePermissions {}.AttributeTypes (),
927
+ )
928
+ }
929
+
930
+ // Internal issue tracker
931
+ if rep .InternalTracker != nil {
932
+ intTracker := repositoryDataSourceInternalTracker {
933
+ EnableTimeTracker : types .BoolValue (rep .InternalTracker .EnableTimeTracker ),
934
+ AllowOnlyContributorsToTrackTime : types .BoolValue (rep .InternalTracker .AllowOnlyContributorsToTrackTime ),
935
+ EnableIssueDependencies : types .BoolValue (rep .InternalTracker .EnableIssueDependencies ),
936
+ }
937
+ intTrackerValue , diags := types .ObjectValueFrom (
938
+ ctx ,
939
+ intTracker .AttributeTypes (),
940
+ intTracker ,
941
+ )
942
+ resp .Diagnostics .Append (diags ... )
943
+ if resp .Diagnostics .HasError () {
944
+ return
945
+ }
946
+ data .InternalTracker = intTrackerValue
947
+ } else {
948
+ data .InternalTracker = types .ObjectNull (
949
+ repositoryResourceInternalTracker {}.AttributeTypes (),
950
+ )
951
+ }
952
+
953
+ // External issue tracker
954
+ if rep .ExternalTracker != nil {
955
+ extTracker := repositoryDataSourceExternalTracker {
956
+ ExternalTrackerURL : types .StringValue (rep .ExternalTracker .ExternalTrackerURL ),
957
+ ExternalTrackerFormat : types .StringValue (rep .ExternalTracker .ExternalTrackerFormat ),
958
+ ExternalTrackerStyle : types .StringValue (rep .ExternalTracker .ExternalTrackerStyle ),
959
+ }
960
+ extTrackerValue , diags := types .ObjectValueFrom (
961
+ ctx ,
962
+ extTracker .AttributeTypes (),
963
+ extTracker ,
964
+ )
965
+ resp .Diagnostics .Append (diags ... )
966
+ if resp .Diagnostics .HasError () {
967
+ return
968
+ }
969
+ data .ExternalTracker = extTrackerValue
970
+ } else {
971
+ data .ExternalTracker = types .ObjectNull (
972
+ repositoryResourceExternalTracker {}.AttributeTypes (),
973
+ )
974
+ }
975
+
976
+ // External wiki
977
+ if rep .ExternalWiki != nil {
978
+ wiki := repositoryDataSourceExternalWiki {
979
+ ExternalWikiURL : types .StringValue (rep .ExternalWiki .ExternalWikiURL ),
980
+ }
981
+ wikiValue , diags := types .ObjectValueFrom (
982
+ ctx ,
983
+ wiki .AttributeTypes (),
984
+ wiki ,
985
+ )
986
+ resp .Diagnostics .Append (diags ... )
987
+ if resp .Diagnostics .HasError () {
988
+ return
989
+ }
990
+ data .ExternalWiki = wikiValue
991
+ } else {
992
+ data .ExternalWiki = types .ObjectNull (
993
+ repositoryResourceExternalWiki {}.AttributeTypes (),
994
+ )
995
+ }
996
+
997
+ // Save data into Terraform state
998
+ diags = resp .State .Set (ctx , & data )
999
+ resp .Diagnostics .Append (diags ... )
828
1000
}
829
1001
830
1002
// Update updates the resource and sets the updated Terraform state on success.
0 commit comments