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
17
18import (
19	"encoding/json"
20	"fmt"
21	"fotff/utils"
22	"net/http"
23	"time"
24)
25
26type CompareParam struct {
27	Head  string
28	Base  string
29	Repo  string
30	Owner string
31}
32
33type CompareResp struct {
34	Commits []*Commit `json:"commits"`
35}
36
37type Commit struct {
38	CommitExtend `json:"-"`
39	URL          string `json:"url"`
40	SHA          string `json:"sha"`
41	Commit       struct {
42		Committer struct {
43			Date string `json:"date"`
44		} `json:"committer"`
45		Message string `json:"message"`
46	} `json:"commit"`
47	Parents []struct {
48		SHA string `json:"sha"`
49		URL string `json:"url"`
50	} `json:"parents"`
51	Files []struct {
52		Filename string `json:"filename"`
53		Status   string `json:"status"`
54		Patch    string `json:"patch,omitempty"`
55	} `json:"files,omitempty"`
56}
57
58type CommitExtend struct {
59	Owner string
60	Repo  string
61}
62
63func GetLatestMRBefore(owner, repo, branch string, before string) (ret *Commit, err error) {
64	branchResp, err := GetBranch(owner, repo, branch)
65	if err != nil {
66		return nil, err
67	}
68	head := branchResp.Commit
69	head.Owner = owner
70	head.Repo = repo
71	for head.Commit.Committer.Date > before {
72		if head, err = GetCommit(owner, repo, head.Parents[0].SHA); err != nil {
73			return nil, err
74		}
75	}
76	return head, nil
77}
78
79func GetBetweenTimeMRs(owner, repo, branch string, from, to time.Time) (ret []*Commit, err error) {
80	branchResp, err := GetBranch(owner, repo, branch)
81	if err != nil {
82		return nil, err
83	}
84	fromStr := from.UTC().Format(time.RFC3339)
85	toStr := to.UTC().Format(time.RFC3339)
86	head := branchResp.Commit
87	head.Owner = owner
88	head.Repo = repo
89	for head.Commit.Committer.Date > fromStr {
90		if head.Commit.Committer.Date < toStr {
91			ret = append(ret, head)
92		}
93		if head, err = GetCommit(owner, repo, head.Parents[0].SHA); err != nil {
94			return nil, err
95		}
96	}
97	return ret, nil
98}
99
100func GetBetweenMRs(param CompareParam) ([]*Commit, error) {
101	commits, err := GetBetweenCommits(param)
102	if err != nil {
103		return nil, err
104	}
105	var ret []*Commit
106	head := param.Head
107	for head != param.Base {
108		for _, commit := range commits {
109			if commit.SHA != head {
110				continue
111			}
112			commit.Owner = param.Owner
113			commit.Repo = param.Repo
114			ret = append(ret, commit)
115			head = commit.Parents[0].SHA
116		}
117	}
118	return ret, nil
119}
120
121func GetBetweenCommits(param CompareParam) ([]*Commit, error) {
122	url := fmt.Sprintf("https://gitee.com/api/v5/repos/%s/%s/compare/%s...%s", param.Owner, param.Repo, param.Base, param.Head)
123	var resp []byte
124	if c, found := utils.CacheGet("gitee", url); found {
125		resp = c.([]byte)
126	} else {
127		var err error
128		resp, err = utils.DoSimpleHttpReq(http.MethodGet, url, nil, nil)
129		if err != nil {
130			return nil, err
131		}
132		utils.CacheSet("gitee", url, resp)
133	}
134	var compareResp CompareResp
135	if err := json.Unmarshal(resp, &compareResp); err != nil {
136		return nil, err
137	}
138	return compareResp.Commits, nil
139}
140