• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16package gitee_common
17
18import (
19	"encoding/json"
20	"fmt"
21	"fotff/utils"
22	"github.com/sirupsen/logrus"
23	"io"
24	"net/http"
25	"os"
26	"path/filepath"
27	"time"
28)
29
30type DailyBuildsQueryParam struct {
31	ProjectName string `json:"projectName"`
32	Branch      string `json:"branch"`
33	Component   string `json:"component"`
34	BuildStatus string `json:"buildStatus"`
35	StartTime   string `json:"startTime"`
36	EndTime     string `json:"endTime"`
37	PageNum     int    `json:"pageNum"`
38	PageSize    int    `json:"pageSize"`
39}
40
41type DailyBuildsResp struct {
42	Result struct {
43		DailyBuildVos []*DailyBuild `json:"dailyBuildVos"`
44		Total         int           `json:"total"`
45	} `json:"result"`
46}
47
48type DailyBuild struct {
49	CurrentStatus   string `json:"currentStatus"`
50	BuildStartTime  string `json:"buildStartTime"`
51	BuildFailReason string `json:"buildFailReason"`
52	Id              string `json:"id"`
53	ObsPath         string `json:"obsPath"`
54	ImgObsPath      string `json:"imgObsPath"`
55}
56
57func (m *Manager) loopCI(param DailyBuildsQueryParam, cur string, getFn func(cur string, resp *DailyBuild) string) string {
58	for {
59		file := func() string {
60			data, err := json.Marshal(param)
61			if err != nil {
62				logrus.Errorf("can not marshal query param: %v", err)
63				return ""
64			}
65			resp, err := utils.DoSimpleHttpReq(http.MethodPost, "http://ci.openharmony.cn/api/ci-backend/ci-portal/v1/dailybuilds", data, map[string]string{"Content-Type": "application/json;charset=UTF-8"})
66			if err != nil {
67				logrus.Errorf("can not query builds: %v", err)
68				return ""
69			}
70			var dailyBuildsResp DailyBuildsResp
71			if err := json.Unmarshal(resp, &dailyBuildsResp); err != nil {
72				logrus.Errorf("can not unmarshal resp [%s]: %v", string(resp), err)
73				return ""
74			}
75			if len(dailyBuildsResp.Result.DailyBuildVos) == 0 {
76				return ""
77			}
78			if dailyBuildsResp.Result.DailyBuildVos[0].CurrentStatus != "end" {
79				return ""
80			}
81			return getFn(cur, dailyBuildsResp.Result.DailyBuildVos[0])
82		}()
83		if file != "" {
84			return file
85		}
86		time.Sleep(10 * time.Minute)
87	}
88}
89
90func (m *Manager) getNewerFromCI(cur string) string {
91	return m.loopCI(DailyBuildsQueryParam{
92		ProjectName: "openharmony",
93		Branch:      m.ManifestBranch,
94		Component:   m.Component,
95		BuildStatus: "success",
96		PageNum:     1,
97		PageSize:    1,
98	}, cur, m.getNewerDailyBuild)
99}
100
101func (m *Manager) getNewerDailyBuild(cur string, db *DailyBuild) string {
102	p := db.ImgObsPath
103	if p == "" {
104		p = db.ObsPath
105	}
106	if filepath.Base(p) == cur {
107		return ""
108	}
109	logrus.Infof("new package found, name: %s", filepath.Base(p))
110	file, err := m.downloadToWorkspace(p)
111	if err != nil {
112		logrus.Errorf("can not download package %s: %v", p, err)
113		return ""
114	}
115	return file
116}
117
118func (m *Manager) downloadToWorkspace(url string) (string, error) {
119	if _, err := parseTime(filepath.Base(url)); err != nil {
120		logrus.Errorf("can not get package time from %s, skipping", filepath.Base(url))
121		return "", fmt.Errorf("can not get package time from %s, skipping", filepath.Base(url))
122	}
123	logrus.Infof("downloading %s", url)
124	resp, err := utils.DoSimpleHttpReqRaw(http.MethodGet, url, nil, nil)
125	if err != nil {
126		return "", err
127	}
128	defer resp.Body.Close()
129	if err := os.MkdirAll(m.ArchiveDir, 0750); err != nil {
130		return "", err
131	}
132	f, err := os.Create(filepath.Join(m.ArchiveDir, filepath.Base(url)))
133	if err != nil {
134		return "", err
135	}
136	defer f.Close()
137	if _, err := io.CopyBuffer(f, resp.Body, make([]byte, 16*1024*1024)); err != nil {
138		return "", err
139	}
140	logrus.Infof("%s downloaded successfully", url)
141	return filepath.Base(url), nil
142}
143