Skip to content

Commit

Permalink
fix(ci): publish & updater
Browse files Browse the repository at this point in the history
feat(ci): get github commits list

fix(ci): missing dedent

fix(ci): wip

fix(ci): changelog username
  • Loading branch information
greenhat616 committed Jan 29, 2024
1 parent c9ee088 commit a715241
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 50 deletions.
222 changes: 176 additions & 46 deletions .github/conventional-changelog/config.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,180 @@
"use strict";
const config = require("conventional-changelog-conventionalcommits");
const conventionalChangelogConfig = require("conventional-changelog-conventionalcommits");
const github = require("@actions/github");
const fs = require("node:fs");
const dedent = require("dedent");

const GIT_COMMIT_WITH_AUTHOR_FORMAT =
"%B%n-hash-%n%H%n-gitTags-%n%d%n-committerDate-%n%ci%n-authorName-%n%an%n-authorEmail-%n%ae%n-gpgStatus-%n%G?%n-gpgSigner-%n%GS";
const extraCommitMsg = `by {{authorName}}`;

const configs = config({
types: [
{
type: "feat",
section: "✨ Features",
},
{
type: "fix",
section: "🐛 Bug Fixes",
},
{
type: "chore",
section: "🧹 Maintenance",
},
{
type: "docs",
section: "📚 Documentation",
},
{
type: "style",
section: "💅 Styles",
},
{
type: "refactor",
section: "🔨 Refactoring",
},
{
type: "perf",
section: "⚡ Performance Improvements",
},
{
type: "test",
section: "✅ Tests",
},
],
});

config.gitRawCommitsOpts.format = GIT_COMMIT_WITH_AUTHOR_FORMAT;
config.writerOpts.commitPartial =
config.writerOpts.commitPartial.replace(/\n*$/, "") +
` {{#if @root.linkReferences~}}${extraCommitMsg}{{~/if}}\n`;

module.exports = configs;

const extraCommitMsg = `by @{{userLogin}}`;

const QUERY_PAGE_SIZE = 100;

function writerOptsTransform(
originalTransform,
commitsSinceLastRelease,
commit,
context,
) {
// execute original writerOpts transform
const extendedCommit = originalTransform(commit, context);

// then add client remote detail (login) to the commit object and return it
if (extendedCommit) {
// search current commit with the commits since last release array returned from fetching GitHub API
const remoteCommit = commitsSinceLastRelease.find(
(c) => c.shortHash === commit.shortHash,
);
if (remoteCommit?.login) {
commit.userLogin = remoteCommit.login;
}
}

return extendedCommit;
}

/**
* From a dot (.) notation path, find and return a property within an object given a complex object path
* Note that the object path does should not include the parent itself
* for example if we want to get `address.zip` from `user` object, we would call `getComplexObjectValue(user, 'address.zip')`
* @param object - object to search from
* @param path - complex object path to find descendant property from, must be a string with dot (.) notation
* @returns outputValue - the object property value found if any
*/
function getComplexObjectValue(object, path) {
if (!object || !path) {
return object;
}
return path.split(".").reduce((obj, prop) => obj?.[prop], object);
}

// Retrieve previous commits since last release from GitHub API
async function retrievePreviousCommits(branchName) {
const octokit = github.getOctokit(process.env.GITHUB_TOKEN);

// first retrieve the latest tag
const {
data: { tag_name },
} = await octokit.rest.repos.getLatestRelease({
owner: "keiko233",
repo: "clash-nyanpasu",
});

// then retrieve the latest tag commit timestamp
const { data: commitData } = await octokit.rest.repos.getCommit({
owner: "keiko233",
repo: "clash-nyanpasu",
ref: tag_name,
});

const sinceDate =
commitData.commit.committer.date || commitData.commit.author.date;

const remoteCommits = [];
let afterCursor = "";
let hasNextPage = false;

do {
const afterCursorStr = afterCursor ? `, after: "${afterCursor}"` : "";
const queryStr = dedent`
query getCommits($repo: String!, $owner: String!, $branchName: String!, $pageSize: Int!, $since: GitTimestamp!) {
repository(name: $repo, owner: $owner) {
ref(qualifiedName: $branchName) {
target { ... on Commit {
history(first: $pageSize, since: $since ${afterCursorStr}) {
nodes { oid, message, author { name, user { login }}}
pageInfo { hasNextPage, endCursor }
}}}}}}
`.trim();

const response = await octokit.graphql(queryStr, {
owner: "keiko233",
repo: "clash-nyanpasu",
afterCursor,
branchName,
pageSize: QUERY_PAGE_SIZE,
since: sinceDate,
});

const historyData = getComplexObjectValue(
response,
"repository.ref.target.history",
);
const pageInfo = historyData?.pageInfo;
hasNextPage = pageInfo?.hasNextPage ?? false;
afterCursor = pageInfo?.endCursor ?? "";

if (historyData?.nodes) {
for (const commit of historyData.nodes) {
if (commit?.oid && commit?.author) {
remoteCommits.push({
shortHash: commit.oid.substring(0, 7),
authorName: commit?.author.name,
login: commit?.author?.user?.login ?? "",
message: commit?.message ?? "",
});
}
}
}
} while (hasNextPage);

console.log(
"github",
"found %s commits since last release timestamp %s",
remoteCommits.length,
sinceDate,
);

return remoteCommits;
}

