|
| 1 | +import * as vscode from "vscode"; |
| 2 | + |
| 3 | +import {TTLCache} from "@actions/languageserver/utils/cache"; |
| 4 | + |
| 5 | +import {getSession} from "../auth/auth"; |
| 6 | +import {getClient} from "../api/api"; |
| 7 | + |
| 8 | +const USES_PATTERN = /uses:\s*(['"]?)([^@\s'"]+)@([^\s'"#]+)/; |
| 9 | +const CACHE_TTL_MS = 5 * 60 * 1000; |
| 10 | + |
| 11 | +const cache = new TTLCache(CACHE_TTL_MS); |
| 12 | + |
| 13 | +interface ActionVersionInfo { |
| 14 | + latest: string; |
| 15 | + latestMajor?: string; |
| 16 | +} |
| 17 | + |
| 18 | +function parseUsesReference( |
| 19 | + line: string |
| 20 | +): {owner: string; name: string; actionPath: string; currentRef: string; refStart: number; refEnd: number} | undefined { |
| 21 | + const match = USES_PATTERN.exec(line); |
| 22 | + if (!match) { |
| 23 | + return undefined; |
| 24 | + } |
| 25 | + |
| 26 | + const actionPath = match[2]; |
| 27 | + const currentRef = match[3]; |
| 28 | + |
| 29 | + const [owner, name] = actionPath.split("/"); |
| 30 | + if (!owner || !name) { |
| 31 | + return undefined; |
| 32 | + } |
| 33 | + |
| 34 | + // Find the position of the @ref part |
| 35 | + const fullMatchStart = match.index + match[0].indexOf(match[2]); |
| 36 | + const refStart = fullMatchStart + actionPath.length + 1; // +1 for @ |
| 37 | + const refEnd = refStart + currentRef.length; |
| 38 | + |
| 39 | + return {owner, name, actionPath, currentRef, refStart, refEnd}; |
| 40 | +} |
| 41 | + |
| 42 | +function extractMajorTag(tag: string): string | undefined { |
| 43 | + const match = /^(v?\d+)[\.\d]*/.exec(tag); |
| 44 | + return match ? match[1] : undefined; |
| 45 | +} |
| 46 | + |
| 47 | +async function fetchLatestVersion(owner: string, name: string): Promise<ActionVersionInfo | undefined> { |
| 48 | + const session = await getSession(true); |
| 49 | + if (!session) { |
| 50 | + return undefined; |
| 51 | + } |
| 52 | + |
| 53 | + const cacheKey = `action-latest-version:${owner}/${name}`; |
| 54 | + return cache.get<ActionVersionInfo | undefined>(cacheKey, undefined, async () => { |
| 55 | + const client = getClient(session.accessToken); |
| 56 | + |
| 57 | + try { |
| 58 | + const {data} = await client.repos.getLatestRelease({owner, repo: name}); |
| 59 | + if (data.tag_name) { |
| 60 | + const major = extractMajorTag(data.tag_name); |
| 61 | + return {latest: data.tag_name, latestMajor: major}; |
| 62 | + } |
| 63 | + } catch { |
| 64 | + // No release found |
| 65 | + } |
| 66 | + |
| 67 | + try { |
| 68 | + const {data} = await client.repos.listTags({owner, repo: name, per_page: 10}); |
| 69 | + if (data.length > 0) { |
| 70 | + const semverTag = data.find(t => /^v?\d+\.\d+/.test(t.name)); |
| 71 | + const tag = semverTag || data[0]; |
| 72 | + const major = extractMajorTag(tag.name); |
| 73 | + return {latest: tag.name, latestMajor: major}; |
| 74 | + } |
| 75 | + } catch { |
| 76 | + // Ignore |
| 77 | + } |
| 78 | + |
| 79 | + return undefined; |
| 80 | + }); |
| 81 | +} |
| 82 | + |
| 83 | +export class ActionVersionCodeActionProvider implements vscode.CodeActionProvider { |
| 84 | + static readonly providedCodeActionKinds = [vscode.CodeActionKind.QuickFix]; |
| 85 | + |
| 86 | + async provideCodeActions( |
| 87 | + document: vscode.TextDocument, |
| 88 | + range: vscode.Range | vscode.Selection, |
| 89 | + _context: vscode.CodeActionContext, |
| 90 | + _token: vscode.CancellationToken |
| 91 | + ): Promise<vscode.CodeAction[] | undefined> { |
| 92 | + const actions: vscode.CodeAction[] = []; |
| 93 | + |
| 94 | + for (let lineNum = range.start.line; lineNum <= range.end.line; lineNum++) { |
| 95 | + const line = document.lineAt(lineNum).text; |
| 96 | + const ref = parseUsesReference(line); |
| 97 | + if (!ref) { |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + const versionInfo = await fetchLatestVersion(ref.owner, ref.name); |
| 102 | + if (!versionInfo) { |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + const isCurrentLatest = ref.currentRef === versionInfo.latest || ref.currentRef === versionInfo.latestMajor; |
| 107 | + |
| 108 | + if (isCurrentLatest) { |
| 109 | + continue; |
| 110 | + } |
| 111 | + |
| 112 | + const refRange = new vscode.Range(lineNum, ref.refStart, lineNum, ref.refEnd); |
| 113 | + |
| 114 | + // Offer update to latest full version |
| 115 | + const updateToLatest = new vscode.CodeAction( |
| 116 | + `Update ${ref.actionPath} to ${versionInfo.latest}`, |
| 117 | + vscode.CodeActionKind.QuickFix |
| 118 | + ); |
| 119 | + updateToLatest.edit = new vscode.WorkspaceEdit(); |
| 120 | + updateToLatest.edit.replace(document.uri, refRange, versionInfo.latest); |
| 121 | + updateToLatest.isPreferred = true; |
| 122 | + actions.push(updateToLatest); |
| 123 | + |
| 124 | + // Offer update to latest major version tag if different |
| 125 | + if ( |
| 126 | + versionInfo.latestMajor && |
| 127 | + versionInfo.latestMajor !== versionInfo.latest && |
| 128 | + versionInfo.latestMajor !== ref.currentRef |
| 129 | + ) { |
| 130 | + const updateToMajor = new vscode.CodeAction( |
| 131 | + `Update ${ref.actionPath} to ${versionInfo.latestMajor}`, |
| 132 | + vscode.CodeActionKind.QuickFix |
| 133 | + ); |
| 134 | + updateToMajor.edit = new vscode.WorkspaceEdit(); |
| 135 | + updateToMajor.edit.replace(document.uri, refRange, versionInfo.latestMajor); |
| 136 | + actions.push(updateToMajor); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return actions.length > 0 ? actions : undefined; |
| 141 | + } |
| 142 | +} |
0 commit comments