|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + "github.com/xanzy/go-gitlab" |
| 10 | +) |
| 11 | + |
| 12 | +var _ = registerDataSource("gitlab_branch", func() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Description: "Provide details about a gitlab project branch", |
| 15 | + |
| 16 | + ReadContext: dataSourceGitlabBranchRead, |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "name": { |
| 19 | + Description: "The name of the branch.", |
| 20 | + Type: schema.TypeString, |
| 21 | + Required: true, |
| 22 | + }, |
| 23 | + "project": { |
| 24 | + Description: "The full path or id of the project.", |
| 25 | + Type: schema.TypeString, |
| 26 | + Required: true, |
| 27 | + }, |
| 28 | + "web_url": { |
| 29 | + Description: "The url of the created branch (https.)", |
| 30 | + Type: schema.TypeString, |
| 31 | + Computed: true, |
| 32 | + }, |
| 33 | + "default": { |
| 34 | + Description: "Bool, true if branch is the default branch for the project.", |
| 35 | + Type: schema.TypeBool, |
| 36 | + Computed: true, |
| 37 | + }, |
| 38 | + "can_push": { |
| 39 | + Description: "Bool, true if you can push to the branch.", |
| 40 | + Type: schema.TypeBool, |
| 41 | + Computed: true, |
| 42 | + }, |
| 43 | + "protected": { |
| 44 | + Description: "Bool, true if branch has branch protection.", |
| 45 | + Type: schema.TypeBool, |
| 46 | + Computed: true, |
| 47 | + }, |
| 48 | + "merged": { |
| 49 | + Description: "Bool, true if the branch has been merged into it's parent.", |
| 50 | + Type: schema.TypeBool, |
| 51 | + Computed: true, |
| 52 | + }, |
| 53 | + "developer_can_merge": { |
| 54 | + Description: "Bool, true if developer level access allows to merge branch.", |
| 55 | + Type: schema.TypeBool, |
| 56 | + Computed: true, |
| 57 | + }, |
| 58 | + "developer_can_push": { |
| 59 | + Description: "Bool, true if developer level access allows git push.", |
| 60 | + Type: schema.TypeBool, |
| 61 | + Computed: true, |
| 62 | + }, |
| 63 | + "commit": { |
| 64 | + Description: "The commit associated with the branch ref.", |
| 65 | + Type: schema.TypeSet, |
| 66 | + Computed: true, |
| 67 | + Set: schema.HashResource(commitSchema), |
| 68 | + Elem: commitSchema, |
| 69 | + }, |
| 70 | + }, |
| 71 | + } |
| 72 | +}) |
| 73 | + |
| 74 | +func dataSourceGitlabBranchRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 75 | + client := meta.(*gitlab.Client) |
| 76 | + name := d.Get("name").(string) |
| 77 | + project := d.Get("project").(string) |
| 78 | + log.Printf("[DEBUG] read gitlab branch %s", name) |
| 79 | + branch, resp, err := client.Branches.GetBranch(project, name, gitlab.WithContext(ctx)) |
| 80 | + if err != nil { |
| 81 | + log.Printf("[DEBUG] failed to read gitlab branch %s response %v", name, resp) |
| 82 | + return diag.FromErr(err) |
| 83 | + } |
| 84 | + |
| 85 | + d.SetId(buildTwoPartID(&project, &name)) |
| 86 | + d.Set("name", branch.Name) |
| 87 | + d.Set("project", project) |
| 88 | + d.Set("web_url", branch.WebURL) |
| 89 | + d.Set("default", branch.Default) |
| 90 | + d.Set("can_push", branch.CanPush) |
| 91 | + d.Set("protected", branch.Protected) |
| 92 | + d.Set("merged", branch.Merged) |
| 93 | + d.Set("developer_can_merge", branch.DevelopersCanMerge) |
| 94 | + d.Set("developer_can_push", branch.DevelopersCanPush) |
| 95 | + if err := d.Set("commit", flattenCommit(branch.Commit)); err != nil { |
| 96 | + return diag.FromErr(err) |
| 97 | + } |
| 98 | + return nil |
| 99 | +} |
0 commit comments