|
1 | 1 | package gitea
|
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "code.gitea.io/sdk/gitea" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 9 | +) |
| 10 | + |
| 11 | +func dataSourceGiteaRepo() *schema.Resource { |
| 12 | + return &schema.Resource{ |
| 13 | + Read: dataSourceGiteaRepoRead, |
| 14 | + |
| 15 | + Schema: map[string]*schema.Schema{ |
| 16 | + "username": { |
| 17 | + Type: schema.TypeString, |
| 18 | + Required: true, |
| 19 | + ForceNew: true, |
| 20 | + }, |
| 21 | + "name": { |
| 22 | + Type: schema.TypeString, |
| 23 | + Required: true, |
| 24 | + ForceNew: true, |
| 25 | + }, |
| 26 | + "full_name": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Computed: true, |
| 29 | + }, |
| 30 | + "description": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Computed: true, |
| 33 | + }, |
| 34 | + "private": { |
| 35 | + Type: schema.TypeBool, |
| 36 | + Computed: true, |
| 37 | + }, |
| 38 | + "fork": { |
| 39 | + Type: schema.TypeBool, |
| 40 | + Computed: true, |
| 41 | + }, |
| 42 | + "mirror": { |
| 43 | + Type: schema.TypeBool, |
| 44 | + Computed: true, |
| 45 | + }, |
| 46 | + "size": { |
| 47 | + Type: schema.TypeInt, |
| 48 | + Computed: true, |
| 49 | + }, |
| 50 | + "html_url": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Computed: true, |
| 53 | + }, |
| 54 | + "ssh_url": { |
| 55 | + Type: schema.TypeString, |
| 56 | + Computed: true, |
| 57 | + }, |
| 58 | + "clone_url": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Computed: true, |
| 61 | + }, |
| 62 | + "website": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Computed: true, |
| 65 | + }, |
| 66 | + "stars": { |
| 67 | + Type: schema.TypeInt, |
| 68 | + Computed: true, |
| 69 | + }, |
| 70 | + "forks": { |
| 71 | + Type: schema.TypeInt, |
| 72 | + Computed: true, |
| 73 | + }, |
| 74 | + "watchers": { |
| 75 | + Type: schema.TypeInt, |
| 76 | + Computed: true, |
| 77 | + }, |
| 78 | + "open_issue_count": { |
| 79 | + Type: schema.TypeInt, |
| 80 | + Computed: true, |
| 81 | + }, |
| 82 | + "default_branch": { |
| 83 | + Type: schema.TypeString, |
| 84 | + Computed: true, |
| 85 | + }, |
| 86 | + "created": { |
| 87 | + Type: schema.TypeString, |
| 88 | + Computed: true, |
| 89 | + }, |
| 90 | + "updated": { |
| 91 | + Type: schema.TypeString, |
| 92 | + Computed: true, |
| 93 | + }, |
| 94 | + "permission_admin": { |
| 95 | + Type: schema.TypeBool, |
| 96 | + Computed: true, |
| 97 | + }, |
| 98 | + "permission_push": { |
| 99 | + Type: schema.TypeBool, |
| 100 | + Computed: true, |
| 101 | + }, |
| 102 | + "permission_pull": { |
| 103 | + Type: schema.TypeBool, |
| 104 | + Computed: true, |
| 105 | + }, |
| 106 | + }, |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func dataSourceGiteaRepoRead(d *schema.ResourceData, meta interface{}) error { |
| 111 | + client := meta.(*gitea.Client) |
| 112 | + |
| 113 | + usernameData, usernameOk := d.GetOk("username") |
| 114 | + if !usernameOk { |
| 115 | + return fmt.Errorf("name of repo owner must be passed") |
| 116 | + } |
| 117 | + username := strings.ToLower(usernameData.(string)) |
| 118 | + |
| 119 | + nameData, nameOk := d.GetOk("username") |
| 120 | + if !nameOk { |
| 121 | + return fmt.Errorf("name of repo must be passed") |
| 122 | + } |
| 123 | + name := strings.ToLower(nameData.(string)) |
| 124 | + |
| 125 | + repo, _, err := client.GetRepo(username, name) |
| 126 | + if err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + |
| 130 | + d.SetId(fmt.Sprintf("%d", repo.ID)) |
| 131 | + d.Set("name", repo.Name) |
| 132 | + d.Set("description", repo.Description) |
| 133 | + d.Set("full_name", repo.FullName) |
| 134 | + d.Set("description", repo.Description) |
| 135 | + d.Set("private", repo.Private) |
| 136 | + d.Set("fork", repo.Fork) |
| 137 | + d.Set("mirror", repo.Mirror) |
| 138 | + d.Set("size", repo.Size) |
| 139 | + d.Set("html_url", repo.HTMLURL) |
| 140 | + d.Set("ssh_url", repo.SSHURL) |
| 141 | + d.Set("clone_url", repo.CloneURL) |
| 142 | + d.Set("website", repo.Website) |
| 143 | + d.Set("stars", repo.Stars) |
| 144 | + d.Set("forks", repo.Forks) |
| 145 | + d.Set("watchers", repo.Watchers) |
| 146 | + d.Set("open_issue_count", repo.OpenIssues) |
| 147 | + d.Set("default_branch", repo.DefaultBranch) |
| 148 | + d.Set("created", repo.Created) |
| 149 | + d.Set("updated", repo.Updated) |
| 150 | + d.Set("permission_admin", repo.Permissions.Admin) |
| 151 | + d.Set("permission_push", repo.Permissions.Push) |
| 152 | + d.Set("permission_pull", repo.Permissions.Pull) |
| 153 | + return nil |
| 154 | +} |
0 commit comments