aboutsummaryrefslogtreecommitdiff
path: root/rss_to_gitea/main.py
blob: 7de5106336b47059a72a0b98ff43339b042fcbb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import xml
import sys
import yaml
import os
import sys

from .gitea import GiteaAPI
from .atom import AtomFeed
from .config import Config

def print_help():
    print('''
USAGE:
    rsstogitea <config>
    ''')

def main():
    if len(sys.argv) <= 1:
        print_help()
        return 1

    config = Config(sys.argv[1])

    token = config.token
    if str.startswith(token, 'env/'):
        splt = token.split('/',1)
        token = os.environ[splt[1]]

    api = GiteaAPI(config.url, token)

    label_id = api.getLabelId(config.owner, config.repo, config.label)

    for feed in config.feeds:
        remote = AtomFeed(feed['url'])
        latest = remote.get_latest(feed['exclude'], feed['include'])

        issue_title = f'{feed["name"]}: {latest["title"]}'
        print("Title=", issue_title)

        ticket = api.getFirstExactIssue(config.owner, config.repo, issue_title, [config.label])
        if ticket is not None:
            print(f'{issue_title} already exists. Skipping')
            continue

        # should we maybe just rename here?
        prev_versions = api.getAllIssuesStartingWith(config.owner, config.repo, f'{feed["name"]}: ', [config.label], _state='open')
        for prev in prev_versions:
            print(f'{prev["title"]} (#{prev["number"]}) already exists. closing.')
            api.changeIssueState(config.owner, config.repo, prev['number'], 'closed')

        result = api.createIssue(config.owner, config.repo, issue_title, latest['link'], feed['assign'], [label_id])