Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an error: issue templates are not created sometimes #400

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/x-post-draft.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jobs:

const tweet = `🚀 New release: ${context.payload.pull_request.title}\n\n${context.payload.pull_request.html_url}`;

// https://developer.x.com/en/docs/x-api/tweets/manage-tweets/api-reference/post-tweets
await client.v2.tweet(tweet);
env:
TWITTER_API_KEY: ${{ secrets.TWITTER_API_KEY }}
Expand Down
34 changes: 15 additions & 19 deletions services/github/github_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import jwt # For generating JWTs (JSON Web Tokens)
import requests
from fastapi import Request
from github import Github, GithubException
from github import Github
from github.ContentFile import ContentFile
from github.PullRequest import PullRequest
from github.Repository import Repository
Expand All @@ -29,7 +29,6 @@
GITHUB_ISSUE_TEMPLATES,
GITHUB_PRIVATE_KEY,
IS_PRD,
MAX_RETRIES,
PRODUCT_NAME,
PRODUCT_URL,
TIMEOUT,
Expand Down Expand Up @@ -62,26 +61,23 @@ def add_issue_templates(full_name: str, installer_name: str, token: str) -> None
print(f"Adding issue templates to the repo: '{full_name}' by '{installer_name}'.\n")
gh = Github(login_or_token=token)
repo: Repository = gh.get_repo(full_name_or_id=full_name)
owner, repo_name = full_name.split("/")
default_branch_name = repo.default_branch

# Get latest commit SHA directly
base_args = {
"owner": owner,
"repo": repo_name,
"base_branch": default_branch_name,
"token": token,
}
clone_url = repo.clone_url
latest_sha = get_latest_remote_commit_sha(clone_url=clone_url, base_args=base_args)

# Create a new branch
default_branch = None
default_branch_name: str = repo.default_branch
retries = 0
while retries < MAX_RETRIES:
try:
default_branch = repo.get_branch(branch=default_branch_name)
break
except GithubException as e:
retries += 1
msg = f"Error getting default branch for repo '{full_name}', branch '{default_branch_name}'. Status: {e.status}, Error: {e.data}"
logging.error(msg)
time.sleep(20)
# Create a new branch using the SHA
new_branch_name: str = f"{PRODUCT_ID}/add-issue-templates-{str(object=uuid4())}"
ref = f"refs/heads/{new_branch_name}"
if default_branch is None:
msg = f"Error: Could not get the default branch for repo: {full_name} and branch: {default_branch_name}."
raise RuntimeError(msg)
repo.create_git_ref(ref=ref, sha=default_branch.commit.sha)
repo.create_git_ref(ref=ref, sha=latest_sha)

# Add issue templates to the new branch
added_templates: list[str] = []
Expand Down
Loading