You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> This is the development branch for `@changesets/ghcommit` v3. For the v2 code, check out the [maintenance/v2](https://github.com/changesets/ghcommit/tree/maintenance/v2) branch.
5
5
6
-
[](https://www.npmjs.com/package/@changesets/ghcommit)
6
+
[](https://npmx.dev/package/@changesets/ghcommit)
NPM / TypeScript package to commit changes GitHub repositories using the GraphQL API.
9
+
Commit file changes to GitHub repositories with the GitHub API.
9
10
10
11
## Why?
11
12
12
13
-**Simplified GPG Signing:**
13
14
14
-
If you or your organisation has strict requirements
15
-
around requiring signed commits (i.e. via Branch Protection or Repo Rulesets), then this can make integrating CI workflows or applications that are designed to make changes to your repos quite difficult. This is because you will need to manage your own GPG keys, assign them to machine accounts (which also means it doesn't work with GitHub Apps), and securely manage and rotate them.
15
+
If you or your organization has strict requirements around requiring signed commits (i.e. via Branch Protection or Repo Rulesets), then this can make integrating CI workflows or applications that are designed to make changes to your repos quite difficult. This is because you will need to manage your own GPG keys, assign them to machine accounts (which also means it doesn't work with GitHub Apps), and securely manage and rotate them.
16
16
17
17
Instead of doing this, if you use the GitHub API to make changes to files (such as what happens when making changes to files directly in the web UI), then GitHub's internal GPG key is used, and commits are all signed and associated with the user of the access token that was used.
18
18
19
-
(And this also works with GitHub Apps too, including the GitHub Actions app).
This library has primarily been designed for use in custom Node GitHub Actions, but can be used in any Node.js or JavaScript project that needs to directly modify files in GitHub repositories.
23
+
This library has primarily been designed for use in GitHub Actions, but can be used in any Node.js or JavaScript project that needs to directly modify files in GitHub repositories.
24
24
25
25
-**Simplified Git Config:**
26
26
@@ -32,15 +32,17 @@ NPM / TypeScript package to commit changes GitHub repositories using the GraphQL
32
32
33
33
### Installation
34
34
35
-
Install using your favourite package manager:
35
+
Install using your favorite package manager:
36
36
37
-
```
37
+
```bash
38
38
pnpm install @changesets/ghcommit
39
39
```
40
40
41
-
### Usage in github actions
41
+
### Octokit
42
+
43
+
The library requires an [Octokit](https://github.com/octokit/octokit.js) client to call the GitHub APIs. It requires the `@octokit/core`, `@octokit/plugin-rest-endpoint-methods`, and `@octokit/plugin-paginate-rest` packages to be set up in order to work.
42
44
43
-
All functions in this library that interact with the GitHub API require an octokit client that can execute GraphQL. If you are writing code that is designed to be run from within a GitHub Action, this can be done using the `@actions/github` library:
45
+
If you're running this in GitHub Actions, this can be done using the `@actions/github` library:
This function will take an existing repository on your filesystem (defaulting to the current working directory). This function is good to use if you're usually working within the context of a git repository, such as after running `@actions/checkout` in github actions.
136
-
137
-
In addition to `CommitFilesBasedArgs`, this function has the following arguments:
97
+
Commit file changes since a local git base (defaults to HEAD). This executes the `git` command to determine the changes and then uses `commitChanges` to commit them to the given branch.
138
98
139
-
```ts
140
-
{
141
-
/**
142
-
* The directory used to find the repository root,
143
-
* and search for changed files to commit.
144
-
*
145
-
* Any files that have been changed outside of this directory will be ignored.
146
-
*/
147
-
cwd: string;
148
-
/**
149
-
* The base commit to build your changes on-top of
150
-
*
151
-
* @defaultHEAD
152
-
*/
153
-
base?: {
154
-
commit: string;
155
-
};
156
-
/**
157
-
* Don't require {@linkcwd} to be the root of the repository,
158
-
* and use it as a starting point to recursively search for the `.git`
159
-
* directory in parent directories.
160
-
*
161
-
* @defaulttrue
162
-
*/
163
-
recursivelyFindRoot?:boolean;
164
-
/**
165
-
* An optional function that can be used to filter which files are included
166
-
* in the commit. True should be returned for files that should be included.
167
-
*
168
-
* By default, all files are included.
169
-
*/
170
-
filterFiles?: (file:string) =>boolean;
171
-
}
172
-
```
99
+
The default HEAD base includes all uncommitted changes since the last commit. If previous commits have been made locally and not pushed, you need to set `base` to the last commit that is known to be in the remote repository.
// Commit & push the files from the current directory
183
-
// e.g. if you're just using @ations/checkout
184
-
awaitcommitChangesFromRepo({
111
+
// Commit the file changes from the current directory
112
+
awaitcommitChangesSinceBase({
185
113
octokit,
186
-
...context.repo,
114
+
owner,
115
+
repo,
187
116
branch: "new-branch-to-create",
188
117
message: "[chore] do something",
189
118
cwd: process.cwd(),
190
119
});
191
120
192
-
// Commit & push the files from a specific directory
193
-
// where we've cloned a repo, and made changes to files
194
-
awaitcommitChangesFromRepo({
121
+
// Commit the file changes from a different directory (and git repo)
122
+
awaitcommitChangesSinceBase({
195
123
octokit,
196
124
owner: "my-org",
197
-
repository: "my-repo",
198
-
branch: "another-new-branch-to-create",
199
-
message: "[chore] do something else\n\nsome more details",
125
+
repo: "my-repo",
126
+
branch: "new-branch-to-create",
127
+
message: "[chore] do something else",
200
128
cwd: "/tmp/some-repo",
201
129
});
202
130
203
-
// Commit & push the files from the current directory,
204
-
// but ensure changes from any locally-made commits are also included
205
-
awaitcommitChangesFromRepo({
131
+
// Commit the file changes including commits added since the workflow run
132
+
awaitcommitChangesSinceBase({
206
133
octokit,
207
-
...context.repo,
208
-
branch: "another-new-branch-to-create",
209
-
message: {
210
-
headline: "[chore] do something else",
211
-
body: "some more details",
212
-
},
134
+
owner,
135
+
repo,
136
+
branch: "new-branch-to-create",
137
+
message: "[chore] do something else",
213
138
cwd: process.cwd(),
214
139
base: {
215
-
// This will be the original sha from the workflow run,
216
-
// even if we've made commits locally
217
-
commit: context.sha,
140
+
commit: context.sha, // The initial SHA of the workflow run
218
141
},
219
142
});
220
143
```
221
144
222
145
## Known Limitations
223
146
224
-
Due to using the GitHub API to make changes to repository contents,
225
-
there are some things it's not possible to commit,
226
-
and where using the Git CLI is still required.
227
-
228
-
- Executable files
229
-
- Symbolic Links
230
-
- Submodule changes
147
+
The GitHub API does not allow committing executable files, symbolic links, or submodule changes. If you need to commit any of these types of files, you will need to use the Git CLI instead.
0 commit comments