Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e71193d

Browse files
Close issues in batches
It seems that GitHub's API has an undocumented limit on how many GraphQL aliases you can put in one mutation query. Current limit is set to 10, since it seems logical.
1 parent 001d03e commit e71193d

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

MassCloseOldIssues/issueupdater.cpp

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ SOFTWARE. */
3232

3333
using json = nlohmann::json;
3434

35+
namespace
36+
{
37+
constexpr int BATCH_SIZE = 10;
38+
}
39+
3540
IssueUpdater::IssueUpdater(const ProgramOptions &programOptions, PostDownloader &downloader,
3641
const std::vector<std::string> &issues,
3742
std::string_view labelID, std::string &error)
@@ -40,12 +45,31 @@ IssueUpdater::IssueUpdater(const ProgramOptions &programOptions, PostDownloader
4045
, m_issues(issues)
4146
, m_labelID(labelID)
4247
, m_error(error)
48+
, m_hasNextBatch(m_issues.size() > 0)
4349
{
4450
m_error.clear();
4551
}
4652

4753
void IssueUpdater::run()
4854
{
55+
if (!hasNextBatch())
56+
return;
57+
58+
m_downloader.setFinishedHandler(beast::bind_front_handler(&IssueUpdater::onFinishedPage, this));
59+
60+
json req;
61+
req["query"] = nextBatch();
62+
m_downloader.setRequestBody(req.dump());
63+
64+
m_downloader.run();
65+
m_downloader.setFinishedHandler(FinishedHandler{});
66+
}
67+
68+
std::string IssueUpdater::nextBatch()
69+
{
70+
if (!hasNextBatch())
71+
return {};
72+
4973
// Sample QraphQL string for the mutation with one alias named 'issue0'
5074
// "mutation UpdateIssue { comment0: : addComment(input: {subjectId:\"ID\", body:\"COMMENT\"}) { clientMutationId }
5175
// label0: addLabelsToLabelable(input: {labelableId:\"ID\", labelIds:[\"ID\"]}) { clientMutationId }
@@ -58,7 +82,9 @@ void IssueUpdater::run()
5882
int counter = 0;
5983
std::ostringstream buffer;
6084
buffer << start;
61-
for (const auto &issueID : m_issues) {
85+
for (; ((m_issuePos < m_issues.size()) && (counter < BATCH_SIZE)); ++m_issuePos) {
86+
const auto &issueID = m_issues[m_issuePos];
87+
6288
if (!m_programOptions.comment.empty())
6389
buffer << makeCommentAlias(counter, issueID);
6490

@@ -72,16 +98,17 @@ void IssueUpdater::run()
7298

7399
++counter;
74100
}
75-
buffer << end;
76101

77-
m_downloader.setFinishedHandler(beast::bind_front_handler(&IssueUpdater::onFinishedPage, this));
102+
if (m_issuePos == m_issues.size())
103+
m_hasNextBatch = false;
78104

79-
json req;
80-
req["query"] = buffer.str();
81-
m_downloader.setRequestBody(req.dump());
105+
buffer << end;
106+
return buffer.str();
107+
}
82108

83-
m_downloader.run();
84-
m_downloader.setFinishedHandler(FinishedHandler{});
109+
bool IssueUpdater::hasNextBatch()
110+
{
111+
return m_hasNextBatch;
85112
}
86113

87114
void IssueUpdater::onFinishedPage()
@@ -97,6 +124,14 @@ void IssueUpdater::onFinishedPage()
97124
}
98125

99126
checkResponse(m_downloader.response().body());
127+
128+
if (!m_error.empty() || !hasNextBatch())
129+
return;
130+
131+
json req;
132+
req["query"] = nextBatch();
133+
m_downloader.setRequestBody(req.dump());
134+
m_downloader.sendRequest();
100135
}
101136

102137
void IssueUpdater::checkResponse(std::string_view response)

MassCloseOldIssues/issueupdater.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class IssueUpdater
3737
std::string_view labelID, std::string &error);
3838

3939
void run();
40+
std::string nextBatch();
41+
bool hasNextBatch();
4042

4143
private:
4244
void onFinishedPage();
@@ -53,4 +55,6 @@ class IssueUpdater
5355
const std::vector<std::string> &m_issues;
5456
const std::string m_labelID;
5557
std::string &m_error;
58+
int m_issuePos = 0;
59+
bool m_hasNextBatch;
5660
};

0 commit comments

Comments
 (0)