Skip to content

Commit 5a3a864

Browse files
committed
Add initial documentation
1 parent 5b7d58b commit 5a3a864

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed

README.md

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
# `@s0/ghcommit`
2+
3+
[![View on NPM](https://badgen.net/npm/v/@s0/ghcommit)](https://www.npmjs.com/package/@s0/ghcommit)
4+
5+
NPM / TypeScript package to commit changes GitHub repositories using the GraphQL API.
6+
7+
## Why?
8+
9+
If you or your organisation has strict requirements
10+
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.
11+
12+
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.
13+
14+
(And this also works with GitHub Apps too, including the GitHub Actions app).
15+
16+
![](docs/verified.png)
17+
18+
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.
19+
20+
## Usage
21+
22+
### Installation
23+
24+
Install using your favourite package manager:
25+
26+
```
27+
pnpm install @s0/ghcommit
28+
```
29+
30+
### Usage in github actions
31+
32+
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:
33+
34+
```ts
35+
import { getOctokit } from "@actions/github";
36+
37+
const octokit = getOctokit(process.env.GITHUB_TOKEN);
38+
```
39+
40+
### Importing specific modules
41+
42+
To allow for you to produce smaller bundle sizes, the functionality exposed in this package is grouped into specific modules that only import the packages required for their use. We recommend that you import from the specific modules rather than the root of the package.
43+
44+
## API
45+
46+
All the functions below accept a single object as its argument, and share the following base arguments:
47+
48+
<!-- TODO: point to some generated docs instead of including a code snippet -->
49+
50+
51+
```ts
52+
{
53+
octokit: GitHubClient;
54+
owner: string;
55+
repository: string;
56+
branch: string;
57+
/**
58+
* Push the commit even if the branch exists and does not match what was
59+
* specified as the base.
60+
*/
61+
force?: boolean;
62+
/**
63+
* The commit message
64+
*/
65+
message: CommitMessage;
66+
log?: Logger;
67+
}
68+
```
69+
70+
### `commitChangesFromRepo`
71+
72+
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.
73+
74+
In addition to `CommitFilesBasedArgs`, this function has the following arguments:
75+
76+
```ts
77+
{
78+
/**
79+
* The root of the repository.
80+
*
81+
* @default process.cwd()
82+
*/
83+
repoDirectory?: string;
84+
}
85+
```
86+
87+
Example:
88+
89+
```ts
90+
import { getOctokit } from "@actions/github";
91+
import { commitChangesFromRepo } from '@s0/ghcommit/git';
92+
93+
const octokit = getOctokit(process.env.GITHUB_TOKEN);
94+
95+
// Commit & push the files from the current directory
96+
// e.g. if you're just using @ations/checkout
97+
await commitChangesFromRepo({
98+
octokit,
99+
owner: 'my-org',
100+
repository: 'my-repo',
101+
branch: 'new-branch-to-create',
102+
message: {
103+
headline: '[chore] do something'
104+
}
105+
});
106+
107+
// Commit & push the files from ta specific directory
108+
// where we've cloned a repo, and made changes to files
109+
await commitChangesFromRepo({
110+
octokit,
111+
owner: 'my-org',
112+
repository: 'my-repo',
113+
branch: 'another-new-branch-to-create',
114+
message: {
115+
headline: '[chore] do something else'
116+
},
117+
repoDirectory: '/tmp/some-repo'
118+
});
119+
```
120+
121+
### `commitFilesFromDirectory`
122+
123+
This function will add or delete specific files from a repository's branch based on files found on the local filesystem. This is good to use when there are specific files that need to be updated on a branch, or if many changes may have been made locally, but only some files need to be pushed.
124+
125+
In addition to `CommitFilesBasedArgs`, this function has the following arguments:
126+
127+
```ts
128+
{
129+
/**
130+
* The directory to consider the root of the repository when calculating
131+
* file paths
132+
*/
133+
workingDirectory?: string;
134+
/**
135+
* The file paths, relative to {@link workingDirectory},
136+
* to add or delete from the branch on GitHub.
137+
*/
138+
fileChanges: {
139+
/** File paths, relative to {@link workingDirectory}, to remove from the repo. */
140+
additions?: string[];
141+
/** File paths, relative to the repository root, to remove from the repo. */
142+
deletions?: string[];
143+
};
144+
}
145+
```
146+
147+
Example:
148+
149+
```ts
150+
import { getOctokit } from "@actions/github";
151+
import { commitFilesFromDirectory } from '@s0/ghcommit/fs';
152+
153+
const octokit = getOctokit(process.env.GITHUB_TOKEN);
154+
155+
// Commit the changes to package.json and package-lock.json
156+
await commitFilesFromDirectory({
157+
octokit,
158+
owner: 'my-org',
159+
repository: 'my-repo',
160+
branch: 'new-branch-to-create',
161+
message: {
162+
headline: '[chore] do something'
163+
},
164+
base: {
165+
branch: "main",
166+
},
167+
workingDirectory: 'foo/bar',
168+
fileChanges: {
169+
additions: ['package-lock.json', 'package.json']
170+
}
171+
});
172+
```
173+
174+
175+
176+
### Pushing locally changed files
177+
178+
If you are working with a locally cloned GitHub repo, and want to push the changes to the files you have made locally (for example when running )
179+
180+
## Other Tools / Alternatives
181+
182+
- [planetscale/ghcommit](https://github.com/planetscale/ghcommit) - Go library for committing to GitHub using graphql
183+
- [planetscale/ghcommit-action](https://github.com/planetscale/ghcommit-action) - GitHub Action to detect file changes and commit using the above library

docs/verified.png

28 KB
Loading

0 commit comments

Comments
 (0)