-
Notifications
You must be signed in to change notification settings - Fork 753
Add support for attestation commit #8794
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
Open
alexr00
wants to merge
4
commits into
main
Choose a base branch
from
alexr00/sore-deer
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.
Open
Changes from all commits
Commits
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
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
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
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
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
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,142 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import * as vscode from 'vscode'; | ||
| import { FolderRepositoryManager } from './folderRepositoryManager'; | ||
| import { PullRequestModel } from './pullRequestModel'; | ||
| import { Repository } from '../api/api'; | ||
| import Logger from '../common/logger'; | ||
| import { ENABLE_ATTESTATION_COMMITS, PR_SETTINGS_NAMESPACE } from '../common/settingKeys'; | ||
| import { formatError } from '../common/utils'; | ||
|
|
||
| const LOG_ID = 'AttestationCommit'; | ||
|
|
||
| const DEFAULT_ATTESTATION_COMMIT_MESSAGE = 'Attestation commit'; | ||
|
|
||
| /** | ||
| * Returns true when the repository appears to have commit signing configured. | ||
| * | ||
| * Accepts any of the following (checked across local + global git config): | ||
| * - `user.signingkey` is set, OR | ||
| * - `commit.gpgsign` is `true` (git will pick a default signing identity), OR | ||
| * - `gpg.format` is set to `ssh` or `x509` (the user is explicitly opting in | ||
| * to a non-default signing format). | ||
| */ | ||
| async function hasCommitSigningConfigured(repository: Repository): Promise<boolean> { | ||
| const read = async (key: string): Promise<string | undefined> => { | ||
| const tryRead = async (fn: (k: string) => Promise<string>): Promise<string | undefined> => { | ||
| try { | ||
| const value = await fn(key); | ||
| return value?.trim() || undefined; | ||
| } catch { | ||
| // `getConfig`/`getGlobalConfig` reject when the key is not set. | ||
| return undefined; | ||
| } | ||
| }; | ||
| return (await tryRead(k => repository.getConfig(k))) | ||
| ?? (await tryRead(k => repository.getGlobalConfig(k))); | ||
| }; | ||
|
|
||
| const [signingKey, commitGpgSign, gpgFormat] = await Promise.all([ | ||
| read('user.signingkey'), | ||
| read('commit.gpgsign'), | ||
| read('gpg.format'), | ||
| ]); | ||
|
|
||
| if (signingKey) { | ||
| return true; | ||
| } | ||
| if (commitGpgSign?.toLowerCase() === 'true') { | ||
| return true; | ||
| } | ||
| if (gpgFormat && ['ssh', 'x509'].includes(gpgFormat.toLowerCase())) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Reads the `githubPullRequests.enableAttestationCommits` setting. | ||
| * Returns `false` when the feature is disabled, otherwise the commit message | ||
| * that should be used for the attestation commit. | ||
| */ | ||
| export function getAttestationCommitSetting(): false | string { | ||
| const value = vscode.workspace | ||
| .getConfiguration(PR_SETTINGS_NAMESPACE) | ||
| .get<boolean | string>(ENABLE_ATTESTATION_COMMITS, false); | ||
|
|
||
| if (typeof value === 'string') { | ||
| const trimmed = value.trim(); | ||
| if (trimmed.length === 0) { | ||
| return false; | ||
| } | ||
| return trimmed; | ||
| } | ||
| return value === true ? DEFAULT_ATTESTATION_COMMIT_MESSAGE : false; | ||
| } | ||
|
|
||
| /** | ||
| * Whether the attestation commit feature is enabled by the user setting. | ||
| */ | ||
| export function isAttestationCommitsEnabled(): boolean { | ||
| return getAttestationCommitSetting() !== false; | ||
| } | ||
|
|
||
| /** | ||
| * Adds an empty, signed "attestation" commit to the head of the given pull request branch | ||
| * and pushes it to the corresponding remote. Requires that the pull request is currently | ||
| * checked out and that the user has commit signing configured. | ||
| * | ||
| * Returns the SHA of the new attestation commit when successful, otherwise `undefined`. | ||
| */ | ||
| export async function addAttestationCommit( | ||
| folderRepositoryManager: FolderRepositoryManager, | ||
| pullRequestModel: PullRequestModel, | ||
| ): Promise<string | undefined> { | ||
| const message = getAttestationCommitSetting(); | ||
| if (message === false) { | ||
| vscode.window.showWarningMessage(vscode.l10n.t('Attestation commits are not enabled. Enable them via the `githubPullRequests.enableAttestationCommits` setting.')); | ||
| return undefined; | ||
| } | ||
|
|
||
| const activePullRequest = folderRepositoryManager.activePullRequest; | ||
| if (!activePullRequest || !activePullRequest.equals(pullRequestModel)) { | ||
| vscode.window.showErrorMessage(vscode.l10n.t('The pull request must be checked out before an attestation commit can be added.')); | ||
| return undefined; | ||
| } | ||
|
|
||
| const repository = folderRepositoryManager.repository; | ||
| const head = repository.state.HEAD; | ||
| if (!head || !head.name) { | ||
| vscode.window.showErrorMessage(vscode.l10n.t('Unable to add an attestation commit: no branch is currently checked out.')); | ||
| return undefined; | ||
| } | ||
|
|
||
| if (!await hasCommitSigningConfigured(repository)) { | ||
| vscode.window.showErrorMessage(vscode.l10n.t('Unable to add an attestation commit: commit signing does not appear to be configured. Set `user.signingkey` (or enable `commit.gpgsign`) in your git config and ensure your signing tool (GPG, SSH, or X.509) is set up.')); | ||
| return undefined; | ||
| } | ||
|
|
||
| try { | ||
| Logger.appendLine(`Creating attestation commit on branch ${head.name} for PR #${pullRequestModel.number}`, LOG_ID); | ||
| await repository.commit(message, { | ||
| empty: true, | ||
| signCommit: true, | ||
| }); | ||
|
|
||
| const upstream = head.upstream; | ||
| if (upstream) { | ||
| await repository.push(upstream.remote, head.name); | ||
| } else { | ||
| await repository.push(); | ||
| } | ||
|
|
||
| return repository.state.HEAD?.commit; | ||
| } catch (e) { | ||
| Logger.error(`Failed to add attestation commit: ${formatError(e)}`, LOG_ID); | ||
| vscode.window.showErrorMessage(vscode.l10n.t('Failed to add attestation commit: {0}', formatError(e))); | ||
| return undefined; | ||
| } | ||
| } |
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.