• 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	"net/http"
24	"os"
25	"path/filepath"
26	"sort"
27	"time"
28)
29
30type TagQueryParam struct {
31	ProjectName  string `json:"projectName"`
32	Branch       string `json:"branch"`
33	ManifestFile string `json:"manifestFile"`
34	StartTime    string `json:"startTime"`
35	EndTime      string `json:"endTime"`
36	PageNum      int    `json:"pageNum"`
37	PageSize     int    `json:"pageSize"`
38}
39
40type TagResp struct {
41	Result struct {
42		TagList []*Tag `json:"tagList"`
43		Total   int    `json:"total"`
44	} `json:"result"`
45}
46
47type Tag struct {
48	Id         string   `json:"id"`
49	Issue      string   `json:"issue"`
50	PrList     []string `json:"prList"`
51	TagFileURL string   `json:"tagFileUrl"`
52	Timestamp  string   `json:"timestamp"`
53}
54
55func (m *Manager) stepsFromCI(from, to string) (pkgs []string, err error) {
56	startTime, err := parseTime(from)
57	if err != nil {
58		return nil, err
59	}
60	endTime, err := parseTime(to)
61	if err != nil {
62		return nil, err
63	}
64	return m.getAllStepsFromTags(startTime, endTime)
65}
66
67func (m *Manager) getAllStepsFromTags(from, to time.Time) (pkgs []string, err error) {
68	tags, err := m.getAllTags(from, to)
69	if err != nil {
70		return nil, err
71	}
72	for _, tag := range tags {
73		pkg, err := m.genTagPackage(tag)
74		if err != nil {
75			return nil, err
76		}
77		pkgs = append(pkgs, pkg)
78	}
79	return pkgs, nil
80}
81
82func (m *Manager) getAllTags(from, to time.Time) (ret []*Tag, err error) {
83	var deDup = make(map[string]*Tag)
84	var pageNum = 1
85	for {
86		var q = TagQueryParam{
87			ProjectName:  "openharmony",
88			Branch:       m.ManifestBranch,
89			ManifestFile: "default.xml",
90			StartTime:    from.Local().Format("2006-01-02"),
91			EndTime:      to.Local().Format("2006-01-02"),
92			PageNum:      pageNum,
93			PageSize:     10000,
94		}
95		data, err := json.Marshal(q)
96		if err != nil {
97			return nil, err
98		}
99		resp, err := utils.DoSimpleHttpReq(http.MethodPost, "http://ci.openharmony.cn/api/ci-backend/ci-portal/v1/build/tag", data, map[string]string{"Content-Type": "application/json;charset=UTF-8"})
100		if err != nil {
101			return nil, err
102		}
103		var tagResp TagResp
104		if err := json.Unmarshal(resp, &tagResp); err != nil {
105			return nil, err
106		}
107		for _, tag := range tagResp.Result.TagList {
108			if _, ok := deDup[tag.Id]; ok {
109				continue
110			}
111			deDup[tag.Id] = tag
112			date, err := time.ParseInLocation("2006-01-02 15:04:05", tag.Timestamp, time.Local)
113			if err != nil {
114				return nil, err
115			}
116			if date.After(from) && date.Before(to) {
117				ret = append(ret, tag)
118			}
119		}
120		if len(deDup) == tagResp.Result.Total {
121			break
122		}
123		pageNum++
124	}
125	sort.Slice(ret, func(i, j int) bool {
126		return ret[i].Timestamp < ret[j].Timestamp
127	})
128	return ret, nil
129}
130
131func (m *Manager) genTagPackage(tag *Tag) (pkg string, err error) {
132	defer func() {
133		logrus.Infof("package dir %s for tag %v generated", pkg, tag.TagFileURL)
134	}()
135	if err := os.MkdirAll(filepath.Join(m.Workspace, tag.Id), 0750); err != nil {
136		return "", err
137	}
138	var issues []string
139	if len(tag.Issue) == 0 {
140		issues = tag.PrList
141	} else {
142		issues = []string{tag.Issue}
143	}
144	if err := os.WriteFile(filepath.Join(m.Workspace, tag.Id, "__last_issue__"), []byte(fmt.Sprintf("%v", issues)), 0640); err != nil {
145		return "", err
146	}
147	resp, err := utils.DoSimpleHttpReq(http.MethodGet, tag.TagFileURL, nil, nil)
148	if err != nil {
149		return "", err
150	}
151	err = os.WriteFile(filepath.Join(m.Workspace, tag.Id, "manifest_tag.xml"), resp, 0640)
152	if err != nil {
153		return "", err
154	}
155	return tag.Id, nil
156}
157