/
pull.ts
117 lines (99 loc) · 3.86 KB
/
pull.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
(() => {
let creds = new Credentials();
var action = new PlugIn.Action(async () => {
try {
let req = URL.FetchRequest.fromString("https://api.github.com/graphql");
if (req === null || req.url === null || req.url.host === null) {
throw "could not parse the URL for GitHub's GraphQL API";
}
/////////////////////////////////////
// Step 1: make sure we have creds //
/////////////////////////////////////
let stored = creds.read(req.url.host);
let login = null;
let key = null;
if (stored === null || app.optionKeyDown) {
let credsForm = new Form();
credsForm.addField(new Form.Field.String("login", "Login"));
credsForm.addField(new Form.Field.Password("key", "API Key"));
await credsForm.show(
"Let's set up: we need your GitHub username and an API key with the\n`repo` permission to pull issues from your assigned repos.\n\nCreate this at https://github.com/settings/tokens\n\nYou can get back here in the future to rotate tokens by holding\noption while activating the workflow.",
"Save Key"
);
login = (credsForm.values as { login: string }).login;
key = (credsForm.values as { key: string }).key;
creds.write(req.url.host, login, key);
} else {
login = stored.user;
key = stored.password;
}
////////////////////////////
// Step 2: get the issues //
////////////////////////////
req.method = "POST";
req.bodyString = `{"query":"{ search(type: ISSUE, query: \\"is:issue assignee:${login} state:open\\", last: 100) { nodes { ... on Issue { number title body url repository { name owner { login } } } } } }"}`;
req.headers = {
"Content-Type": "application/json",
Authorization: `bearer ${key}`,
};
let resp = await req.fetch().catch((err) => {
console.error("Problem fetching issues:", err);
let alert = new Alert("Problem fetching from GitHub", err);
alert.show();
throw err;
});
if (resp.bodyString === null) {
throw "body string was null. Did the request succeed?";
}
let body = JSON.parse(resp.bodyString);
//////////////////////////////////
// Step 3: make the tasks in OF //
//////////////////////////////////
type Issue = {
number: number;
title: string;
body: string;
url: string;
repository: {
name: string;
owner: {
login: string;
};
};
};
let toFocus: Array<Project> = [];
for (let issue of body.data.search.nodes as Issue[]) {
let gitHubTag =
flattenedTags.byName("from GitHub") || new Tag("from GitHub");
let orgTag =
gitHubTag.tagNamed(issue.repository.owner.login) ||
new Tag(issue.repository.owner.login, gitHubTag);
let repoTag =
orgTag.tagNamed(issue.repository.name) ||
new Tag(issue.repository.name, orgTag);
let repo = `${issue.repository.owner.login}/${issue.repository.name}`;
let projectName = `${repo}#${issue.number}: ${issue.title}`;
let project =
flattenedProjects.byName(projectName) || new Project(projectName);
project.addTag(repoTag);
project.status = Project.Status.Active;
project.note = `${issue.url}\n\n---\n\n${issue.body}`;
toFocus.push(project);
if (project.tasks.length === 0) {
new Task(
`what needs to be done for ${repo}#${issue.number}?`,
project
);
}
}
if (app.platformName === "macOS") {
document.windows[0].perspective = Perspective.BuiltIn.Projects;
document.windows[0].focus = toFocus as SectionArray;
}
} catch (err) {
console.error(err);
throw err;
}
});
return action;
})();