package posts.util.github;

import org.junit.jupiter.api.Test;
import org.lym.pom.util.github.GitHubFileUtil;
import org.lym.pom.util.github.dto.GithubFileInfo;
import org.lym.pom.util.github.dto.GithubFileAndCommit;

import java.util.List;

public class GithubTest {

    @Test
    void testAll() {
        // 上传文件 GithubFileContent
        GithubFileAndCommit updateContent = GitHubFileUtil.readFileAndUpload("D:\\codes\\java\\downloads\\NiterForum\\images\\002.png", ".png", "测试提交");
        // {"content":{"name":"5e2900e2f2db406b8d2e6853a97bb126.png","path":"posts/posts/5e2900e2f2db406b8d2e6853a97bb126.png","sha":"571b54a68cd374b3866fa9046ea1ef0920fd2cc6","size":1169866,"url":"https://api.github.com/repos/ChinaLym/images/contents/posts/posts/5e2900e2f2db406b8d2e6853a97bb126.png?ref=main","html_url":"https://github.com/ChinaLym/images/blob/main/posts/posts/5e2900e2f2db406b8d2e6853a97bb126.png","git_url":"https://api.github.com/repos/ChinaLym/images/git/blobs/571b54a68cd374b3866fa9046ea1ef0920fd2cc6","download_url":"https://raw.githubusercontent.com/ChinaLym/images/main/posts/posts/5e2900e2f2db406b8d2e6853a97bb126.png","type":"file","_links":{"self":"https://api.github.com/repos/ChinaLym/images/contents/posts/posts/5e2900e2f2db406b8d2e6853a97bb126.png?ref=main","git":"https://api.github.com/repos/ChinaLym/images/git/blobs/571b54a68cd374b3866fa9046ea1ef0920fd2cc6","html":"https://github.com/ChinaLym/images/blob/main/posts/posts/5e2900e2f2db406b8d2e6853a97bb126.png"}},"commit":{"sha":"fc555b24bac83880f226c05bd906c0a671f19888","node_id":"C_kwDOLII5LdoAKGZjNTU1YjI0YmFjODM4ODBmMjI2YzA1YmQ5MDZjMGE2NzFmMTk4ODg","url":"https://api.github.com/repos/ChinaLym/images/git/commits/fc555b24bac83880f226c05bd906c0a671f19888","html_url":"https://github.com/ChinaLym/images/commit/fc555b24bac83880f226c05bd906c0a671f19888","author":{"name":"lym","email":"cn_lym@foxmai.com","date":"2024-01-23T14:44:55Z"},"committer":{"name":"lym","email":"cn_lym@foxmai.com","date":"2024-01-23T14:44:55Z"},"tree":{"sha":"fa7bd562e13bbf71ed9b2379777f00dc70be681e","url":"https://api.github.com/repos/ChinaLym/images/git/trees/fa7bd562e13bbf71ed9b2379777f00dc70be681e"},"message":"测试提交","parents":[{"sha":"3c1db50d50725fe273940eb6a2db11f30160610a","url":"https://api.github.com/repos/ChinaLym/images/git/commits/3c1db50d50725fe273940eb6a2db11f30160610a","html_url":"https://github.com/ChinaLym/images/commit/3c1db50d50725fe273940eb6a2db11f30160610a"}],"verification":{"verified":false,"reason":"unsigned","signature":null,"payload":null}}}
        String filePath = updateContent.getContent().getPath();
        System.out.println("访问|下载地址 = " + updateContent.getContent().getDownload_url());

        // list List<GithubContent>
        List<GithubFileInfo> imagesUrlList = GitHubFileUtil.list("");
        // [{"name":"5e2900e2f2db406b8d2e6853a97bb126.png","path":"posts/posts/5e2900e2f2db406b8d2e6853a97bb126.png","sha":"571b54a68cd374b3866fa9046ea1ef0920fd2cc6","size":1169866,"url":"https://api.github.com/repos/ChinaLym/images/contents/posts/posts/5e2900e2f2db406b8d2e6853a97bb126.png?ref=main","html_url":"https://github.com/ChinaLym/images/blob/main/posts/posts/5e2900e2f2db406b8d2e6853a97bb126.png","git_url":"https://api.github.com/repos/ChinaLym/images/git/blobs/571b54a68cd374b3866fa9046ea1ef0920fd2cc6","download_url":"https://raw.githubusercontent.com/ChinaLym/images/main/posts/posts/5e2900e2f2db406b8d2e6853a97bb126.png","type":"file","_links":{"self":"https://api.github.com/repos/ChinaLym/images/contents/posts/posts/5e2900e2f2db406b8d2e6853a97bb126.png?ref=main","git":"https://api.github.com/repos/ChinaLym/images/git/blobs/571b54a68cd374b3866fa9046ea1ef0920fd2cc6","html":"https://github.com/ChinaLym/images/blob/main/posts/posts/5e2900e2f2db406b8d2e6853a97bb126.png"}}]
        imagesUrlList.forEach(System.out::println);
        // [{"name":"063da8e1d24f403f9442db5f052e6075.png","path":"posts/063da8e1d24f403f9442db5f052e6075.png","sha":"571b54a68cd374b3866fa9046ea1ef0920fd2cc6","size":1169866,"url":"https://api.github.com/repos/ChinaLym/images/contents/posts/063da8e1d24f403f9442db5f052e6075.png?ref=main","html_url":"https://github.com/ChinaLym/images/blob/main/posts/063da8e1d24f403f9442db5f052e6075.png","git_url":"https://api.github.com/repos/ChinaLym/images/git/blobs/571b54a68cd374b3866fa9046ea1ef0920fd2cc6","download_url":"https://raw.githubusercontent.com/ChinaLym/images/main/posts/063da8e1d24f403f9442db5f052e6075.png","type":"file","_links":{"self":"https://api.github.com/repos/ChinaLym/images/contents/posts/063da8e1d24f403f9442db5f052e6075.png?ref=main","git":"https://api.github.com/repos/ChinaLym/images/git/blobs/571b54a68cd374b3866fa9046ea1ef0920fd2cc6","html":"https://github.com/ChinaLym/images/blob/main/posts/063da8e1d24f403f9442db5f052e6075.png"}},{"name":"1624ed08dcd44b35a7a0ba3387657112.png","path":"posts/1624ed08dcd44b35a7a0ba3387657112.png","sha":"571b54a68cd374b3866fa9046ea1ef0920fd2cc6","size":1169866,"url":"https://api.github.com/repos/ChinaLym/images/contents/posts/1624ed08dcd44b35a7a0ba3387657112.png?ref=main","html_url":"https://github.com/ChinaLym/images/blob/main/posts/1624ed08dcd44b35a7a0ba3387657112.png","git_url":"https://api.github.com/repos/ChinaLym/images/git/blobs/571b54a68cd374b3866fa9046ea1ef0920fd2cc6","download_url":"https://raw.githubusercontent.com/ChinaLym/images/main/posts/1624ed08dcd44b35a7a0ba3387657112.png","type":"file","_links":{"self":"https://api.github.com/repos/ChinaLym/images/contents/posts/1624ed08dcd44b35a7a0ba3387657112.png?ref=main","git":"https://api.github.com/repos/ChinaLym/images/git/blobs/571b54a68cd374b3866fa9046ea1ef0920fd2cc6","html":"https://github.com/ChinaLym/images/blob/main/posts/1624ed08dcd44b35a7a0ba3387657112.png"}}]

        // 替换文件 GithubContent
        GithubFileInfo queryContent = GitHubFileUtil.queryFile(filePath);
        // {"name":"5e2900e2f2db406b8d2e6853a97bb126.png","path":"posts/posts/5e2900e2f2db406b8d2e6853a97bb126.png","sha":"571b54a68cd374b3866fa9046ea1ef0920fd2cc6","size":1169866,"url":"https://api.github.com/repos/ChinaLym/images/contents/posts/posts/5e2900e2f2db406b8d2e6853a97bb126.png?ref=main","html_url":"https://github.com/ChinaLym/images/blob/main/posts/posts/5e2900e2f2db406b8d2e6853a97bb126.png","git_url":"https://api.github.com/repos/ChinaLym/images/git/blobs/571b54a68cd374b3866fa9046ea1ef0920fd2cc6","download_url":"https://raw.githubusercontent.com/ChinaLym/images/main/posts/posts/5e2900e2f2db406b8d2e6853a97bb126.png","type":"file","content":"","encoding":"none","_links":{"self":"https://api.github.com/repos/ChinaLym/images/contents/posts/posts/5e2900e2f2db406b8d2e6853a97bb126.png?ref=main","git":"https://api.github.com/repos/ChinaLym/images/git/blobs/571b54a68cd374b3866fa9046ea1ef0920fd2cc6","html":"https://github.com/ChinaLym/images/blob/main/posts/posts/5e2900e2f2db406b8d2e6853a97bb126.png"}}
        GithubFileAndCommit updateFile = GitHubFileUtil.updateFile(queryContent.getPath(), queryContent.getSha(), "D:\\codes\\java\\downloads\\NiterForum\\images\\001.png", "替换文件");
        // {"content":{"name":"5e2900e2f2db406b8d2e6853a97bb126.png","path":"posts/posts/5e2900e2f2db406b8d2e6853a97bb126.png","sha":"b7b55815e9ab9be6bd6e321e1b0b5e0ffa59f034","size":854942,"url":"https://api.github.com/repos/ChinaLym/images/contents/posts/posts/5e2900e2f2db406b8d2e6853a97bb126.png?ref=main","html_url":"https://github.com/ChinaLym/images/blob/main/posts/posts/5e2900e2f2db406b8d2e6853a97bb126.png","git_url":"https://api.github.com/repos/ChinaLym/images/git/blobs/b7b55815e9ab9be6bd6e321e1b0b5e0ffa59f034","download_url":"https://raw.githubusercontent.com/ChinaLym/images/main/posts/posts/5e2900e2f2db406b8d2e6853a97bb126.png","type":"file","_links":{"self":"https://api.github.com/repos/ChinaLym/images/contents/posts/posts/5e2900e2f2db406b8d2e6853a97bb126.png?ref=main","git":"https://api.github.com/repos/ChinaLym/images/git/blobs/b7b55815e9ab9be6bd6e321e1b0b5e0ffa59f034","html":"https://github.com/ChinaLym/images/blob/main/posts/posts/5e2900e2f2db406b8d2e6853a97bb126.png"}},"commit":{"sha":"36dcc31c9ccc32efae38b963b28858d1c6074ddc","node_id":"C_kwDOLII5LdoAKDM2ZGNjMzFjOWNjYzMyZWZhZTM4Yjk2M2IyODg1OGQxYzYwNzRkZGM","url":"https://api.github.com/repos/ChinaLym/images/git/commits/36dcc31c9ccc32efae38b963b28858d1c6074ddc","html_url":"https://github.com/ChinaLym/images/commit/36dcc31c9ccc32efae38b963b28858d1c6074ddc","author":{"name":"lym","email":"cn_lym@foxmai.com","date":"2024-01-23T15:02:12Z"},"committer":{"name":"lym","email":"cn_lym@foxmai.com","date":"2024-01-23T15:02:12Z"},"tree":{"sha":"02e525f2f18136f2dc230e45f2a19650fa36cc02","url":"https://api.github.com/repos/ChinaLym/images/git/trees/02e525f2f18136f2dc230e45f2a19650fa36cc02"},"message":"替换文件","parents":[{"sha":"652bcbc7ec0c38a0c97a081b6923b0afcfc81624","url":"https://api.github.com/repos/ChinaLym/images/git/commits/652bcbc7ec0c38a0c97a081b6923b0afcfc81624","html_url":"https://github.com/ChinaLym/images/commit/652bcbc7ec0c38a0c97a081b6923b0afcfc81624"}],"verification":{"verified":false,"reason":"unsigned","signature":null,"payload":null}}}

        // 删除文件
        String updatedPath = updateFile.getContent().getPath();
        GithubFileInfo queryContent2 = GitHubFileUtil.queryFile(updatedPath);
        GithubFileAndCommit delResult = GitHubFileUtil.delFile(updatedPath, queryContent2.getSha(), "测试删除");
    }

}