Commit c5fc18ac by Dominik Prokop Committed by GitHub

grafana/toolkit: Support paging in cherrypick task (#24402)

* Support paging in cherrypick task

* Update packages/grafana-toolkit/src/cli/tasks/cherrypick.ts

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>

* Update packages/grafana-toolkit/src/cli/tasks/cherrypick.ts

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>

* Do not cherry pick closed PRs

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
parent 2f5003eb
......@@ -5,22 +5,63 @@ interface CherryPickOptions {
enterprise: boolean;
}
const cherryPickRunner: TaskRunner<CherryPickOptions> = async ({ enterprise }) => {
const githubClient = new GithubClient({ enterprise });
const client = githubClient.client;
// https://github.com/lisposter/github-pagination/blob/master/lib/octopage.js
const pagingParser = (linkStr: string): { prev?: string; next?: string; last?: string; first?: string } => {
return linkStr
.split(',')
.map(rel => {
//@ts-ignore
return rel.split(';').map((curr, idx) => {
if (idx === 0) {
//@ts-ignore
return /[^_]page=(\d+)/.exec(curr)[1];
}
if (idx === 1) {
//@ts-ignore
return /rel="(.+)"/.exec(curr)[1];
}
});
})
.reduce(function(obj, curr, i) {
//@ts-ignore
obj[curr[1]] = curr[0];
return obj;
}, {});
};
const res = await client.get('/issues', {
const getIssues = async (client: any, page: string) => {
const result = await client.get('/issues', {
params: {
state: 'closed',
per_page: 100,
labels: 'cherry-pick needed',
sort: 'closed',
direction: 'asc',
page,
},
});
let data = result.data;
if (!result.headers.link) {
return data;
}
const pages = pagingParser(result.headers.link);
if (pages.next) {
const nextPage = await getIssues(client, pages.next);
data = data.concat(nextPage);
}
return data;
};
const cherryPickRunner: TaskRunner<CherryPickOptions> = async ({ enterprise }) => {
const githubClient = new GithubClient({ enterprise });
const client = githubClient.client;
const results = await getIssues(client, '1');
// sort by closed date ASC
res.data.sort((a: any, b: any) => {
results.sort((a: any, b: any) => {
return new Date(a.closed_at).getTime() - new Date(b.closed_at).getTime();
});
......@@ -30,13 +71,17 @@ const cherryPickRunner: TaskRunner<CherryPickOptions> = async ({ enterprise }) =
console.log('Printing PRs with cherry-pick-needed, in ASC merge date order');
console.log('--------------------------------------------------------------------');
for (const item of res.data) {
for (const item of results) {
if (!item.milestone) {
console.log(item.number + ' missing milestone!');
continue;
}
const issueDetails = await client.get(item.pull_request.url);
if (!issueDetails.data.merged) {
continue;
}
console.log(`* ${item.title}, (#${item.number}), merge-sha: ${issueDetails.data.merge_commit_sha}`);
commands += `git cherry-pick -x ${issueDetails.data.merge_commit_sha}\n`;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment