-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
97 lines (83 loc) · 3.38 KB
/
index.js
File metadata and controls
97 lines (83 loc) · 3.38 KB
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
import * as core from '@actions/core';
import { HttpClient } from '@actions/http-client';
import * as github from '@actions/github';
import yaml from 'js-yaml';
async function fetchUsernameMapping(octokit, owner) {
try {
const { data } = await octokit.rest.repos.getContent({
owner,
repo: '.github',
path: 'discourse.yml',
});
const content = Buffer.from(data.content, 'base64').toString('utf-8');
const config = yaml.load(content);
return config?.usernames || {};
} catch (error) {
core.debug(`Could not fetch discourse.yml: ${error.message}`);
return {};
}
}
async function run() {
try {
const context = github.context;
const {owner, repo} = context.repo;
const release = context.payload.release;
const discourseBaseUrl = core.getInput('discourse-base-url', { required: true });
const discourseApiKey = core.getInput('discourse-api-key', { required: true });
const manualUsername = core.getInput('discourse-author-username').trim();
const discourseCategory = core.getInput('discourse-category') || '';
const discourseTags = core.getMultilineInput('discourse-tags') || [];
const packageName = core.getInput('package-name') || repo;
// Get the GitHub username of the release author
const githubUsername = release?.author?.login;
// Keep existing behavior for backwards compatibility:
// manual input remains the authoritative value with a 'system' fallback.
// Username mapping is additive and only attempted when no manual override is set.
let discourseAuthorUsername = manualUsername || 'system';
if (manualUsername) {
discourseAuthorUsername = manualUsername;
core.info(`Using manual Discourse username '${discourseAuthorUsername}'`);
} else {
// Fetch username mapping from .github/discourse.yml when a token is available.
const token = core.getInput('github-token') || process.env.GITHUB_TOKEN;
if (token && githubUsername) {
const octokit = github.getOctokit(token);
const usernameMapping = await fetchUsernameMapping(octokit, owner);
if (usernameMapping[githubUsername]) {
discourseAuthorUsername = usernameMapping[githubUsername];
core.info(`Mapped GitHub user '${githubUsername}' to Discourse user '${discourseAuthorUsername}'`);
} else {
core.info(`No username mapping found for '${githubUsername}', using default 'system'`);
}
} else {
core.info(`No GitHub token or release author available, using default 'system'`);
}
}
const client = new HttpClient('discourse-api-client');
client.requestOptions = {
headers: {
'Api-Key': discourseApiKey,
'Api-Username': discourseAuthorUsername
}
}
const title = `${repo} ${release.tag_name} released`;
const topic = {
title,
raw: `[${title}](${release.html_url})
${release.body}`,
}
if (discourseCategory !== "") {
topic["category"] = parseInt(discourseCategory, 10);
}
if (discourseTags.length > 0) {
topic["tags"] = discourseTags;
}
const { result: data } = await client.postJson(`${discourseBaseUrl}/posts.json`, topic);
const topicUrl = `${discourseBaseUrl}/t/${data.topic_slug}/${data.topic_id}`;
core.setOutput('topic-url', topicUrl);
core.info(`Topic created: ${topicUrl}`)
} catch (error) {
core.setFailed(error.message);
}
}
run();