-
Notifications
You must be signed in to change notification settings - Fork 287
feat: add revision guard draft conversion flow #2279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
NssGourav
wants to merge
17
commits into
hiero-ledger:main
Choose a base branch
from
NssGourav:revision-guard-draft-flow
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
807125d
simplify CodeRabbit workflow
NssGourav 1191294
fix malformed CodeRabbit workflow condition
NssGourav c537a55
feat: add revision guard draft conversion flow
NssGourav ccb988f
fix: address review feedback on revision-guard draft conversion flow
NssGourav c3537b0
fix: wrap state-mutating ops in try/catch with contextual error logging
NssGourav 533d4f3
Update .github/workflows/revision-guard.yml
NssGourav 8e29a51
Merge branch 'main' into revision-guard-draft-flow
NssGourav b042778
fix: use REST API for draft conversion to avoid contents:write permis…
NssGourav be7b22d
Merge branch 'main' into revision-guard-draft-flow
NssGourav b47afb8
Merge fork main into revision guard draft flow
NssGourav 409988f
Merge remote-tracking branch 'origin/main' into revision-guard-draft-…
NssGourav 97481d7
fix: convert revision guard PRs to draft via GraphQL
NssGourav 9158691
fix: support review bot token for draft conversion
NssGourav 393f8e2
fix: make revision guard draft conversion optional
NssGourav efe44f1
Merge remote-tracking branch 'upstream/main' into revision-guard-draf…
NssGourav daf0df3
chore: align revision guard test workflow
NssGourav ca14b19
Merge branch 'main' into revision-guard-draft-flow
NssGourav File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| const DEFAULT_MANAGED_LABELS = [ | ||
| 'queue:junior-committer', | ||
| 'queue:committers', | ||
| 'queue:maintainers', | ||
| 'status: ready-to-merge', | ||
| 'status: failed checks', | ||
| 'open to community review', | ||
| ]; | ||
|
|
||
| function parseManagedLabels(value) { | ||
| if (typeof value !== 'string' || value.trim().length === 0) { | ||
| return [...DEFAULT_MANAGED_LABELS]; | ||
| } | ||
|
|
||
| const customLabels = value | ||
| .split(',') | ||
| .map(label => label.trim()) | ||
| .filter(Boolean); | ||
|
|
||
| // Merge custom labels with defaults so that setting REVISION_GUARD_MANAGED_LABELS | ||
| // adds to the managed set rather than silently discarding the default labels. | ||
| const merged = new Set([...DEFAULT_MANAGED_LABELS, ...customLabels]); | ||
| return [...merged]; | ||
| } | ||
|
|
||
| module.exports = { | ||
| DEFAULT_MANAGED_LABELS, | ||
| parseManagedLabels, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| function isBotAuthor(pr) { | ||
|
exploreriii marked this conversation as resolved.
|
||
| const login = pr?.user?.login || ''; | ||
| return pr?.user?.type === 'Bot' || login.endsWith('[bot]'); | ||
| } | ||
|
|
||
| function isDraft(pr) { | ||
| return pr?.draft === true; | ||
| } | ||
|
|
||
| async function convertToDraft(github, { pullRequestId }) { | ||
| await github.graphql( | ||
| ` | ||
| mutation ConvertPullRequestToDraft($pullRequestId: ID!) { | ||
| convertPullRequestToDraft(input: { pullRequestId: $pullRequestId }) { | ||
| pullRequest { | ||
| id | ||
| isDraft | ||
| } | ||
| } | ||
| } | ||
| `, | ||
| { pullRequestId } | ||
| ); | ||
| } | ||
|
|
||
| module.exports = { | ||
| convertToDraft, | ||
| isBotAuthor, | ||
| isDraft, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| const { DEFAULT_MANAGED_LABELS, parseManagedLabels } = require('./constants'); | ||
| const { convertToDraft, isBotAuthor, isDraft } = require('./draft'); | ||
| const { getManagedLabels, getPresentManagedLabels, removeManagedLabels } = require('./labels'); | ||
|
|
||
| module.exports = { | ||
| DEFAULT_MANAGED_LABELS, | ||
| parseManagedLabels, | ||
| convertToDraft, | ||
| isBotAuthor, | ||
| isDraft, | ||
| getManagedLabels, | ||
| getPresentManagedLabels, | ||
| removeManagedLabels, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| const { parseManagedLabels } = require('./constants'); | ||
|
|
||
| function getManagedLabels() { | ||
| return parseManagedLabels(process.env.REVISION_GUARD_MANAGED_LABELS); | ||
| } | ||
|
|
||
| function getPresentManagedLabels(prLabels, managedLabels = getManagedLabels()) { | ||
| const currentLabels = Array.isArray(prLabels) | ||
| ? prLabels | ||
| .map(label => typeof label === 'string' ? label : label?.name) | ||
| .filter(Boolean) | ||
| : []; | ||
|
|
||
| return managedLabels.filter(label => currentLabels.includes(label)); | ||
| } | ||
|
|
||
| async function removeManagedLabels(github, { owner, repo, issueNumber, labels }) { | ||
| for (const name of labels) { | ||
| try { | ||
| await github.rest.issues.removeLabel({ | ||
|
exploreriii marked this conversation as resolved.
|
||
| owner, | ||
| repo, | ||
| issue_number: issueNumber, | ||
| name, | ||
| }); | ||
| } catch (error) { | ||
| if (error?.status !== 404) { | ||
| throw error; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| module.exports = { | ||
| getManagedLabels, | ||
| getPresentManagedLabels, | ||
| removeManagedLabels, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| const { | ||
| convertToDraft, | ||
| getPresentManagedLabels, | ||
| isBotAuthor, | ||
| isDraft, | ||
| removeManagedLabels, | ||
| } = require('./helpers'); | ||
|
|
||
| module.exports = async function revisionGuard({ github, context, core }) { | ||
| const payload = context?.payload; | ||
| const repo = context?.repo; | ||
| const reviewState = payload?.review?.state; | ||
| const pr = payload?.pull_request; | ||
|
|
||
| if ( | ||
| !payload || | ||
| !repo?.owner || | ||
| !repo?.repo || | ||
| !pr || | ||
| typeof pr.number !== 'number' || | ||
| reviewState !== 'changes_requested' | ||
| ) { | ||
| core?.info?.('Skipping revision guard due to missing or non-matching payload data.'); | ||
| return; | ||
| } | ||
|
|
||
| if (isBotAuthor(pr)) { | ||
| core?.info?.(`Skipping PR #${pr.number} because it is bot-authored.`); | ||
| return; | ||
| } | ||
|
|
||
| const labelsToRemove = getPresentManagedLabels(pr.labels); | ||
|
|
||
| if (!isDraft(pr)) { | ||
| if (process.env.REVIEWBOT_TOKEN && typeof pr.node_id === 'string') { | ||
| try { | ||
| await convertToDraft(github, { | ||
| pullRequestId: pr.node_id, | ||
| }); | ||
| core?.info?.(`Converted PR #${pr.number} to draft.`); | ||
| } catch (error) { | ||
| core?.error?.( | ||
| `Failed to convert PR #${pr.number} to draft: ${error.message}. ` + | ||
| 'Continuing with managed label cleanup.' | ||
| ); | ||
| } | ||
| } else { | ||
| core?.info?.( | ||
| `Skipping draft conversion for PR #${pr.number}; REVIEWBOT_TOKEN is not configured.` | ||
| ); | ||
| } | ||
| } else { | ||
| core?.info?.(`PR #${pr.number} is already a draft.`); | ||
| } | ||
|
|
||
| if (labelsToRemove.length === 0) { | ||
| core?.info?.(`No managed labels to remove for PR #${pr.number}.`); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| await removeManagedLabels(github, { | ||
| owner: repo.owner, | ||
| repo: repo.repo, | ||
| issueNumber: pr.number, | ||
| labels: labelsToRemove, | ||
| }); | ||
| core?.info?.( | ||
| `Removed managed labels from PR #${pr.number}: ${labelsToRemove.join(', ')}.` | ||
| ); | ||
| } catch (error) { | ||
| core?.error?.( | ||
| `Failed to remove labels from PR #${pr.number}: ${error.message}. ` + | ||
| `Labels to remove: ${labelsToRemove.join(', ')}.` | ||
| ); | ||
| throw error; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| const { describe, it, beforeEach, afterEach } = require('node:test'); | ||
|
exploreriii marked this conversation as resolved.
|
||
| const assert = require('node:assert/strict'); | ||
|
|
||
| function freshRequire() { | ||
| const indexPath = require.resolve('./index.js'); | ||
| const helpersPath = require.resolve('./helpers/index.js'); | ||
| const labelsPath = require.resolve('./helpers/labels.js'); | ||
| const constantsPath = require.resolve('./helpers/constants.js'); | ||
| const draftPath = require.resolve('./helpers/draft.js'); | ||
|
|
||
| delete require.cache[indexPath]; | ||
| delete require.cache[helpersPath]; | ||
| delete require.cache[labelsPath]; | ||
| delete require.cache[constantsPath]; | ||
| delete require.cache[draftPath]; | ||
|
|
||
| return require('./index.js'); | ||
| } | ||
|
|
||
| function createGithubMock() { | ||
| const removedLabels = []; | ||
| const graphqlCalls = []; | ||
| let graphqlError; | ||
|
|
||
| return { | ||
| removedLabels, | ||
| graphqlCalls, | ||
| failGraphql(error) { | ||
| graphqlError = error; | ||
| }, | ||
| graphql: async (query, variables) => { | ||
| graphqlCalls.push({ query, variables }); | ||
| if (graphqlError) { | ||
| throw graphqlError; | ||
| } | ||
| return { | ||
| convertPullRequestToDraft: { | ||
| pullRequest: { | ||
| id: variables.pullRequestId, | ||
| isDraft: true, | ||
| }, | ||
| }, | ||
| }; | ||
| }, | ||
| rest: { | ||
| issues: { | ||
| removeLabel: async ({ name }) => { | ||
| removedLabels.push(name); | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function createContext(overrides = {}) { | ||
| return { | ||
| repo: { owner: 'hiero-ledger', repo: 'hiero-sdk-python' }, | ||
| payload: { | ||
| review: { state: 'changes_requested' }, | ||
| pull_request: { | ||
| number: 42, | ||
| node_id: 'PR_node_42', | ||
| draft: false, | ||
| user: { login: 'contributor', type: 'User' }, | ||
| labels: [ | ||
| { name: 'queue:committers' }, | ||
| { name: 'status: ready-to-merge' }, | ||
| { name: 'some other label' }, | ||
| ], | ||
| }, | ||
| ...overrides, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| describe('revision-guard index', () => { | ||
| beforeEach(() => { | ||
| delete process.env.REVISION_GUARD_MANAGED_LABELS; | ||
| delete process.env.REVIEWBOT_TOKEN; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| delete process.env.REVISION_GUARD_MANAGED_LABELS; | ||
| delete process.env.REVIEWBOT_TOKEN; | ||
| }); | ||
|
|
||
| it('removes only managed labels without draft conversion when no review bot token is configured', async () => { | ||
| const handler = freshRequire(); | ||
| const github = createGithubMock(); | ||
| const context = createContext(); | ||
|
|
||
| await handler({ github, context, core: { info() {} } }); | ||
|
|
||
| assert.equal(github.graphqlCalls.length, 0); | ||
| assert.deepEqual(github.removedLabels, [ | ||
| 'queue:committers', | ||
| 'status: ready-to-merge', | ||
| ]); | ||
| }); | ||
|
|
||
| it('skips bot-authored PRs', async () => { | ||
| const handler = freshRequire(); | ||
| const github = createGithubMock(); | ||
| const context = createContext({ | ||
| pull_request: { | ||
| number: 43, | ||
| node_id: 'PR_node_43', | ||
| draft: false, | ||
| user: { login: 'github-actions[bot]', type: 'Bot' }, | ||
| labels: [{ name: 'queue:committers' }], | ||
| }, | ||
| }); | ||
|
|
||
| await handler({ github, context, core: { info() {} } }); | ||
|
|
||
| assert.equal(github.graphqlCalls.length, 0); | ||
| assert.equal(github.removedLabels.length, 0); | ||
| }); | ||
|
|
||
| it('removes managed labels from already-draft PRs without converting again', async () => { | ||
| const handler = freshRequire(); | ||
| const github = createGithubMock(); | ||
| const context = createContext({ | ||
| pull_request: { | ||
| number: 44, | ||
| node_id: 'PR_node_44', | ||
| draft: true, | ||
| user: { login: 'contributor', type: 'User' }, | ||
| labels: [{ name: 'queue:committers' }], | ||
| }, | ||
| }); | ||
|
|
||
| await handler({ github, context, core: { info() {} } }); | ||
|
|
||
| assert.equal(github.graphqlCalls.length, 0); | ||
| assert.deepEqual(github.removedLabels, ['queue:committers']); | ||
| }); | ||
|
|
||
| it('converts to draft when a review bot token is configured', async () => { | ||
| process.env.REVIEWBOT_TOKEN = 'token'; | ||
| const handler = freshRequire(); | ||
| const github = createGithubMock(); | ||
| const context = createContext(); | ||
|
|
||
| await handler({ github, context, core: { info() {} } }); | ||
|
|
||
| assert.equal(github.graphqlCalls.length, 1); | ||
| assert.match(github.graphqlCalls[0].query, /convertPullRequestToDraft/); | ||
| assert.deepEqual(github.graphqlCalls[0].variables, { pullRequestId: 'PR_node_42' }); | ||
| assert.deepEqual(github.removedLabels, [ | ||
| 'queue:committers', | ||
| 'status: ready-to-merge', | ||
| ]); | ||
| }); | ||
|
|
||
| it('still removes managed labels when draft conversion fails', async () => { | ||
| process.env.REVIEWBOT_TOKEN = 'token'; | ||
| const handler = freshRequire(); | ||
| const github = createGithubMock(); | ||
| github.failGraphql(new Error('Resource not accessible by integration')); | ||
| const context = createContext(); | ||
|
|
||
| await handler({ github, context, core: { error() {}, info() {} } }); | ||
|
|
||
| assert.equal(github.graphqlCalls.length, 1); | ||
| assert.deepEqual(github.removedLabels, [ | ||
| 'queue:committers', | ||
| 'status: ready-to-merge', | ||
| ]); | ||
| }); | ||
|
|
||
| it('uses configurable managed labels and still removes defaults', async () => { | ||
| process.env.REVISION_GUARD_MANAGED_LABELS = 'custom: one, custom: two'; | ||
| const handler = freshRequire(); | ||
| const github = createGithubMock(); | ||
| const context = createContext({ | ||
| pull_request: { | ||
| number: 45, | ||
| node_id: 'PR_node_45', | ||
| draft: false, | ||
| user: { login: 'contributor', type: 'User' }, | ||
| labels: [ | ||
| { name: 'custom: one' }, | ||
| { name: 'queue:committers' }, | ||
| { name: 'custom: two' }, | ||
| ], | ||
| }, | ||
| }); | ||
|
|
||
| await handler({ github, context, core: { info() {} } }); | ||
|
|
||
| assert.equal(github.graphqlCalls.length, 0); | ||
| // Custom labels AND the matching default (queue:committers) must both be removed. | ||
| assert.deepEqual(github.removedLabels, ['queue:committers', 'custom: one', 'custom: two']); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.