-
Notifications
You must be signed in to change notification settings - Fork 176
feat: Show latest version of action in hover tooltip #584
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
ZiuChen
wants to merge
2
commits into
github:main
Choose a base branch
from
ZiuChen:feat/actions-version
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.
+251
−0
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| import * as vscode from "vscode"; | ||
|
|
||
| import {TTLCache} from "@actions/languageserver/utils/cache"; | ||
|
|
||
| import {getSession} from "../auth/auth"; | ||
| import {getClient} from "../api/api"; | ||
|
|
||
| const USES_PATTERN = /uses:\s*(['"]?)([^@\s'"]+)@([^\s'"#]+)/; | ||
| const CACHE_TTL_MS = 5 * 60 * 1000; | ||
|
|
||
| const cache = new TTLCache(CACHE_TTL_MS); | ||
|
|
||
|
ZiuChen marked this conversation as resolved.
Outdated
|
||
| interface ActionVersionInfo { | ||
| latest: string; | ||
| latestMajor?: string; | ||
| } | ||
|
|
||
| function parseUsesReference( | ||
| line: string | ||
| ): {owner: string; name: string; actionPath: string; currentRef: string; refStart: number; refEnd: number} | undefined { | ||
| const match = USES_PATTERN.exec(line); | ||
| if (!match) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const actionPath = match[2]; | ||
| const currentRef = match[3]; | ||
|
|
||
| const [owner, name] = actionPath.split("/"); | ||
| if (!owner || !name) { | ||
| return undefined; | ||
| } | ||
|
|
||
| // Find the position of the @ref part | ||
| const fullMatchStart = match.index + match[0].indexOf(match[2]); | ||
| const refStart = fullMatchStart + actionPath.length + 1; // +1 for @ | ||
| const refEnd = refStart + currentRef.length; | ||
|
|
||
| return {owner, name, actionPath, currentRef, refStart, refEnd}; | ||
| } | ||
|
|
||
| function extractMajorTag(tag: string): string | undefined { | ||
| const match = /^(v?\d+)[\.\d]*/.exec(tag); | ||
| return match ? match[1] : undefined; | ||
| } | ||
|
|
||
| async function fetchLatestVersion(owner: string, name: string): Promise<ActionVersionInfo | undefined> { | ||
| const session = await getSession(true); | ||
| if (!session) { | ||
| return undefined; | ||
| } | ||
|
|
||
|
ZiuChen marked this conversation as resolved.
Outdated
|
||
| const cacheKey = `action-latest-version:${owner}/${name}`; | ||
| return cache.get<ActionVersionInfo | undefined>(cacheKey, undefined, async () => { | ||
| const client = getClient(session.accessToken); | ||
|
|
||
| try { | ||
| const {data} = await client.repos.getLatestRelease({owner, repo: name}); | ||
| if (data.tag_name) { | ||
| const major = extractMajorTag(data.tag_name); | ||
| return {latest: data.tag_name, latestMajor: major}; | ||
| } | ||
| } catch { | ||
| // No release found | ||
| } | ||
|
|
||
| try { | ||
| const {data} = await client.repos.listTags({owner, repo: name, per_page: 10}); | ||
| if (data.length > 0) { | ||
| const semverTag = data.find(t => /^v?\d+\.\d+/.test(t.name)); | ||
| const tag = semverTag || data[0]; | ||
| const major = extractMajorTag(tag.name); | ||
|
ZiuChen marked this conversation as resolved.
Outdated
|
||
| return {latest: tag.name, latestMajor: major}; | ||
| } | ||
| } catch { | ||
| // Ignore | ||
| } | ||
|
|
||
| return undefined; | ||
| }); | ||
| } | ||
|
|
||
| export class ActionVersionCodeActionProvider implements vscode.CodeActionProvider { | ||
| static readonly providedCodeActionKinds = [vscode.CodeActionKind.QuickFix]; | ||
|
|
||
| async provideCodeActions( | ||
| document: vscode.TextDocument, | ||
| range: vscode.Range | vscode.Selection, | ||
| _context: vscode.CodeActionContext, | ||
| _token: vscode.CancellationToken | ||
| ): Promise<vscode.CodeAction[] | undefined> { | ||
| const actions: vscode.CodeAction[] = []; | ||
|
|
||
| for (let lineNum = range.start.line; lineNum <= range.end.line; lineNum++) { | ||
| const line = document.lineAt(lineNum).text; | ||
| const ref = parseUsesReference(line); | ||
| if (!ref) { | ||
| continue; | ||
| } | ||
|
|
||
|
ZiuChen marked this conversation as resolved.
Outdated
|
||
| const versionInfo = await fetchLatestVersion(ref.owner, ref.name); | ||
| if (!versionInfo) { | ||
| continue; | ||
| } | ||
|
|
||
| const isCurrentLatest = ref.currentRef === versionInfo.latest || ref.currentRef === versionInfo.latestMajor; | ||
|
|
||
| if (isCurrentLatest) { | ||
| continue; | ||
| } | ||
|
|
||
|
ZiuChen marked this conversation as resolved.
|
||
| const refRange = new vscode.Range(lineNum, ref.refStart, lineNum, ref.refEnd); | ||
|
|
||
| // Offer update to latest full version | ||
| const updateToLatest = new vscode.CodeAction( | ||
| `Update ${ref.actionPath} to ${versionInfo.latest}`, | ||
| vscode.CodeActionKind.QuickFix | ||
| ); | ||
| updateToLatest.edit = new vscode.WorkspaceEdit(); | ||
| updateToLatest.edit.replace(document.uri, refRange, versionInfo.latest); | ||
| updateToLatest.isPreferred = true; | ||
| actions.push(updateToLatest); | ||
|
|
||
| // Offer update to latest major version tag if different | ||
| if ( | ||
| versionInfo.latestMajor && | ||
| versionInfo.latestMajor !== versionInfo.latest && | ||
| versionInfo.latestMajor !== ref.currentRef | ||
| ) { | ||
| const updateToMajor = new vscode.CodeAction( | ||
| `Update ${ref.actionPath} to ${versionInfo.latestMajor}`, | ||
| vscode.CodeActionKind.QuickFix | ||
| ); | ||
| updateToMajor.edit = new vscode.WorkspaceEdit(); | ||
| updateToMajor.edit.replace(document.uri, refRange, versionInfo.latestMajor); | ||
| actions.push(updateToMajor); | ||
| } | ||
| } | ||
|
|
||
| return actions.length > 0 ? actions : undefined; | ||
| } | ||
| } | ||
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,128 @@ | ||
| import * as vscode from "vscode"; | ||
|
|
||
| import {TTLCache} from "@actions/languageserver/utils/cache"; | ||
|
|
||
| import {getSession} from "../auth/auth"; | ||
| import {getClient} from "../api/api"; | ||
|
|
||
| const USES_PATTERN = /uses:\s*(['"]?)([^@\s'"]+)@([^\s'"#]+)/; | ||
| const CACHE_TTL_MS = 5 * 60 * 1000; // 5 minutes | ||
|
|
||
|
ZiuChen marked this conversation as resolved.
Outdated
|
||
| const cache = new TTLCache(CACHE_TTL_MS); | ||
|
|
||
| interface ActionVersionInfo { | ||
| latest: string; | ||
| /** The latest major version tag, e.g. "v4" */ | ||
| latestMajor?: string; | ||
| } | ||
|
|
||
| /** | ||
| * Parses the `uses:` value from a workflow line and returns owner, name, and current ref. | ||
| */ | ||
| function parseUsesReference( | ||
| line: string | ||
| ): {owner: string; name: string; currentRef: string; valueStart: number; valueEnd: number} | undefined { | ||
| const match = USES_PATTERN.exec(line); | ||
| if (!match) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const actionPath = match[2]; // e.g. "actions/checkout" or "actions/cache/restore" | ||
| const currentRef = match[3]; | ||
|
|
||
| const [owner, name] = actionPath.split("/"); | ||
| if (!owner || !name) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const valueStart = match.index + match[0].indexOf(match[2]); | ||
| const valueEnd = valueStart + actionPath.length + 1 + currentRef.length; // +1 for @ | ||
|
|
||
| return {owner, name, currentRef, valueStart, valueEnd}; | ||
| } | ||
|
|
||
| async function fetchLatestVersion(owner: string, name: string): Promise<ActionVersionInfo | undefined> { | ||
| const session = await getSession(true); | ||
|
ZiuChen marked this conversation as resolved.
Outdated
|
||
| if (!session) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const cacheKey = `action-latest-version:${owner}/${name}`; | ||
| return cache.get<ActionVersionInfo | undefined>(cacheKey, undefined, async () => { | ||
| const client = getClient(session.accessToken); | ||
|
|
||
| // Try latest release first | ||
| try { | ||
| const {data} = await client.repos.getLatestRelease({owner, repo: name}); | ||
| if (data.tag_name) { | ||
| const major = extractMajorTag(data.tag_name); | ||
| return {latest: data.tag_name, latestMajor: major}; | ||
| } | ||
| } catch { | ||
| // No release found, fallback to tags | ||
| } | ||
|
|
||
| // Fallback: list tags and find latest semver | ||
| try { | ||
| const {data} = await client.repos.listTags({owner, repo: name, per_page: 10}); | ||
| if (data.length > 0) { | ||
| // Find the latest semver-like tag | ||
| const semverTag = data.find(t => /^v?\d+\.\d+/.test(t.name)); | ||
| const tag = semverTag || data[0]; | ||
| const major = extractMajorTag(tag.name); | ||
| return {latest: tag.name, latestMajor: major}; | ||
| } | ||
| } catch { | ||
| // Ignore | ||
| } | ||
|
|
||
| return undefined; | ||
| }); | ||
| } | ||
|
|
||
| function extractMajorTag(tag: string): string | undefined { | ||
| const match = /^(v?\d+)[\.\d]*/.exec(tag); | ||
| return match ? match[1] : undefined; | ||
| } | ||
|
|
||
| export class ActionVersionHoverProvider implements vscode.HoverProvider { | ||
| async provideHover( | ||
| document: vscode.TextDocument, | ||
| position: vscode.Position, | ||
| _token: vscode.CancellationToken | ||
| ): Promise<vscode.Hover | undefined> { | ||
|
ZiuChen marked this conversation as resolved.
|
||
| const line = document.lineAt(position).text; | ||
| const ref = parseUsesReference(line); | ||
| if (!ref) { | ||
| return undefined; | ||
| } | ||
|
|
||
| // Ensure cursor is within the action reference range | ||
| if (position.character < ref.valueStart || position.character > ref.valueEnd) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const versionInfo = await fetchLatestVersion(ref.owner, ref.name); | ||
| if (!versionInfo) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const md = new vscode.MarkdownString(); | ||
| md.isTrusted = true; | ||
|
ZiuChen marked this conversation as resolved.
Outdated
|
||
|
|
||
| const isCurrentLatest = ref.currentRef === versionInfo.latest || ref.currentRef === versionInfo.latestMajor; | ||
|
|
||
| if (isCurrentLatest) { | ||
| md.appendMarkdown(`**Latest version:** \`${versionInfo.latest}\` ✓`); | ||
| } else { | ||
| md.appendMarkdown(`**Latest version:** \`${versionInfo.latest}\``); | ||
| if (versionInfo.latestMajor && ref.currentRef !== versionInfo.latestMajor) { | ||
| md.appendMarkdown(` (major: \`${versionInfo.latestMajor}\`)`); | ||
| } | ||
| } | ||
|
|
||
| const range = new vscode.Range(position.line, ref.valueStart, position.line, ref.valueEnd); | ||
|
|
||
| return new vscode.Hover(md, range); | ||
| } | ||
| } | ||
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.