Skip to content

Commit fc190de

Browse files
Fix 403 error on private repos (attempt)
Signed-off-by: Lukasz Gryglicki <[email protected]> Assisted by [OpenAI](https://platform.openai.com/) Assisted by [GitHub Copilot](https://github.com/features/copilot)
1 parent 2d6e98d commit fc190de

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

cla-backend-go/github/github_repository.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1523,7 +1523,31 @@ func EditIssueCommentIfChanged(ctx context.Context, client *github.Client, owner
15231523
func GetPRHeadSHA(ctx context.Context, gh *github.Client, owner, repo string, prNumber int) (string, error) {
15241524
pr, _, err := gh.PullRequests.Get(ctx, owner, repo, prNumber)
15251525
if err != nil {
1526-
return "", err
1526+
f := logrus.Fields{
1527+
"functionName": "github.github_repository.GetPRHeadSHA",
1528+
"owner": owner,
1529+
"repo": repo,
1530+
"pullRequestID": prNumber,
1531+
}
1532+
log.WithFields(f).WithError(err).Warnf("cannot get PR head SHA using PullRequests.Get: %+v, trying PullRequests.ListCommits", err)
1533+
opts := &github.ListOptions{PerPage: 1}
1534+
commits, resp, comErr := gh.PullRequests.ListCommits(ctx, owner, repo, prNumber, opts)
1535+
if comErr != nil {
1536+
log.WithFields(f).WithError(comErr).Warnf("problem listing commits for repo: %s/%s pull request: %d", owner, repo, prNumber)
1537+
return "", comErr
1538+
}
1539+
if resp != nil && resp.LastPage > 1 {
1540+
opts.Page = resp.LastPage
1541+
commits, _, comErr = gh.PullRequests.ListCommits(ctx, owner, repo, prNumber, opts)
1542+
if comErr != nil {
1543+
log.WithFields(f).WithError(comErr).Warnf("problem listing commits for repo: %s/%s pull request: %d (last page)", owner, repo, prNumber)
1544+
return "", comErr
1545+
}
1546+
}
1547+
if len(commits) == 0 || commits[0].SHA == nil {
1548+
return "", fmt.Errorf("missing head SHA for %s/%s PR #%d (via ListCommits)", owner, repo, prNumber)
1549+
}
1550+
return *commits[0].SHA, nil
15271551
}
15281552
sha := ""
15291553
if pr.Head != nil && pr.Head.SHA != nil {

cla-backend/cla/models/github_models.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2641,11 +2641,18 @@ def update_pull_request(
26412641
fn = "cla.models.github_models.update_pull_request"
26422642
notification = cla.conf["GITHUB_PR_NOTIFICATION"]
26432643
both = notification == "status+comment" or notification == "comment+status"
2644-
last_commit_sha = getattr(getattr(pull_request, "head", None), "sha", None)
2644+
cla.log.debug(f"{fn} - Updating PR {pull_request.number} with notification={notification}, both={both}")
2645+
try:
2646+
last_commit_sha = getattr(getattr(pull_request, "head", None), "sha", None)
2647+
# commit_obj = pull_request.base.repo.get_commit(last_commit_sha)
2648+
commit_obj = pull_request.head.repo.get_commit(last_commit_sha)
2649+
except (GithubException, AttributeError, TypeError) as exc:
2650+
cla.log.error(f"{fn} - PR {pull_request.number}: exception getting head.sha: {exc}")
2651+
commit_obj = pull_request.get_commits().reversed[0]
2652+
last_commit_sha = commit_obj.sha
26452653
if not last_commit_sha:
26462654
cla.log.error(f"{fn} - PR {pull_request.number}: missing head.sha; cannot create statuses")
26472655
return
2648-
commit_obj = pull_request.base.repo.get_commit(last_commit_sha)
26492656

26502657
# Here we update the PR status by adding/updating the PR body - this is the way the EasyCLA app
26512658
# knows if it is pass/fail.
@@ -2814,7 +2821,7 @@ def create_commit_status(commit_obj, state, sign_url, body, context):
28142821
resp = commit_obj.create_status(state, sign_url, body, context)
28152822
cla.log.info(
28162823
f"Successfully posted status '{state}': Commit {sha} "
2817-
f"with SignUrl : {sign_url} with response: {resp}"
2824+
f"with SignUrl: {sign_url} with response: {resp}"
28182825
)
28192826
except GithubException as exc:
28202827
sha = getattr(commit_obj, "sha", "(unknown)")

utils/search_aws_log_group.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/bin/bash
22
# STAGE=dev DEBUG=1 DTFROM='3 days ago' DTTO='2 days ago' ./utils/search_aws_log_group.sh 'cla-backend-dev-githubactivity' 'error'
33
# STAGE=dev DEBUG=1 DTFROM='3 days ago' DTTO='2 days ago' ./utils/search_aws_log_group.sh 'cla-backend-dev-githubactivity' 'Runtime exited with'
4+
# STAGE=dev DEBUG=1 DTFROM='3 days ago' DTTO='2 days ago' ./utils/search_aws_log_group.sh 'cla-backend-dev-githubactivity' 'Traceback'
45
# STAGE=dev DEBUG=1 DTFROM='3 days ago' DTTO='2 days ago' ./utils/search_aws_log_group.sh 'cla-backend-dev-githubactivity' '---' # all
56
# REGION=us-east-2 STAGE=prod DEBUG=1 DTFROM='15 minutes ago' DTTO='1 second ago' ./utils/search_aws_log_group.sh 'cla-backend-go-api-v4-lambda' 'LG:api-request-path'
67
# REGION=us-east-1 STAGE=prod DEBUG=1 DTFROM='15 minutes ago' DTTO='1 second ago' ./utils/search_aws_log_group.sh 'cla-backend-prod-api-v3-lambda' 'LG:api-request-path'

0 commit comments

Comments
 (0)