Skip to content

Commit 0a5bd0c

Browse files
committed
generate the report even without github
Signed-off-by: Gaëtan Lehmann <gaetan.lehmann@vates.tech>
1 parent ec8652f commit 0a5bd0c

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

scripts/pkg_in_pipe/pkg_in_pipe.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import koji
1414
import requests
1515
from github.Commit import Commit
16+
from github.GithubException import BadCredentialsException
1617
from github.PullRequest import PullRequest
1718

1819

@@ -38,6 +39,15 @@ def print_plane_warning(out):
3839
</div>
3940
</div>'''), file=out)
4041

42+
def print_github_warning(out):
43+
print(dedent('''
44+
<div class="px-3 py-3">
45+
<div class="bg-orange-100 border-l-4 border-orange-500 text-orange-700 p-4" role="alert">
46+
<p class="font-bold">Github access problem</p>
47+
<p>The pull requests come from the cache and may not be up to date.</p>
48+
</div>
49+
</div>'''), file=out)
50+
4151
def print_koji_error(out):
4252
print(dedent('''
4353
<div class="px-3 py-3">
@@ -184,11 +194,12 @@ def find_commits(gh, repo, start_sha, end_sha) -> list[Commit]:
184194
if cache_key in CACHE:
185195
return cast(list[Commit], CACHE[cache_key])
186196
commits = []
187-
for commit in gh.get_repo(repo).get_commits(start_sha):
188-
if commit.sha == end_sha:
189-
break
190-
commits.append(commit)
191-
CACHE[cache_key] = commits
197+
if gh:
198+
for commit in gh.get_repo(repo).get_commits(start_sha):
199+
if commit.sha == end_sha:
200+
break
201+
commits.append(commit)
202+
CACHE[cache_key] = commits
192203
return commits
193204

194205
def find_pull_requests(gh, repo, start_sha, end_sha):
@@ -198,7 +209,7 @@ def find_pull_requests(gh, repo, start_sha, end_sha):
198209
cache_key = f'commit-prs-{commit.sha}'
199210
if cache_key in CACHE:
200211
prs.update(cast(list[PullRequest], CACHE[cache_key]))
201-
else:
212+
elif gh:
202213
commit_prs = list(commit.get_pulls())
203214
CACHE[cache_key] = commit_prs
204215
prs.update(commit_prs)
@@ -210,6 +221,9 @@ def find_pull_requests(gh, repo, start_sha, end_sha):
210221
parser.add_argument(
211222
'--plane-token', help="The token used to access the plane api", default=os.environ.get('PLANE_TOKEN')
212223
)
224+
parser.add_argument(
225+
'--github-token', help="The token used to access the Github api", default=os.environ.get('GITHUB_TOKEN')
226+
)
213227
parser.add_argument('--cache', help="The cache path", default="/tmp/pkg_in_pipe.cache")
214228
args = parser.parse_args()
215229

@@ -223,13 +237,22 @@ def find_pull_requests(gh, repo, start_sha, end_sha):
223237
issues = resp.json().get('results', [])
224238

225239
# connect to github
226-
gh = github.Github(auth=github.Auth.Token(os.environ['GITHUB_TOKEN']))
240+
if args.github_token:
241+
gh = github.Github(auth=github.Auth.Token(args.github_token))
242+
try:
243+
gh.get_repo('xcp-ng/xcp') # check that the token is valid
244+
except BadCredentialsException:
245+
gh = None
246+
else:
247+
gh = None
227248

228249
ok = True
229250
with open(args.output, 'w') as out:
230251
print_header(out)
231252
if not issues:
232253
print_plane_warning(out)
254+
if not gh:
255+
print_github_warning(out)
233256
tags = [f'v{v}-{p}' for v in ['8.2', '8.3'] for p in ['incoming', 'ci', 'testing', 'candidates', 'lab']]
234257
temp_out = io.StringIO()
235258
try:

0 commit comments

Comments
 (0)