module.exports = (async () => {
const config = await conventionalChangelogConfig({
types: [
{
type: "feat",
section: "✨ Features",
},
{
type: "fix",
section: "🐛 Bug Fixes",
},
{
type: "chore",
section: "🧹 Maintenance",
},
{
type: "docs",
section: "📚 Documentation",
},
{
type: "style",
section: "💅 Styles",
},
{
type: "refactor",
section: "🔨 Refactoring",
},
{
type: "perf",
section: "⚡ Performance Improvements",
},
{
type: "test",
section: "✅ Tests",
},
],
});
const commitsSinceLastRelease = await retrievePreviousCommits("main");
config.gitRawCommitsOpts.format = GIT_COMMIT_WITH_AUTHOR_FORMAT;
config.writerOpts.commitPartial =
config.writerOpts.commitPartial.replace(/\n*$/, "") +
` {{#if @root.linkReferences~}}${extraCommitMsg}{{~/if}}\n`;
config.writerOpts.transform = writerOptsTransform.bind(
null,
config.writerOpts.transform,
commitsSinceLastRelease,
);
return config;
})();
3 changes: 1 addition & 2 deletions .github/workflows/dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ jobs:
cat > release.txt << 'EOF'
## Clash Nyanpasu Nightly Build
Release created at ${{ env.BUILDTIME }}.
Daily build of **Clash Nyanpasu** on *dev* branch.
Daily build of **Clash Nyanpasu** on *main* branch.
***[See the development log here](https://t.me/keikolog/462)***
EOF
- name: Update Release
Expand All @@ -176,7 +176,6 @@ jobs:
body_path: release.txt
prerelease: true
generate_release_notes: true
target_commitish: dev
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
updater:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ jobs:
git-branch: "main"
env:
NYANPASU_VERSION: ${{ steps.update-version.outputs.version }}
GITHUB_TOKEN: ${{ secrets.github_token }}

- name: Release
uses: softprops/action-gh-release@v1
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@
"@emotion/styled": "11.11.0",
"@juggle/resize-observer": "3.4.0",
"@mui/icons-material": "5.15.6",
"@mui/lab": "5.0.0-alpha.162",
"@mui/material": "5.15.6",
"@mui/x-data-grid": "6.19.2",
"@mui/lab": "5.0.0-alpha.162",
"@tauri-apps/api": "1.5.3",
"ahooks": "3.7.9",
"axios": "1.6.7",
Expand Down Expand Up @@ -95,6 +95,7 @@
"consola": "3.2.3",
"conventional-changelog-conventionalcommits": "7.0.2",
"cross-env": "7.0.3",
"dedent": "1.5.1",
"eslint": "8.56.0",
"eslint-config-prettier": "9.1.0",
"eslint-config-standard": "17.1.0",
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion scripts/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async function resolveUpdater() {

const updateData = {
name: tag.name,
notes: (await resolveUpdateLog(tag.name)) || UPDATE_RELEASE_BODY || "", // use updatelog.md
notes: UPDATE_RELEASE_BODY || (await resolveUpdateLog(tag.name)), // use updatelog.md
pub_date: new Date().toISOString(),
platforms: {
win64: { signature: "", url: "" }, // compatible with older formats
Expand Down

0 comments on commit a715241

Please sign in to comment.