Skip to content

Commit a402a26

Browse files
authored
Merge pull request #13 from linyinfeng/commit-notifier-ng
Refactor
2 parents 066f1f8 + 25eb1d2 commit a402a26

32 files changed

+2699
-1855
lines changed

Cargo.lock

Lines changed: 30 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
[package]
22
name = "commit-notifier"
33
authors = [ "Lin Yinfeng <lin.yinfeng@outlook.com>" ]
4-
version = "0.1.3"
4+
version = "0.2.0"
55
edition = "2024"
66
description = """
7-
A simple bot tracking commits in branches
7+
A simple bot tracking git commits/PRs/issues/branches
88
"""
99

1010
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -22,12 +22,13 @@ thiserror = "*"
2222
clap = { version = "*", features = [ "cargo", "derive" ] }
2323
regex = "*"
2424
serde_json = "*"
25+
serde_regex = "*"
2526
serde = "*"
2627
cron = "*"
2728
chrono = "*"
2829
once_cell = "*"
2930
fs4 = "*"
3031
octocrab = "*"
3132
url = "*"
32-
scopeguard = "*"
3333
lockable = "*"
34+
version-compare = "*"

README.md

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ A simple telegram bot monitoring commit status.
2020
```console
2121
$ commit-notifier" \
2222
--working-dir /var/lib/commit-notifier \
23-
--cron "0 */5 * * * *"
23+
--cron "0 */5 * * * *" \
24+
--admin-chat-id="{YOUR_ADMIN_CHAT_ID}"
2425
```
2526

2627
Automatic check will be triggered based on the cron expression. In the example, `0 */5 * * * *` means "at every 5th minute". cron documentation: <https://docs.rs/cron/latest/cron>.
@@ -44,6 +45,7 @@ My instance: <https://github.com/linyinfeng/dotfiles/blob/main/nixos/profiles/se
4445
services.commit-notifier = {
4546
enable = true;
4647
cron = "0 */5 * * * *";
48+
adminChatId = "{YOUR_ADMIN_CHAT_ID}";
4749
tokenFiles = {
4850
telegramBot = /path/to/telegram/bot/token;
4951
github = /path/to/github/token;
@@ -71,7 +73,7 @@ The telegram bot has only one command `/notifier`. But this command provides a f
7173

7274
## Allow List
7375

74-
Since the bot can clone any git repository into its working directory, only manually allowed users/groups can access the bot.
76+
Currently the bot use `GITHUB_TOKEN` to check status for issues/pull requests, so only manually allowed users/groups can access the bot.
7577

7678
The bot in a new chat returns this kind of error:
7779

@@ -92,15 +94,77 @@ Currently, the bot does not have an admin interface in telegram. So adding chats
9294
* For direct chat:
9395

9496
```console
95-
$ cd {WORKING_DIR}
96-
$ mkdir 888888888 # chat id
97+
$ mkdir -p {WORKING_DIR}/chats/888888888 # chat id
9798
```
9899

99100
* For group chat
100101

101102
```console
102-
$ cd {WORKING_DIR}
103-
$ mkdir _1008888888888 # chat id (replace "-" with "_")
103+
$ mkdir -p {WORKING_DIR}/chats/_1008888888888 # chat id (replace "-" with "_")
104104
```
105105

106106
**Make sure the new directory is writable by `commit-notifier`.** All data (repositories, settings, check results) related to the chat will be saved in the directory.
107+
108+
## Migration Guide
109+
110+
### From `0.1.x` to `0.2.0`
111+
112+
There are several differences between `0.1.x` and `0.2.x`.
113+
114+
* In `0.1.x`, chats data are saved at `{WORKING_DIR}`; in `0.2.x`, chats data are saved in `{WORKING_DIR}/chats`.
115+
* In `0.1.x`, repositories and their settings are managed by every chat; in `0.2.x`, repositories and their settings are saved in `{WORKING_DIR}/repositories`, and can only managed by the admin chat. Also, in `0.2.x`, repositories are shared between all chats.
116+
* In `0.1.x`, caches are built in a per-commit manner; in `0.2.x`, caches are built in a per-branch manner, including every branch matches `--branch-regex`.
117+
118+
#### How to migrate
119+
120+
1. Backup old `{WORKING_DIR}` to `{BACKUP_DIR}`.
121+
2. Start the bot.
122+
3. Check old configurations in `{BACKUP_DIR}`, find all repositories.
123+
4. In admin chat, manually run `/notifier repo-add ...` for each repositories.
124+
5. Properly configure each repositories.
125+
126+
* Use `/notifier repo-edit ...` to set branch regex. Use `/notifier condition-add ...` to set conditions.
127+
128+
* Or just edit `repositories/{REPO_NAME}/settings.json` manually.
129+
130+
<details>
131+
<summary>An example configuration for nixpkgs</summary>
132+
133+
```json
134+
{
135+
"branch_regex": "^(master|nixos-unstable|nixpkgs-unstable|staging|release-\\d\\d\\.\\d\\d|nixos-\\d\\d\\.\\d\\d)$",
136+
"github_info": {
137+
"owner": "nixos",
138+
"repo": "nixpkgs"
139+
},
140+
"conditions": {
141+
"in-nixos-release": {
142+
"condition": {
143+
"InBranch": {
144+
"branch_regex": "^nixos-\\d\\d\\.\\d\\d$"
145+
}
146+
}
147+
},
148+
"in-nixos-unstable": {
149+
"condition": {
150+
"InBranch": {
151+
"branch_regex": "^nixos-unstable$"
152+
}
153+
}
154+
},
155+
"master-to-staging": {
156+
"condition": {
157+
"SuppressFromTo": {
158+
"from_regex": "main",
159+
"to_regex": "staging(-next)?"
160+
}
161+
}
162+
}
163+
}
164+
}
165+
```
166+
167+
</details>
168+
169+
6. Wait for the first update (first-time cache building can be slow). Restart the bot to trigger update immediately.
170+
7. Restore chat configurations. `rsync --recursive {BACKUP_DIR}/ {WORKING_DIR}/chats/ --exclude cache.sqlite --exclude lock --exclude repo --verbose` (trailing `/` is important.)

flake.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nixos/commit-notifier.nix

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ in {
2828
Update cron expression.
2929
'';
3030
};
31+
adminChatId = lib.mkOption {
32+
type = lib.types.str;
33+
description = ''
34+
Chat id of the admin chat.
35+
'';
36+
};
3137
tokenFiles = {
3238
telegramBot = lib.mkOption {
3339
type = lib.types.str;
@@ -61,7 +67,8 @@ in {
6167
6268
"${cfg.package}/bin/commit-notifier" \
6369
--working-dir /var/lib/commit-notifier \
64-
--cron "${cfg.cron}"
70+
--cron "${cfg.cron}" \
71+
--admin-chat-id="${cfg.adminChatId}"
6572
'';
6673

6774
path = [

src/cache.rs

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)