Skip to content

Commit 97481d7

Browse files
committed
fix: convert revision guard PRs to draft via GraphQL
Signed-off-by: Gourav NSS <gourav341111@gmail.com>
1 parent 409988f commit 97481d7

3 files changed

Lines changed: 38 additions & 25 deletions

File tree

.github/scripts/revision-guard/helpers/draft.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,20 @@ function isDraft(pr) {
77
return pr?.draft === true;
88
}
99

10-
async function convertToDraft(github, { owner, repo, pullNumber }) {
11-
await github.rest.pulls.update({
12-
owner,
13-
repo,
14-
pull_number: pullNumber,
15-
draft: true,
16-
});
10+
async function convertToDraft(github, { pullRequestId }) {
11+
await github.graphql(
12+
`
13+
mutation ConvertPullRequestToDraft($pullRequestId: ID!) {
14+
convertPullRequestToDraft(input: { pullRequestId: $pullRequestId }) {
15+
pullRequest {
16+
id
17+
isDraft
18+
}
19+
}
20+
}
21+
`,
22+
{ pullRequestId }
23+
);
1724
}
1825

1926
module.exports = {

.github/scripts/revision-guard/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = async function revisionGuard({ github, context, core }) {
1818
!repo?.repo ||
1919
!pr ||
2020
typeof pr.number !== 'number' ||
21+
typeof pr.node_id !== 'string' ||
2122
reviewState !== 'changes_requested'
2223
) {
2324
core?.info?.('Skipping revision guard due to missing or non-matching payload data.');
@@ -36,9 +37,7 @@ module.exports = async function revisionGuard({ github, context, core }) {
3637

3738
try {
3839
await convertToDraft(github, {
39-
owner: repo.owner,
40-
repo: repo.repo,
41-
pullNumber: pr.number,
40+
pullRequestId: pr.node_id,
4241
});
4342
core?.info?.(`Converted PR #${pr.number} to draft.`);
4443
} catch (error) {

.github/scripts/revision-guard/index.test.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,41 @@ function freshRequire() {
66
const helpersPath = require.resolve('./helpers/index.js');
77
const labelsPath = require.resolve('./helpers/labels.js');
88
const constantsPath = require.resolve('./helpers/constants.js');
9+
const draftPath = require.resolve('./helpers/draft.js');
910

1011
delete require.cache[indexPath];
1112
delete require.cache[helpersPath];
1213
delete require.cache[labelsPath];
1314
delete require.cache[constantsPath];
15+
delete require.cache[draftPath];
1416

1517
return require('./index.js');
1618
}
1719

1820
function createGithubMock() {
1921
const removedLabels = [];
20-
const pullUpdates = [];
22+
const graphqlCalls = [];
2123

2224
return {
2325
removedLabels,
24-
pullUpdates,
26+
graphqlCalls,
27+
graphql: async (query, variables) => {
28+
graphqlCalls.push({ query, variables });
29+
return {
30+
convertPullRequestToDraft: {
31+
pullRequest: {
32+
id: variables.pullRequestId,
33+
isDraft: true,
34+
},
35+
},
36+
};
37+
},
2538
rest: {
2639
issues: {
2740
removeLabel: async ({ name }) => {
2841
removedLabels.push(name);
2942
},
3043
},
31-
pulls: {
32-
update: async (params) => {
33-
pullUpdates.push(params);
34-
},
35-
},
3644
},
3745
};
3846
}
@@ -74,9 +82,9 @@ describe('revision-guard index', () => {
7482

7583
await handler({ github, context, core: { info() {} } });
7684

77-
assert.deepEqual(github.pullUpdates, [
78-
{ owner: 'hiero-ledger', repo: 'hiero-sdk-python', pull_number: 42, draft: true },
79-
]);
85+
assert.equal(github.graphqlCalls.length, 1);
86+
assert.match(github.graphqlCalls[0].query, /convertPullRequestToDraft/);
87+
assert.deepEqual(github.graphqlCalls[0].variables, { pullRequestId: 'PR_node_42' });
8088
assert.deepEqual(github.removedLabels, [
8189
'queue:committers',
8290
'status: ready-to-merge',
@@ -98,7 +106,7 @@ describe('revision-guard index', () => {
98106

99107
await handler({ github, context, core: { info() {} } });
100108

101-
assert.equal(github.pullUpdates.length, 0);
109+
assert.equal(github.graphqlCalls.length, 0);
102110
assert.equal(github.removedLabels.length, 0);
103111
});
104112

@@ -117,7 +125,7 @@ describe('revision-guard index', () => {
117125

118126
await handler({ github, context, core: { info() {} } });
119127

120-
assert.equal(github.pullUpdates.length, 0);
128+
assert.equal(github.graphqlCalls.length, 0);
121129
assert.equal(github.removedLabels.length, 0);
122130
});
123131

@@ -142,9 +150,8 @@ describe('revision-guard index', () => {
142150
await handler({ github, context, core: { info() {} } });
143151

144152
// Draft conversion must also fire for configurable-label scenarios.
145-
assert.deepEqual(github.pullUpdates, [
146-
{ owner: 'hiero-ledger', repo: 'hiero-sdk-python', pull_number: 45, draft: true },
147-
]);
153+
assert.equal(github.graphqlCalls.length, 1);
154+
assert.deepEqual(github.graphqlCalls[0].variables, { pullRequestId: 'PR_node_45' });
148155
// Custom labels AND the matching default (queue:committers) must both be removed.
149156
assert.deepEqual(github.removedLabels, ['queue:committers', 'custom: one', 'custom: two']);
150157
});

0 commit comments

Comments
 (0)