-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathref.ts
76 lines (69 loc) · 1.96 KB
/
ref.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import Resource from "./resource";
import { Repo } from "./repo";
export class Ref extends Resource {
prefix: string;
name: string;
commitOid: string;
treeOid: string;
constructor(readonly repo: Repo, readonly ref: string) {
super();
}
async save(): Promise<void> {
throw new Error("Not implemented");
}
get fullyQualifiedName(): string {
return this.prefix + this.name;
}
async load(): Promise<void> {
type ResponseShape = {
data: {
repository: {
ref: {
name: string;
prefix: string;
commit: {
oid: string;
tree: {
oid: string;
};
};
};
};
};
};
const response = await this.graphql(
`query inspectRef($owner: String!, $name: String!, $ref: String!) {
repository(owner: $owner, name: $name) {
ref(qualifiedName: $ref) {
name
prefix
commit: target {
... on Commit {
oid
tree {
oid
}
}
}
}
}
}`,
{ owner: this.repo.owner, name: this.repo.name, ref: this.ref }
);
this.name = (response.data as ResponseShape).data.repository.ref.name;
this.prefix = (response.data as ResponseShape).data.repository.ref.prefix;
this.commitOid = (response.data as ResponseShape).data.repository.ref.commit.oid;
this.treeOid = (response.data as ResponseShape).data.repository.ref.commit.tree.oid;
this.debug(
`Ref: ${this.fullyQualifiedName}, prefix: ${this.prefix}, commitOid: ${this.commitOid}, treeOid: ${this.treeOid}`
);
}
async update(sha: string): Promise<void> {
// Update ref
// Via: PATCH https://api.github.com/repos/$GITHUB_REPOSITORY/git/$REF
await this.github.patch(
`/repos/${this.repo.nameWithOwner}/git/${this.fullyQualifiedName}`,
{ sha }
);
}
}