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