updater.sh 2.1 KB
Newer Older
Snehit Sah's avatar
Snehit Sah committed
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash

######################################################
###                    VARIABLES
######################################################

API_LINK="https://gitlab.com/api/v4/"
COMMIT_MSG_PREFIX="Update modules"


######################################################
###                    FUNCTIONS
######################################################

UPDATES=""
update () {
    temp=`/app/flatpak-external-data-checker --verbose --edit-only "$1" \
        | grep "CHANGE SOON:\|BROKEN:" \
        | cut -d ":" -f 2`
    if [[ ! -z "$temp" ]]; then
    UPDATES+=`echo $temp \
            | sed "s/ /\\\n/g"`
    UPDATES+="\n"
    fi
}

update_loop () {
    for f in *.json; do
        update "$f"
    done
}


push_updates () {
    COMMIT_DESC="$UPDATES"
    UPDATES=$(printf "%s " `tr ' ' '\n' <<< "$UPDATES" | sort -u -f`)
    if [[ `git status --porcelain` ]]; then
        HASH=$(echo -e "$UPDATES" | sha256sum | cut -c1-8 )
        if (curl "${API_LINK}merge_requests?state=opened&search=$HASH&private_token=$TOKEN" | grep -q "$COMMIT_MSG_PREFIX $HASH") ; then
            echo "Found existing pull request"
        else
            LAST_COMMIT=`git log --oneline -n1 | cut -d ' ' -f1-1`
            git checkout -b update-$LAST_COMMIT-$HASH
            git add -u
            echo -e "$COMMIT_MSG_PREFIX $HASH\n\n$COMMIT_DESC" | git commit --file -
            MR_DESC=`printf "%s" "$UPDATES" | sed "s/ /<br>/g"`
            MR_DESC=`printf "%s" "$MR_DESC" | sed "s/\\\\\n/<br>/g"`
            printf "%s" "$MR_DESC"
            git push http://root:$TOKEN@$CI_SERVER_HOST/$CI_PROJECT_PATH.git \
                HEAD:update-$LAST_COMMIT-$HASH \
                -o merge_request.create \
                -o merge_request.title="$COMMIT_MSG_PREFIX $HASH" \
                -o merge_request.description="$MR_DESC"
        fi
    else
        echo "No change"
    fi
}


######################################################
###                    ENTRYPOINT
######################################################

if [ $# -eq 0 ]; then
    update_loop
    push_updates
elif [ "$1" -eq "file" ]; then
    update "$1"
fi