Skip to content

Commit

Permalink
added docker files
Browse files Browse the repository at this point in the history
  • Loading branch information
iolave committed Sep 2, 2024
1 parent 14b66dc commit 09a0d32
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 46 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [unreleased]
### Added
- Docker config files.
- Added `--transitions-to-wip` option to specify Jira cloud transitions required in order to transition a task to a "Dev in progress" state.
- Added `--transitions-to-done` option to specify Jira cloud transitions required in order to transition a task to a "Done" state.
- Added `--sleep-time` that enables new executions tiggered when the sleep time passes by.
Expand Down
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM oven/bun:alpine

ADD ./package.json ./bun.lockb .

RUN bun install

ADD ./entrypoint.sh .
ENTRYPOINT sh ./entrypoint.sh

ADD ./tsconfig.json ./index.ts .
ADD ./helpers helpers
ADD ./services services
ADD ./business-logic business-logic
ADD ./commander commander

120 changes: 74 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
# Sync Jira and GitHub projects
I was forced by my job to use Jira to keep track of tasks, but I kept forgetting to update status within Jira. Most of time im in GitHub and it's just easier to have issues and close them via PR's.

...so i built this tool for myself and hopefully, it will help you too.

> [!WARNING]
> All versions released prior to `v1.0.0` are to be considered [breaking changes](https://semver.org/#how-do-i-know-when-to-release-100) (I'll try my best to not push breaking changes btw).
To install dependencies:

```bash
bun install -g https://github.com/iolave/bun-jira-tickets-from-gh#latest
```

To run:

```bash
jira-tickets-from-gh --help
```

## Usage
For the cli to work properly, you will need a GitHub project with the following fields:
## Pre-requisites for running the CLI
### A GitHub project with required fields
Make sure you have a GitHub project with the following fields:

- `Title`: Title for the task.
- `Jira issue type`: choice field with available jira issue types.
Expand All @@ -24,59 +18,93 @@ For the cli to work properly, you will need a GitHub project with the following
- `Estimate`: Number field.
- `Repository`: Default field for repository info.

### Get a jira token
- Get a jira api token from your jira cloud account.
- Form a base64 encoded string (basic auth) out of your jira cloud account and the api token: `base64(EMAIL:API_TOKEN)`.

### Get the id of your github project
#### Organization projects
The cli is shipped with a utility that's going to help us search our GitHub project id.
```bash
jira-tickets-from-gh --gh-token=GH_TOKEN github-projects listOrganizationProjects --org=YOU_ORG
jira-tickets-from-gh --gh-token=GH_TOKEN github-projects listOrganization --org=YOU_ORG
```

#### User projects
NOT YET AVAIALBLE

### Help command
```bash
Usage: jira-tickets-from-gh [options] [command]

generate Jira tickets from github project
_NOT YET AVAIALBLE_

Options:
-V, --version output the version number
--gh-token <TOKEN> GitHub token
--jira-token <TOKEN> Jira token
-v --verbose verbose mode
-h, --help display help for command
### Get a Jira cloud token
- Get a jira api token from your jira cloud account.
- Form a base64 encoded string (basic auth) out of your jira cloud account and the api token: `base64(EMAIL:API_TOKEN)`.

Commands:
github-projects GitHub projects utilities
sync [options] sync GitHub project tickets with Jira
help [command] display help for command
### Environment variables
- `GITHUB_TOKEN`: Your GitHub token. If the project you're trying to sync is in an organization, make sure the token have access to it.
- `JIRA_TOKEN`: Basic authorization token, to form it `base64([JIRA_CLOUD_ACCOUNT]:[JIRA_API_KEY])`

## Environment variables
- `GITHUB_TOKEN`
- `JIRA_TOKEN`: Basic authorization token, to form it `base64([JIRA_ACCOUNT_ID]:[JIRA_API_KEY])`
## Using the CLI to sync projects
Install the CLI
```bash
bun install -g jira-tickets-from-gh
```

### Example sync command
_The `--jira-project-key` is the short name of the jira cloud project._
Use `jira-tickets-from-gh sync [Options]` command to sync a GitHub project with a Jira cloud project.

| Option | Required | Description |
|---------------------------------------------|----------|-------------|
|`--transitions-to-wip <NUMBER,...>` | `false` | list of jira issue transitions in order to have a wip task |
|`--transitions-to-done <NUMBER,...>` | `false` | list of jira issue transitions in order to have a done task |
|`--gh-assignees-map <GH_USER:JIRA_USER,...>` | `false` | map of GitHub users to Jira ones (email) |
|`--sleep-time <ms>` | `false` | sleep time between executions. If not specified the program will run once |
|`--gh-project-id <STRING>` | `true` | Github project ID |
|`--jira-project-key <STRING>` | `true` | Jira project KEY |
|`--jira-subdomain <STRING>` | `true` | Jira subdomain |
|`--help` | `false` | display help for command |

### Example
*Using environment variables*
```bash
export GITHUB_TOKEN=token
export JIRA_TOKEN=token
jira-tickets-from-gh sync \
--gh-project-id=PROJECT_ID \
--jira-project-key=PROJECT_KEY \
--jira-subdomain=MYSUBDOMAIN \
--gh-assignees-map=iolave:abc@abc.com
```

*Passing tokens via the cli*
```bash
jira-tickets-from-gh --gh-token=GH_TOKEN --jira-token=JIRA_TOKEN sync \
export GITHUB_TOKEN=token
export JIRA_TOKEN=token
jira-tickets-from-gh --gh-token=TOKEN --jira-token=TOKEN sync \
--gh-project-id=PROJECT_ID \
--jira-project-key=PROJECT_KEY \
--jira-subdomain=MYSUBDOMAIN \
--gh-assignees-map=iolave:my-private@email.com
--gh-assignees-map=iolave:abc@abc.com
```

*Execution example*
```
[2024-08-01 10:45:46][INFO] syncCmd.action creating jira issue {"title":"TEST: bun-jira-tickets-from-gh"}
[2024-08-01 10:45:47][INFO] syncCmd.action created issue {"url":"https://mfhnet.atlassian.net/browse/TEST3-112"}
```

## Running using Docker
### Environment variables
| Env | Maps to option |
|-----------------------|-------------------------|
| GITHUB_TOKEN | `--gh-token` |
| GH_PROJECT_ID | `--gh-project-id` |
| GH_USERS_MAP | `--gh-assignees-map` |
| JIRA_TOKEN | `--jira-token` |
| JIRA_SUBDOMAIN | `--jira-subdomain` |
| JIRA_PROJECT_KEY | `--jira-project-key` |
| JIRA_WIP_TRANSITIONS | `--transitions-to-wip` |
| JIRA_DONE_TRANSITIONS | `--transitions-to-done` |
| SLEEP_TIME | `--sleep-time` |

### Build
```bash
docker compose build
```

*execution example*
### Run
```bash
[INFO] syncCmd.action creating jira issue {"title":"TEST: bun-jira-tickets-from-gh"}
[INFO] syncCmd.action created issue {"url":"https://mfhnet.atlassian.net/browse/TEST3-100"}
[INFO] syncCmd.action updated jira url in github {"updateId":"0b2f5b21-d3b7-4580-9327-6a57fa1924db"}
docker compose --env-file=path/to/env up -d
```

This project was created using `bun init` in bun v1.1.24. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
39 changes: 39 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
services:
jira-tickets-from-gh:
build:
context: .
dockerfile: ./Dockerfile
network: host
image: jira-tickets-from-gh
# network mode should not be host, for some reason my docker containers
# dont have internat access :(
network_mode: host
deploy:
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
window: 120s
resources:
limits:
cpus: '0.5'
memory: 256M
environment:
GH_TOKEN: /run/secrets/github_token
GH_PROJECT_ID: ${GH_PROJECT_ID}
GH_USERS_MAP: ${GH_USERS_MAP}
JIRA_TOKEN: /run/secrets/jira_token
JIRA_SUBDOMAIN: ${JIRA_SUBDOMAIN}
JIRA_PROJECT_KEY: ${JIRA_PROJECT_KEY}
JIRA_WIP_TRANSITIONS: ${JIRA_WIP_TRANSITIONS}
JIRA_DONE_TRANSITIONS: ${JIRA_DONE_TRANSITIONS}
SLEEP_TIME: ${SLEEP_TIME}
secrets:
- jira_token
- github_token
secrets:
jira_token:
environment: "JIRA_TOKEN"
github_token:
environment: "GITHUB_TOKEN"

21 changes: 21 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Replace docker secretes for it's real value
egrep_res=$(egrep -v '^#' /run/secrets/* | xargs)
for to_change in $egrep_res; do
secret_key=$(echo "${to_change}" | awk -F: '{print $1}')
secret=$(echo "${to_change}" | awk -F: '{print $2}')
env_res=$(env | grep -Eo "[A-Za-z_]+\=${secret_key}")
env_key=$(echo "${env_res}" | awk -F= '{print $1}')

eval "export ${env_key}=${secret}"
done

bun run index.ts sync \
--gh-token=${GH_TOKEN} \
--gh-project-id=${GH_PROJECT_ID} \
--gh-assignees-map=${GH_USERS_MAP} \
--jira-token=${JIRA_TOKEN} \
--jira-subdomain=${JIRA_SUBDOMAIN} \
--jira-project-key=${JIRA_PROJECT_KEY} \
--transitions-to-wip=${JIRA_WIP_TRANSITIONS} \
--transitions-to-done=${JIRA_DONE_TRANSITIONS} \
--sleep-time=${SLEEP_TIME}

0 comments on commit 09a0d32

Please sign in to comment.