Skip to content

Commit 685b37e

Browse files
committed
update readme and .gitignore to exclude ide config
1 parent 23856f2 commit 685b37e

File tree

3 files changed

+82
-50
lines changed

3 files changed

+82
-50
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,6 @@ dist
102102

103103
# TernJS port file
104104
.tern-port
105+
106+
# ide config
107+
.vscode

README.md

+69-39
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
# ukko
22

3-
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4-
### apply gmail labels using google script
3+
**manage your gmail using google apps script**
54

6-
[Ukko](https://en.wikipedia.org/wiki/Ukko) is a code name for a [google script](https://developers.google.com/apps-script) tool accessing gmail inbox threads, looping over them and assigning labels based on the email metadata. It's intended be setup via google drive and have time driven trigger.
5+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) ![node workflow](https://github.com/T0MASD/ukko/actions/workflows/node.js.yml/badge.svg)
6+
7+
8+
- [ukko](#ukko)
9+
- [Using on google drive](#using-on-google-drive)
10+
- [Setting up scheduled execution](#setting-up-scheduled-execution)
11+
- [Developing and testing locally](#developing-and-testing-locally)
12+
- [Install javascript dependencies](#install-javascript-dependencies)
13+
- [Run tests](#run-tests)
14+
- [Run lint](#run-lint)
15+
- [Run Ukko](#run-ukko)
16+
- [Extending label rules](#extending-label-rules)
17+
- [Contributions](#contributions)
18+
19+
[Ukko](https://en.wikipedia.org/wiki/Ukko) is a code name for a [google apps script](https://developers.google.com/apps-script) runs on google servers. It can get emails in your inbox or use search, looping over email threads and assigning labels based on the email metadata. It's intended be setup via google drive and have time driven trigger.
720

821
# Using on google drive
922
1. Access your [google drive](https://en.wikipedia.org/wiki/Ukko), click **New** and select ["Google Apps Script"](https://script.google.com/create).
10-
2. Name the project "Ukko" and paste the contents of ukko.js, exclude `import` and `export` statements those are used for testing Ukko locally.
23+
2. Name the project "Ukko" and paste the contents of [modules/ukko.js](https://github.com/T0MASD/ukko/blob/main/modules/ukko.js) , exclude `import` and `export` statements those are used for testing Ukko locally.
1124
3. Click "Run" icon at the top of the editor, you will be prompted to grant access to your gmail.
1225
4. If there are any messages in your inbox that have header "List-Id" it will create and assign labels.
1326

@@ -21,12 +34,12 @@
2134
- "lists/my-list/company"
2235
- Labelled threads are shown at all nesting levels.
2336

24-
**NOTE**: To automatically "archive" the thread after labels are applied, uncomment following line in ukko.js:
25-
```
37+
**NOTE**: To automatically "archive" the thread after labels are applied, uncomment following line in [modules/ukko.js](https://github.com/T0MASD/ukko/blob/main/modules/ukko.js) :
38+
```javascript
2639
if (labels.length) { inboxThread.moveToArchive() }
2740
```
28-
Expect similar output:
29-
```
41+
Expect similar output when executing the script in google apps script "execution log":
42+
```shell
3043
from:[email protected] labels:lists/planet-list
3144
from:[email protected] labels:lists/announce-list
3245
from:nothandled@domain labels:
@@ -38,11 +51,11 @@ from:nothandled@domain labels:
3851
3. Make sure "runUkko" is selected along with "Interval" at which Ukko runs.
3952
4. Click save and enjoy!
4053

41-
# Testing locally
42-
To try locally you will need [nodejs](https://nodejs.org/en/) and [npm](https://www.npmjs.com/) installed on your machine.
43-
## Install dependencies
44-
Pull dependencies (see package.json) locally to `node_modules` dir (feel free to delete it and run `npm install` any time):
45-
```
54+
# Developing and testing locally
55+
To try [ukko](https://github.com/T0MASD/ukko) locally you will need [nodejs](https://nodejs.org/en/) and [npm](https://www.npmjs.com/) installed on your machine.
56+
## Install javascript dependencies
57+
To pull javascirpt dependencies (see package.json) locally run `npm install`:
58+
```shell
4659
[tomas@dev ukko]$ npm install
4760
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/fsevents):
4861
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
@@ -57,73 +70,90 @@ found 0 vulnerabilities
5770

5871
## Run tests
5972
Mocha is used for testing, if you have installed [mocha](https://www.npmjs.com/package/mocha) globally you can call it directly with `mocha`, *OR* via `npm test`:
60-
```
61-
[tomas@dev ukko]$ npm test
73+
```shell
74+
[tomas@dev ukko]$ npm run test
6275

63-
> [email protected] test /home/tomas/ukko
76+
> [email protected] test /ukko
6477
> mocha
6578

79+
from:[email protected] labels:lists/announce-list/example
80+
from:[email protected] labels:lists/planet-list/example
6681

6782

68-
email filter tests
69-
get labels tests
70-
✓ should return list of labels for header named List-Id
71-
✓ should return list of labels for emails from @github.com
72-
run filter tests
73-
from:[email protected] labels:lists/planet-list
74-
from:[email protected] labels:lists/announce-list
75-
✓ output list of processed emails with labels applied
83+
email filter tests
84+
get regex tests
85+
✓ should return domain
86+
✓ should return username
87+
✓ should return jira project
88+
✓ should return email
89+
✓ should return list id
90+
get labels tests
91+
✓ should return list of labels for header named List-Id
92+
✓ should return list of labels for emails from @github.com
93+
✓ should return list of labels for emails from [email protected]
94+
✓ should return list of labels for emails from team
95+
✓ should return list of labels for emails from [email protected]
96+
✓ should return list of labels for emails for jira
97+
✓ should return list of labels for emails for jira project
98+
should return list of assigned labels
99+
✓ output list of processed emails with labels applied
100+
run filter tests
101+
from:[email protected] labels:lists/announce-list/example
102+
from:[email protected] labels:lists/planet-list/example
103+
✓ output list of processed emails with labels applied
76104

77105

78-
3 passing (17ms)
106+
14 passing (20ms)
79107
```
80108

81109
## Run lint
82110
Eslint is used for linting all js code, if you have installed [eslint](https://eslint.org/) globally you can call it directly with `eslint .` *OR* via `npm lint`:
83-
```
111+
```shell
84112
[tomas@dev ukko]$ npm run lint
85113

86-
> [email protected] lint /home/tomas/ukko
114+
> [email protected] lint /ukko
87115
> eslint .
88116
```
89117

90118
## Run Ukko
91119
To Ukko locally with mock data call `node .` or `npm start`
92-
```
120+
```shell
93121
[tomas@dev ukko]$ npm start
94122

95-
> [email protected] start /home/tomas/ukko
123+
> [email protected] start /ukko
96124
> node .
97125

98126
from:[email protected] labels:lists/planet-list
99127
from:[email protected] labels:lists/announce-list
100128
```
101129

102130
## Extending label rules
103-
Modify `getLabels` function of `modules/ukko.js` to add your own logic for setting up labels.
131+
Modify `getLabels` function of [modules/ukko.js](https://github.com/T0MASD/ukko/blob/main/modules/ukko.js) to add your own logic for setting up labels. Few example rules are included with ukko, feel free to change to suit your needs.
104132

105133
Create "catch all" filters for given domain:
106-
```
134+
```javascript
107135
// process @github.com
108-
if (message.getFrom().includes('@github.com')) {
136+
if (messageFrom.includes('@github.com')) {
109137
labels.push('github')
110138
}
111139
```
112140
Here's an sample to extract GitLab project name from header named `X-GitLab-Project` and assign label `gitlab/projectname`:
113-
```
141+
```javascript
114142
// process gitlab notifications
115143
if (message.getHeader('X-GitLab-Project')) {
116-
const listLabel = 'gitlab/' + message.getHeader('X-GitLab-Project')
117-
labels.push(listLabel)
144+
labels.push(`gitlab/${message.getHeader('X-GitLab-Project')}`)
118145
}
119146
```
120-
Here's a sample to assign label from the value of `List-Id` header:
121-
```
147+
Here's a sample to assign label from the value of `List-Id` header, in this example extra label is added where sender domain doesn't match `mydomain`:
148+
```javascript
122149
// process mailing lists
123150
if (message.getHeader('List-Id')) {
124151
// extract my-list from 'My List <my-list.example.com>'
125-
const listIDshort = message.getHeader('List-Id').match(/<([^.]+).+>/).pop()
126-
const listLabel = 'lists/' + listIDshort
152+
const listIDshort = getReMatch('listid', message.getHeader('List-Id'))
153+
let listLabel = 'lists/' + listIDshort
154+
if (messageFromDomain !== 'mydomain') {
155+
listLabel += `/${messageFromDomain}`
156+
}
127157
labels.push(listLabel)
128158
}
129159
```

test/ukko.test.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ describe('email filter tests', () => {
1414
expect(result).to.eql(expectResult)
1515
})
1616
it('should return username', () => {
17-
const from = 'Github <noreply@github.com>'
17+
const from = 'First Last <flast@example.com>'
1818
const result = getReMatch('username', from)
19-
const expectResult = 'noreply'
19+
const expectResult = 'flast'
2020
expect(result).to.eql(expectResult)
2121
})
2222
it('should return jira project', () => {
@@ -57,9 +57,9 @@ describe('email filter tests', () => {
5757
const expectResult = ['github']
5858
expect(result).to.eql(expectResult)
5959
})
60-
it('should return list of labels for emails from errata@redhat.com', () => {
60+
it('should return list of labels for emails from errata@security.com', () => {
6161
const headers = {
62-
From: 'Red Hat Errata Notifications <errata@redhat.com>'
62+
From: 'Errata <errata@security.com>'
6363
}
6464
const message = new GmailMessage(headers)
6565
const result = getLabels(message)
@@ -68,17 +68,17 @@ describe('email filter tests', () => {
6868
})
6969
it('should return list of labels for emails from team', () => {
7070
const headers = {
71-
From: 'First Last <flast@redhat.com>'
71+
From: 'First Last <flast@example.com>'
7272
}
7373
const message = new GmailMessage(headers)
7474
const result = getLabels(message)
7575
const expectResult = ['team/flast']
7676
expect(result).to.eql(expectResult)
7777
})
78-
it('should return list of labels for emails from some@434324325214.amazon.com', () => {
78+
it('should return list of labels for emails from some@subdomain.domain.com', () => {
7979
const headers = {
8080
81-
'List-Id': '<some.subdomain.amazon.com>'
81+
'List-Id': '<some.subdomain.domain.com>'
8282
}
8383
const message = new GmailMessage(headers)
8484
const result = getLabels(message)
@@ -87,8 +87,7 @@ describe('email filter tests', () => {
8787
})
8888
it('should return list of labels for emails for jira', () => {
8989
const headers = {
90-
91-
Subject: '[Red Hat JIRA] Subscription: Filter for KC-STI'
90+
9291
}
9392
const message = new GmailMessage(headers)
9493
const result = getLabels(message)
@@ -97,8 +96,8 @@ describe('email filter tests', () => {
9796
})
9897
it('should return list of labels for emails for jira project', () => {
9998
const headers = {
100-
From: 'issues@jboss.org',
101-
Subject: '[Red Hat JIRA] (proj-123)'
99+
From: 'issues@example.come',
100+
Subject: '[JIRA] (proj-123)'
102101
}
103102
const message = new GmailMessage(headers)
104103
const result = getLabels(message)

0 commit comments

Comments
 (0)