Skip to content

Commit 254251a

Browse files
committed
fix(github): call draft conversion with PAT via direct fetch, octokit overrides auth header
1 parent 849daf2 commit 254251a

1 file changed

Lines changed: 20 additions & 10 deletions

File tree

.github/workflows/pr-governance.yml

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,27 @@ jobs:
108108
if (!passed) {
109109
try {
110110
const pat = process.env.DRAFT_PAT;
111-
await github.graphql(
112-
`mutation ($id: ID!) {
113-
convertPullRequestToDraft(input: { pullRequestId: $id }) {
114-
pullRequest { isDraft }
115-
}
116-
}`,
117-
{
118-
id: pr.node_id,
119-
...(pat ? { headers: { authorization: `Bearer ${pat}` } } : {}),
111+
if (!pat) throw new Error('PR_GOVERNANCE_PAT is not configured');
112+
// 不走 github.graphql:octokit 的 auth 钩子会强制覆盖 authorization 头,传入的 PAT 会被 GITHUB_TOKEN 顶掉,而该 mutation 不接受 integration token。
113+
const res = await fetch('https://api.github.com/graphql', {
114+
method: 'POST',
115+
headers: {
116+
authorization: `Bearer ${pat}`,
117+
'content-type': 'application/json',
120118
},
121-
);
119+
body: JSON.stringify({
120+
query: `mutation ($id: ID!) {
121+
convertPullRequestToDraft(input: { pullRequestId: $id }) {
122+
pullRequest { isDraft }
123+
}
124+
}`,
125+
variables: { id: pr.node_id },
126+
}),
127+
});
128+
const out = await res.json();
129+
if (!res.ok || out.errors?.length) {
130+
throw new Error(out.errors?.map((e) => e.message).join('; ') || `HTTP ${res.status}`);
131+
}
122132
drafted = true;
123133
} catch (e) {
124134
core.warning(`Failed to convert to draft (${e.message}); the check is still red and merge blocking is unaffected.`);

0 commit comments

Comments
 (0)