UNPKG

2.59 kBJavaScriptView Raw
1import fetch from "node-fetch";
2
3import { PullRequest } from "repository-provider";
4import { join } from "./util.mjs";
5
6/**
7 *
8 */
9export class GiteaPullRequest extends PullRequest {
10 static get validStates() {
11 return new Set(["OPEN", "CLOSED"]);
12 }
13
14 /**
15 * List all pull request for a given repo
16 * result will be filtered by source branch, destination branch and states
17 * @param {Repository} respository
18 * @param {Object} filter
19 * @param {Branch?} filter.source
20 * @param {Branch?} filter.destination
21 * @param {Set<string>?} filter.states
22 * @return {Iterator<PullRequest>}
23 */
24 static async *list(respository, filter = {}) {
25 const provider = respository.provider;
26
27 let state = "all";
28
29 if (filter.states) {
30 for (const s of GiteaPullRequest.validStates)
31 if (filter.states.has(s)) {
32 state = s.toLocaleLowerCase();
33 break;
34 }
35 }
36
37 const result = await fetch(
38 join(provider.api, "repos", respository.fullName, `pulls?state=${state}`),
39 {
40 headers: provider.headers,
41 accept: "application/json"
42 }
43 );
44
45 const getBranch = async u =>
46 provider.branch([u.repo.full_name, u.ref].join("#"));
47
48 const json = await result.json();
49 //console.log("list pulls", json);
50 for (const p of json) {
51 const source = await getBranch(p.head);
52 if (filter.source && !source.equals(filter.source)) {
53 continue;
54 }
55
56 const destination = await getBranch(p.base);
57 if (filter.destination && !destination.equals(filter.destination)) {
58 continue;
59 }
60
61 yield new provider.pullRequestClass(source, destination, p.number, {
62 id: p.id,
63 title: p.title,
64 body: p.body,
65 state: p.state
66 });
67 }
68 }
69
70 static async open(source, destination, options) {
71 const provider = source.provider;
72
73 const data = {
74 base: source.name,
75 head: destination.name,
76 ...options
77 };
78
79 const result = await fetch(
80 join(provider.api, "repos", destination.repository.fullName, "pulls"),
81 {
82 method: "POST",
83 headers: {
84 "Content-Type": "application/json",
85 ...provider.headers
86 },
87 body: JSON.stringify(data)
88 }
89 );
90
91 // console.log(await result.text());
92
93 const json = await result.json();
94 console.log(json);
95
96 return new this(source, destination, json.number, {
97 body: json.body,
98 title: json.title,
99 state: json.state
100 });
101 }
102
103 async decline() {}
104
105 async _write() {}
106
107 async _merge(method) {}
108}