-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
144 lines (117 loc) · 3.62 KB
/
index.js
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const github = require("@actions/github");
const core = require("@actions/core");
class SubmitNVDAAddonAction {
constructor() {
this.initialize();
}
initialize() {
this.token = this.getRequiredInput("token");
this.octokit = github.getOctokit(this.token);
const { repo, owner } = github.context.repo;
this.repo = repo;
this.owner = owner;
this.addonName = this.getRequiredInput("addon_name");
this.addonVersion = this.getValidatedVersion();
this.downloadUrl = this.getRequiredInput("download_url");
this.sourceUrl = core.getInput("source_url") || `https://github.com/${this.owner}/${this.repo}`;
this.publisher = core.getInput("publisher") || this.owner;
this.channel = this.getValidatedChannel();
this.licenseName = core.getInput("license_name");
this.licenseUrl = core.getInput("license_url");
this.issueTitle = `[Submit add-on]: ${this.addonName}-v${this.addonVersion}`;
this.issueTemplate = "Add-on registration";
this.markdownTemplate = this.generateMarkdownTemplate();
}
getRequiredInput(name) {
const input = core.getInput(name);
if (!input) {
throw new Error(`Input "${name}" is required but was not provided.`);
}
return input;
}
getValidatedChannel() {
const validChannels = ["stable", "beta", "dev"];
const channel = core.getInput("channel").trim().toLowerCase();
if (channel && !validChannels.includes(channel)) {
throw new Error(`Invalid channel: "${channel}". Valid options are: ${validChannels.join(", ")}.`);
}
return channel;
}
getValidatedVersion() {
const version = this.getRequiredInput("addon_version");
const versionRegex = /^(?:\d+\.\d+)(?:\.\d+)?$/;
if (!versionRegex.test(version)) {
throw new Error(`The version format "${version}" is invalid. Expected formats are Major.Minor.Patch or Major.Minor.`);
}
return version;
}
generateMarkdownTemplate() {
return `
### Download URL
${this.downloadUrl}
### Source URL
${this.sourceUrl}
### Publisher
${this.publisher}
### Channel
${this.channel}
### License Name
${this.licenseName}
### License URL
${this.licenseUrl}
`;
}
async getRepositoryNodeId(owner, repo) {
const query = `
query ($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
id
}
}
`;
try {
const { repository: { id } } = await this.octokit.graphql(query, { owner, repo });
return id;
} catch (error) {
throw new Error(`Error fetching repository node ID for ${owner}/${repo}: ${error.message}`);
}
}
async createIssue(repoId) {
const mutation = `
mutation CreateIssue($input: CreateIssueInput!) {
createIssue(input: $input) {
issue {
number
}
}
}
`;
const variables = {
input: {
repositoryId: repoId,
title: this.issueTitle,
body: this.markdownTemplate,
issueTemplate: this.issueTemplate,
},
};
try {
const { createIssue: { issue: { number } } } = await this.octokit.graphql(mutation, variables);
return number;
} catch (error) {
throw new Error(`Error creating issue in repository ${repoId}: ${error.message}`);
}
}
async run() {
const nodeId = await this.getRepositoryNodeId("nvaccess", "addon-datastore");
const issueNumber = await this.createIssue(nodeId);
core.setOutput("issue_number", issueNumber);
}
}
(async () => {
try {
const submitAction = new SubmitNVDAAddonAction();
await submitAction.run();
} catch (error) {
core.setFailed(`Action failed: ${error.message}`);
}
})();