github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/github/util_permissions.go (about)

     1  package github
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/google/go-github/github"
     8  )
     9  
    10  const (
    11  	pullPermission  string = "pull"
    12  	pushPermission  string = "push"
    13  	adminPermission string = "admin"
    14  
    15  	writePermission string = "write"
    16  	readPermission  string = "read"
    17  )
    18  
    19  func getRepoPermission(p *map[string]bool) (string, error) {
    20  
    21  	// Permissions are returned in this map format such that if you have a certain level
    22  	// of permission, all levels below are also true. For example, if a team has push
    23  	// permission, the map will be: {"pull": true, "push": true, "admin": false}
    24  	if (*p)[adminPermission] {
    25  		return adminPermission, nil
    26  	} else if (*p)[pushPermission] {
    27  		return pushPermission, nil
    28  	} else {
    29  		if (*p)[pullPermission] {
    30  			return pullPermission, nil
    31  		}
    32  		return "", errors.New("At least one permission expected from permissions map.")
    33  	}
    34  }
    35  
    36  func getInvitationPermission(i *github.RepositoryInvitation) (string, error) {
    37  	// Permissions for some GitHub API routes are expressed as "read",
    38  	// "write", and "admin"; in other places, they are expressed as "pull",
    39  	// "push", and "admin".
    40  	if *i.Permissions == readPermission {
    41  		return pullPermission, nil
    42  	} else if *i.Permissions == writePermission {
    43  		return pushPermission, nil
    44  	} else if *i.Permissions == adminPermission {
    45  		return adminPermission, nil
    46  	}
    47  
    48  	return "", fmt.Errorf("unexpected permission value: %v", *i.Permissions)
    49  }