MCPcopy Index your code
hub / github.com/bytebase/bytebase / getBitbucketVCSUser

Function getBitbucketVCSUser

action/command/vcs_user.go:136–201  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

134}
135
136func 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

Callers 1

getVCSUserFunction · 0.85

Calls 10

NewHTTPClientFunction · 0.92
getBitbucketRepositoryFunction · 0.85
getBitbucketAPIBaseURLFunction · 0.85
cancelFunction · 0.85
isBitbucketBotUserFunction · 0.85
DoMethod · 0.80
UnmarshalMethod · 0.80
CloseMethod · 0.65
SetMethod · 0.45

Tested by

no test coverage detected