Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moving over from old repo #1

Merged
merged 1 commit into from Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions .gitignore
@@ -0,0 +1,8 @@
.mypy_cache
.venv

# Dev
.vscode
Test.md
.github/workflows
*.secrets
13 changes: 13 additions & 0 deletions Dockerfile
@@ -0,0 +1,13 @@
FROM python:3.9-alpine

WORKDIR /tmp
RUN pip install pipenv
COPY ./Pipfile* ./
RUN pipenv install --system --deploy


WORKDIR /action
COPY ./src .

ENTRYPOINT [ "python" ]
CMD [ "/action/main.py" ]
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Niccolo Borgioli

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
16 changes: 16 additions & 0 deletions Pipfile
@@ -0,0 +1,16 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
requests = "*"
markdown = "*"
py-gfm = "*"

[dev-packages]
autopep8 = "*"
mypy = "*"

[requires]
python_version = "3.9"
169 changes: 169 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 75 additions & 2 deletions README.md 100644 → 100755
@@ -1,2 +1,75 @@
# Github-action-confluence-sync
Github action for syncing files with confluence pages.
# Confluence Markdown Sync Action

This Github Action serves the purpose of copying the contents of a Markdown `.md` file to a Confluence Cloud Page.

## Getting Started

```yml
# .github/workflows/my-workflow.yml
on: [push]

jobs:
dev:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- uses: confluence-markdown-sync
with:
from: './README.md'
to: '123456' # The confluence page id where to write the output
cloud: <my-confluence-cloud-id>
user: <my.user@example.org>
token: <my-token>
```

## Authentication

Uses basic auth for the rest api.

- `cloud`: The ID can be found by looking at yout confluence domain: `https://xxx.atlassian.net/...`
- `user`: The user that generated the access token
- `token`: You can generate the token [here](https://id.atlassian.com/manage-profile/security/api-tokens). Link to [Docs](https://confluence.atlassian.com/cloud/api-tokens-938839638.html)

- `to`: The page ID can be found by simply navigating to the page where you want the content to be postet to and looke at the url. It will look something like this: `https://<cloud-id>.atlassian.net/wiki/spaces/<space>/pages/<page-id>/<title>`

### Using secrets

It's **higly reccomended** that you use secrets!

To use them you need them to specify them before in your repo. [Docs](https://docs.github.com/en/free-pro-team@latest/actions/reference/encrypted-secrets)

The you can use them in any input field.

```yml
# .github/workflows/my-workflow.yml
# ...
token: ${{ secrets.token }}
```

## Development

1. Clone the repo
2. Install [act](https://github.com/nektos/act)
3. Create the same config in the repo folder as in the getting started section above.
4. Change `uses: confluence-markdown-sync` -> `uses: ./`
5. Create an example markdown file `Some.md` and set it in the config `from: './Some.md'`
6. Run locally `act -b`

### With secrets

You can simply create a `.secrets` file and specify it to `act`

```
token=abc123
```

```yml
# .github/workflows/dev.yml
# ...
token: ${{ secrets.token }}
```

```bash
act -b --secret-file .secrets
```
25 changes: 25 additions & 0 deletions action.yml
@@ -0,0 +1,25 @@
# action.yml
name: 'confluence-markdown-sync'
description: 'Copy content of a markdown file to a confluence site'
branding:
icon: 'upload-cloud'
color: 'blue'
inputs:
from:
description: 'Path to the markdown file. Relative to root of repository'
required: true
to:
description: 'The page ID in confluence'
required: true
cloud:
description: 'Atlassian Cloud ID'
required: true
user:
description: 'Username of the token user'
required: true
token:
description: 'Token for the user'
required: true
runs:
using: 'docker'
image: 'Dockerfile'
44 changes: 44 additions & 0 deletions src/main.py
@@ -0,0 +1,44 @@
from typing import Dict, List
from os import listdir, environ
from os.path import join

import requests
from markdown import markdown
from mdx_gfm import GithubFlavoredMarkdownExtension

workspace = environ.get('GITHUB_WORKSPACE')
if not workspace:
raise Exception('No workspace is set')

envs: Dict[str, str] = {}
for key in ['from', 'to', 'cloud', 'user', 'token']:
value = environ.get(f'INPUT_{key.upper()}')
if not value:
raise Exception(f'Missing value for {key}')
envs[key] = value

with open(join(workspace, envs['from'])) as f:
md = f.read()

url = f"https://{envs['cloud']}.atlassian.net/wiki/rest/api/content/{envs['to']}"

current = requests.get(url, auth=(envs['user'], envs['token'])).json()

html = markdown(md, extensions=[GithubFlavoredMarkdownExtension()])
content = {
'id': current['id'],
'type': current['type'],
'title': current['title'],
'version': {'number': current['version']['number'] + 1},
'body': {
'editor': {
'value': html,
'representation': 'editor'
}
}
}

updated = requests.put(url, json=content, auth=(
envs['user'], envs['token'])).json()
link = updated['_links']['base'] + updated['_links']['webui']
print(f'Uploaded content successfully to page {link}')