mikepenz / release-changelog-builder-action

A GitHub action that builds your release notes / changelog fast, easy and exactly the way you want.
https://blog.mikepenz.dev
Apache License 2.0
689 stars 99 forks source link

Introduce support for `gitea` #1273

Closed dreamncn closed 8 months ago

dreamncn commented 8 months ago

Changes

  1. Move pr-controller/src to the src directory and rename to pr-controller;
  2. Extract calls related to the Github API into the GithubRepository class;
  3. Added support for Gitea;
  4. Added test cases related to Gitea;
  5. Fixed test case errors caused by extracting the API.

Usage

Since some action libraries have not yet been mirrored on Gitea, I have used my own internal proxy to access them (https://proxy.ankio.top).

name: Release

on:
  push:
    tags:
      - 'v*'
permissions:
  contents: write
  pull-requests: write
jobs:
  build-and-release:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: https://proxy.ankio.top/actions/checkout@v2
        with:
          fetch-depth: 0
      - name: Get tag name
        id: get_tag
        run: echo ::set-output name=tag_name::$(git describe --tags --abbrev=0)
      - name: Build Changelog
        id: github_release
        uses: https://proxy.ankio.top/dreamncn/release-changelog-builder-action@v4.0.1
        with:
          platform: "gitea"
          baseUrl: "https://gitea.ankio.top"
          commitMode: true
          configuration: "configuration.json"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Create Release
        uses: https://proxy.ankio.top/mikepenz/action-gh-release@v0.2.0-a03 #softprops/action-gh-release
        with:
          body: ${{steps.github_release.outputs.changelog}}

Focus on this part only:

- name: Build Changelog
        id: github_release
        uses: https://proxy.ankio.top/dreamncn/release-changelog-builder-action@v4.0.1   # If someone is interested in trying, replace it with https://github.com/dreamncn/release-changelog-builder-action@v4.0.1.
        with:
          platform: "gitea" # gitea or github, default is github
          baseUrl: "https://gitea.ankio.top" # default is https://api.github.com, if your platform is gitea, default is https://gitea.com
          commitMode: true
          configuration: "configuration.json"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Do not change this

Support for More Platforms

  1. In src/repositories, create a class file, for example, named GitLabRepository and inherit the BaseRepository class.

  2. You must implement these functions:

    
    
    export class GitLabRepository extends BaseRepository {
    
     get defaultUrl(): string {
       return 'https://gitea.com'  // Your platform API URL
     }
    
     constructor(token: string, url: string | undefined, repositoryPath: string) {
       super(token, url, repositoryPath)
       this.url = url || this.defaultUrl
      // You must initialize your httpclient here.
     }
    
       // In this function, you must get the tag release date by the tag name
     async fillTagInformation(repositoryPath: string, owner: string, repo: string, tagInfo: TagInfo): Promise<TagInfo> {
    
       // Perform your HTTP request
    
       // Just format your date
    
        tagInfo.date = moment(response.data.commit.created)
    
       // Don't forget the last line
    
       return await this.getTagByCreateTime(repositoryPath, tagInfo)
     }
    
    // Get closed PRs within a time range
     async getBetweenDates(
       owner: string,
       repo: string,
       fromDate: moment.Moment,
       toDate: moment.Moment,
       maxPullRequests: number
     ): Promise<PullRequestInfo[]> {
       const mergedPRs: PullRequestInfo[] = []
    
       // Perform your HTTP request
    
       // And create PullRequestInfo objects and push them into mergedPRs
    
       return mergedPRs
     }
    
     // Get comparison of remote branches
     async getDiffRemote(owner: string, repo: string, base: string, head: string): Promise<DiffInfo> {
       // If your platform does not have an API about comparing commits, you can try the functionality in ./src/repositories/GiteaRepository.ts, which uses `git`
       return {
         changedFiles: changedFilesCount,
         additions: additionCount,
         deletions: deletionCount,
         changes: changeCount,
         commits: commitCount,
         commitInfo
       }
     }
    
    // Get pull request for commitHash
    
     async getForCommitHash(owner: string, repo: string, commit_sha: string, maxPullRequests: number): Promise<PullRequestInfo[]> {
       const mergedPRs: PullRequestInfo[] = []
    
       // Execute your HTTP request, only selecting the status as 'closed'
      // Make sure your hash is the merge_commit_sha
    
       core.debug(`⚠️ No more PRs retrieved by the API. So far fetched: ${mergedPRs.length}`)
    
       return mergedPRs
     }
    // Get open pull requests
     async getOpen(owner: string, repo: string, maxPullRequests: number): Promise<PullRequestInfo[]> {
    
       const openPrs: PullRequestInfo[] = []
       // Perform your request
       return openPrs
     }
    // Get all reviews from a PR
    
     async getReviews(owner: string, repo: string, pr: PullRequestInfo): Promise<void> {
      // You can use pr.number to get reviews
       pr.reviews = prReviews
     }
    
    // Get all tags
    
     async getTags(owner: string, repo: string, maxTagsToFetch: number): Promise<TagInfo[]> {
       // Perform your HTTP request
       // Build an array like this
    
       core.info(`ℹ️ Retrieved ${tagsInfo.length} tags from GitHub API for ${owner}/${repo} (maximum fetch count: ${maxTagsToFetch})`)
       return tagsInfo
     }
    
    // Your platform URL, not the API URL, such as https://gitea.com or https://github.com
     get homeUrl(): string {
       return 'https://gitea.com'
     }
    }

More information can be found here:

src/repositories/GiteaRepository.ts src/repositories/BaseRepository.ts

  1. Add your class to the src/main.ts file at line 10, as shown below:

      const supportedPlatform = {
        github: GithubRepository,
        gitea: GiteaRepository,
        gitlab: GitLabRepository // add your class here
      }
  2. Use the gitlab platform in the YAML configuration:

      ...others
      with:
        platform: "gitlab"
      ...others

Tests

Others

This pull request introduces significant changes to the project's structure, so please merge with caution.
mikepenz commented 8 months ago

Thank you very much for the pr!

I'll need some time to review as I'm unfortunately not available the next few days