-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
120 lines (96 loc) · 4.81 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
const core = require('@actions/core');
const github = require('@actions/github');
const axios = require('axios');
const exec = require('child_process').execSync;
function sanitizeNugetSource(nugetSource) {
return nugetSource.replace(/\/index\.json$/, '').replace(/\/$/, '');
}
function execCommand(command) {
console.log(`Executing command: ${command}`);
try {
const output = exec(command, { stdio: 'pipe', shell: true, encoding: 'utf-8' }).trim();
console.log(`Raw command output:\n${output}`);
return output;
} catch (error) {
console.error(`Error executing command: ${command}`);
console.error(`Command stdout: ${error.stdout?.toString() || 'null'}`);
console.error(`Command stderr: ${error.stderr?.toString() || 'null'}`);
throw error;
}
}
async function run() {
try {
// Inputs
const githubToken = core.getInput('github-token', { required: true });
const nugetSource = core.getInput('nuget-source', { required: true });
const packageId = core.getInput('package-id', { required: true });
const workDirectory = core.getInput('work-directory', { required: true });
const versionPrefix = core.getInput('version-prefix') || 'processed-';
const commitRange = core.getInput('commit-range') || '10';
const noBuild = core.getInput('no-build') === 'true';
const packAndPublish = core.getInput('pack-and-publish') === 'true';
process.chdir(workDirectory);
// Get the last commits
const lastCommits = execCommand(`git log -n ${commitRange} --pretty=format:"%H %s"`);
// Filter matching commits
const matchingCommit = lastCommits.split('\n').find(commit => commit.match(/\(#update package version to \d+\.\d+\.\d+\)/));
console.log("Matching commits:\n" + (matchingCommit || "None found"));
let newVersion = '';
if (matchingCommit) {
const commitHash = matchingCommit.split(' ')[0];
console.log(`Found commit with version update indicator: ${commitHash}`);
try {
execCommand(`git rev-parse "${versionPrefix}${commitHash}"`);
console.log('This commit has already been processed for version update. Skipping.');
} catch {
// Extract version from the commit message
const customVersionMatch = matchingCommit.match(/\(#update package version to (\d+\.\d+\.\d+)\)/);
if (customVersionMatch) {
newVersion = customVersionMatch[1];
console.log(`Using custom version: ${newVersion}`);
// Tag the commit to prevent reprocessing
execCommand(`git tag "${versionPrefix}${commitHash}"`);
execCommand(`git push origin "${versionPrefix}${commitHash}"`);
} else {
throw new Error('Failed to extract version from commit message.');
}
}
}
if (!newVersion) {
console.log('No unprocessed custom version found in the last commits. Proceeding to fetch and increment the latest version from the feed.');
nugetSourceSanitized = sanitizeNugetSource(nugetSource);
const feedUrl = `${nugetSourceSanitized}/query?q=${packageId}`;
const response = await axios.get(feedUrl, {
headers: {
Authorization: `token ${githubToken}`
}
});
const latestVersion = response.data.data[0]?.version;
if (!latestVersion) {
console.log('No existing version found in the feed. Defaulting to version 1.0.0');
newVersion = '1.0.0';
} else {
console.log(`Latest version is ${latestVersion}`);
const versionParts = latestVersion.split('.');
newVersion = `${versionParts[0]}.${versionParts[1]}.${parseInt(versionParts[2], 10) + 1}`;
console.log(`Incrementing to new version: ${newVersion}`);
}
}
core.setOutput('NEW_VERSION', newVersion);
if (packAndPublish) {
console.log('Building, packing, and publishing the package.');
execCommand('dotnet build -c Release');
if (noBuild) {
execCommand(`dotnet pack --no-build -c Release -p:PackageVersion="${newVersion}" --output .`);
} else {
execCommand(`dotnet pack -c Release -p:PackageVersion="${newVersion}" --output .`);
}
execCommand(`dotnet nuget push "*.nupkg" --api-key "${githubToken}" --source "${nugetSource}"`);
} else {
console.log('Skipping packing and publishing as per configuration.');
}
} catch (error) {
core.setFailed(error.message);
}
}
run();