1#!/bin/bash -e
2#
3# get-latest-series: Download latest patch series from Patchwork
4#
5# Copyright (C) 2023 BayLibre Inc.
6#
7# SPDX-License-Identifier: GPL-2.0-only
8#
9
10# the interval into the past which we want to check for new series, in minutes
11INTERVAL_MINUTES=30
12
13# Maximum number of series to retrieve. the Patchwork API can support up to 250
14# at once
15SERIES_LIMIT=250
16
17# Location to save patches
18DOWNLOAD_PATH="."
19
20# Name of the file to use/check as a log of previously-tested series IDs
21SERIES_TEST_LOG=".series_test.log"
22
23# Patchwork project to pull series patches from
24PROJECT="oe-core"
25
26# The Patchwork server to pull from
27SERVER="https://patchwork.yoctoproject.org/api/1.2/"
28
29help()
30{
31    echo "Usage: get-latest-series [ -i | --interval MINUTES ]
32        [ -d | --directory DIRECTORY ]
33        [ -l | --limit COUNT ]
34        [ -h | --help ]
35        [ -t | --tested-series LOGFILE]
36        [ -p | --project PROJECT ]
37        [ -s | --server SERVER ]"
38    exit 2
39}
40
41while [ "$1" != "" ]; do
42    case $1 in
43        -i|--interval)
44        INTERVAL_MINUTES=$2
45        shift 2
46        ;;
47    -l|--limit)
48        SERIES_LIMIT=$2
49        shift 2
50        ;;
51    -d|--directory)
52        DOWNLOAD_PATH=$2
53        shift 2
54        ;;
55    -p|--project)
56        PROJECT=$2
57        shift 2
58        ;;
59    -s|--server)
60        SERVER=$2
61        shift 2
62        ;;
63    -t|--tested-series)
64        SERIES_TEST_LOG=$2
65        shift 2
66        ;;
67    -h|--help)
68            help
69        ;;
70    *)
71        echo "Unknown option $1"
72        help
73        ;;
74    esac
75done
76
77# The time this script is running at
78START_TIME=$(date --date "now" +"%Y-%m-%dT%H:%M:%S")
79
80# the corresponding timestamp we want to check against for new patch series
81SERIES_CHECK_LIMIT=$(date --date "now - ${INTERVAL_MINUTES} minutes" +"%Y-%m-%dT%H:%M:%S")
82
83echo "Start time is $START_TIME"
84echo "Series check limit is $SERIES_CHECK_LIMIT"
85
86# Create DOWNLOAD_PATH if it doesn't exist
87if [ ! -d "$DOWNLOAD_PATH" ]; then
88    mkdir "${DOWNLOAD_PATH}"
89fi
90
91# Create SERIES_TEST_LOG if it doesn't exist
92if [ ! -f "$SERIES_TEST_LOG" ]; then
93    touch "${SERIES_TEST_LOG}"
94fi
95
96# Retrieve a list of series IDs from the 'git-pw series list' output. The API
97# supports a maximum of 250 results, so make sure we allow that when required
98SERIES_LIST=$(git-pw --project "${PROJECT}" --server "${SERVER}" series list --since "${SERIES_CHECK_LIMIT}" --limit "${SERIES_LIMIT}" | awk '{print $2}' | xargs | sed -e 's/[^0-9 ]//g')
99
100if [ -z "$SERIES_LIST" ]; then
101    echo "No new series for project ${PROJECT} since ${SERIES_CHECK_LIMIT}"
102    exit 0
103fi
104
105# Check each series ID
106for SERIES in $SERIES_LIST; do
107    # Download the series only if it's not found in the SERIES_TEST_LOG
108    if ! grep -w --quiet "${SERIES}" "${SERIES_TEST_LOG}"; then
109        echo "Downloading $SERIES..."
110        git-pw series download --separate "${SERIES}" "${DOWNLOAD_PATH}"
111        echo "${SERIES}" >> "${SERIES_TEST_LOG}"
112    else
113        echo "Already tested ${SERIES}. Skipping..."
114    fi
115done
116