|
| 1 | +name: Create verified commit |
| 2 | +description: Create a GitHub-verified commit from local workspace changes. |
| 3 | + |
| 4 | +inputs: |
| 5 | + base-ref: |
| 6 | + description: Branch name or refs/heads reference used as the commit parent and base tree. |
| 7 | + required: true |
| 8 | + commit-message: |
| 9 | + description: Commit message for the new commit. |
| 10 | + required: true |
| 11 | + |
| 12 | +outputs: |
| 13 | + has-changes: |
| 14 | + description: Whether there were local changes to commit. |
| 15 | + value: ${{ steps.create.outputs.has-changes }} |
| 16 | + commit-sha: |
| 17 | + description: SHA of the created commit. |
| 18 | + value: ${{ steps.create.outputs.commit-sha }} |
| 19 | + parent-sha: |
| 20 | + description: SHA of the parent commit used for the new commit. |
| 21 | + value: ${{ steps.create.outputs.parent-sha }} |
| 22 | + |
| 23 | +runs: |
| 24 | + using: composite |
| 25 | + steps: |
| 26 | + - id: create |
| 27 | + uses: actions/github-script@v7 |
| 28 | + env: |
| 29 | + BASE_REF: ${{ inputs.base-ref }} |
| 30 | + COMMIT_MESSAGE: ${{ inputs.commit-message }} |
| 31 | + with: |
| 32 | + script: | |
| 33 | + const fs = require('fs') |
| 34 | + const { execSync } = require('child_process') |
| 35 | +
|
| 36 | + const owner = context.repo.owner |
| 37 | + const repo = context.repo.repo |
| 38 | + const baseRefInput = process.env.BASE_REF |
| 39 | + const baseRef = baseRefInput.replace(/^refs\/heads\//, '') |
| 40 | + const commitMessage = process.env.COMMIT_MESSAGE |
| 41 | +
|
| 42 | + const statusLines = execSync('git status --porcelain=v1 --untracked-files=all', { |
| 43 | + encoding: 'utf-8' |
| 44 | + }) |
| 45 | + .split('\n') |
| 46 | + .filter(Boolean) |
| 47 | +
|
| 48 | + if (statusLines.length === 0) { |
| 49 | + core.info('No changes to commit.') |
| 50 | + core.setOutput('has-changes', 'false') |
| 51 | + return |
| 52 | + } |
| 53 | +
|
| 54 | + const baseRefResponse = await github.rest.git.getRef({ |
| 55 | + owner, |
| 56 | + repo, |
| 57 | + ref: `heads/${baseRef}` |
| 58 | + }) |
| 59 | +
|
| 60 | + const parentSha = baseRefResponse.data.object.sha |
| 61 | + const baseCommit = await github.rest.git.getCommit({ |
| 62 | + owner, |
| 63 | + repo, |
| 64 | + commit_sha: parentSha |
| 65 | + }) |
| 66 | +
|
| 67 | + async function createBlobEntry(filePath) { |
| 68 | + const content = fs.readFileSync(filePath, 'utf8') |
| 69 | + const blob = await github.rest.git.createBlob({ |
| 70 | + owner, |
| 71 | + repo, |
| 72 | + content, |
| 73 | + encoding: 'utf-8' |
| 74 | + }) |
| 75 | +
|
| 76 | + return { |
| 77 | + path: filePath, |
| 78 | + mode: '100644', |
| 79 | + type: 'blob', |
| 80 | + sha: blob.data.sha |
| 81 | + } |
| 82 | + } |
| 83 | +
|
| 84 | + const tree = [] |
| 85 | +
|
| 86 | + for (const line of statusLines) { |
| 87 | + const status = line.slice(0, 2) |
| 88 | + const rawPath = line.slice(3) |
| 89 | +
|
| 90 | + if (status.includes('R') && rawPath.includes(' -> ')) { |
| 91 | + const [oldPath, newPath] = rawPath.split(' -> ') |
| 92 | +
|
| 93 | + tree.push({ |
| 94 | + path: oldPath, |
| 95 | + mode: '100644', |
| 96 | + type: 'blob', |
| 97 | + sha: null |
| 98 | + }) |
| 99 | +
|
| 100 | + tree.push(await createBlobEntry(newPath)) |
| 101 | + continue |
| 102 | + } |
| 103 | +
|
| 104 | + if (status.includes('D')) { |
| 105 | + tree.push({ |
| 106 | + path: rawPath, |
| 107 | + mode: '100644', |
| 108 | + type: 'blob', |
| 109 | + sha: null |
| 110 | + }) |
| 111 | + continue |
| 112 | + } |
| 113 | +
|
| 114 | + tree.push(await createBlobEntry(rawPath)) |
| 115 | + } |
| 116 | +
|
| 117 | + const newTree = await github.rest.git.createTree({ |
| 118 | + owner, |
| 119 | + repo, |
| 120 | + base_tree: baseCommit.data.tree.sha, |
| 121 | + tree |
| 122 | + }) |
| 123 | +
|
| 124 | + const newCommit = await github.rest.git.createCommit({ |
| 125 | + owner, |
| 126 | + repo, |
| 127 | + message: commitMessage, |
| 128 | + tree: newTree.data.sha, |
| 129 | + parents: [parentSha] |
| 130 | + }) |
| 131 | +
|
| 132 | + core.setOutput('has-changes', 'true') |
| 133 | + core.setOutput('commit-sha', newCommit.data.sha) |
| 134 | + core.setOutput('parent-sha', parentSha) |
0 commit comments