Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce support for gitea #1273

Merged
merged 21 commits into from
Nov 5, 2023
Merged

Conversation

dreamncn
Copy link

@dreamncn dreamncn commented Nov 1, 2023

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
      }
  1. Use the gitlab platform in the YAML configuration:
      ...others
      with:
        platform: "gitlab"
      ...others
      

Tests

  • Successfully passed local Jest testing.
  • Successfully passed the GitHub action call test.
  • Successfully passed the test call on the self-hosted Gitea platform.

Others

This pull request introduces significant changes to the project's structure, so please merge with caution.

@mikepenz
Copy link
Owner

mikepenz commented Nov 1, 2023

Thank you very much for the pr!

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

@mikepenz mikepenz self-requested a review November 5, 2023 07:47
@mikepenz mikepenz merged commit e86f2bb into mikepenz:develop Nov 5, 2023
@mikepenz mikepenz changed the title merge: support gitea Introduce support for gitea Nov 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants