Skip to content

Commit 69c7b42

Browse files
committed
feat: access tokens per repo
1 parent eb91014 commit 69c7b42

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

src/libfetchers/github.cc

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,20 +172,45 @@ struct GitArchiveInputScheme : InputScheme
172172
return input;
173173
}
174174

175-
std::optional<std::string> getAccessToken(const fetchers::Settings & settings, const std::string & host) const
175+
std::optional<std::string> getAccessToken(const fetchers::Settings & settings, const std::string & host, const std::string & url) const
176176
{
177177
auto tokens = settings.accessTokens.get();
178+
std::string answer;
179+
size_t answer_match_len = 0;
180+
if(! url.empty()) {
181+
for (auto & token : tokens) {
182+
auto match_len = url.find(token.first);
183+
if (match_len != std::string::npos && token.first.length() > answer_match_len) {
184+
answer = token.second;
185+
answer_match_len = token.first.length();
186+
}
187+
}
188+
if (!answer.empty())
189+
return answer;
190+
}
178191
if (auto token = get(tokens, host))
179192
return *token;
180193
return {};
181194
}
182195

183196
Headers makeHeadersWithAuthTokens(
184197
const fetchers::Settings & settings,
185-
const std::string & host) const
198+
const std::string & host,
199+
const Input & input) const
200+
{
201+
auto owner = getStrAttr(input.attrs, "owner");
202+
auto repo = getStrAttr(input.attrs, "repo");
203+
auto urlGen = fmt( "%s/%s/%s", host, owner, repo);
204+
return makeHeadersWithAuthTokens(settings, host, urlGen);
205+
}
206+
207+
Headers makeHeadersWithAuthTokens(
208+
const fetchers::Settings & settings,
209+
const std::string & host,
210+
const std::string & url) const
186211
{
187212
Headers headers;
188-
auto accessToken = getAccessToken(settings, host);
213+
auto accessToken = getAccessToken(settings, host, url);
189214
if (accessToken) {
190215
auto hdr = accessHeaderFromToken(*accessToken);
191216
if (hdr)
@@ -366,7 +391,7 @@ struct GitHubInputScheme : GitArchiveInputScheme
366391
: "https://%s/api/v3/repos/%s/%s/commits/%s",
367392
host, getOwner(input), getRepo(input), *input.getRef());
368393

369-
Headers headers = makeHeadersWithAuthTokens(*input.settings, host);
394+
Headers headers = makeHeadersWithAuthTokens(*input.settings, host, input);
370395

371396
auto json = nlohmann::json::parse(
372397
readFile(
@@ -383,7 +408,7 @@ struct GitHubInputScheme : GitArchiveInputScheme
383408
{
384409
auto host = getHost(input);
385410

386-
Headers headers = makeHeadersWithAuthTokens(*input.settings, host);
411+
Headers headers = makeHeadersWithAuthTokens(*input.settings, host, input);
387412

388413
// If we have no auth headers then we default to the public archive
389414
// urls so we do not run into rate limits.
@@ -440,7 +465,7 @@ struct GitLabInputScheme : GitArchiveInputScheme
440465
auto url = fmt("https://%s/api/v4/projects/%s%%2F%s/repository/commits?ref_name=%s",
441466
host, getStrAttr(input.attrs, "owner"), getStrAttr(input.attrs, "repo"), *input.getRef());
442467

443-
Headers headers = makeHeadersWithAuthTokens(*input.settings, host);
468+
Headers headers = makeHeadersWithAuthTokens(*input.settings, host, input);
444469

445470
auto json = nlohmann::json::parse(
446471
readFile(
@@ -470,7 +495,7 @@ struct GitLabInputScheme : GitArchiveInputScheme
470495
host, getStrAttr(input.attrs, "owner"), getStrAttr(input.attrs, "repo"),
471496
input.getRev()->to_string(HashFormat::Base16, false));
472497

473-
Headers headers = makeHeadersWithAuthTokens(*input.settings, host);
498+
Headers headers = makeHeadersWithAuthTokens(*input.settings, host, input);
474499
return DownloadUrl { url, headers };
475500
}
476501

@@ -510,7 +535,7 @@ struct SourceHutInputScheme : GitArchiveInputScheme
510535
auto base_url = fmt("https://%s/%s/%s",
511536
host, getStrAttr(input.attrs, "owner"), getStrAttr(input.attrs, "repo"));
512537

513-
Headers headers = makeHeadersWithAuthTokens(*input.settings, host);
538+
Headers headers = makeHeadersWithAuthTokens(*input.settings, host, input);
514539

515540
std::string refUri;
516541
if (ref == "HEAD") {
@@ -557,7 +582,7 @@ struct SourceHutInputScheme : GitArchiveInputScheme
557582
host, getStrAttr(input.attrs, "owner"), getStrAttr(input.attrs, "repo"),
558583
input.getRev()->to_string(HashFormat::Base16, false));
559584

560-
Headers headers = makeHeadersWithAuthTokens(*input.settings, host);
585+
Headers headers = makeHeadersWithAuthTokens(*input.settings, host, input);
561586
return DownloadUrl { url, headers };
562587
}
563588

0 commit comments

Comments
 (0)