()
| 134 | } |
| 135 | |
| 136 | func getBitbucketVCSUser() *v1pb.VCSUser { |
| 137 | pullRequestID := os.Getenv("BITBUCKET_PR_ID") |
| 138 | if pullRequestID == "" { |
| 139 | return nil |
| 140 | } |
| 141 | workspace, repoSlug := getBitbucketRepository() |
| 142 | if workspace == "" || repoSlug == "" { |
| 143 | return nil |
| 144 | } |
| 145 | |
| 146 | apiURL := getBitbucketAPIBaseURL() |
| 147 | requestURL, err := buildBitbucketPullRequestURL(apiURL, workspace, repoSlug, pullRequestID) |
| 148 | if err != nil { |
| 149 | return nil |
| 150 | } |
| 151 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 152 | defer cancel() |
| 153 | req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL, nil) |
| 154 | if err != nil { |
| 155 | return nil |
| 156 | } |
| 157 | req.Header.Set("Accept", "application/json") |
| 158 | |
| 159 | resp, err := bitbucket.NewHTTPClient(apiURL).Do(req) |
| 160 | if err != nil { |
| 161 | return nil |
| 162 | } |
| 163 | defer resp.Body.Close() |
| 164 | if resp.StatusCode != http.StatusOK { |
| 165 | return nil |
| 166 | } |
| 167 | |
| 168 | data, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20)) |
| 169 | if err != nil { |
| 170 | slog.Warn("failed to read Bitbucket pull request for VCS user attribution", "error", err) |
| 171 | return nil |
| 172 | } |
| 173 | var pullRequest struct { |
| 174 | Author struct { |
| 175 | AccountID string `json:"account_id"` |
| 176 | UUID string `json:"uuid"` |
| 177 | Nickname string `json:"nickname"` |
| 178 | DisplayName string `json:"display_name"` |
| 179 | Type string `json:"type"` |
| 180 | } `json:"author"` |
| 181 | } |
| 182 | if err := json.Unmarshal(data, &pullRequest); err != nil { |
| 183 | slog.Warn("failed to parse Bitbucket pull request for VCS user attribution", "error", err) |
| 184 | return nil |
| 185 | } |
| 186 | |
| 187 | author := pullRequest.Author |
| 188 | userID := author.AccountID |
| 189 | if userID == "" { |
| 190 | userID = strings.Trim(author.UUID, "{}") |
| 191 | } |
| 192 | if userID == "" || isBitbucketBotUser(author.Type, author.Nickname) { |
| 193 | return nil |
no test coverage detected