checkRepositoryAccess checks if the current user has write access to the target repository
(owner, repo string)
| 103 | |
| 104 | // checkRepositoryAccess checks if the current user has write access to the target repository |
| 105 | func checkRepositoryAccess(owner, repo string) (bool, error) { |
| 106 | prLog.Printf("Checking repository access: %s/%s", owner, repo) |
| 107 | |
| 108 | // Get current user |
| 109 | output, err := workflow.RunGH("Fetching user info...", "api", "/user", "--jq", ".login") |
| 110 | if err != nil { |
| 111 | prLog.Printf("Failed to get current user: %s", err) |
| 112 | return false, fmt.Errorf("failed to get current user: %w", err) |
| 113 | } |
| 114 | username := strings.TrimSpace(string(output)) |
| 115 | prLog.Printf("Current user: %s", username) |
| 116 | |
| 117 | // Check user's permission level for the repository |
| 118 | output, err = workflow.RunGH("Checking repository permissions...", "api", fmt.Sprintf("/repos/%s/%s/collaborators/%s/permission", owner, repo, username)) |
| 119 | if err != nil { |
| 120 | // If we get an error, it likely means we don't have access or the repo doesn't exist |
| 121 | prLog.Print("Repository access denied or repository not found") |
| 122 | return false, nil |
| 123 | } |
| 124 | |
| 125 | var permissionInfo struct { |
| 126 | Permission string `json:"permission"` |
| 127 | } |
| 128 | |
| 129 | if err := json.Unmarshal(output, &permissionInfo); err != nil { |
| 130 | return false, fmt.Errorf("failed to parse permission info: %w", err) |
| 131 | } |
| 132 | |
| 133 | // Check if user has write, maintain, or admin access |
| 134 | permission := permissionInfo.Permission |
| 135 | hasWriteAccess := permission == "write" || permission == "maintain" || permission == "admin" |
| 136 | prLog.Printf("User permission level: %s, has write access: %v", permission, hasWriteAccess) |
| 137 | |
| 138 | return hasWriteAccess, nil |
| 139 | } |
| 140 | |
| 141 | // createForkIfNeeded creates a fork of the target repository and returns the fork repo name |
| 142 | func createForkIfNeeded(targetOwner, targetRepo string, verbose bool) (forkOwner, forkRepo string, err error) { |
no test coverage detected